Skip to content

Commit cd650f8

Browse files
authored
Merge pull request #3134 from WhiteRabbitDE/fix-oauth2-implicit-workflow
[Swagger-UI] add missing oauth2-redirect configuration
2 parents 6ea8030 + 717596b commit cd650f8

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

src/Bridge/Symfony/Bundle/Resources/public/init-swagger-ui.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ window.onload = function() {
4545
spec: data.spec,
4646
dom_id: '#swagger-ui',
4747
validatorUrl: null,
48+
oauth2RedirectUrl: data.oauth.redirectUrl,
4849
presets: [
4950
SwaggerUIBundle.presets.apis,
5051
SwaggerUIStandalonePreset,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!doctype html>
2+
<html lang="en-US">
3+
<body onload="run()">
4+
</body>
5+
</html>
6+
<script>
7+
'use strict';
8+
function run () {
9+
var oauth2 = window.opener.swaggerUIRedirectOauth2;
10+
var sentState = oauth2.state;
11+
var redirectUrl = oauth2.redirectUrl;
12+
var isValid, qp, arr;
13+
14+
if (/code|token|error/.test(window.location.hash)) {
15+
qp = window.location.hash.substring(1);
16+
} else {
17+
qp = location.search.substring(1);
18+
}
19+
20+
arr = qp.split("&")
21+
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';})
22+
qp = qp ? JSON.parse('{' + arr.join() + '}',
23+
function (key, value) {
24+
return key === "" ? value : decodeURIComponent(value)
25+
}
26+
) : {}
27+
28+
isValid = qp.state === sentState
29+
30+
if ((
31+
oauth2.auth.schema.get("flow") === "accessCode"||
32+
oauth2.auth.schema.get("flow") === "authorizationCode"
33+
) && !oauth2.auth.code) {
34+
if (!isValid) {
35+
oauth2.errCb({
36+
authId: oauth2.auth.name,
37+
source: "auth",
38+
level: "warning",
39+
message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
40+
});
41+
}
42+
43+
if (qp.code) {
44+
delete oauth2.state;
45+
oauth2.auth.code = qp.code;
46+
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
47+
} else {
48+
let oauthErrorMsg
49+
if (qp.error) {
50+
oauthErrorMsg = "["+qp.error+"]: " +
51+
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
52+
(qp.error_uri ? "More info: "+qp.error_uri : "");
53+
}
54+
55+
oauth2.errCb({
56+
authId: oauth2.auth.name,
57+
source: "auth",
58+
level: "error",
59+
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server"
60+
});
61+
}
62+
} else {
63+
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
64+
}
65+
window.close();
66+
}
67+
</script>

src/Bridge/Symfony/Bundle/Resources/views/SwaggerUi/index.html.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
<link rel="stylesheet" href="{{ asset('bundles/apiplatform/style.css') }}">
1111
{% endblock %}
1212

13+
{% set oauth_data = {'oauth': swagger_data.oauth|merge({'redirectUrl' : absolute_url(asset('bundles/apiplatform/swagger-ui/oauth2-redirect.html')) })} %}
1314
{# json_encode(65) is for JSON_UNESCAPED_SLASHES|JSON_HEX_TAG to avoid JS XSS #}
14-
<script id="swagger-data" type="application/json">{{ swagger_data|json_encode(65)|raw }}</script>
15+
<script id="swagger-data" type="application/json">{{ swagger_data|merge(oauth_data)|json_encode(65)|raw }}</script>
1516
</head>
1617

1718
<body>

update-js.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ cp node_modules/swagger-ui-dist/swagger-ui-standalone-preset.js "$dest"
1515
cp node_modules/swagger-ui-dist/swagger-ui-standalone-preset.js.map "$dest"
1616
cp node_modules/swagger-ui-dist/swagger-ui.css "$dest"
1717
cp node_modules/swagger-ui-dist/swagger-ui.css.map "$dest"
18+
cp node_modules/swagger-ui-dist/oauth2-redirect.html "$dest"
1819

1920
dest=src/Bridge/Symfony/Bundle/Resources/public/react/
2021
if [[ -d "$dest" ]]; then

0 commit comments

Comments
 (0)