Skip to content

Commit

Permalink
PR feedback. Rename CacheManager -> TokenCache
Browse files Browse the repository at this point in the history
  • Loading branch information
sangonzal committed Jun 22, 2020
1 parent 6d17dc4 commit f5a23a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const defaultSerializedCache: JsonCache = {
/**
* In-memory token cache manager
*/
export class CacheManager {
export class TokenCache {

private storage: Storage;
private hasChanged: boolean;
Expand Down Expand Up @@ -84,7 +84,6 @@ export class CacheManager {
async writeToPersistence(): Promise<void> {
if (this.persistence) {
let cache = Serializer.serializeAllCache(this.storage.getCache() as InMemoryCache);

const getMergedState = (stateFromDisk: string) => {

if (!StringUtils.isEmpty(stateFromDisk)) {
Expand Down Expand Up @@ -144,13 +143,13 @@ export class CacheManager {
*/
private mergeUpdates(oldState: any, newState: any): JsonCache {

let finalState = oldState;
Object.keys(newState).forEach((newKey) => {
let finalState = { ...oldState };
Object.keys(newState).forEach((newKey: string) => {
let newValue = newState[newKey];

// if oldState does not contain value but newValue does, add it
if (!finalState.hasOwnProperty(newKey)) {
if (newValue != null) {
if (newValue !== null) {
finalState[newKey] = newValue;
}
} else {
Expand All @@ -177,11 +176,11 @@ export class CacheManager {
* @param newState
*/
private mergeRemovals(oldState: JsonCache, newState: JsonCache): JsonCache {
const accounts = oldState.Account != null ? this.mergeRemovalsStringDict(oldState.Account, newState.Account) : oldState.Account;
const accessTokens = oldState.AccessToken != null ? this.mergeRemovalsStringDict(oldState.AccessToken, newState.AccessToken) : oldState.AccessToken;
const refreshTokens = oldState.RefreshToken != null ? this.mergeRemovalsStringDict(oldState.RefreshToken, newState.RefreshToken) : oldState.RefreshToken;
const idTokens = oldState.IdToken != null ? this.mergeRemovalsStringDict(oldState.IdToken, newState.IdToken) : oldState.IdToken;
const appMetadata = oldState.AppMetadata != null ? this.mergeRemovalsStringDict(oldState.AppMetadata, newState.AppMetadata) : oldState.AppMetadata;
const accounts = oldState.Account !== null ? this.mergeRemovalsStringDict(oldState.Account, newState.Account) : oldState.Account;
const accessTokens = oldState.AccessToken !== null ? this.mergeRemovalsStringDict(oldState.AccessToken, newState.AccessToken) : oldState.AccessToken;
const refreshTokens = oldState.RefreshToken !== null ? this.mergeRemovalsStringDict(oldState.RefreshToken, newState.RefreshToken) : oldState.RefreshToken;
const idTokens = oldState.IdToken !== null ? this.mergeRemovalsStringDict(oldState.IdToken, newState.IdToken) : oldState.IdToken;
const appMetadata = oldState.AppMetadata !== null ? this.mergeRemovalsStringDict(oldState.AppMetadata, newState.AppMetadata) : oldState.AppMetadata;

return {
Account: accounts,
Expand All @@ -192,14 +191,14 @@ export class CacheManager {
}
}

private mergeRemovalsStringDict(oldAccounts: StringDict, newAccounts?: StringDict): StringDict {
let finalAccounts = oldAccounts;
Object.keys(oldAccounts).forEach((oldKey) => {
if (!newAccounts || !(newAccounts.hasOwnProperty(oldKey))) {
delete finalAccounts[oldKey];
private mergeRemovalsStringDict(oldState: StringDict, newState?: StringDict): StringDict {
let finalState = { ...oldState };
Object.keys(oldState).forEach((oldKey) => {
if (!newState || !(newState.hasOwnProperty(oldKey))) {
delete finalState[oldKey];
}
});
return finalAccounts;
return finalState;
}

private overlayDefaults(passedInCache: JsonCache): JsonCache {
Expand Down
10 changes: 5 additions & 5 deletions lib/msal-node/src/client/ClientApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import { CryptoProvider } from '../crypto/CryptoProvider';
import { Storage } from '../cache/Storage';
import { version } from '../../package.json';
import { Constants as NodeConstants } from './../utils/Constants';
import { CacheManager } from '../cache/CacheManager';
import { TokenCache } from '../cache/TokenCache';

export abstract class ClientApplication {
private config: Configuration;
private _authority: Authority;
private readonly cryptoProvider: CryptoProvider;
private storage: Storage;
private cacheManager: CacheManager;
private tokenCache: TokenCache;

/**
* @constructor
Expand All @@ -40,7 +40,7 @@ export abstract class ClientApplication {
protected constructor(configuration: Configuration) {
this.config = buildAppConfiguration(configuration);
this.storage = new Storage();
this.cacheManager = new CacheManager(
this.tokenCache = new TokenCache(
this.storage,
this.config.cache?.cachePlugin
);
Expand Down Expand Up @@ -106,8 +106,8 @@ export abstract class ClientApplication {
return refreshTokenClient.acquireToken(this.initializeRequestScopes(request) as RefreshTokenRequest);
}

getCacheManager(): CacheManager {
return this.cacheManager;
getCacheManager(): TokenCache {
return this.tokenCache;
}

protected async buildOauthClientConfiguration(authority?: string): Promise<ClientConfiguration> {
Expand Down
2 changes: 1 addition & 1 deletion lib/msal-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { PublicClientApplication } from './client/PublicClientApplication';
export { ConfidentialClientApplication } from './client/ConfidentialClientApplication';
export { Configuration, buildAppConfiguration } from './config/Configuration';
export { Storage } from './cache/Storage';
export { CacheManager } from './cache/CacheManager';
export { TokenCache } from './cache/TokenCache';
export { ICachePlugin } from './cache/ICachePlugin';

// crypto
Expand Down

0 comments on commit f5a23a9

Please sign in to comment.