Skip to content

Commit 5f49a5e

Browse files
authored
Refactor token cache entities into types (#6580)
Refactors `CredentialEntity`, `IdTokenEntity`, `AccessTokenEntity`, and `RefreshTokenEntity` to be Types rather than a Class and moves static class methods into functions exported to the `CacheHelpers` namespace. Making these types and separating the class methods from the type definition allows us to read from the cache and directly use the value without needing to first copy each key/value pair into an instance of the class (the `toObject` helper function). Doing it this way also results in a small bundle size improvement. Reviews can be focused on the `msal-common/src/cache` folder. The rest of the changes are repetitively changing references to the affected functions
1 parent f0092e2 commit 5f49a5e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+991
-1451
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Refactor token cache entities to be defined as Types rather than Classes #6580",
4+
"packageName": "@azure/msal-browser",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Refactor token cache entities to be defined as Types rather than Classes #6580",
4+
"packageName": "@azure/msal-common",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Refactor token cache entities to be defined as Types rather than Classes #6580",
4+
"packageName": "@azure/msal-node",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

lib/msal-browser/src/cache/BrowserCacheManager.ts

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
PerformanceEvents,
3737
IPerformanceClient,
3838
StaticAuthorityOptions,
39+
CacheHelpers,
3940
} from "@azure/msal-common";
4041
import { CacheOptions } from "../config/Configuration";
4142
import {
@@ -234,17 +235,15 @@ export class BrowserCacheManager extends CacheManager {
234235
if (credObj && credObj.hasOwnProperty("credentialType")) {
235236
switch (credObj["credentialType"]) {
236237
case CredentialType.ID_TOKEN:
237-
if (IdTokenEntity.isIdTokenEntity(credObj)) {
238+
if (CacheHelpers.isIdTokenEntity(credObj)) {
238239
this.logger.trace(
239240
"BrowserCacheManager:createKeyMaps - idToken found, saving key to token key map"
240241
);
241242
this.logger.tracePii(
242243
`BrowserCacheManager:createKeyMaps - idToken with key: ${key} found, saving key to token key map`
243244
);
244-
const idTokenEntity = CacheManager.toObject(
245-
new IdTokenEntity(),
246-
credObj
247-
);
245+
const idTokenEntity =
246+
credObj as IdTokenEntity;
248247
const newKey =
249248
this.updateCredentialCacheKey(
250249
key,
@@ -266,22 +265,15 @@ export class BrowserCacheManager extends CacheManager {
266265
break;
267266
case CredentialType.ACCESS_TOKEN:
268267
case CredentialType.ACCESS_TOKEN_WITH_AUTH_SCHEME:
269-
if (
270-
AccessTokenEntity.isAccessTokenEntity(
271-
credObj
272-
)
273-
) {
268+
if (CacheHelpers.isAccessTokenEntity(credObj)) {
274269
this.logger.trace(
275270
"BrowserCacheManager:createKeyMaps - accessToken found, saving key to token key map"
276271
);
277272
this.logger.tracePii(
278273
`BrowserCacheManager:createKeyMaps - accessToken with key: ${key} found, saving key to token key map`
279274
);
280275
const accessTokenEntity =
281-
CacheManager.toObject(
282-
new AccessTokenEntity(),
283-
credObj
284-
);
276+
credObj as AccessTokenEntity;
285277
const newKey =
286278
this.updateCredentialCacheKey(
287279
key,
@@ -303,9 +295,7 @@ export class BrowserCacheManager extends CacheManager {
303295
break;
304296
case CredentialType.REFRESH_TOKEN:
305297
if (
306-
RefreshTokenEntity.isRefreshTokenEntity(
307-
credObj
308-
)
298+
CacheHelpers.isRefreshTokenEntity(credObj)
309299
) {
310300
this.logger.trace(
311301
"BrowserCacheManager:createKeyMaps - refreshToken found, saving key to token key map"
@@ -314,10 +304,7 @@ export class BrowserCacheManager extends CacheManager {
314304
`BrowserCacheManager:createKeyMaps - refreshToken with key: ${key} found, saving key to token key map`
315305
);
316306
const refreshTokenEntity =
317-
CacheManager.toObject(
318-
new RefreshTokenEntity(),
319-
credObj
320-
);
307+
credObj as RefreshTokenEntity;
321308
const newKey =
322309
this.updateCredentialCacheKey(
323310
key,
@@ -718,7 +705,7 @@ export class BrowserCacheManager extends CacheManager {
718705
}
719706

720707
const parsedIdToken = this.validateAndParseJson(value);
721-
if (!parsedIdToken || !IdTokenEntity.isIdTokenEntity(parsedIdToken)) {
708+
if (!parsedIdToken || !CacheHelpers.isIdTokenEntity(parsedIdToken)) {
722709
this.logger.trace(
723710
"BrowserCacheManager.getIdTokenCredential: called, no cache hit"
724711
);
@@ -729,7 +716,7 @@ export class BrowserCacheManager extends CacheManager {
729716
this.logger.trace(
730717
"BrowserCacheManager.getIdTokenCredential: cache hit"
731718
);
732-
return CacheManager.toObject(new IdTokenEntity(), parsedIdToken);
719+
return parsedIdToken as IdTokenEntity;
733720
}
734721

735722
/**
@@ -738,7 +725,7 @@ export class BrowserCacheManager extends CacheManager {
738725
*/
739726
setIdTokenCredential(idToken: IdTokenEntity): void {
740727
this.logger.trace("BrowserCacheManager.setIdTokenCredential called");
741-
const idTokenKey = idToken.generateCredentialKey();
728+
const idTokenKey = CacheHelpers.generateCredentialKey(idToken);
742729

743730
this.setItem(idTokenKey, JSON.stringify(idToken));
744731

@@ -761,7 +748,7 @@ export class BrowserCacheManager extends CacheManager {
761748
const parsedAccessToken = this.validateAndParseJson(value);
762749
if (
763750
!parsedAccessToken ||
764-
!AccessTokenEntity.isAccessTokenEntity(parsedAccessToken)
751+
!CacheHelpers.isAccessTokenEntity(parsedAccessToken)
765752
) {
766753
this.logger.trace(
767754
"BrowserCacheManager.getAccessTokenCredential: called, no cache hit"
@@ -773,10 +760,7 @@ export class BrowserCacheManager extends CacheManager {
773760
this.logger.trace(
774761
"BrowserCacheManager.getAccessTokenCredential: cache hit"
775762
);
776-
return CacheManager.toObject(
777-
new AccessTokenEntity(),
778-
parsedAccessToken
779-
);
763+
return parsedAccessToken as AccessTokenEntity;
780764
}
781765

782766
/**
@@ -787,7 +771,7 @@ export class BrowserCacheManager extends CacheManager {
787771
this.logger.trace(
788772
"BrowserCacheManager.setAccessTokenCredential called"
789773
);
790-
const accessTokenKey = accessToken.generateCredentialKey();
774+
const accessTokenKey = CacheHelpers.generateCredentialKey(accessToken);
791775
this.setItem(accessTokenKey, JSON.stringify(accessToken));
792776

793777
this.addTokenKey(accessTokenKey, CredentialType.ACCESS_TOKEN);
@@ -811,7 +795,7 @@ export class BrowserCacheManager extends CacheManager {
811795
const parsedRefreshToken = this.validateAndParseJson(value);
812796
if (
813797
!parsedRefreshToken ||
814-
!RefreshTokenEntity.isRefreshTokenEntity(parsedRefreshToken)
798+
!CacheHelpers.isRefreshTokenEntity(parsedRefreshToken)
815799
) {
816800
this.logger.trace(
817801
"BrowserCacheManager.getRefreshTokenCredential: called, no cache hit"
@@ -823,10 +807,7 @@ export class BrowserCacheManager extends CacheManager {
823807
this.logger.trace(
824808
"BrowserCacheManager.getRefreshTokenCredential: cache hit"
825809
);
826-
return CacheManager.toObject(
827-
new RefreshTokenEntity(),
828-
parsedRefreshToken
829-
);
810+
return parsedRefreshToken as RefreshTokenEntity;
830811
}
831812

832813
/**
@@ -837,7 +818,8 @@ export class BrowserCacheManager extends CacheManager {
837818
this.logger.trace(
838819
"BrowserCacheManager.setRefreshTokenCredential called"
839820
);
840-
const refreshTokenKey = refreshToken.generateCredentialKey();
821+
const refreshTokenKey =
822+
CacheHelpers.generateCredentialKey(refreshToken);
841823
this.setItem(refreshTokenKey, JSON.stringify(refreshToken));
842824

843825
this.addTokenKey(refreshTokenKey, CredentialType.REFRESH_TOKEN);
@@ -1804,7 +1786,7 @@ export class BrowserCacheManager extends CacheManager {
18041786
currentCacheKey: string,
18051787
credential: ValidCredentialType
18061788
): string {
1807-
const updatedCacheKey = credential.generateCredentialKey();
1789+
const updatedCacheKey = CacheHelpers.generateCredentialKey(credential);
18081790

18091791
if (currentCacheKey !== updatedCacheKey) {
18101792
const cacheItem = this.getItem(currentCacheKey);
@@ -1860,7 +1842,7 @@ export class BrowserCacheManager extends CacheManager {
18601842
| RedirectRequest
18611843
| PopupRequest
18621844
): Promise<void> {
1863-
const idTokenEntity = IdTokenEntity.createIdTokenEntity(
1845+
const idTokenEntity = CacheHelpers.createIdTokenEntity(
18641846
result.account?.homeAccountId,
18651847
result.account?.environment,
18661848
result.idToken,
@@ -1872,7 +1854,7 @@ export class BrowserCacheManager extends CacheManager {
18721854
if (request.claims) {
18731855
claimsHash = await this.cryptoImpl.hashString(request.claims);
18741856
}
1875-
const accessTokenEntity = AccessTokenEntity.createAccessTokenEntity(
1857+
const accessTokenEntity = CacheHelpers.createAccessTokenEntity(
18761858
result.account?.homeAccountId,
18771859
result.account.environment,
18781860
result.accessToken,
@@ -1881,7 +1863,7 @@ export class BrowserCacheManager extends CacheManager {
18811863
result.scopes.join(" "),
18821864
result.expiresOn?.getTime() || 0,
18831865
result.extExpiresOn?.getTime() || 0,
1884-
this.cryptoImpl,
1866+
base64Decode,
18851867
undefined, // refreshOn
18861868
result.tokenType as AuthenticationScheme,
18871869
undefined, // userAssertionHash

lib/msal-browser/src/cache/TokenCache.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
Constants,
1919
CacheRecord,
2020
TokenClaims,
21+
CacheHelpers,
2122
} from "@azure/msal-common";
2223
import { BrowserConfiguration } from "../config/Configuration";
2324
import { SilentRequest } from "../request/SilentRequest";
@@ -294,7 +295,7 @@ export class TokenCache implements ITokenCache {
294295
environment: string,
295296
tenantId: string
296297
): IdTokenEntity {
297-
const idTokenEntity = IdTokenEntity.createIdTokenEntity(
298+
const idTokenEntity = CacheHelpers.createIdTokenEntity(
298299
homeAccountId,
299300
environment,
300301
idToken,
@@ -355,7 +356,7 @@ export class TokenCache implements ITokenCache {
355356
response.expires_in + new Date().getTime() / 1000;
356357
const extendedExpiresOn = options.extendedExpiresOn;
357358

358-
const accessTokenEntity = AccessTokenEntity.createAccessTokenEntity(
359+
const accessTokenEntity = CacheHelpers.createAccessTokenEntity(
359360
homeAccountId,
360361
environment,
361362
response.access_token,
@@ -364,7 +365,7 @@ export class TokenCache implements ITokenCache {
364365
scopes,
365366
expiresOn,
366367
extendedExpiresOn,
367-
this.cryptoObj
368+
base64Decode
368369
);
369370

370371
if (this.isBrowserEnvironment) {
@@ -399,7 +400,7 @@ export class TokenCache implements ITokenCache {
399400
return null;
400401
}
401402

402-
const refreshTokenEntity = RefreshTokenEntity.createRefreshTokenEntity(
403+
const refreshTokenEntity = CacheHelpers.createRefreshTokenEntity(
403404
homeAccountId,
404405
environment,
405406
response.refresh_token,

lib/msal-browser/src/interaction_client/NativeInteractionClient.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
invokeAsync,
3434
createAuthError,
3535
AuthErrorCodes,
36+
CacheHelpers,
3637
} from "@azure/msal-common";
3738
import { BaseInteractionClient } from "./BaseInteractionClient";
3839
import { BrowserConfiguration } from "../config/Configuration";
@@ -656,7 +657,7 @@ export class NativeInteractionClient extends BaseInteractionClient {
656657
reqTimestamp: number
657658
): void {
658659
const cachedIdToken: IdTokenEntity | null =
659-
IdTokenEntity.createIdTokenEntity(
660+
CacheHelpers.createIdTokenEntity(
660661
homeAccountIdentifier,
661662
request.authority,
662663
response.id_token || "",
@@ -675,7 +676,7 @@ export class NativeInteractionClient extends BaseInteractionClient {
675676
const responseScopes = this.generateScopes(response, request);
676677

677678
const cachedAccessToken: AccessTokenEntity | null =
678-
AccessTokenEntity.createAccessTokenEntity(
679+
CacheHelpers.createAccessTokenEntity(
679680
homeAccountIdentifier,
680681
request.authority,
681682
responseAccessToken,
@@ -684,7 +685,7 @@ export class NativeInteractionClient extends BaseInteractionClient {
684685
responseScopes.printScopes(),
685686
tokenExpirationSeconds,
686687
0,
687-
this.browserCrypto
688+
base64Decode
688689
);
689690

690691
const nativeCacheRecord = new CacheRecord(

0 commit comments

Comments
 (0)