-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoidc-client.js
202 lines (164 loc) · 5.49 KB
/
oidc-client.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const localStorageKey = "sessionData";
function createClient(config) {
let client = new OIDCClient(config);
return client;
}
class OIDCClient {
config;
accessToken;
oidcClaims;
accessTokenExpiration;
constructor(config) {
console.log("Creating a new instance of OIDCClient");
this.config = config;
this.getLocalStorageData();
}
loginWithRedirect(parameters) {
console.log("oauthAuthorize");
let queryStringParams = {
response_type: 'code',
client_id: this.config.clientId,
redirect_uri: this.config.redirectUri,
...parameters
}
let authUrl = this.config.azServerUrl + this.config.azEndpoint + "?" + new URLSearchParams(queryStringParams).toString();
window.location.replace(authUrl);
}
logoutWithRedirect() {
console.log("oauthLogout");
let queryStringParams = {
//post_logout_redirect_uri: this.config.redirectUri
TargetResource: this.config.redirectUri
}
let authUrl = this.config.azServerUrl + this.config.logoutEndpoint + "?" + new URLSearchParams(queryStringParams).toString();
window.location.replace(authUrl);
this.clearLocalStorageData();
}
async handleRedirectBack() {
const codeMatch = window.location.href.match('[?#&]code=([^&]*)');
if (codeMatch && codeMatch[1]) {
return await this.fetchTokens(codeMatch[1]);
}
}
async fetchTokens(azCode) {
console.log("fetchTokens with code " + azCode);
let bodyParams = {
grant_type: 'authorization_code',
code: azCode,
client_id: this.config.clientId,
client_secret: this.config.clientSecret,
redirect_uri: this.config.redirectUri
}
let response = await fetch(this.config.azServerUrl + this.config.tokenEndpoint, {
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams(bodyParams).toString()
})
console.log(response);
let body = await response.json();
this.parseAzServerResponse(body);
this.storeLocalStorageData(body);
console.log("OIDC Claims");
console.log(this.oidcClaims);
}
parseAzServerResponse(body) {
this.accessToken = body.access_token;
this.oidcClaims = this.verifyIdToken(body.id_token, this.config.azServerUrl, this.config.clientId).payload;
this.accessTokenExpiration = Math.floor(Date.now() / 1000) + 1;
}
getLocalStorageData() {
let azServerData = localStorage.getItem(localStorageKey);
if (azServerData) {
this.parseAzServerResponse(JSON.parse(azServerData))
}
}
clearLocalStorageData() {
localStorage.clear();
}
storeLocalStorageData(body) {
localStorage.setItem(localStorageKey, JSON.stringify(body));
}
getOidcClaims() {
return this.oidcClaims;
}
getAccessToken() {
return this.accessToken;
}
isUserAuthenticated() {
if (!this.oidcClaims) {
return false;
}
if (this.isExpired(this.oidcClaims.exp)) {
return false;
}
return true;
}
isNumber = n => typeof n === 'number';
verifyIdToken(idToken, expectedIssuer, expectedAudience) {
let decodedToken = this.decodeJWT(idToken);
var aud = decodedToken.payload.aud;
var sub = decodedToken.payload.sub;
var iss = decodedToken.payload.iss;
var exp = decodedToken.payload.exp;
var iat = decodedToken.payload.iat;
if (!iss || typeof iss !== 'string') {
throw new Error("Issuer (iss) not present");
}
if (iss !== expectedIssuer) {
throw new Error("Issuer (iss) mismatch :" +expectedIssuer+ "Iss: "+iss)
}
if (!sub || typeof sub !== 'string') {
throw new Error("Subject (sub) not present");
}
if (!aud || typeof aud !== 'string') {
throw new Error("Audience (aud) not present");
}
if (aud !== expectedAudience) {
throw new Error("Audience (aud) mismatch");
}
if (!exp || !this.isNumber(exp)) {
throw new Error("Expiration Time (exp) not present");
}
if (this.isExpired(exp)) {
throw new Error("Token expired");
}
if (!iat || !this.isNumber(exp)) {
throw new Error("Issued At (iat) not present");
}
return decodedToken;
}
isExpired(exp) {
let expTimeDate = new Date(0);
expTimeDate.setUTCSeconds(exp);
let now = new Date();
if (now > expTimeDate) {
return true;
}
return false;
}
decodeJWT(Jwt) {
let parts = Jwt.split('.');
let header;
let payload;
if (parts.length !== 3) {
throw new Error('Malformed JWT');
}
header = JSON.parse(this.base64urldecodeStr(parts[0]));
payload = JSON.parse(this.base64urldecodeStr(parts[1]));
return {
header: header,
payload: payload,
encoded: {
header: parts[0],
payload: parts[1],
signature: parts[2]
}
};
}
base64urldecodeStr(str) {
str = str.replace(/-/g, '+').replace(/_/g, '/');
return atob(str);
}
}