-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoidc-adapter.html
69 lines (61 loc) · 1.87 KB
/
oidc-adapter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script>
async function getToken(code, path, search, hash) {
try {
const _path = encodeURIComponent(path);
const _search = encodeURIComponent(search);
const _hash = encodeURIComponent(hash);
const req = `/oidc/tokenize` +
`?code=${code}` +
`&path=${_path}` +
`&search=${_search}` +
`&hash=${_hash}`;
const res = await fetch(req, {
headers: {
"Accept": "application/json",
},
});
return await res.json();
} catch {
return undefined;
}
}
async function adapter() {
const qs = new URLSearchParams(window.location.search);
const err = qs.get("error");
const sessionState = qs.get("session_state");
const code = qs.get("code");
const path = qs.get("path");
const search = qs.get("search") || "";
const hash = qs.get("hash") || "";
if (!path.match("^/")) throw new Error("invalid path");
let target = `${path}?` +
`${search}${search ? "&" : ""}` +
`oidc=unauthorized` +
`${hash ? "#" : ""}${hash}`;
if (!err) {
const token = await getToken(code, path, search, hash);
if (token) {
target = `${path}?` +
`${search}${search ? "&" : ""}` +
`oidc=authorized` +
`&jwt=${token}` +
`${hash ? "#" : ""}${hash}`;
} else {
target = `${path}?` +
`${search}${search ? "&" : ""}` +
`oidc=failed` +
`${hash ? "#" : ""}${hash}`;
}
}
window.location = target;
}
</script>
</head>
<body onload="adapter()">
adapting...
</body>
</html>