-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
167 lines (148 loc) · 5.18 KB
/
index.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>OAuth</title>
</head>
<body>
<div>
<div>
<h1>Sign-in via OAuth</h1>
<button onclick="signInVia('')">Internal</button>
<button onclick="signInVia('Google')">Google</button>
<button onclick="signInVia('Facebook')">Facebook</button>
<button onclick="signInVia('Microsoft')">Microsoft</button>
<button onclick="signInVia('OpenIdConnect')">AzureAD</button>
<button onclick="signInVia('GitHub')">GitHub</button>
<button onclick="signInVia('Twitter')">Twitter</button>
</div>
<div>
<h1>Values</h1>
<button onclick="getValues()">Get Values</button>
<button onclick="getDocuments()">Get Documents</button>
<button onclick="getUsers()">Get Users</button>
</div>
<div>
<h1>Other</h1>
<button onclick="refreshToken(true)">Refresh token from IdentityServer</button>
<button onclick="refreshToken(false)">Refresh token</button>
<button onclick="logOut()">Log Out</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/oidc-client-ts/2.0.1/browser/oidc-client-ts.min.js"
integrity="sha512-ymGcBIbfvSF053DTI0N2/9xMVbZtBSb3E5eR38SG+Ei8ITM/1XFQILwtRDD8QgUhSgjr2cA05BiQbQN76Nc6/w=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script type="module">
let _accessToken = '';
let _refreshToken = '';
const authCallbackPath = '/index.html?auth-callback=1';
const logoutCallbackPath = '/index.html?logout-callback=1';
const scopes = 'offline_access';
const backendUri = `${window.location.protocol}//${window.location.hostname}${window.location.port ? ':' : ''}${window.location.port}`;
const redirectUri = `${backendUri}${authCallbackPath}`;
const logoutRedirectUri = `${backendUri}${logoutCallbackPath}`;
const clientId = 'web_client';
const clientSettings = {
authority: backendUri,
client_id: clientId,
redirect_uri: redirectUri,
post_logout_redirect_uri: logoutRedirectUri,
response_type: 'code',
filterProtocolClaims: true,
loadUserInfo: false,
scope: scopes,
};
const userManager = new oidc.UserManager(clientSettings);
if (window.location.href.includes('auth-callback')) {
userManager.signinPopupCallback();
} else if (window.location.href.includes('logout-callback')) {
userManager.signoutPopupCallback();
}
async function logOut() {
try {
await userManager.signoutPopup();
alert('LogOut succeeded');
_accessToken = null;
_refreshToken = null;
} catch (e) {
console.error('Error during authentication', e);
}
}
async function refreshToken(isFromIdentityServer) {
const refreshToken = isFromIdentityServer ? "123" : _refreshToken;
if (!refreshToken) {
alert('Not logged in');
return;
}
try {
const client = userManager._client;
await client._tokenClient.exchangeRefreshToken({refresh_token: refreshToken});
alert('Refresh token succeeded');
} catch (e) {
alert('Refresh token failed');
console.error('Error during refresh token', e);
}
}
async function signInVia(provider) {
try {
const user = await openExternalLoginPopup(provider);
alert('Authentication succeeded');
console.log('User', user);
_accessToken = user.access_token;
_refreshToken = user.refresh_token;
} catch (e) {
console.error('Error during authentication', e);
}
}
async function openExternalLoginPopup(provider) {
try {
const user = await userManager.signinPopup({
extraQueryParams: {provider: provider},
prompt: 'login'
});
return user;
} catch (e) {
console.error('Error during external authentication', e);
throw e;
}
}
async function getData(url) {
try {
const data = await fetch(url,
{
headers: {
'Authorization': `Bearer ${_accessToken}`,
'Content-Type': 'application/json;'
}
})
if (!data.ok) {
alert('Error (Unauthorized)');
return null;
}
const dt = await data.json();
if (dt != null) {
console.log(dt);
alert(dt);
}
} catch (e) {
alert(e);
}
}
function getValues() {
getData('/api/values');
}
function getUsers() {
getData('/api/permissions/users');
}
function getDocuments() {
getData('/api/permissions/documents');
}
window.signInVia = signInVia;
window.getValues = getValues;
window.getDocuments = getDocuments;
window.getUsers = getUsers;
window.logOut = logOut;
window.refreshToken = refreshToken;
</script>
</body>
</html>