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

ClientCredentials: Fixed bug where user-supplied cache is loaded into memory only after network request #6218

Merged
merged 11 commits into from
Jul 12, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "ClientCredentials: Fixed bug where user-supplied cache is loaded into memory only after network request #6218",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "ClientCredentials: Fixed bug where user-supplied cache is loaded into memory only after network request #6218",
"packageName": "@azure/msal-node",
"email": "[email protected]",
"dependentChangeType": "patch"
}
22 changes: 22 additions & 0 deletions lib/msal-common/src/cache/CacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import { AuthorityMetadataEntity } from "./entities/AuthorityMetadataEntity";
import { BaseAuthRequest } from "../request/BaseAuthRequest";
import { Logger } from "../logger/Logger";
import { name, version } from "../packageMetadata";
import { ISerializableTokenCache } from "./interface/ISerializableTokenCache";
import { ICachePlugin } from "./interface/ICachePlugin";
import { TokenCacheContext } from "./persistence/TokenCacheContext";

/**
* Interface class which implement cache storage functions used by MSAL to perform validity checks, and store tokens.
Expand Down Expand Up @@ -216,6 +219,25 @@ export abstract class CacheManager implements ICacheManager {
credential: ValidCredentialType
): string;

/**
* Read the user-supplied cache into memory, if applicable
* @param serializableCache
* @param persistencePlugin
*/
async readUserDefinedCacheIntoMemory(
Robbie-Microsoft marked this conversation as resolved.
Show resolved Hide resolved
serializableCache: ISerializableTokenCache | null,
persistencePlugin: ICachePlugin | null
): Promise<void> {
if (serializableCache && persistencePlugin) {
const cacheContext = new TokenCacheContext(
serializableCache,
true,
Robbie-Microsoft marked this conversation as resolved.
Show resolved Hide resolved
);

await persistencePlugin.beforeCacheAccess(cacheContext);
Robbie-Microsoft marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Returns all accounts in cache
*/
Expand Down
3 changes: 3 additions & 0 deletions lib/msal-node/src/client/ClientCredentialClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export class ClientCredentialClient extends BaseClient {
private async getCachedAuthenticationResult(
request: CommonClientCredentialRequest
): Promise<AuthenticationResult | null> {
// read the user-supplied cache into memory, if applicable
await this.cacheManager.readUserDefinedCacheIntoMemory(this.config.serializableCache, this.config.persistencePlugin);
Robbie-Microsoft marked this conversation as resolved.
Show resolved Hide resolved

const cachedAccessToken = this.readAccessTokenFromCache();

if (!cachedAccessToken) {
Expand Down
Loading