@@ -11,40 +11,73 @@ import { IProvider, LoginType, ProviderState } from '@microsoft/mgt-element';
1111import { AuthenticationParameters , AuthError , AuthResponse , Configuration , UserAgentApplication } from 'msal' ;
1212
1313/**
14- * config for MSAL authentication
14+ * base config for MSAL authentication
1515 *
1616 * @export
17- * @interface MsalConfig
17+ * @interface MsalConfigBase
1818 */
19- export interface MsalConfig {
19+ interface MsalConfigBase {
2020 /**
21- * clientId alphanumeric code
21+ * scopes
22+ *
23+ * @type {string[] }
24+ * @memberof MsalConfigBase
25+ */
26+ scopes ?: string [ ] ;
27+ /**
28+ * loginType if login uses popup
29+ *
30+ * @type {LoginType }
31+ * @memberof MsalConfigBase
32+ */
33+ loginType ?: LoginType ;
34+ /**
35+ * login hint value
2236 *
2337 * @type {string }
24- * @memberof MsalConfig
38+ * @memberof MsalConfigBase
2539 */
26- clientId : string ;
40+ loginHint ?: string ;
41+ }
42+
43+ /**
44+ * config for MSAL authentication where a UserAgentApplication already exists
45+ *
46+ * @export
47+ * @interface MsalConfig
48+ */
49+ export interface MsalUserAgentApplicationConfig extends MsalConfigBase {
2750 /**
28- * scopes
51+ * UserAgentApplication instance to use
2952 *
30- * @type {string[] }
53+ * @type {UserAgentApplication }
3154 * @memberof MsalConfig
3255 */
33- scopes ?: string [ ] ;
56+ userAgentApplication : UserAgentApplication ;
57+ }
58+
59+ /**
60+ * config for MSAL authentication
61+ *
62+ * @export
63+ * @interface MsalConfig
64+ */
65+ export interface MsalConfig extends MsalConfigBase {
3466 /**
35- * config authority
67+ * clientId alphanumeric code
3668 *
3769 * @type {string }
3870 * @memberof MsalConfig
3971 */
40- authority ?: string ;
72+ clientId : string ;
73+
4174 /**
42- * loginType if login uses popup
75+ * config authority
4376 *
44- * @type {LoginType }
77+ * @type {string }
4578 * @memberof MsalConfig
4679 */
47- loginType ?: LoginType ;
80+ authority ?: string ;
4881 /**
4982 * options as defined in
5083 * https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-js-initializing-client-applications#configuration-options
@@ -53,13 +86,6 @@ export interface MsalConfig {
5386 * @memberof MsalConfig
5487 */
5588 options ?: Configuration ;
56- /**
57- * login hint value
58- *
59- * @type {string }
60- * @memberof MsalConfig
61- */
62- loginHint ?: string ;
6389 /**
6490 * redirect Uri
6591 *
@@ -109,7 +135,7 @@ export class MsalProvider extends IProvider {
109135 private sessionStorageRequestedScopesKey = 'mgt-requested-scopes' ;
110136 private sessionStorageDeniedScopesKey = 'mgt-denied-scopes' ;
111137
112- constructor ( config : MsalConfig ) {
138+ constructor ( config : MsalConfig | MsalUserAgentApplicationConfig ) {
113139 super ( ) ;
114140 this . initProvider ( config ) ;
115141 }
@@ -329,38 +355,58 @@ export class MsalProvider extends IProvider {
329355 return false ;
330356 }
331357
332- private initProvider ( config : MsalConfig ) {
358+ private initProvider ( config : MsalConfig | MsalUserAgentApplicationConfig ) {
333359 this . scopes = typeof config . scopes !== 'undefined' ? config . scopes : [ 'user.read' ] ;
334360 this . _loginType = typeof config . loginType !== 'undefined' ? config . loginType : LoginType . Redirect ;
335361 this . _loginHint = config . loginHint ;
336362
337- if ( config . clientId ) {
338- const msalConfig : Configuration = config . options || { auth : { clientId : config . clientId } } ;
363+ let userAgentApplication : UserAgentApplication ;
364+ let clientId : string ;
339365
340- msalConfig . auth . clientId = config . clientId ;
341- msalConfig . cache = msalConfig . cache || { } ;
342- msalConfig . cache . cacheLocation = msalConfig . cache . cacheLocation || 'localStorage' ;
343- msalConfig . cache . storeAuthStateInCookie = msalConfig . cache . storeAuthStateInCookie || true ;
366+ if ( 'clientId' in config ) {
367+ if ( config . clientId ) {
368+ const msalConfig : Configuration = config . options || { auth : { clientId : config . clientId } } ;
344369
345- if ( config . authority ) {
346- msalConfig . auth . authority = config . authority ;
347- }
370+ msalConfig . auth . clientId = config . clientId ;
371+ msalConfig . cache = msalConfig . cache || { } ;
372+ msalConfig . cache . cacheLocation = msalConfig . cache . cacheLocation || 'localStorage' ;
373+ msalConfig . cache . storeAuthStateInCookie = msalConfig . cache . storeAuthStateInCookie || true ;
348374
349- if ( config . redirectUri ) {
350- msalConfig . auth . redirectUri = config . redirectUri ;
351- }
375+ if ( config . authority ) {
376+ msalConfig . auth . authority = config . authority ;
377+ }
378+
379+ if ( config . redirectUri ) {
380+ msalConfig . auth . redirectUri = config . redirectUri ;
381+ }
352382
353- this . clientId = config . clientId ;
383+ clientId = config . clientId ;
354384
355- this . _userAgentApplication = new UserAgentApplication ( msalConfig ) ;
356- this . _userAgentApplication . handleRedirectCallback (
357- response => this . tokenReceivedCallback ( response ) ,
358- ( error , state ) => this . errorReceivedCallback ( error , state )
359- ) ;
385+ userAgentApplication = new UserAgentApplication ( msalConfig ) ;
386+ } else {
387+ throw new Error ( 'clientId must be provided' ) ;
388+ }
389+ } else if ( 'userAgentApplication' in config ) {
390+ if ( config . userAgentApplication ) {
391+ userAgentApplication = config . userAgentApplication ;
392+ const msalConfig = userAgentApplication . getCurrentConfiguration ( ) ;
393+
394+ clientId = msalConfig . auth . clientId ;
395+ } else {
396+ throw new Error ( 'userAgentApplication must be provided' ) ;
397+ }
360398 } else {
361- throw new Error ( 'clientId must be provided' ) ;
399+ throw new Error ( 'either clientId or userAgentApplication must be provided' ) ;
362400 }
363401
402+ this . clientId = clientId ;
403+
404+ this . _userAgentApplication = userAgentApplication ;
405+ this . _userAgentApplication . handleRedirectCallback (
406+ response => this . tokenReceivedCallback ( response ) ,
407+ ( error , state ) => this . errorReceivedCallback ( error , state )
408+ ) ;
409+
364410 this . graph = createFromProvider ( this ) ;
365411
366412 this . trySilentSignIn ( ) ;
0 commit comments