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

Node cache interface #1801

Merged
merged 2 commits into from Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 12 additions & 1 deletion lib/msal-common/src/error/ClientAuthError.ts
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
@@ -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
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