-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathredirect.html
More file actions
122 lines (112 loc) · 4.11 KB
/
Copy pathredirect.html
File metadata and controls
122 lines (112 loc) · 4.11 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!--
This HTML page works as a redirect_uri, post_logout_redirect_uri, frontchannel_logout_uri
but for frontchannel_logout_uri to work, it must be called like this:
/redirect.html?requestType=front-channel-logout
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Flutter Oidc Redirect</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript">
const stateNamespace = 'state';
const stateResponseNamespace = 'response.state';
const requestNamespace = 'request';
const requestBroadcastChannel = 'oidc_flutter_web/request';
const redirectBroadcastChannel = 'oidc_flutter_web/redirect';
//if the OP isn't requesting logout, handle redirect.
if (!handleFrontChannelLogout()) {
handleRedirect();
}
function handleRedirect() {
// For supported browsers: https://caniuse.com/broadcastchannel
var bc = new BroadcastChannel(redirectBroadcastChannel);
bc.postMessage(window.location.toString());
bc.close();
//The rest of this function handles same page redirects
let dataSrc;
dataSrc = new URLSearchParams(window.location.search);
var state = dataSrc.get('state');
if (!state) {
if (window.location.hash) {
dataSrc = new URLSearchParams(
window.location.hash.substring(1)
);
state = dataSrc.get('state');
}
}
if (!state) {
return;
}
const stateDataRaw = getLocalStorage(stateNamespace, state);
if (!stateDataRaw) {
console.error('state not found, key: ' + state);
return;
}
setLocalStorage(stateResponseNamespace, state, window.location.toString());
//we call JSON.parse twice, since shared_preferences double encodes json strings for some reason.
const parsedStateString = JSON.parse(stateDataRaw);
if (!parsedStateString) {
console.error('parsed state is null');
return;
}
// Read the mode from the state.
const webLaunchMode = parsedStateString.options?.webLaunchMode;
if (!webLaunchMode) {
console.error('webLaunchMode not found in parsed state.');
return;
}
if (webLaunchMode != 'samePage') {
return;
}
const original_uri = parsedStateString.original_uri;
if (!original_uri) {
console.warn("it's preferred that original_uri is used when webLaunchMode is samePage.");
return;
}
window.location.assign(original_uri);
}
function handleFrontChannelLogout() {
const queryParams = new URLSearchParams(window.location.search);
if (queryParams.get('requestType') == 'front-channel-logout') {
// For supported browsers: https://caniuse.com/broadcastchannel
var bc = new BroadcastChannel(requestBroadcastChannel);
bc.postMessage(window.location.toString());
bc.close();
// this puts a marker for the flutter app that the user wants to logout.
//
// in the flutter app, if this marker exists,
// we don't auth the cached user in `UserManager.init()`, and we clear the cached data.
setLocalStorage(requestNamespace, 'front-channel-logout', window.location.toString());
return true;
}
return false;
}
function getLocalStorage(namespace, key) {
const rawRes = localStorage.getItem('oidc.' + namespace + '.' + key);
if (!rawRes) {
return null;
}
return rawRes;
}
function setLocalStorage(namespace, key, value) {
const keysEntryKey = 'oidc.keys.' + namespace;
var keys = localStorage.getItem(keysEntryKey);
if (!keys) {
keys = "[]";
}
const parsedKeys = JSON.parse(keys);
if (!(parsedKeys instanceof Array)) {
console.error('parsedKeys is not an array.', parsedKeys);
}
parsedKeys.push(key);
localStorage.setItem(keysEntryKey, JSON.stringify(parsedKeys));
localStorage.setItem('oidc.' + namespace + '.' + key, value);
}
</script>
</head>
<body>
<h3>Authentication completed! Please close this page.</h3>
</body>
</html>