@@ -17,6 +17,7 @@ import {
1717 PublicClientApplication
1818} from '@azure/msal-node' ;
1919import { AuthenticationProviderOptions } from '@microsoft/microsoft-graph-client' ;
20+ import { GraphEndpoint } from '@microsoft/mgt-element' ;
2021import { BrowserWindow , ipcMain } from 'electron' ;
2122import { CustomFileProtocolListener } from './CustomFileProtocol' ;
2223import { REDIRECT_URI , COMMON_AUTHORITY_URL } from './Constants' ;
@@ -65,6 +66,10 @@ export interface MsalElectronConfig {
6566 * @memberof MsalElectronConfig
6667 */
6768 cachePlugin ?: ICachePlugin ;
69+ /**
70+ * The base URL for the graph client
71+ */
72+ baseURL ?: GraphEndpoint ;
6873}
6974
7075/**
@@ -86,6 +91,11 @@ enum AuthState {
8691 LOGGED_OUT = 'logged_out'
8792}
8893
94+ /**
95+ * AccountDetails defines the available AccountInfo or undefined.
96+ */
97+ type AccountDetails = AccountInfo | undefined ;
98+
8999/**
90100 * ElectronAuthenticator class to be instantiated in the main process.
91101 * Responsible for MSAL authentication flow and token acqusition.
@@ -133,10 +143,10 @@ export class ElectronAuthenticator {
133143 * Logged in account
134144 *
135145 * @private
136- * @type {AccountInfo }
146+ * @type {AccountDetails }
137147 * @memberof ElectronAuthenticator
138148 */
139- private account : AccountInfo ;
149+ private account : AccountDetails ;
140150
141151 /**
142152 * Params to generate the URL for MSAL auth
@@ -182,7 +192,7 @@ export class ElectronAuthenticator {
182192 */
183193 private constructor ( config : MsalElectronConfig ) {
184194 this . setConfig ( config ) ;
185- this . account = null ;
195+ this . account = undefined ;
186196 this . mainWindow = config . mainWindow ;
187197 this . setRequestObjects ( config . scopes ) ;
188198 this . setupProvider ( ) ;
@@ -224,7 +234,7 @@ export class ElectronAuthenticator {
224234 clientId : config . clientId ,
225235 authority : config . authority ? config . authority : COMMON_AUTHORITY_URL
226236 } ,
227- cache : config . cachePlugin ? { cachePlugin : config . cachePlugin } : null ,
237+ cache : config . cachePlugin ? { cachePlugin : config . cachePlugin } : undefined ,
228238 system : {
229239 loggerOptions : {
230240 loggerCallback ( loglevel , message , containsPii ) { } ,
@@ -255,7 +265,7 @@ export class ElectronAuthenticator {
255265 this . authCodeRequest = {
256266 scopes : requestScopes ,
257267 redirectUri,
258- code : null
268+ code : ''
259269 } ;
260270 }
261271
@@ -311,7 +321,7 @@ export class ElectronAuthenticator {
311321 * @return {* } {Promise<string>}
312322 * @memberof ElectronAuthenticator
313323 */
314- protected async getAccessToken ( options ?: AuthenticationProviderOptions ) : Promise < string > {
324+ protected async getAccessToken ( options ?: AuthenticationProviderOptions ) : Promise < string | undefined > {
315325 let authResponse ;
316326 const scopes = options && options . scopes ? options . scopes : this . authCodeUrlParams . scopes ;
317327 const account = this . account || ( await this . getAccount ( ) ) ;
@@ -326,6 +336,7 @@ export class ElectronAuthenticator {
326336 if ( authResponse ) {
327337 return authResponse . accessToken ;
328338 }
339+ return undefined ;
329340 }
330341
331342 /**
@@ -337,7 +348,7 @@ export class ElectronAuthenticator {
337348 * @return {* } {Promise<AuthenticationResult>}
338349 * @memberof ElectronAuthenticator
339350 */
340- protected async getTokenSilent ( tokenRequest , scopes ?) : Promise < AuthenticationResult > {
351+ protected async getTokenSilent ( tokenRequest , scopes ?) : Promise < AuthenticationResult | null > {
341352 try {
342353 return await this . clientApplication . acquireTokenSilent ( tokenRequest ) ;
343354 } catch ( error ) {
@@ -366,7 +377,7 @@ export class ElectronAuthenticator {
366377 protected async logout ( ) : Promise < void > {
367378 if ( this . account ) {
368379 await this . clientApplication . getTokenCache ( ) . removeAccount ( this . account ) ;
369- this . account = null ;
380+ this . account = undefined ;
370381 }
371382 }
372383
@@ -379,8 +390,8 @@ export class ElectronAuthenticator {
379390 * @memberof ElectronAuthenticator
380391 */
381392 private async setAccountFromResponse ( response : AuthenticationResult ) {
382- if ( response !== null ) {
383- this . account = response . account ;
393+ if ( response ) {
394+ this . account = response ? .account || undefined ;
384395 } else {
385396 this . account = await this . getAccount ( ) ;
386397 }
@@ -412,7 +423,7 @@ export class ElectronAuthenticator {
412423 . acquireTokenByCode ( {
413424 ...this . authCodeRequest ,
414425 scopes : requestScopes ,
415- code : authCode
426+ code : authCode || ''
416427 } )
417428 . catch ( ( e : AuthError ) => {
418429 throw e ;
@@ -429,7 +440,7 @@ export class ElectronAuthenticator {
429440 * @return {* } {Promise<string>}
430441 * @memberof ElectronAuthenticator
431442 */
432- private async listenForAuthCode ( navigateUrl : string , prompt_type : promptType ) : Promise < string > {
443+ private async listenForAuthCode ( navigateUrl : string , prompt_type : promptType ) : Promise < string | null > {
433444 this . setAuthWindow ( true ) ;
434445 await this . authWindow . loadURL ( navigateUrl ) ;
435446 return new Promise ( ( resolve , reject ) => {
@@ -474,20 +485,12 @@ export class ElectronAuthenticator {
474485 * @return {* } {Promise<AccountInfo>}
475486 * @memberof ElectronAuthenticator
476487 */
477- private async getAccount ( ) : Promise < AccountInfo > {
488+ private async getAccount ( ) : Promise < AccountDetails > {
478489 const cache = this . clientApplication . getTokenCache ( ) ;
479490 const currentAccounts = await cache . getAllAccounts ( ) ;
480-
481- if ( currentAccounts === null ) {
482- return null ;
483- }
484-
485- if ( currentAccounts . length > 1 ) {
491+ if ( currentAccounts ?. length >= 1 ) {
486492 return currentAccounts [ 0 ] ;
487- } else if ( currentAccounts . length === 1 ) {
488- return currentAccounts [ 0 ] ;
489- } else {
490- return null ;
491493 }
494+ return undefined ;
492495 }
493496}
0 commit comments