Skip to content

Commit

Permalink
Merge f5a23a9 into 25d53da
Browse files Browse the repository at this point in the history
  • Loading branch information
sangonzal committed Jun 22, 2020
2 parents 25d53da + f5a23a9 commit f305ff8
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 145 deletions.
13 changes: 12 additions & 1 deletion lib/msal-common/src/error/ClientAuthError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export const ClientAuthErrorMessage = {
noAccountFound: {
code: "no_account_found",
desc: "No account found in cache for given key."
},
CachePluginError: {
code: "no cache plugin set on CacheManager",
desc: "ICachePlugin needs to be set before using readFromStorage or writeFromStorage"
}
};

Expand Down Expand Up @@ -259,7 +263,7 @@ export class ClientAuthError extends AuthError {
return new ClientAuthError(ClientAuthErrorMessage.multipleMatchingTokens.code,
`Cache error for scope ${scope}: ${ClientAuthErrorMessage.multipleMatchingTokens.desc}.`);
}

/**
* Throws error when multiple tokens are in cache for the given scope.
* @param scope
Expand Down Expand Up @@ -342,4 +346,11 @@ export class ClientAuthError extends AuthError {
static createNoAccountFoundError(): ClientAuthError {
return new ClientAuthError(ClientAuthErrorMessage.noAccountFound.code, ClientAuthErrorMessage.noAccountFound.desc);
}

/**
* Throws error if ICachePlugin not set on CacheManager
*/
static createCachePluginError(): ClientAuthError {
return new ClientAuthError(ClientAuthErrorMessage.CachePluginError.code, `${ClientAuthErrorMessage.CachePluginError.desc}`);
}
}
57 changes: 0 additions & 57 deletions lib/msal-node/src/cache/CacheContext.ts

This file was deleted.

11 changes: 11 additions & 0 deletions lib/msal-node/src/cache/ICachePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

export interface ICachePlugin {
readFromStorage: () => Promise<string>;
writeToStorage: (
getMergedState: (oldState: string) => string
) => Promise<void>;
}
30 changes: 23 additions & 7 deletions lib/msal-node/src/cache/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
AppMetadataEntity,
CacheManager
} from '@azure/msal-common';
import { CacheOptions } from '../config/Configuration';
import { Deserializer } from "./serializer/Deserializer";
import { Serializer } from "./serializer/Serializer";
import { InMemoryCache, JsonCache } from "./serializer/SerializerTypes";
Expand All @@ -23,14 +22,27 @@ import { InMemoryCache, JsonCache } from "./serializer/SerializerTypes";
*/
export class Storage extends CacheManager {
// Cache configuration, either set by user or default values.
private cacheConfig: CacheOptions;
private inMemoryCache: InMemoryCache;

constructor(cacheConfig: CacheOptions) {
constructor() {
super();
this.cacheConfig = cacheConfig;
if (this.cacheConfig.cacheLocation! === 'fileCache')
this.inMemoryCache = this.cacheConfig.cacheInMemory!;
}

private inMemoryCache: InMemoryCache = {
accounts: {},
accessTokens: {},
refreshTokens: {},
appMetadata: {},
idTokens: {},
};

private changeEmitters: Array<Function> = [];

registerChangeEmitter(func: () => void): void {
this.changeEmitters.push(func);
}

emitChange() {
this.changeEmitters.forEach(func => func.call(null));
}

/**
Expand All @@ -46,6 +58,7 @@ export class Storage extends CacheManager {
*/
setCache(inMemoryCache: InMemoryCache) {
this.inMemoryCache = inMemoryCache;
this.emitChange();
}

/**
Expand Down Expand Up @@ -99,6 +112,7 @@ export class Storage extends CacheManager {

// update inMemoryCache
this.setCache(cache);
this.emitChange();
}

/**
Expand Down Expand Up @@ -209,6 +223,7 @@ export class Storage extends CacheManager {
// write to the cache after removal
if (result) {
this.setCache(cache);
this.emitChange();
}
return result;
}
Expand Down Expand Up @@ -253,6 +268,7 @@ export class Storage extends CacheManager {
this.removeItem(internalKey);
});
});
this.emitChange();
}

/**
Expand Down

0 comments on commit f305ff8

Please sign in to comment.