Skip to content

Commit 4c77d1c

Browse files
authored
Merge pull request #1522 from AzureAD/msal-node-cache-final
[msal-node] Cache-5: Msal node cache additional changes for end-to-end cache to work
2 parents cbf1105 + 36ac337 commit 4c77d1c

27 files changed

+342
-185
lines changed

lib/msal-common/src/cache/CacheHelpers.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class CacheHelpers {
2323

2424
constructor(cacheImpl: ICacheStorage) {
2525
this.cacheStorage = cacheImpl;
26+
2627
}
2728

2829
/**
@@ -167,11 +168,11 @@ export class CacheHelpers {
167168

168169
/**
169170
* Checks that any parameters are exact matches for key value, since key.match in the above functions only do contains checks, not exact matches.
170-
* @param atKey
171-
* @param clientId
172-
* @param authority
173-
* @param resource
174-
* @param homeAccountIdentifier
171+
* @param atKey
172+
* @param clientId
173+
* @param authority
174+
* @param resource
175+
* @param homeAccountIdentifier
175176
*/
176177
private checkForExactKeyMatch(atKey: AccessTokenKey, clientId: string, authority: string, resource?: string, homeAccountIdentifier?: string): boolean {
177178
const hasClientId = (atKey.clientId === clientId);

lib/msal-common/src/cache/ICacheStorage.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
*/
99
export interface ICacheStorage {
1010
/**
11-
* Function to read serialized cache from disk
11+
* Function to read serialized Cache from disk
12+
* @param key
13+
* @param value
1214
*/
1315
getSerializedCache(): Promise<string>;
1416

1517
/**
16-
* Function to write serialized cache to disk
18+
* Function to write serialized Cache to disk
1719
* @param cache
1820
*/
1921
setSerializedCache(cache: string): void;

lib/msal-common/src/client/AuthorizationCodeClient.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import { RequestParameterBuilder } from "../server/RequestParameterBuilder";
1111
import { RequestValidator } from "../request/RequestValidator";
1212
import { GrantType } from "../utils/Constants";
1313
import { ClientConfiguration } from "../config/ClientConfiguration";
14-
import {ServerAuthorizationTokenResponse} from "../server/ServerAuthorizationTokenResponse";
15-
import {NetworkResponse} from "../network/NetworkManager";
16-
import {ScopeSet} from "../request/ScopeSet";
14+
import { ServerAuthorizationTokenResponse } from "../server/ServerAuthorizationTokenResponse";
15+
import { NetworkResponse } from "../network/NetworkManager";
16+
import { ScopeSet } from "../request/ScopeSet";
17+
import { ResponseHandler } from "../response/ResponseHandler";
18+
import { AuthenticationResult } from "../response/AuthenticationResult";
1719

1820
/**
1921
* Oauth2.0 Authorization Code client

lib/msal-common/src/client/BaseClient.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import { Account } from "../account/Account";
1111
import { Authority } from "../authority/Authority";
1212
import { Logger } from "../logger/Logger";
1313
import { AuthorityFactory } from "../authority/AuthorityFactory";
14-
import {AADServerParamKeys, Constants, HeaderNames} from "../utils/Constants";
15-
import {ClientAuthError} from "../error/ClientAuthError";
16-
import {NetworkResponse} from "../network/NetworkManager";
14+
import { AADServerParamKeys, Constants, HeaderNames } from "../utils/Constants";
15+
import { ClientAuthError } from "../error/ClientAuthError";
16+
import { NetworkResponse } from "../network/NetworkManager";
1717
import { ServerAuthorizationTokenResponse } from "../server/ServerAuthorizationTokenResponse";
1818
import { UnifiedCacheManager } from "../unifiedCache/UnifiedCacheManager";
1919
import { Serializer } from "../unifiedCache/serialize/Serializer";
@@ -39,9 +39,9 @@ export abstract class BaseClient {
3939
protected networkClient: INetworkModule;
4040

4141
// Helper API object for running cache functions
42-
protected cacheManager: CacheHelpers;
42+
protected spaCacheManager: CacheHelpers;
4343

44-
// Helper API object for serialized cache operations
44+
// Helper API object for serialized cache operations
4545
protected unifiedCacheManager: UnifiedCacheManager;
4646

4747
// Account object
@@ -64,7 +64,7 @@ export abstract class BaseClient {
6464
this.cacheStorage = this.config.storageInterface;
6565

6666
// Initialize storage helper object
67-
this.cacheManager = new CacheHelpers(this.cacheStorage);
67+
this.spaCacheManager = new CacheHelpers(this.cacheStorage);
6868

6969
// Initialize serialized cache manager
7070
this.unifiedCacheManager = new UnifiedCacheManager(this.cacheStorage);
@@ -140,10 +140,10 @@ export abstract class BaseClient {
140140
});
141141
}
142142

143-
/**
143+
/**
144144
* Set the cache post acquireToken call
145145
*/
146-
protected setCache() {
146+
protected setCache(): void {
147147
const inMemCache = this.unifiedCacheManager.getCacheInMemory();
148148
const cache = this.unifiedCacheManager.generateJsonCache(inMemCache);
149149
this.cacheStorage.setSerializedCache(Serializer.serializeJSONBlob(cache));

lib/msal-common/src/client/SPAClient.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ServerCodeRequestParameters } from "../server/ServerCodeRequestParamete
1111
import { ServerTokenRequestParameters } from "../server/ServerTokenRequestParameters";
1212
import { CodeResponse } from "../response/CodeResponse";
1313
import { TokenResponse } from "../response/TokenResponse";
14-
import { SPAResponseHandler } from "../response/SpaResponseHandler";
14+
import { SPAResponseHandler } from "../response/SPAResponseHandler";
1515
import { ServerAuthorizationCodeResponse } from "../server/ServerAuthorizationCodeResponse";
1616
import { ServerAuthorizationTokenResponse } from "../server/ServerAuthorizationTokenResponse";
1717
import { ClientAuthError } from "../error/ClientAuthError";
@@ -101,7 +101,7 @@ export class SPAClient extends BaseClient {
101101
}
102102

103103
// Update required cache entries for request.
104-
this.cacheManager.updateCacheEntries(requestParameters, request.account);
104+
this.spaCacheManager.updateCacheEntries(requestParameters, request.account);
105105

106106
// Populate query parameters (sid/login_hint/domain_hint) and any other extraQueryParameters set by the developer.
107107
requestParameters.populateQueryParams(adalIdToken);
@@ -123,7 +123,7 @@ export class SPAClient extends BaseClient {
123123
return urlNavigate;
124124
} catch (e) {
125125
// Reset cache items before re-throwing.
126-
this.cacheManager.resetTempCacheItems(requestParameters && requestParameters.state);
126+
this.spaCacheManager.resetTempCacheItems(requestParameters && requestParameters.state);
127127
throw e;
128128
}
129129
}
@@ -171,7 +171,7 @@ export class SPAClient extends BaseClient {
171171
return await this.getTokenResponse(tokenEndpoint, tokenReqParams, tokenRequest, codeResponse);
172172
} catch (e) {
173173
// Reset cache items and set account to null before re-throwing.
174-
this.cacheManager.resetTempCacheItems(codeResponse && codeResponse.userRequestState);
174+
this.spaCacheManager.resetTempCacheItems(codeResponse && codeResponse.userRequestState);
175175
this.account = null;
176176
throw e;
177177
}
@@ -243,7 +243,7 @@ export class SPAClient extends BaseClient {
243243
}
244244
} catch (e) {
245245
// Reset cache items and set account to null before re-throwing.
246-
this.cacheManager.resetTempCacheItems();
246+
this.spaCacheManager.resetTempCacheItems();
247247
this.account = null;
248248
throw e;
249249
}
@@ -261,7 +261,7 @@ export class SPAClient extends BaseClient {
261261
// Check for homeAccountIdentifier. Do not send anything if it doesn't exist.
262262
const homeAccountIdentifier = currentAccount ? currentAccount.homeAccountIdentifier : "";
263263
// Remove all pertinent access tokens.
264-
this.cacheManager.removeAllAccessTokens(this.config.authOptions.clientId, authorityUri, "", homeAccountIdentifier);
264+
this.spaCacheManager.removeAllAccessTokens(this.config.authOptions.clientId, authorityUri, "", homeAccountIdentifier);
265265
// Clear remaining cache items.
266266
this.cacheStorage.clear();
267267
// Clear current account.
@@ -298,7 +298,7 @@ export class SPAClient extends BaseClient {
298298
*/
299299
public handleFragmentResponse(hashFragment: string): CodeResponse {
300300
// Handle responses.
301-
const responseHandler = new SPAResponseHandler(this.config.authOptions.clientId, this.cacheStorage, this.cacheManager, this.cryptoUtils, this.logger);
301+
const responseHandler = new SPAResponseHandler(this.config.authOptions.clientId, this.cacheStorage, this.spaCacheManager, this.cryptoUtils, this.logger);
302302
// Deserialize hash fragment response parameters.
303303
const hashUrlString = new UrlString(hashFragment);
304304
const serverParams = hashUrlString.getDeserializedHash<ServerAuthorizationCodeResponse>();
@@ -315,7 +315,7 @@ export class SPAClient extends BaseClient {
315315
*/
316316
public cancelRequest(): void {
317317
const cachedState = this.cacheStorage.getItem(TemporaryCacheKeys.REQUEST_STATE);
318-
this.cacheManager.resetTempCacheItems(cachedState || "");
318+
this.spaCacheManager.resetTempCacheItems(cachedState || "");
319319
}
320320

321321
/**
@@ -329,7 +329,7 @@ export class SPAClient extends BaseClient {
329329
this.cacheStorage.removeItem(TemporaryCacheKeys.REQUEST_PARAMS);
330330
// Get cached authority and use if no authority is cached with request.
331331
if (StringUtils.isEmpty(parsedRequest.authority)) {
332-
const authorityKey: string = this.cacheManager.generateAuthorityKey(state);
332+
const authorityKey: string = this.spaCacheManager.generateAuthorityKey(state);
333333
const cachedAuthority: string = this.cacheStorage.getItem(authorityKey);
334334
parsedRequest.authority = cachedAuthority;
335335
}
@@ -348,7 +348,7 @@ export class SPAClient extends BaseClient {
348348
*/
349349
private getCachedTokens(requestScopes: ScopeSet, authorityUri: string, resourceId: string, homeAccountIdentifier: string): AccessTokenCacheItem {
350350
// Get all access tokens with matching authority, resource id and home account ID
351-
const tokenCacheItems: Array<AccessTokenCacheItem> = this.cacheManager.getAllAccessTokens(this.config.authOptions.clientId, authorityUri || "", resourceId || "", homeAccountIdentifier || "");
351+
const tokenCacheItems: Array<AccessTokenCacheItem> = this.spaCacheManager.getAllAccessTokens(this.config.authOptions.clientId, authorityUri || "", resourceId || "", homeAccountIdentifier || "");
352352
if (tokenCacheItems.length === 0) {
353353
throw ClientAuthError.createNoTokensFoundError(requestScopes.printScopes());
354354
}
@@ -388,7 +388,7 @@ export class SPAClient extends BaseClient {
388388
);
389389

390390
// Create response handler
391-
const responseHandler = new SPAResponseHandler(this.config.authOptions.clientId, this.cacheStorage, this.cacheManager, this.cryptoUtils, this.logger);
391+
const responseHandler = new SPAResponseHandler(this.config.authOptions.clientId, this.cacheStorage, this.spaCacheManager, this.cryptoUtils, this.logger);
392392
// Validate response. This function throws a server error if an error is returned by the server.
393393
responseHandler.validateServerAuthorizationTokenResponse(acquiredTokenResponse.body);
394394
// Return token response with given parameters

lib/msal-common/src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// App Auth Modules and Configuration
22
export { SPAClient } from "./client/SPAClient";
3-
export { AuthorizationCodeClient } from "./client/AuthorizationCodeClient";
3+
export { AuthorizationCodeClient} from "./client/AuthorizationCodeClient";
44
export { DeviceCodeClient } from "./client/DeviceCodeClient";
55
export { RefreshTokenClient } from "./client/RefreshTokenClient";
66
export { AuthOptions, SystemOptions, LoggerOptions, TelemetryOptions } from "./config/ClientConfiguration";
@@ -13,6 +13,9 @@ export { Authority } from "./authority/Authority";
1313
export { AuthorityFactory } from "./authority/AuthorityFactory";
1414
// Cache
1515
export { ICacheStorage } from "./cache/ICacheStorage";
16+
export { UnifiedCacheManager } from "./unifiedCache/UnifiedCacheManager";
17+
export { JsonCache, InMemoryCache } from "./unifiedCache/utils/CacheTypes";
18+
export { Serializer } from "./unifiedCache/serialize/Serializer";
1619
// Network Interface
1720
export { INetworkModule, NetworkRequestOptions } from "./network/INetworkModule";
1821
export { NetworkResponse } from "./network/NetworkManager";
@@ -43,3 +46,4 @@ export { ClientConfigurationError, ClientConfigurationErrorMessage } from "./err
4346
// Constants and Utils
4447
export { Constants, PromptValue, TemporaryCacheKeys, PersistentCacheKeys } from "./utils/Constants";
4548
export { StringUtils } from "./utils/StringUtils";
49+
export { StringDict } from "./utils/MsalTypes";

lib/msal-common/src/request/AuthorizationCodeUrlRequest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export type AuthorizationCodeUrlRequest = {
6767
/**
6868
* Provides a hint about the tenant or domain that the user should use to sign in. The value
6969
* of the domain hint is a registered domain for the tenant.
70+
* TODO: Name this as "extraQueryParameters"
7071
*/
7172
domainHint?: string;
7273

0 commit comments

Comments
 (0)