Skip to content

Commit

Permalink
fix(oauth2): generate default oauth2RedirectUrl based on page locatio…
Browse files Browse the repository at this point in the history
…n and path (#7476)


Co-authored-by: Tim Lai <timothy.lai@gmail.com>
  • Loading branch information
astellingwerf and tim-lai committed Feb 2, 2022
1 parent a0e9923 commit a89c90e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/index.js
Expand Up @@ -31,7 +31,7 @@ export default function SwaggerUI(opts) {
maxDisplayedTags: null,
filter: null,
validatorUrl: "https://validator.swagger.io/validator",
oauth2RedirectUrl: `${window.location.protocol}//${window.location.host}/oauth2-redirect.html`,
oauth2RedirectUrl: `${window.location.protocol}//${window.location.host}${window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/"))}/oauth2-redirect.html`,
persistAuthorization: false,
configs: {},
custom: {},
Expand Down
67 changes: 67 additions & 0 deletions test/e2e-cypress/static/pages/5085/index.html
@@ -0,0 +1,67 @@
<!-- HTML for dev server -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="/swagger-ui.css" >
<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
}
body {
margin:0;
background: #fafafa;
}
</style>
</head>

<body>

<div id="swagger-ui"></div>

<script src="/swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="/swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script>
window.onload = function() {
window["SwaggerUIBundle"] = window["swagger-ui-bundle"]
window["SwaggerUIStandalonePreset"] = window["swagger-ui-standalone-preset"]
// Build a system
const ui = SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: SwaggerUIStandalonePreset ? "StandaloneLayout" : "BaseLayout",
onComplete: () => {
if(window.completeCount) {
window.completeCount++
} else {
window.completeCount = 1
}
}
})

window.ui = ui
}
</script>
</body>

</html>
18 changes: 18 additions & 0 deletions test/e2e-cypress/tests/features/dynamic-default-oauth.js
Expand Up @@ -5,4 +5,22 @@ describe("dynamic default oauth2RedirectUrl", () => {
.then(win => win.ui.getConfigs())
.should("include", { oauth2RedirectUrl: "http://localhost:3230/oauth2-redirect.html" })
})
it("should compute an oauth2RedirectUrl based on the browser's location at runtime, including the path", () => {
cy.visit("/pages/5085/")
.window()
.then(win => win.ui.getConfigs())
.should("include", { oauth2RedirectUrl: "http://localhost:3230/pages/5085/oauth2-redirect.html" })
})
it("should compute an oauth2RedirectUrl based on the browser's location at runtime, including the path, without confusing the file name for a folder name", () => {
cy.visit("/pages/5085/index.html")
.window()
.then(win => win.ui.getConfigs())
.should("include", { oauth2RedirectUrl: "http://localhost:3230/pages/5085/oauth2-redirect.html" })
})
it("should compute an oauth2RedirectUrl based on the browser's location at runtime, including the path, even it does not end with a slash", () => {
cy.visit("/pages/5085")
.window()
.then(win => win.ui.getConfigs())
.should("include", { oauth2RedirectUrl: "http://localhost:3230/pages/5085/oauth2-redirect.html" })
})
})

0 comments on commit a89c90e

Please sign in to comment.