-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth-resp.html
More file actions
105 lines (104 loc) · 4.23 KB
/
oauth-resp.html
File metadata and controls
105 lines (104 loc) · 4.23 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Authentication Success</title>
</head>
<body>
<script>
function convertPairStringToMap(str) {
var map = {};
var str_pairs = str.split('&');
for (var str_pair of str_pairs) {
var split_pair = str_pair.split('=');
if (split_pair[1]) {
map[decodeURIComponent(split_pair[0])] =
decodeURIComponent(split_pair[1]);
}
}
return map;
}
function handleAuthAndContinue() {
var origin = window.location.origin;
if (
window != window.top &&
window != window.parent &&
origin &&
origin === window.parent.location.origin
) {
console.log(
'[PlaceOS][Auth] iFrame Detected. Processing message passing...',
);
var message = 'error';
if (window.location.hash.indexOf('access_token') !== -1) {
message = {
type: 'place-os',
token: window.location.hash.replace(
/^.*access_token=([^&]+).*$/,
'$1',
),
expires_in: parseInt(
window.location.hash.replace(
/^.*expires_in=([^&]+).*$/,
'$1',
),
),
};
} else if (window.location.search.indexOf('code') !== -1) {
message = {
type: 'place-os',
code: window.location.search.replace(
/^.*code=([^&]+).*$/,
'$1',
),
state: window.location.search.replace(
/^.*state=([^&]+).*$/,
'$1',
),
};
}
try {
// Standards based browsers
parent.postMessage(message, origin);
} catch (e) {
console.error(e);
}
} else {
console.log(
'[PlaceOS][Auth] Main Frame Detected. Processing redirection...',
);
var data = window.location.search || window.location.hash;
data = data.replace(/(^#|^\?)/g, '');
var loc = (location.origin + location.pathname).replace(
'oauth-resp.html',
'',
);
console.log('[PlaceOS][Auth] Returning to ' + loc);
var search_index = loc.indexOf('?');
var hash_index = loc.indexOf('#');
console.log(
'[PlaceOS][Auth] Auth parameters:',
convertPairStringToMap(data),
);
// Append to existing query params if they exist
data = search_index >= 0 ? '&' + data : (data = '?' + data);
var continue_url = (loc + data)
.replace(/$#/g, '')
.replace(/#&/g, '#')
.replace(/\?&/g, '?')
.replace(/&&/g, '');
window.location.href = continue_url;
}
}
if (window.addEventListener) {
window.addEventListener(
'DOMContentLoaded',
handleAuthAndContinue,
);
} else {
handleAuthAndContinue(); // IE 8
}
</script>
</body>
</html>