Skip to content

Commit

Permalink
AuthorityMetadataEntity refactor into Type (#6802)
Browse files Browse the repository at this point in the history
Continues the work to convert cache entities from Classes into Types for
size/perf gains
  • Loading branch information
tnorling authored Jan 8, 2024
1 parent 19811d7 commit 2c5e457
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 353 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Refactor AuthorityMetadataEntity into type",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Refactor AuthorityMetadataEntity into type",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Refactor AuthorityMetadataEntity into type",
"packageName": "@azure/msal-node",
"email": "[email protected]",
"dependentChangeType": "patch"
}
10 changes: 2 additions & 8 deletions lib/msal-browser/src/cache/BrowserCacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,18 +950,12 @@ export class BrowserCacheManager extends CacheManager {
const parsedMetadata = this.validateAndParseJson(value);
if (
parsedMetadata &&
AuthorityMetadataEntity.isAuthorityMetadataEntity(
key,
parsedMetadata
)
CacheHelpers.isAuthorityMetadataEntity(key, parsedMetadata)
) {
this.logger.trace(
"BrowserCacheManager.getAuthorityMetadata: cache hit"
);
return CacheManager.toObject(
new AuthorityMetadataEntity(),
parsedMetadata
);
return parsedMetadata as AuthorityMetadataEntity;
}
return null;
}
Expand Down
65 changes: 21 additions & 44 deletions lib/msal-browser/test/app/PublicClientApplication.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,27 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
NavigationClient.prototype,
"navigateInternal"
).mockImplementation();

jest.spyOn(
CacheManager.prototype,
"getAuthorityMetadataByAlias"
).mockImplementation((host) => {
const authorityMetadata: AuthorityMetadataEntity = {
aliases: [host],
preferred_cache: host,
preferred_network: host,
aliasesFromNetwork: false,
canonical_authority: host,
authorization_endpoint: "",
token_endpoint: "",
end_session_endpoint: "",
issuer: "",
jwks_uri: "",
endpointsFromNetwork: false,
expiresAt: CacheHelpers.generateAuthorityMetadataExpiresAt(),
};
return authorityMetadata;
});
});

afterEach(() => {
Expand Down Expand Up @@ -5088,21 +5109,6 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
pca = (pca as any).controller;
await pca.initialize();

sinon
.stub(CacheManager.prototype, "getAuthorityMetadataByAlias")
.callsFake((host) => {
const authorityMetadata = new AuthorityMetadataEntity();
authorityMetadata.updateCloudDiscoveryMetadata(
{
aliases: [host],
preferred_cache: host,
preferred_network: host,
},
false
);
return authorityMetadata;
});

// @ts-ignore
pca.getBrowserStorage().setAccount(testAccount);
// @ts-ignore
Expand Down Expand Up @@ -5172,20 +5178,6 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
beforeEach(async () => {
pca = (pca as any).controller;
await pca.initialize();
sinon
.stub(CacheManager.prototype, "getAuthorityMetadataByAlias")
.callsFake((host) => {
const authorityMetadata = new AuthorityMetadataEntity();
authorityMetadata.updateCloudDiscoveryMetadata(
{
aliases: [host],
preferred_cache: host,
preferred_network: host,
},
false
);
return authorityMetadata;
});

// @ts-ignore
pca.getBrowserStorage().setAccount(testAccount1);
Expand Down Expand Up @@ -5418,21 +5410,6 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
pca.getBrowserStorage().setIdTokenCredential(idToken1);
// @ts-ignore
pca.getBrowserStorage().setIdTokenCredential(idToken2);

sinon
.stub(CacheManager.prototype, "getAuthorityMetadataByAlias")
.callsFake((host) => {
const authorityMetadata = new AuthorityMetadataEntity();
authorityMetadata.updateCloudDiscoveryMetadata(
{
aliases: [host],
preferred_cache: host,
preferred_network: host,
},
false
);
return authorityMetadata;
});
});

afterEach(() => {
Expand Down
128 changes: 70 additions & 58 deletions lib/msal-browser/test/cache/BrowserCacheManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1404,27 +1404,33 @@ describe("BrowserCacheManager tests", () => {

describe("AuthorityMetadata", () => {
const key = `authority-metadata-${TEST_CONFIG.MSAL_CLIENT_ID}-${Constants.DEFAULT_AUTHORITY_HOST}`;
const testObj: AuthorityMetadataEntity =
new AuthorityMetadataEntity();
testObj.aliases = [Constants.DEFAULT_AUTHORITY_HOST];
testObj.preferred_cache = Constants.DEFAULT_AUTHORITY_HOST;
testObj.preferred_network = Constants.DEFAULT_AUTHORITY_HOST;
testObj.canonical_authority = Constants.DEFAULT_AUTHORITY;
testObj.authorization_endpoint =
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.authorization_endpoint;
testObj.token_endpoint =
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.token_endpoint;
testObj.end_session_endpoint =
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.end_session_endpoint;
//@ts-ignore
testObj.issuer = DEFAULT_OPENID_CONFIG_RESPONSE.body.issuer;
//@ts-ignore
testObj.jwks_uri = DEFAULT_OPENID_CONFIG_RESPONSE.body.jwks_uri;
testObj.aliasesFromNetwork = false;
testObj.endpointsFromNetwork = false;
const testObj: AuthorityMetadataEntity = {
aliases: [Constants.DEFAULT_AUTHORITY_HOST],
preferred_cache: Constants.DEFAULT_AUTHORITY_HOST,
preferred_network: Constants.DEFAULT_AUTHORITY_HOST,
canonical_authority: Constants.DEFAULT_AUTHORITY,
authorization_endpoint:
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body
.authorization_endpoint,
token_endpoint:
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.token_endpoint,
end_session_endpoint:
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body
.end_session_endpoint,
issuer:
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.issuer,
jwks_uri:
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.jwks_uri,
aliasesFromNetwork: false,
endpointsFromNetwork: false,
expiresAt:
CacheHelpers.generateAuthorityMetadataExpiresAt(),
};

it("getAuthorityMetadata() returns null if key is not in cache", () => {
expect(
Expand All @@ -1436,14 +1442,14 @@ describe("BrowserCacheManager tests", () => {
});

it("getAuthorityMetadata() returns null if isAuthorityMetadataEntity returns false", () => {
sinon
.stub(
AuthorityMetadataEntity,
"isAuthorityMetadataEntity"
)
.returns(false);
browserSessionStorage.setAuthorityMetadata(key, testObj);
browserLocalStorage.setAuthorityMetadata(key, testObj);
browserSessionStorage.setAuthorityMetadata(key, {
// @ts-ignore
invalidKey: "invalidValue",
});
browserLocalStorage.setAuthorityMetadata(key, {
// @ts-ignore
invalidKey: "invalidValue",
});
expect(
browserSessionStorage.getAuthorityMetadata(key)
).toBeNull();
Expand Down Expand Up @@ -2308,27 +2314,33 @@ describe("BrowserCacheManager tests", () => {

describe("AuthorityMetadata", () => {
const key = `authority-metadata-${TEST_CONFIG.MSAL_CLIENT_ID}-${Constants.DEFAULT_AUTHORITY_HOST}`;
const testObj: AuthorityMetadataEntity =
new AuthorityMetadataEntity();
testObj.aliases = [Constants.DEFAULT_AUTHORITY_HOST];
testObj.preferred_cache = Constants.DEFAULT_AUTHORITY_HOST;
testObj.preferred_network = Constants.DEFAULT_AUTHORITY_HOST;
testObj.canonical_authority = Constants.DEFAULT_AUTHORITY;
testObj.authorization_endpoint =
// @ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.authorization_endpoint;
testObj.token_endpoint =
// @ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.token_endpoint;
testObj.end_session_endpoint =
// @ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.end_session_endpoint;
// @ts-ignore
testObj.issuer = DEFAULT_OPENID_CONFIG_RESPONSE.body.issuer;
// @ts-ignore
testObj.jwks_uri = DEFAULT_OPENID_CONFIG_RESPONSE.body.jwks_uri;
testObj.aliasesFromNetwork = false;
testObj.endpointsFromNetwork = false;
const testObj: AuthorityMetadataEntity = {
aliases: [Constants.DEFAULT_AUTHORITY_HOST],
preferred_cache: Constants.DEFAULT_AUTHORITY_HOST,
preferred_network: Constants.DEFAULT_AUTHORITY_HOST,
canonical_authority: Constants.DEFAULT_AUTHORITY,
authorization_endpoint:
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body
.authorization_endpoint,
token_endpoint:
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.token_endpoint,
end_session_endpoint:
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body
.end_session_endpoint,
issuer:
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.issuer,
jwks_uri:
//@ts-ignore
DEFAULT_OPENID_CONFIG_RESPONSE.body.jwks_uri,
aliasesFromNetwork: false,
endpointsFromNetwork: false,
expiresAt:
CacheHelpers.generateAuthorityMetadataExpiresAt(),
};

it("getAuthorityMetadata() returns null if key is not in cache", () => {
expect(
Expand All @@ -2340,14 +2352,14 @@ describe("BrowserCacheManager tests", () => {
});

it("getAuthorityMetadata() returns null if isAuthorityMetadataEntity returns false", () => {
sinon
.stub(
AuthorityMetadataEntity,
"isAuthorityMetadataEntity"
)
.returns(false);
browserSessionStorage.setAuthorityMetadata(key, testObj);
browserLocalStorage.setAuthorityMetadata(key, testObj);
browserSessionStorage.setAuthorityMetadata(key, {
// @ts-ignore
invalidKey: "invalidValue",
});
browserLocalStorage.setAuthorityMetadata(key, {
// @ts-ignore
invalidKey: "invalidValue",
});

expect(
browserSessionStorage.getAuthorityMetadata(key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ClientConfigurationErrorCodes,
CacheManager,
IdTokenEntity,
AuthorityMetadataEntity,
CacheHelpers,
} from "@azure/msal-common";
import {
TEST_DATA_CLIENT_INFO,
Expand All @@ -25,6 +25,7 @@ import {
} from "../utils/StringConstants";
import { BaseInteractionClient } from "../../src/interaction_client/BaseInteractionClient";
import { EndSessionRequest, PublicClientApplication } from "../../src";
import { OpenIdConfigResponse } from "@azure/msal-common/dist/authority/OpenIdConfigResponse";

class testInteractionClient extends BaseInteractionClient {
acquireToken(): Promise<void> {
Expand Down Expand Up @@ -154,15 +155,24 @@ describe("BaseInteractionClient", () => {
const metadata =
DEFAULT_TENANT_DISCOVERY_RESPONSE.body.metadata[0];
const openIdConfigResponse =
DEFAULT_OPENID_CONFIG_RESPONSE.body;
const authorityMetadata = new AuthorityMetadataEntity();
authorityMetadata.updateCloudDiscoveryMetadata(metadata, true);
authorityMetadata.updateEndpointMetadata(
// @ts-ignore
openIdConfigResponse,
true
);
return authorityMetadata;
DEFAULT_OPENID_CONFIG_RESPONSE.body as OpenIdConfigResponse;
return {
aliases: [],
preferred_cache: metadata.preferred_cache,
preferred_network: metadata.preferred_network,
canonical_authority: host,
authorization_endpoint:
openIdConfigResponse.authorization_endpoint,
token_endpoint: openIdConfigResponse.token_endpoint,
end_session_endpoint:
openIdConfigResponse.end_session_endpoint,
issuer: openIdConfigResponse.issuer,
aliasesFromNetwork: true,
endpointsFromNetwork: true,
expiresAt:
CacheHelpers.generateAuthorityMetadataExpiresAt(),
jwks_uri: openIdConfigResponse.jwks_uri,
};
});
});

Expand Down
Loading

0 comments on commit 2c5e457

Please sign in to comment.