Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

msal-browser uses AuthCodeClient and SilentFlowClient #1793

Merged
merged 20 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
232 changes: 142 additions & 90 deletions lib/msal-browser/src/app/PublicClientApplication.ts

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions lib/msal-browser/src/cache/BrowserStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,17 @@ export class BrowserStorage extends CacheManager {
this.setItem(this.generateCacheKey(authorityKey), authority, CacheSchemaType.TEMPORARY);
}

/**
* Gets the cached authority based on the cached state. Returns empty if no cached state found.
*/
getCachedAuthority(): string {
const state = this.getItem(this.generateCacheKey(TemporaryCacheKeys.REQUEST_STATE), CacheSchemaType.TEMPORARY) as string;
if (!state) {
return null;
}
return this.getItem(this.generateCacheKey(this.generateAuthorityKey(state)), CacheSchemaType.TEMPORARY) as string;
}

/**
* Updates account, authority, and state in cache
* @param serverAuthenticationRequest
Expand Down Expand Up @@ -375,6 +386,7 @@ export class BrowserStorage extends CacheManager {
this.removeItem(this.generateCacheKey(TemporaryCacheKeys.REQUEST_STATE));
this.removeItem(this.generateCacheKey(TemporaryCacheKeys.REQUEST_PARAMS));
this.removeItem(this.generateCacheKey(TemporaryCacheKeys.ORIGIN_URI));
this.removeItem(this.generateCacheKey(TemporaryCacheKeys.URL_HASH));
}

cleanRequest(): void {
Expand Down
24 changes: 24 additions & 0 deletions lib/msal-browser/src/error/BrowserConfigurationAuthError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import { AuthError } from "@azure/msal-common";
* BrowserAuthErrorMessage class containing string constants used by error codes and messages.
*/
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."
Expand Down Expand Up @@ -36,6 +44,22 @@ 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
Expand Down
16 changes: 8 additions & 8 deletions lib/msal-browser/src/interaction_handler/InteractionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { SPAClient, StringUtils, AuthorizationCodeRequest, CacheSchemaType, AuthenticationResult } from "@azure/msal-common";
import { StringUtils, AuthorizationCodeRequest, CacheSchemaType, AuthenticationResult, AuthorizationCodeClient } from "@azure/msal-common";
import { BrowserStorage } from "../cache/BrowserStorage";
import { BrowserAuthError } from "../error/BrowserAuthError";
import { TemporaryCacheKeys } from "../utils/BrowserConstants";
Expand All @@ -12,11 +12,11 @@ import { TemporaryCacheKeys } from "../utils/BrowserConstants";
*/
export abstract class InteractionHandler {

protected authModule: SPAClient;
protected authModule: AuthorizationCodeClient;
protected browserStorage: BrowserStorage;
protected authCodeRequest: AuthorizationCodeRequest;

constructor(authCodeModule: SPAClient, storageImpl: BrowserStorage) {
constructor(authCodeModule: AuthorizationCodeClient, storageImpl: BrowserStorage) {
this.authModule = authCodeModule;
this.browserStorage = storageImpl;
}
Expand All @@ -37,19 +37,19 @@ export abstract class InteractionHandler {
throw BrowserAuthError.createEmptyHashError(locationHash);
}

// Get cached items
// Handle code response.
const requestState = this.browserStorage.getItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.REQUEST_STATE), CacheSchemaType.TEMPORARY) as string;
const authCode = this.authModule.handleFragmentResponse(locationHash, requestState);

// Get cached items
const cachedNonceKey = this.browserStorage.generateNonceKey(requestState);
const cachedNonce = this.browserStorage.getItem(this.browserStorage.generateCacheKey(cachedNonceKey), CacheSchemaType.TEMPORARY) as string;

// Handle code response.
const authCode = this.authModule.handleFragmentResponse(locationHash, requestState);

// Assign code to request
this.authCodeRequest.code = authCode;

// Acquire token with retrieved code.
const tokenResponse = await this.authModule.acquireToken(this.authCodeRequest, requestState, cachedNonce);
const tokenResponse = await this.authModule.acquireToken(this.authCodeRequest, cachedNonce, requestState);
this.browserStorage.cleanRequest();
return tokenResponse;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/msal-browser/src/interaction_handler/PopupHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { UrlString, StringUtils, Constants, SPAClient, AuthorizationCodeRequest, CacheSchemaType } from "@azure/msal-common";
import { UrlString, StringUtils, Constants, AuthorizationCodeRequest, CacheSchemaType, AuthorizationCodeClient } from "@azure/msal-common";
import { InteractionHandler } from "./InteractionHandler";
import { BrowserAuthError } from "../error/BrowserAuthError";
import { BrowserConstants } from "../utils/BrowserConstants";
Expand All @@ -16,7 +16,7 @@ export class PopupHandler extends InteractionHandler {

private currentWindow: Window;

constructor(authCodeModule: SPAClient, storageImpl: BrowserStorage) {
constructor(authCodeModule: AuthorizationCodeClient, storageImpl: BrowserStorage) {
super(authCodeModule, storageImpl);

// Properly sets this reference for the unload event.
Expand Down
10 changes: 5 additions & 5 deletions lib/msal-browser/src/interaction_handler/RedirectHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ export class RedirectHandler extends InteractionHandler {
// Interaction is completed - remove interaction status.
this.browserStorage.removeItem(this.browserStorage.generateCacheKey(BrowserConstants.INTERACTION_STATUS_KEY));

// Get cached items
// Handle code response.
const requestState = this.browserStorage.getItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.REQUEST_STATE), CacheSchemaType.TEMPORARY) as string;
const authCode = this.authModule.handleFragmentResponse(locationHash, requestState);

// Get cached items
const cachedNonceKey = this.browserStorage.generateNonceKey(requestState);
const cachedNonce = this.browserStorage.getItem(this.browserStorage.generateCacheKey(cachedNonceKey), CacheSchemaType.TEMPORARY) as string;
this.authCodeRequest = this.browserStorage.getCachedRequest(requestState, browserCrypto);

// Handle code response.
const authCode = this.authModule.handleFragmentResponse(locationHash, requestState);
this.authCodeRequest.code = authCode;

// Hash was processed successfully - remove from cache
this.browserStorage.removeItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.URL_HASH));

// Acquire token with retrieved code.
const tokenResponse = await this.authModule.acquireToken(this.authCodeRequest, requestState, cachedNonce);
const tokenResponse = await this.authModule.acquireToken(this.authCodeRequest, cachedNonce, requestState);
this.browserStorage.cleanRequest();
return tokenResponse;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/msal-browser/src/interaction_handler/SilentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { UrlString, SPAClient, StringUtils, AuthorizationCodeRequest } from "@azure/msal-common";
import { UrlString, StringUtils, AuthorizationCodeRequest, AuthorizationCodeClient } from "@azure/msal-common";
import { InteractionHandler } from "./InteractionHandler";
import { BrowserConstants } from "../utils/BrowserConstants";
import { BrowserAuthError } from "../error/BrowserAuthError";
Expand All @@ -11,7 +11,7 @@ import { BrowserStorage } from "../cache/BrowserStorage";
export class SilentHandler extends InteractionHandler {

private loadFrameTimeout: number;
constructor(authCodeModule: SPAClient, storageImpl: BrowserStorage, configuredLoadFrameTimeout: number) {
constructor(authCodeModule: AuthorizationCodeClient, storageImpl: BrowserStorage, configuredLoadFrameTimeout: number) {
super(authCodeModule, storageImpl);
this.loadFrameTimeout = configuredLoadFrameTimeout;
}
Expand Down