Skip to content

Commit dd41eb1

Browse files
authored
fix: restore provided msal public client behavior (#1931)
a regression removed the functionality to correctly use an explicity provided Msal PublicClientApplication originaly delivered in #1181. This fixes the regresssion, cleans up repeated code, and adds missing doc comments
1 parent a573bdf commit dd41eb1

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

packages/mgt-element/src/providers/IProvider.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,22 @@ export abstract class IProvider implements AuthenticationProvider {
2626
*/
2727
public graph: IGraph;
2828

29+
/**
30+
* Specifies if the provider has enabled support for multiple accounts
31+
*
32+
* @protected
33+
* @type {boolean}
34+
* @memberof IProvider
35+
*/
36+
protected isMultipleAccountDisabled: boolean = true;
37+
2938
/**
3039
* Specifies if Multi account functionality is supported by the provider and enabled.
3140
*
3241
* @readonly
3342
* @type {boolean}
3443
* @memberof IProvider
3544
*/
36-
protected isMultipleAccountDisabled: boolean = true;
3745
public get isMultiAccountSupportedAndEnabled(): boolean {
3846
return false;
3947
}

packages/providers/mgt-msal2-provider/src/Msal2Provider.ts

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,13 @@ export interface Msal2PublicClientApplicationConfig extends Msal2ConfigBase {
150150
* @export
151151
* @enum {number}
152152
*/
153+
// tslint:disable: completed-docs
153154
export enum PromptType {
154155
SELECT_ACCOUNT = 'select_account',
155156
LOGIN = 'login',
156157
CONSENT = 'consent'
157158
}
159+
// tslint:enable: completed-docs
158160

159161
/**
160162
* MSAL2Provider using msal-browser to acquire tokens for authentication
@@ -223,6 +225,7 @@ export class Msal2Provider extends IProvider {
223225
* @type {Configuration}
224226
* @memberof Msal2Provider
225227
*/
228+
// tslint:disable-next-line: variable-name
226229
private ms_config: Configuration;
227230

228231
/**
@@ -255,14 +258,37 @@ export class Msal2Provider extends IProvider {
255258
public scopes: string[];
256259

257260
/**
258-
*
259261
* Enables multi account functionality if true, disables if false
262+
*
260263
* @private
261264
* @type {boolean}
262265
* @memberof Msal2Provider
263266
*/
264267
public isMultipleAccountEnabled: boolean = true;
265268

269+
/**
270+
* Indicates if multi account functionality is disabled
271+
*
272+
* @protected
273+
* @type {boolean}
274+
* @memberof Msal2Provider
275+
*/
276+
protected get isMultiAccountDisabled(): boolean {
277+
return !this.isMultipleAccountEnabled;
278+
}
279+
280+
/**
281+
* Disables or enables multi account functionality
282+
* Uses isMultipleAccountEnabled as the backing property
283+
* Property provided to ensure adherence to the IProvider interface
284+
*
285+
* @protected
286+
* @memberof Msal2Provider
287+
*/
288+
protected set isMultiAccountDisabled(value: boolean) {
289+
this.isMultipleAccountEnabled = !value;
290+
}
291+
266292
/**
267293
* Specifies if Multi account functionality is supported by the provider and enabled.
268294
*
@@ -319,27 +345,11 @@ export class Msal2Provider extends IProvider {
319345
} else {
320346
throw new Error('clientId must be provided');
321347
}
322-
this.ms_config.system = msalConfig.system || {};
323-
this.ms_config.system.iframeHashTimeout = msalConfig.system.iframeHashTimeout || 10000;
324-
this._loginType = typeof config.loginType !== 'undefined' ? config.loginType : LoginType.Redirect;
325-
this._loginHint = typeof config.loginHint !== 'undefined' ? config.loginHint : null;
326-
this._sid = typeof config.sid !== 'undefined' ? config.sid : null;
327-
this._domainHint = typeof config.domainHint !== 'undefined' ? config.domainHint : null;
328-
this.scopes = typeof config.scopes !== 'undefined' ? config.scopes : ['user.read'];
329-
this._publicClientApplication = new PublicClientApplication(this.ms_config);
330-
this._prompt = typeof config.prompt !== 'undefined' ? config.prompt : PromptType.SELECT_ACCOUNT;
331-
this.isMultipleAccountDisabled =
332-
typeof config.isMultiAccountDisabled !== 'undefined' ? config.isMultiAccountDisabled : false;
333-
this.graph = createFromProvider(this);
334-
try {
335-
const tokenResponse = await this._publicClientApplication.handleRedirectPromise();
336-
if (tokenResponse !== null) {
337-
this.handleResponse(tokenResponse?.account);
338-
} else {
339-
this.trySilentSignIn();
340-
}
341-
} catch (e) {
342-
throw e;
348+
} else if ('publicClientApplication' in config) {
349+
if (config.publicClientApplication) {
350+
this._publicClientApplication = config.publicClientApplication;
351+
} else {
352+
throw new Error('publicClientApplication must be provided');
343353
}
344354
} else {
345355
throw new Error('either clientId or publicClientApplication must be provided');
@@ -379,7 +389,7 @@ export class Msal2Provider extends IProvider {
379389
* @memberof Msal2Provider
380390
*/
381391
public async trySilentSignIn() {
382-
let silentRequest: any = {
392+
const silentRequest: any = {
383393
scopes: this.scopes,
384394
domainHint: this._domainHint
385395
};
@@ -420,7 +430,7 @@ export class Msal2Provider extends IProvider {
420430
prompt: this._prompt,
421431
domainHint: this._domainHint
422432
};
423-
if (this._loginType == LoginType.Popup) {
433+
if (this._loginType === LoginType.Popup) {
424434
const response = await this._publicClientApplication.loginPopup(loginRequest);
425435
this.handleResponse(response?.account);
426436
} else {
@@ -436,7 +446,7 @@ export class Msal2Provider extends IProvider {
436446
* @memberof Msal2Provider
437447
*/
438448
public getAllAccounts() {
439-
let usernames = [];
449+
const usernames = [];
440450
this._publicClientApplication.getAllAccounts().forEach((account: AccountInfo) => {
441451
usernames.push({ name: account.name, mail: account.username, id: account.homeAccountId } as IProviderAccount);
442452
});
@@ -636,12 +646,12 @@ export class Msal2Provider extends IProvider {
636646
account: logOutAccount
637647
};
638648
this.clearStoredAccount();
639-
if (this._loginType == LoginType.Redirect) {
649+
if (this._loginType === LoginType.Redirect) {
640650
this._publicClientApplication.logoutRedirect(logOutRequest);
641651
this.setState(ProviderState.SignedOut);
642652
} else {
643653
await this._publicClientApplication.logoutPopup({ ...logOutRequest });
644-
if (this._publicClientApplication.getAllAccounts.length == 1 || !this.isMultipleAccountEnabled) {
654+
if (this._publicClientApplication.getAllAccounts.length === 1 || !this.isMultipleAccountEnabled) {
645655
this.setState(ProviderState.SignedOut);
646656
} else {
647657
this.trySilentSignIn();
@@ -659,7 +669,7 @@ export class Msal2Provider extends IProvider {
659669
public async getAccessToken(options?: AuthenticationProviderOptions): Promise<string> {
660670
const scopes = options ? options.scopes || this.scopes : this.scopes;
661671
const accessTokenRequest: SilentRequest = {
662-
scopes: scopes,
672+
scopes,
663673
account: this.getAccount()
664674
};
665675
try {

0 commit comments

Comments
 (0)