Skip to content

Commit

Permalink
Refactor BrowserConfigurationAuthError for reduced size (#6473)
Browse files Browse the repository at this point in the history
Same changes as #6433 , #6471 , #6414, #6472
  • Loading branch information
tnorling authored Sep 14, 2023
1 parent 5f19f7f commit 6eaf83f
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 207 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Refactor BrowserConfigurationAuthError #6473",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
73 changes: 55 additions & 18 deletions lib/msal-browser/src/app/IPublicClientApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { PopupRequest } from "../request/PopupRequest";
import { SilentRequest } from "../request/SilentRequest";
import { SsoSilentRequest } from "../request/SsoSilentRequest";
import { EndSessionRequest } from "../request/EndSessionRequest";
import { BrowserConfigurationAuthError } from "../error/BrowserConfigurationAuthError";
import {
BrowserConfigurationAuthErrorCodes,
createBrowserConfigurationAuthError,
} from "../error/BrowserConfigurationAuthError";
import { WrapperSKU } from "../utils/BrowserConstants";
import { INavigationClient } from "../navigation/INavigationClient";
import { EndSessionPopupRequest } from "../request/EndSessionPopupRequest";
Expand Down Expand Up @@ -74,27 +77,37 @@ export interface IPublicClientApplication {
export const stubbedPublicClientApplication: IPublicClientApplication = {
initialize: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
acquireTokenPopup: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
acquireTokenRedirect: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
acquireTokenSilent: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
acquireTokenByCode: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
getAllAccounts: () => {
Expand All @@ -111,37 +124,51 @@ export const stubbedPublicClientApplication: IPublicClientApplication = {
},
handleRedirectPromise: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
loginPopup: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
loginRedirect: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
logout: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
logoutRedirect: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
logoutPopup: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
ssoSilent: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
addEventCallback: () => {
Expand All @@ -163,10 +190,14 @@ export const stubbedPublicClientApplication: IPublicClientApplication = {
return;
},
getTokenCache: () => {
throw BrowserConfigurationAuthError.createStubPcaInstanceCalledError();
throw createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
);
},
getLogger: () => {
throw BrowserConfigurationAuthError.createStubPcaInstanceCalledError();
throw createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
);
},
setLogger: () => {
return;
Expand All @@ -184,16 +215,22 @@ export const stubbedPublicClientApplication: IPublicClientApplication = {
return;
},
getConfiguration: () => {
throw BrowserConfigurationAuthError.createStubPcaInstanceCalledError();
throw createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
);
},
hydrateCache: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
clearCache: () => {
return Promise.reject(
BrowserConfigurationAuthError.createStubPcaInstanceCalledError()
createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled
)
);
},
};
20 changes: 9 additions & 11 deletions lib/msal-browser/src/cache/BrowserStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
* Licensed under the MIT License.
*/

import { BrowserConfigurationAuthError } from "../error/BrowserConfigurationAuthError";
import {
BrowserConfigurationAuthErrorCodes,
createBrowserConfigurationAuthError,
} from "../error/BrowserConfigurationAuthError";
import { BrowserCacheLocation } from "../utils/BrowserConstants";
import { IWindowStorage } from "./IWindowStorage";

Expand All @@ -17,17 +20,12 @@ export class BrowserStorage implements IWindowStorage<string> {

private validateWindowStorage(cacheLocation: string): void {
if (
cacheLocation !== BrowserCacheLocation.LocalStorage &&
cacheLocation !== BrowserCacheLocation.SessionStorage
(cacheLocation !== BrowserCacheLocation.LocalStorage &&
cacheLocation !== BrowserCacheLocation.SessionStorage) ||
!window[cacheLocation]
) {
throw BrowserConfigurationAuthError.createStorageNotSupportedError(
cacheLocation
);
}
const storageSupported = !!window[cacheLocation];
if (!storageSupported) {
throw BrowserConfigurationAuthError.createStorageNotSupportedError(
cacheLocation
throw createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.storageNotSupported
);
}
}
Expand Down
9 changes: 7 additions & 2 deletions lib/msal-browser/src/controllers/StandardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ import { SsoSilentRequest } from "../request/SsoSilentRequest";
import { EventCallbackFunction, EventError } from "../event/EventMessage";
import { EventType } from "../event/EventType";
import { EndSessionRequest } from "../request/EndSessionRequest";
import { BrowserConfigurationAuthError } from "../error/BrowserConfigurationAuthError";
import {
BrowserConfigurationAuthErrorCodes,
createBrowserConfigurationAuthError,
} from "../error/BrowserConfigurationAuthError";
import { EndSessionPopupRequest } from "../request/EndSessionPopupRequest";
import { INavigationClient } from "../navigation/INavigationClient";
import { EventHandler } from "../event/EventHandler";
Expand Down Expand Up @@ -1380,7 +1383,9 @@ export class StandardController implements IController {
BrowserCacheLocation.MemoryStorage &&
!this.config.cache.storeAuthStateInCookie
) {
throw BrowserConfigurationAuthError.createInMemoryRedirectUnavailableError();
throw createBrowserConfigurationAuthError(
BrowserConfigurationAuthErrorCodes.inMemRedirectUnavailable
);
}

if (
Expand Down
135 changes: 33 additions & 102 deletions lib/msal-browser/src/error/BrowserConfigurationAuthError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,41 @@
*/

import { AuthError } from "@azure/msal-common";
import * as BrowserConfigurationAuthErrorCodes from "./BrowserConfigurationAuthErrorCodes";
export { BrowserConfigurationAuthErrorCodes };

export const BrowserConfigurationAuthErrorMessages = {
[BrowserConfigurationAuthErrorCodes.storageNotSupported]:
"Given storage configuration option was not supported.",
[BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled]:
"Stub instance of Public Client Application was called. If using msal-react, please ensure context is not used without a provider. For more visit: aka.ms/msaljs/browser-errors",
[BrowserConfigurationAuthErrorCodes.inMemRedirectUnavailable]:
"Redirect cannot be supported. In-memory storage was selected and storeAuthStateInCookie=false, which would cause the library to be unable to handle the incoming hash. If you would like to use the redirect API, please use session/localStorage or set storeAuthStateInCookie=true.",
};

/**
* BrowserAuthErrorMessage class containing string constants used by error codes and messages.
* @deprecated Use BrowserAuthErrorCodes instead
*/
export const BrowserConfigurationAuthErrorMessage = {
redirectUriNotSet: {
code: "redirect_uri_empty",
desc: "A redirect URI is required for all calls, and none has been set.",
},
postLogoutUriNotSet: {
code: "post_logout_uri_empty",
desc: "A post logout redirect has not been set.",
},
storageNotSupportedError: {
code: "storage_not_supported",
desc: "Given storage configuration option was not supported.",
},
noRedirectCallbacksSet: {
code: "no_redirect_callbacks",
desc:
"No redirect callbacks have been set. Please call setRedirectCallbacks() with the appropriate function arguments before continuing. " +
"More information is available here: https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/MSAL-basics.",
},
invalidCallbackObject: {
code: "invalid_callback_object",
desc:
"The object passed for the callback was invalid. " +
"More information is available here: https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/MSAL-basics.",
code: BrowserConfigurationAuthErrorCodes.storageNotSupported,
desc: BrowserConfigurationAuthErrorMessages[
BrowserConfigurationAuthErrorCodes.storageNotSupported
],
},
stubPcaInstanceCalled: {
code: "stubbed_public_client_application_called",
desc: "Stub instance of Public Client Application was called. If using msal-react, please ensure context is not used without a provider. For more visit: aka.ms/msaljs/browser-errors",
code: BrowserConfigurationAuthErrorCodes.stubbedPublicClientApplicationCalled,
desc: BrowserConfigurationAuthErrorMessages[
BrowserConfigurationAuthErrorCodes
.stubbedPublicClientApplicationCalled
],
},
inMemRedirectUnavailable: {
code: "in_mem_redirect_unavailable",
desc: "Redirect cannot be supported. In-memory storage was selected and storeAuthStateInCookie=false, which would cause the library to be unable to handle the incoming hash. If you would like to use the redirect API, please use session/localStorage or set storeAuthStateInCookie=true.",
},
entropyNotProvided: {
code: "entropy_not_provided",
desc: "The available browser crypto interface requires entropy set via system.cryptoOptions.entropy configuration option.",
code: BrowserConfigurationAuthErrorCodes.inMemRedirectUnavailable,
desc: BrowserConfigurationAuthErrorMessages[
BrowserConfigurationAuthErrorCodes.inMemRedirectUnavailable
],
},
};

Expand All @@ -57,77 +52,13 @@ export class BrowserConfigurationAuthError extends AuthError {

Object.setPrototypeOf(this, BrowserConfigurationAuthError.prototype);
}
}

/**
* Creates an error thrown when the redirect uri is empty (not set by caller)
*/
static createRedirectUriEmptyError(): BrowserConfigurationAuthError {
return new BrowserConfigurationAuthError(
BrowserConfigurationAuthErrorMessage.redirectUriNotSet.code,
BrowserConfigurationAuthErrorMessage.redirectUriNotSet.desc
);
}

/**
* Creates an error thrown when the post-logout redirect uri is empty (not set by caller)
*/
static createPostLogoutRedirectUriEmptyError(): BrowserConfigurationAuthError {
return new BrowserConfigurationAuthError(
BrowserConfigurationAuthErrorMessage.postLogoutUriNotSet.code,
BrowserConfigurationAuthErrorMessage.postLogoutUriNotSet.desc
);
}

/**
* Creates error thrown when given storage location is not supported.
* @param givenStorageLocation
*/
static createStorageNotSupportedError(
givenStorageLocation: string
): BrowserConfigurationAuthError {
return new BrowserConfigurationAuthError(
BrowserConfigurationAuthErrorMessage.storageNotSupportedError.code,
`${BrowserConfigurationAuthErrorMessage.storageNotSupportedError.desc} Given Location: ${givenStorageLocation}`
);
}

/**
* Creates error thrown when redirect callbacks are not set before calling loginRedirect() or acquireTokenRedirect().
*/
static createRedirectCallbacksNotSetError(): BrowserConfigurationAuthError {
return new BrowserConfigurationAuthError(
BrowserConfigurationAuthErrorMessage.noRedirectCallbacksSet.code,
BrowserConfigurationAuthErrorMessage.noRedirectCallbacksSet.desc
);
}

/**
* Creates error thrown when the stub instance of PublicClientApplication is called.
*/
static createStubPcaInstanceCalledError(): BrowserConfigurationAuthError {
return new BrowserConfigurationAuthError(
BrowserConfigurationAuthErrorMessage.stubPcaInstanceCalled.code,
BrowserConfigurationAuthErrorMessage.stubPcaInstanceCalled.desc
);
}

/*
* Create an error thrown when in-memory storage is used and storeAuthStateInCookie=false.
*/
static createInMemoryRedirectUnavailableError(): BrowserConfigurationAuthError {
return new BrowserConfigurationAuthError(
BrowserConfigurationAuthErrorMessage.inMemRedirectUnavailable.code,
BrowserConfigurationAuthErrorMessage.inMemRedirectUnavailable.desc
);
}

/**
* Creates an error thrown when a crypto interface that requires entropy is initialized without entropy
*/
static createEntropyNotProvided(): BrowserConfigurationAuthError {
return new BrowserConfigurationAuthError(
BrowserConfigurationAuthErrorMessage.entropyNotProvided.code,
BrowserConfigurationAuthErrorMessage.entropyNotProvided.desc
);
}
export function createBrowserConfigurationAuthError(
errorCode: string
): BrowserConfigurationAuthError {
return new BrowserConfigurationAuthError(
errorCode,
BrowserConfigurationAuthErrorMessages[errorCode]
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

export const storageNotSupported = "storage_not_supported";
export const stubbedPublicClientApplicationCalled =
"stubbed_public_client_application_called";
export const inMemRedirectUnavailable = "in_mem_redirect_unavailable";
1 change: 1 addition & 0 deletions lib/msal-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export {
} from "./error/BrowserAuthError";
export {
BrowserConfigurationAuthError,
BrowserConfigurationAuthErrorCodes,
BrowserConfigurationAuthErrorMessage,
} from "./error/BrowserConfigurationAuthError";

Expand Down
Loading

0 comments on commit 6eaf83f

Please sign in to comment.