Skip to content

Commit 245c1a0

Browse files
committed
Upgrade to Swagger-UI 5.29.0
1 parent 122bc4b commit 245c1a0

File tree

3 files changed

+41
-81
lines changed

3 files changed

+41
-81
lines changed

src/SwaggerUI.OpenApi/Endpoints.cs

Lines changed: 13 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -198,87 +198,20 @@ private static string GetIndexEnd()
198198
public static string GetOAuthRedirectHtml()
199199
{
200200
return """
201-
<!doctype html>
202-
<html lang="en-US">
203-
<head>
204-
<title>Swagger UI: OAuth2 Redirect</title>
205-
</head>
206-
<body>
207-
<script>
208-
'use strict';
209-
function run () {
210-
var oauth2 = window.opener.swaggerUIRedirectOauth2;
211-
var sentState = oauth2.state;
212-
var redirectUrl = oauth2.redirectUrl;
213-
var isValid, qp, arr;
214-
215-
if (/code|token|error/.test(window.location.hash)) {
216-
qp = window.location.hash.substring(1).replace('?', '&');
217-
} else {
218-
qp = location.search.substring(1);
219-
}
220-
221-
arr = qp.split("&");
222-
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';});
223-
qp = qp ? JSON.parse('{' + arr.join() + '}',
224-
function (key, value) {
225-
return key === "" ? value : decodeURIComponent(value);
226-
}
227-
) : {};
228-
229-
isValid = qp.state === sentState;
230-
231-
if ((
232-
oauth2.auth.schema.get("flow") === "accessCode" ||
233-
oauth2.auth.schema.get("flow") === "authorizationCode" ||
234-
oauth2.auth.schema.get("flow") === "authorization_code"
235-
) && !oauth2.auth.code) {
236-
if (!isValid) {
237-
oauth2.errCb({
238-
authId: oauth2.auth.name,
239-
source: "auth",
240-
level: "warning",
241-
message: "Authorization may be unsafe, passed state was changed in server. The passed state wasn't returned from auth server."
242-
});
243-
}
244-
245-
if (qp.code) {
246-
delete oauth2.state;
247-
oauth2.auth.code = qp.code;
248-
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
249-
} else {
250-
let oauthErrorMsg;
251-
if (qp.error) {
252-
oauthErrorMsg = "["+qp.error+"]: " +
253-
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
254-
(qp.error_uri ? "More info: "+qp.error_uri : "");
255-
}
256-
257-
oauth2.errCb({
258-
authId: oauth2.auth.name,
259-
source: "auth",
260-
level: "error",
261-
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server."
262-
});
263-
}
264-
} else {
265-
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
266-
}
267-
window.close();
268-
}
269-
270-
if (document.readyState !== 'loading') {
271-
run();
272-
} else {
273-
document.addEventListener('DOMContentLoaded', function () {
274-
run();
275-
});
276-
}
277-
</script>
278-
</body>
279-
</html>
201+
<!doctype html>
202+
<html lang="en-US">
203+
<body>
204+
</body>
205+
</html>
206+
<script src="oauth2-redirect.js"></script>
207+
""";
208+
}
280209

281-
""";
210+
public static string GetOAuthRedirectJs()
211+
{
212+
return """
213+
"use strict";function run(){var e,r,t,a=window.opener.swaggerUIRedirectOauth2,o=a.state,n=a.redirectUrl;if((t=(r=/code|token|error/.test(window.location.hash)?window.location.hash.substring(1).replace("?","&"):location.search.substring(1)).split("&")).forEach((function(e,r,t){t[r]='"'+e.replace("=",'":"')+'"'})),e=(r=r?JSON.parse("{"+t.join()+"}",(function(e,r){return""===e?r:decodeURIComponent(r)})):{}).state===o,"accessCode"!==a.auth.schema.get("flow")&&"authorizationCode"!==a.auth.schema.get("flow")&&"authorization_code"!==a.auth.schema.get("flow")||a.auth.code)a.callback({auth:a.auth,token:r,isValid:e,redirectUrl:n});else if(e||a.errCb({authId:a.auth.name,source:"auth",level:"warning",message:"Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"}),r.code)delete a.state,a.auth.code=r.code,a.callback({auth:a.auth,redirectUrl:n});else{let e;r.error&&(e="["+r.error+"]: "+(r.error_description?r.error_description+". ":"no accessCode received from the server. ")+(r.error_uri?"More info: "+r.error_uri:"")),a.errCb({authId:a.auth.name,source:"auth",level:"error",message:e||"[Authorization failed]: no accessCode received from the server"})}window.close()}"loading"!==document.readyState?run():document.addEventListener("DOMContentLoaded",(function(){run()}));
214+
""";
282215
}
283216

284217
private static List<string> GetDocumentNames(IServiceProvider serviceProvider)

src/SwaggerUI.OpenApi/SwaggerUIEndpointRouteBuilderExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public static RouteGroupBuilder MapSwaggerUIFromGroup(RouteGroupBuilder group)
3939
group.MapGet("oauth2-redirect.html", () =>
4040
Results.Content(Endpoints.GetOAuthRedirectHtml(), "text/html"));
4141

42+
group.MapGet("oauth2-redirect.js", () =>
43+
Results.Content(Endpoints.GetOAuthRedirectJs(), "text/javascript"));
44+
4245
return group;
4346
}
4447
}

test/SwaggerUI.OpenApi.Tests/ServiceCollectionExtensionsTests.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public async Task SpecificDocumentWithMultipleDocumentsAndConfigs()
425425
}
426426

427427
[Fact]
428-
public async Task OAuth2RedirectIsLatestVersion()
428+
public async Task OAuth2RedirectHtmlIsLatestVersion()
429429
{
430430
using var httpClient = new HttpClient();
431431
var expected = await httpClient.GetStringAsync("https://raw.githubusercontent.com/swagger-api/swagger-ui/master/dist/oauth2-redirect.html", TestContext.Current.CancellationToken);
@@ -448,6 +448,30 @@ public async Task OAuth2RedirectIsLatestVersion()
448448
Assert.Equal(expected, actual);
449449
}
450450

451+
[Fact]
452+
public async Task OAuth2RedirectJsIsLatestVersion()
453+
{
454+
using var httpClient = new HttpClient();
455+
var expected = await httpClient.GetStringAsync("https://raw.githubusercontent.com/swagger-api/swagger-ui/master/dist/oauth2-redirect.js", TestContext.Current.CancellationToken);
456+
expected = expected.Replace("\n", "\r\n");
457+
458+
var builder = WebApplication.CreateBuilder();
459+
builder.WebHost.UseTestServer();
460+
builder.Services.AddOpenApi();
461+
builder.Services.AddSwaggerUI();
462+
463+
var app = builder.Build();
464+
app.MapOpenApi();
465+
app.MapSwaggerUI();
466+
467+
app.Start();
468+
var client = app.GetTestClient();
469+
470+
var actual = await client.GetStringAsync("swagger/oauth2-redirect.js", TestContext.Current.CancellationToken);
471+
472+
Assert.Equal(expected, actual);
473+
}
474+
451475
[Fact]
452476
public async Task AddIdentityServer()
453477
{

0 commit comments

Comments
 (0)