forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImplicitMSALAuthenticationProvider.ts
103 lines (96 loc) · 3.46 KB
/
ImplicitMSALAuthenticationProvider.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
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/**
* @module ImplicitMSALAuthenticationProvider
*/
import { AuthenticationParameters, AuthResponse, InteractionRequiredAuthError, UserAgentApplication } from "msal";
import { AuthenticationProvider } from "./IAuthenticationProvider";
import { AuthenticationProviderOptions } from "./IAuthenticationProviderOptions";
import { MSALAuthenticationProviderOptions } from "./MSALAuthenticationProviderOptions";
/**
* @class
* Class representing ImplicitMSALAuthenticationProvider
* @extends AuthenticationProvider
*/
export class ImplicitMSALAuthenticationProvider implements AuthenticationProvider {
/**
* @private
* A member holding an instance of MSALAuthenticationProviderOptions
*/
private options: MSALAuthenticationProviderOptions;
/**
* @private
* A member holding an instance of MSAL UserAgentApplication
*/
private msalApplication: UserAgentApplication;
/**
* @public
* @constructor
* Creates an instance of ImplicitMSALAuthenticationProvider
* @param {UserAgentApplication} msalApplication - An instance of MSAL UserAgentApplication
* @param {MSALAuthenticationProviderOptions} options - An instance of MSALAuthenticationProviderOptions
* @returns An instance of ImplicitMSALAuthenticationProvider
*/
public constructor(msalApplication: UserAgentApplication, options: MSALAuthenticationProviderOptions) {
this.options = options;
this.msalApplication = msalApplication;
}
/**
* @public
* @async
* To get the access token
* @param {AuthenticationProviderOptions} authenticationProviderOptions - The authentication provider options object
* @returns The promise that resolves to an access token
*/
public async getAccessToken(authenticationProviderOptions?: AuthenticationProviderOptions): Promise<string> {
const options: MSALAuthenticationProviderOptions = authenticationProviderOptions as MSALAuthenticationProviderOptions;
let scopes: string[];
if (typeof options !== "undefined") {
scopes = options.scopes;
}
if (typeof scopes === "undefined" || scopes.length === 0) {
scopes = this.options.scopes;
}
if (scopes.length === 0) {
const error = new Error();
error.name = "EmptyScopes";
error.message = "Scopes cannot be empty, Please provide a scopes";
throw error;
}
if (this.msalApplication.getAccount()) {
const tokenRequest: AuthenticationParameters = {
scopes,
};
try {
const authResponse: AuthResponse = await this.msalApplication.acquireTokenSilent(tokenRequest);
return authResponse.accessToken;
} catch (error) {
if (error instanceof InteractionRequiredAuthError) {
try {
const authResponse: AuthResponse = await this.msalApplication.acquireTokenPopup(tokenRequest);
return authResponse.accessToken;
} catch (error) {
throw error;
}
} else {
throw error;
}
}
} else {
try {
const tokenRequest: AuthenticationParameters = {
scopes,
};
await this.msalApplication.loginPopup(tokenRequest);
const authResponse: AuthResponse = await this.msalApplication.acquireTokenSilent(tokenRequest);
return authResponse.accessToken;
} catch (error) {
throw error;
}
}
}
}