-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMsalService.ts
323 lines (268 loc) · 9.76 KB
/
MsalService.ts
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { filter, take } from 'rxjs/operators';
import { ApiService } from '../ApiService/ApiService';
import { LocalStorageService } from '../LocalStorage/LocalStorageService';
declare var azureClientId;
declare var azureTenantId;
declare var dynamicsCRMTenantId;
const dynamicsCrmScopes: string[] = ["https://" + dynamicsCRMTenantId + "/.default", "openid", "offline_access"];
@Injectable()
export class MsalService {
private _user: IUser;
constructor(private localStorageService: LocalStorageService,
private httpClient: HttpClient,
private apiService: ApiService) {
}
get user(): IUser {
return this._getUser();
}
get hasUser(): boolean {
return !!this.user;
}
public processAuthorizationCallback(): IAuthorizationCallback {
if (window.location.hash && window.location.hash.length > 1) {
let hash: string[] = window.location.hash.substring(1).split("&");
let authorizationCallback: IAuthorizationCallback = this.buildAuthorizationCallback(hash);
return authorizationCallback;
}
return {};
}
private buildAuthorizationCallback(hash: string[]): IAuthorizationCallback {
return {
code: this.findSegment(hash, "code"),
error: this.findSegment(hash, "error"),
error_description: this.findSegment(hash, "error_description"),
state: this.findSegment(hash, "state")
}
}
private findSegment(hash: string[], segment: string): string {
let match: string = hash.find((value: string) => { return value.startsWith(segment + "=") });
if (match) {
return match.substring((segment + "=").length);
}
return null;
}
public login(): void {
let url: string = `https://login.microsoftonline.com/${azureTenantId}/oauth2/v2.0/authorize?`;
let params: string[] = [
`client_id=${azureClientId}`,
`response_type=code`,
`response_mode=fragment`,
`scope=${this.getScopes()}`,
`redirect_uri=${this.getRedirectUrl()}`
]
window.location.replace(url + params.join("&"));
}
public logout(userRequested?: boolean): void {
this.clearCache();
if (userRequested) {
window.location.replace(`https://login.microsoftonline.com/common/oauth2/v2.0/logout?post_logout_redirect_uri=${this.getRedirectUrl()}`);
}
}
public cacheAuthentication(json: any): void {
this.localStorageService.setItem(this.accessToken, json.access_token);
this.localStorageService.setItem(this.expiresIn, (this.getUnixTime() + json.expires_in - 1).toString());
this.localStorageService.setItem(this.refreshToken, json.refresh_token);
this._user = null;
}
private getUnixTime(): number {
return (new Date().getTime() / 1000);
}
private get accessToken(): string {
return "access_token_mvgo_";
}
private get expiresIn(): string {
return "expires_in_mvgo_";
}
private get refreshToken(): string {
return "refresh_token_mvgo_";
}
public acquireToken(resourceUri: string, tokenCallback: ITokenCallback): void {
// resourceUri not currently used.
// MSAL supports multiple tokens for multiple different resources.
// Multiple resource support has not been implemented.
this.refreshAccessToken().then((token: any) => {
tokenCallback(token, null);
}).catch((error) => {
tokenCallback(null, error);
})
}
private tokenSubject: BehaviorSubject<string> = new BehaviorSubject(null);
private isRefreshingToken: boolean;
private refreshAccessToken(): Promise<any> {
let promise: Promise<any> = new Promise<any>((resolve, reject) => {
try {
let token: string = this.localStorageService.getItem(this.accessToken);
let expiration: number = parseInt(this.localStorageService.getItem(this.expiresIn));
if (this.getUnixTime() >= expiration || isNaN(expiration) || !token) {
if (!this.isRefreshingToken) {
this.isRefreshingToken = true;
try {
this.tokenSubject.next(null);
let refreshToken: string = this.localStorageService.getItem(this.refreshToken);
if (refreshToken) {
this.httpClient.post(this.apiService.uri + "/api/Auth/RefreshToken", {
authorizationCode: refreshToken,
}).subscribe(
(json: any) => {
this.isRefreshingToken = false;
this.cacheAuthentication(json);
this.tokenSubject.next(json.access_token);
resolve(json.access_token);
},
(error: any) => {
this.isRefreshingToken = false;
reject(error);
});
} else {
this.isRefreshingToken = false;
reject("No refresh token found");
}
} catch (error) {
this.isRefreshingToken = false;
reject(error);
}
} else {
try {
this.tokenSubject.pipe(filter(token => token != null)).pipe(take(1)).subscribe(
(token: any) => {
resolve(token);
},
(error: any) => {
reject(error);
});
} catch (error) {
reject(error);
}
}
} else if (token) {
resolve(token);
}
} catch (error) {
reject(error);
}
});
return promise;
}
public clearCache(): void {
this.localStorageService.removeItem(this.accessToken);
this.localStorageService.removeItem(this.expiresIn);
this.localStorageService.removeItem(this.refreshToken);
this._user = null;
}
public getRedirectUrl(): string {
let url: string = window.location.href;
let hashIndex: number = window.location.hash ? url.indexOf(window.location.hash) : -1;
url = ((hashIndex >= 0 ? url.substr(0, hashIndex) : url) + "#/login/");
if (window.location.host !== "localhost") {
url = url.replace("http:", "https:")
}
return encodeURI(url);
}
public getScopes(): string {
return encodeURI(dynamicsCrmScopes.join(' '));
}
private _getUser(): IUser {
var rawIdToken = this.localStorageService.getItem(this.accessToken);
if (rawIdToken) {
if (this._user) {
return this._user;
}
let decodedToken: any = this.decodeJwt(rawIdToken);
try {
var base64IdToken = decodedToken.JWSPayload;
var base64Decoded = this.base64DecodeStringUrlSafe(base64IdToken);
if (!base64Decoded) {
return null;
}
let tokenJson = JSON.parse(base64Decoded);
let user: IUser = {
name: tokenJson.name,
userIdentifier: tokenJson.upn
};
this._user = user;
return this._user;
}
finally {
}
}
return null;
}
private base64DecodeStringUrlSafe(base64IdToken: string): string {
base64IdToken = base64IdToken.replace(/-/g, "+").replace(/_/g, "/");
if (window.atob) {
return decodeURIComponent(encodeURIComponent(window.atob(base64IdToken)));
}
else {
return decodeURIComponent(encodeURIComponent(this.decode(base64IdToken)));
}
}
private decode(base64IdToken: string): string {
var codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
base64IdToken = String(base64IdToken).replace(/=+$/, "");
var length = base64IdToken.length;
if (length % 4 === 1) {
throw new Error("The token to be decoded is not correctly encoded.");
}
var h1, h2, h3, h4, bits, c1, c2, c3, decoded = "";
for (var i = 0; i < length; i += 4) {
//Every 4 base64 encoded character will be converted to 3 byte string, which is 24 bits
// then 6 bits per base64 encoded character
h1 = codes.indexOf(base64IdToken.charAt(i));
h2 = codes.indexOf(base64IdToken.charAt(i + 1));
h3 = codes.indexOf(base64IdToken.charAt(i + 2));
h4 = codes.indexOf(base64IdToken.charAt(i + 3));
// For padding, if last two are "="
if (i + 2 === length - 1) {
bits = h1 << 18 | h2 << 12 | h3 << 6;
c1 = bits >> 16 & 255;
c2 = bits >> 8 & 255;
decoded += String.fromCharCode(c1, c2);
break;
}
// if last one is "="
else if (i + 1 === length - 1) {
bits = h1 << 18 | h2 << 12;
c1 = bits >> 16 & 255;
decoded += String.fromCharCode(c1);
break;
}
bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
// then convert to 3 byte chars
c1 = bits >> 16 & 255;
c2 = bits >> 8 & 255;
c3 = bits & 255;
decoded += String.fromCharCode(c1, c2, c3);
}
return decoded;
}
private decodeJwt(jwtToken: string): any {
var idTokenPartsRegex = /^([^\.\s]*)\.([^\.\s]+)\.([^\.\s]*)$/;
var matches = idTokenPartsRegex.exec(jwtToken);
if (!matches || matches.length < 4) {
//this._requestContext.logger.warn("The returned id_token is not parseable.");
return null;
}
var crackedToken = {
header: matches[1],
JWSPayload: matches[2],
JWSSig: matches[3]
};
return crackedToken;
}
}
export interface ITokenCallback {
(token: string, error: string): void;
};
export interface IUser {
name: string;
userIdentifier: string;
}
export interface IAuthorizationCallback {
code?: string;
state?: string;
error?: string;
error_description?: string;
}