Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions lib/msal-browser/src/cache/AccountManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ export function getAccount(
browserStorage: BrowserCacheManager,
correlationId: string
): AccountInfo | null {
logger.trace("getAccount called", correlationId);
if (Object.keys(accountFilter).length === 0) {
logger.warning("getAccount: No accountFilter provided", correlationId);
return null;
}

logger.trace("getAccount called");
const account: AccountInfo | null = browserStorage.getAccountInfoFilteredBy(
accountFilter,
correlationId
Expand Down
19 changes: 19 additions & 0 deletions lib/msal-browser/test/cache/BrowserCacheManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
AccountEntityUtils,
Constants,
CredentialEntity,
AccountFilter,
} from "@azure/msal-common";
import {
BrowserCacheLocation,
Expand All @@ -50,6 +51,7 @@ import { BrowserPerformanceClient } from "../../src/telemetry/BrowserPerformance
import { EventHandler } from "../../src/event/EventHandler.js";
import { version } from "../../src/packageMetadata.js";
import * as CacheKeys from "../../src/cache/CacheKeys.js";
import { getAccount } from "../../src/cache/AccountManager.js";

describe("BrowserCacheManager tests", () => {
let cacheConfig: Required<CacheOptions>;
Expand Down Expand Up @@ -2311,6 +2313,23 @@ describe("BrowserCacheManager tests", () => {
)
).toEqual(testAccount);
});


it("getAccount returns null if accountfilter is passed but values are undefined", () => {
const testAccountFilter: AccountFilter = {
loginHint: undefined,
sid: undefined,
};

expect(
getAccount(
testAccountFilter,
logger,
browserSessionStorage,
TEST_CONFIG.CORRELATION_ID
)
).toEqual(null);
});
});

describe("IdTokenCredential", () => {
Expand Down
9 changes: 9 additions & 0 deletions lib/msal-common/src/cache/CacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ export abstract class CacheManager implements ICacheManager {
accountFilter: AccountFilter,
correlationId: string
): AccountInfo | null {
if (
Object.keys(accountFilter).length === 0 ||
Object.values(accountFilter).every((value) => !value)
) {
this.commonLogger.warning(
"getAccountInfoFilteredBy: Account filter is empty or invalid, returning null"
);
return null;
}
const allAccounts = this.getAllAccounts(accountFilter, correlationId);
if (allAccounts.length > 1) {
// If one or more accounts are found, prioritize accounts that have an ID token
Expand Down
12 changes: 12 additions & 0 deletions lib/msal-common/test/cache/CacheManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,18 @@ describe("CacheManager.ts test cases", () => {
).toBeNull();
});

it("returns null if filter passed in is empty", () => {
expect(
mockCache.cacheManager.getAccountInfoFilteredBy(
{
homeAccountId: "",
loginHint: "",
},
RANDOM_TEST_GUID
)
).toBeNull();
});

it("returns an account matching filter", () => {
const resultAccount =
mockCache.cacheManager.getAccountInfoFilteredBy(
Expand Down
Loading