Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit 527f4da

Browse files
author
Kevin Richter
committed
Fix login redirect URLs
1 parent 5e17062 commit 527f4da

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

source/v1/api/index.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
window.onload = function () {
3636
// Build a system
3737
const ui = SwaggerUIBundle({
38-
url: "https://api.beta.maps4news.com/docs/assets/api-docs.json",
38+
url: "https://api.beta.maps4news.com/docs/assets/api-docs.json", // TODO change to live
3939
dom_id: '#swagger-ui',
40-
// oauth2RedirectUrl: "none", // TODO
40+
oauth2RedirectUrl: `${document.location.origin}/v1/api/oauth2-redirect.html`,
4141
presets: [
4242
SwaggerUIBundle.presets.apis,
4343
SwaggerUIStandalonePreset
@@ -48,6 +48,8 @@
4848
layout: "StandaloneLayout"
4949
});
5050

51+
ui.initOAuth({ clientId: 1 });
52+
5153
window.ui = ui
5254
}
5355
</script>

source/v1/api/oauth2-redirect.html

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>

0 commit comments

Comments
 (0)