Skip to content

Commit

Permalink
Move constants and add CacheSchemaType to calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jmckennon committed Jul 20, 2020
1 parent 3fa4f45 commit 0c1e716
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
9 changes: 8 additions & 1 deletion lib/msal-browser/src/cache/BrowserStorage.ts
Expand Up @@ -23,7 +23,7 @@ export class BrowserStorage extends CacheManager {
// Window storage object (either local or sessionStorage)
private windowStorage: Storage;
// Client id of application. Used in cache keys to partition cache correctly in the case of multiple instances of MSAL.
public clientId: string;
private clientId: string;

constructor(clientId: string, cacheConfig: CacheOptions) {
super();
Expand Down Expand Up @@ -420,4 +420,11 @@ export class BrowserStorage extends CacheManager {
throw BrowserAuthError.createTokenRequestCacheError(err);
}
}

/**
* Gets the clientId
*/
getClientId(): string {
return this.clientId;
}
}
Expand Up @@ -68,7 +68,7 @@ export class RedirectHandler extends InteractionHandler {
this.browserStorage.removeItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.URL_HASH));

// Remove throttle if it exists
ThrottlingUtils.removeThrottle(this.browserStorage, this.browserStorage.clientId, this.authCodeRequest.authority, this.authCodeRequest.scopes);
ThrottlingUtils.removeThrottle(this.browserStorage, this.browserStorage.getClientId(), this.authCodeRequest.authority, this.authCodeRequest.scopes);

// Acquire token with retrieved code.
const tokenResponse = await this.authModule.acquireToken(this.authCodeRequest, cachedNonce, requestState);
Expand Down
8 changes: 7 additions & 1 deletion lib/msal-browser/test/cache/BrowserStorage.spec.ts
Expand Up @@ -495,6 +495,12 @@ describe("BrowserStorage() tests", () => {
// Perform test
const tokenRequest = browserStorage.getCachedRequest(RANDOM_TEST_GUID, browserCrypto);
expect(tokenRequest.authority).to.be.eq(alternateAuthority);
});
});

it("getClientId returns a clientId", () => {
const browserStorage = new BrowserStorage(TEST_CONFIG.MSAL_CLIENT_ID, cacheConfig);
const clientId = browserStorage.getClientId();
expect(clientId).to.deep.eq(TEST_CONFIG.MSAL_CLIENT_ID);
});
});
});
17 changes: 9 additions & 8 deletions lib/msal-common/src/network/ThrottlingUtils.ts
Expand Up @@ -5,7 +5,7 @@

import { NetworkResponse } from "./NetworkManager";
import { ServerAuthorizationTokenResponse } from "../server/ServerAuthorizationTokenResponse";
import { Constants, HeaderNames } from "../utils/Constants";
import { HeaderNames, CacheSchemaType, ThrottleConstants } from "../utils/Constants";
import { CacheManager } from "../cache/CacheManager";
import { StringUtils } from "../utils/StringUtils";
import { ServerError } from "../error/ServerError";
Expand All @@ -19,7 +19,7 @@ export class ThrottlingUtils {
* @param thumbprint
*/
static generateThrottlingStorageKey(thumbprint: RequestThumbprint): string {
return `${Constants.THROTTLE_PREFIX}.${JSON.stringify(thumbprint)}`;
return `${ThrottleConstants.THROTTLE_PREFIX}.${JSON.stringify(thumbprint)}`;
}

/**
Expand All @@ -29,14 +29,14 @@ export class ThrottlingUtils {
*/
static preProcess(cacheManager: CacheManager, thumbprint: RequestThumbprint): void {
const key = ThrottlingUtils.generateThrottlingStorageKey(thumbprint);
const storageValue = cacheManager.getItem(key) as string;
const storageValue = cacheManager.getItem(key, CacheSchemaType.TEMPORARY) as string;

if (storageValue) {
const parsedValue = StringUtils.jsonParseHelper<RequestThumbprintValue>(storageValue);

if (parsedValue) {
if (parsedValue.throttleTime < Date.now()) {
cacheManager.removeItem(key);
cacheManager.removeItem(key, CacheSchemaType.TEMPORARY);
return;
}
throw new ServerError(parsedValue.errorCodes.join(" "), parsedValue.errorMessage, parsedValue.subError);
Expand All @@ -61,7 +61,8 @@ export class ThrottlingUtils {
};
cacheManager.setItem(
ThrottlingUtils.generateThrottlingStorageKey(thumbprint),
JSON.stringify(thumbprintValue)
JSON.stringify(thumbprintValue),
CacheSchemaType.TEMPORARY
);
}
}
Expand Down Expand Up @@ -95,8 +96,8 @@ export class ThrottlingUtils {
}
const currentSeconds = Date.now() / 1000;
return Math.floor(Math.min(
currentSeconds + (throttleTime || Constants.DEFAULT_THROTTLE_TIME_SECONDS),
currentSeconds + Constants.DEFAULT_MAX_THROTTLE_TIME_SECONDS
currentSeconds + (throttleTime || ThrottleConstants.DEFAULT_THROTTLE_TIME_SECONDS),
currentSeconds + ThrottleConstants.DEFAULT_MAX_THROTTLE_TIME_SECONDS
) * 1000);
}

Expand All @@ -109,6 +110,6 @@ export class ThrottlingUtils {
};

const key = this.generateThrottlingStorageKey(thumbprint);
return cacheManager.removeItem(key);
return cacheManager.removeItem(key, CacheSchemaType.TEMPORARY);
}
}
17 changes: 12 additions & 5 deletions lib/msal-common/src/utils/Constants.ts
Expand Up @@ -7,8 +7,6 @@ export const Constants = {
SKU: "msal.js.common",
// Prefix for all library cache entries
CACHE_PREFIX: "msal",
// Prefix for storing throttling entries
THROTTLE_PREFIX: "throttle",
// default authority
DEFAULT_AUTHORITY: "https://login.microsoftonline.com/common/",
DEFAULT_AUTHORITY_HOST: "login.microsoftonline.com",
Expand Down Expand Up @@ -37,9 +35,6 @@ export const Constants = {
URL_FORM_CONTENT_TYPE: "application/x-www-form-urlencoded;charset=utf-8",
AUTHORIZATION_PENDING: "authorization_pending",
NOT_DEFINED: "not_defined",
// Default time to throttle RequestThumbprint in milliseconds
DEFAULT_THROTTLE_TIME_SECONDS: 60,
DEFAULT_MAX_THROTTLE_TIME_SECONDS: 3600
};

/**
Expand Down Expand Up @@ -255,3 +250,15 @@ export enum CacheType {
*/
export const APP_META_DATA = "appmetadata";
export const ClientInfo = "client_info";

/**
* Constants related to throttling
*/
export const ThrottleConstants = {
// Default time to throttle RequestThumbprint in milliseconds
DEFAULT_THROTTLE_TIME_SECONDS: 60,
// Default maximum time to throttle in milliseconds, overrides what the server sends back
DEFAULT_MAX_THROTTLE_TIME_SECONDS: 3600,
// Prefix for storing throttling entries
THROTTLE_PREFIX: "throttle"
}

0 comments on commit 0c1e716

Please sign in to comment.