-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauth-redirect.html
64 lines (55 loc) · 1.78 KB
/
oauth-redirect.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
<!DOCTYPE html>
<html>
<body id='root' style='font-size:16px; font-family:sans-serif;'>
<p id="msg">
</p>
<script>
var id = "@targetid@";
function getLocal(n) {
return localStorage[id + "/" + n]
}
function setLocal(n, v) {
localStorage[id + "/" + n] = v
}
function delLocal(n) {
delete localStorage[id + "/" + n]
}
function parseQueryString(qs) {
var r = {}
qs.replace(/\+/g, " ").replace(/([^&=]+)=?([^&]*)/g, (f, k, v) => {
r[decodeURIComponent(k)] = decodeURIComponent(v)
return ""
})
return r
}
function err(h) {
document.getElementById("msg").textContent = "Error: " + h;
setTimeout(function() {
location.href = "/";
}, 3000)
throw new Error(h)
}
function store() {
var str = (location.hash || "#").slice(1);
// .replace(/%23access_token/, "access_token")
var qs = parseQueryString(str);
if (qs["access_token"]) {
let ex = getLocal("oauthState");
if (!ex) err("OAuth not started");
if (ex != qs["state"]) err("OAuth state mismatch");
delLocal("oauthState");
setLocal("oauthHash", str);
location.hash = "#";
location.href = getLocal("oauthRedirect");
if (window.opener && window.opener !== window) {
// We are in a popup
window.close();
}
} else {
err("access_token not in #")
}
}
setTimeout(store, 100)
</script>
</body>
</html>