11import * as Msal from "@azure/msal-browser" ;
2+ import { ISettingsProvider } from "@paperbits/common/configuration/ISettingsProvider" ;
23import { IAuthenticator , AccessToken } from "." ;
4+ import { SettingNames } from "../constants" ;
35
46
5- const aadClientId = "a962e1ed-5694-4abe-9e9b-d08d35877efc" ; // test app
6- const loginRequest = { scopes : [ "openid" , "profile" , "https://management.azure.com/user_impersonation" ] , account : null } ;
7- const authority = "https://login.microsoftonline.com/common" ;
8- const redirectUri = "https://apimanagement-cors-proxy-df.azure-api.net/portal/signin-aad" ;
7+ // const aadClientId = "a962e1ed-5694-4abe-9e9b-d08d35877efc"; // test app PROD
8+ // const aadClientId = "4c6edb5e-d0fb-4ca1-ac29-8c181c1a9522"; // test app PPE
9+
10+ // const authority = "https://login.microsoftonline.com/common"; // PROD
11+ // const authority = "https://login.windows-ppe.net/common"; // PPE
12+
13+ // const redirectUri = "https://apimanagement-cors-proxy-df.azure-api.net/portal/signin-aad";
14+
15+ // login example
16+ // http://localhost:8080?subscriptionId=b8ff56dc-3bc7-4174-a1e8-3726ab15d0e2&resourceGroupName=Admin-ResourceGroup&serviceName=igo-east
917
1018export class ArmAuthenticator implements IAuthenticator {
1119 private accessToken : AccessToken ;
20+ private loginRequest : Msal . SilentRequest ;
1221 private msalInstance : Msal . PublicClientApplication ;
1322 private authPromise : Promise < AccessToken > ;
1423
15- constructor ( ) {
24+ private initializePromise : Promise < void > ;
25+
26+ constructor (
27+ private readonly settingsProvider : ISettingsProvider
28+ ) { }
29+
30+ private async ensureInitialized ( ) : Promise < void > {
31+ if ( ! this . initializePromise ) {
32+ this . initializePromise = this . initInstance ( ) ;
33+ }
34+ return this . initializePromise ;
35+ }
36+
37+ private async initInstance ( ) : Promise < void > {
38+ const settings = await this . settingsProvider . getSettings ( ) ;
39+ const aadClientId = settings [ SettingNames . aadClientId ] ;
40+ const authority = settings [ SettingNames . aadAuthority ] ;
41+ this . loginRequest = settings [ SettingNames . aadLoginRequest ] ;
42+
43+ if ( ! aadClientId || ! authority || ! this . loginRequest ) {
44+ throw new Error ( "Settings was not provided for Msal.Configuration" ) ;
45+ }
46+
47+ const redirectUri = location . origin ;
48+
1649 const msalConfig : Msal . Configuration = {
1750 auth : {
1851 clientId : aadClientId ,
@@ -29,8 +62,8 @@ export class ArmAuthenticator implements IAuthenticator {
2962 }
3063
3164 public async checkCallbacks ( ) : Promise < Msal . AuthenticationResult > {
65+ await this . ensureInitialized ( ) ;
3266 try {
33-
3467 return await this . msalInstance . handleRedirectPromise ( ) ;
3568 }
3669 catch ( error ) {
@@ -49,6 +82,7 @@ export class ArmAuthenticator implements IAuthenticator {
4982 }
5083
5184 private async tryAcquireToken ( ) : Promise < AccessToken > {
85+ await this . ensureInitialized ( ) ;
5286 const account = this . getAccount ( ) ;
5387
5488 if ( ! account ) {
@@ -59,22 +93,22 @@ export class ArmAuthenticator implements IAuthenticator {
5993 return parsedToken ;
6094 }
6195
62- await this . msalInstance . acquireTokenRedirect ( loginRequest ) ;
96+ await this . msalInstance . acquireTokenRedirect ( this . loginRequest ) ;
6397 return ;
6498 }
6599
66- loginRequest . account = account ;
100+ this . loginRequest . account = account ;
67101
68102 try {
69- const result = await this . msalInstance . acquireTokenSilent ( loginRequest ) ;
103+ const result = await this . msalInstance . acquireTokenSilent ( this . loginRequest ) ;
70104 const token = AccessToken . parse ( `${ result . tokenType } ${ result . accessToken } ` ) ;
71105
72106 return token ;
73107 }
74108 catch ( error ) {
75109 if ( error instanceof Msal . InteractionRequiredAuthError ) {
76110 // fallback to interaction when silent call fails
77- await this . msalInstance . acquireTokenRedirect ( loginRequest ) ;
111+ await this . msalInstance . acquireTokenRedirect ( this . loginRequest ) ;
78112 }
79113 else {
80114 console . warn ( error ) ;
0 commit comments