Skip to content

Commit a6d60de

Browse files
authored
feat: add oauth2 redirect route (#4822)
1 parent 93b5fc1 commit a6d60de

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

public/oauth2-redirect.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!doctype html>
2+
<html lang="en-US">
3+
<body>
4+
</body>
5+
</html>
6+
<script src="oauth2-redirect.js"> </script>

public/oauth2-redirect.js

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

0 commit comments

Comments
 (0)