- Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathAuthCodeMSALBrowserAuthenticationProvider.ts
78 lines (73 loc) · 3.11 KB
/
AuthCodeMSALBrowserAuthenticationProvider.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
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/**
* @module AuthCodeMSALBrowserAuthenticationProvider
*/
import{AuthenticationResult,InteractionRequiredAuthError,InteractionType,PublicClientApplication}from"@azure/msal-browser";
import{GraphClientError}from"../../GraphClientError";
import{AuthenticationProvider}from"../../IAuthenticationProvider";
import{AuthCodeMSALBrowserAuthenticationProviderOptions}from"../msalOptions/MSALAuthenticationProviderOptions";
/**
* an AuthenticationProvider implementation supporting msal-browser library.
* This feature is introduced in Version 3.0.0
* @class
* @extends AuthenticationProvider
*/
exportclassAuthCodeMSALBrowserAuthenticationProviderimplementsAuthenticationProvider{
/**
* @public
* @constructor
* Creates an instance of ImplicitMSALAuthenticationProvider
* @param {PublicClientApplication} msalApplication - An instance of MSAL PublicClientApplication
* @param {AuthCodeMSALBrowserAuthenticationProviderOptions} options - An instance of MSALAuthenticationProviderOptions
* @returns An instance of ImplicitMSALAuthenticationProvider
*/
publicconstructor(privatepublicClientApplication: PublicClientApplication,privateoptions: AuthCodeMSALBrowserAuthenticationProviderOptions){
if(!options||!publicClientApplication){
thrownewGraphClientError("Please pass valid PublicClientApplication instance and AuthCodeMSALBrowserAuthenticationProviderOptions instance to instantiate MSALBrowserAuthenticationProvider");
}
}
/**
* @public
* @async
* To get the access token for the request
* @returns The promise that resolves to an access token
*/
publicasyncgetAccessToken(): Promise<string>{
constscopes=this.options&&this.options.scopes;
constaccount=this.options&&this.options.account;
consterror=newGraphClientError();
if(!scopes||scopes.length===0){
error.name="Empty Scopes";
error.message="Scopes cannot be empty, Please provide scopes";
throwerror;
}
try{
constresponse: AuthenticationResult=awaitthis.publicClientApplication.acquireTokenSilent({
scopes,
account,
});
if(!response||!response.accessToken){
error.name="Access token is undefined";
error.message="Received empty access token from PublicClientApplication";
throwerror;
}
returnresponse.accessToken;
}catch(error){
if(errorinstanceofInteractionRequiredAuthError){
if(this.options.interactionType===InteractionType.Redirect){
this.publicClientApplication.acquireTokenRedirect({ scopes });
}elseif(this.options.interactionType===InteractionType.Popup){
constresponse: AuthenticationResult=awaitthis.publicClientApplication.acquireTokenPopup({ scopes });
returnresponse.accessToken;
}
}else{
throwerror;
}
}
}
}