fix: Add username to the tokenStoreKey so the saved token is invalidated when the user changes in the environment (#426)

Co-authored-by: Gregory Schier <gschier1990@gmail.com>
This commit is contained in:
Michaël Arnauts
2026-08-14 13:50:48 -07:00
committed by GitHub
co-authored by Gregory Schier
parent 4f03c5c390
commit be004425fa
4 changed files with 68 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
import { describe, expect, test } from "vite-plus/test";
import type { TokenStoreArgs } from "../src/store";
import { getToken, storeToken } from "../src/store";
function createMockContext() {
const values = new Map<string, unknown>();
return {
store: {
async set<T>(key: string, value: T) {
values.set(key, value);
},
async get<T>(key: string) {
return values.get(key) as T | undefined;
},
},
} as never;
}
describe("token store", () => {
test("separates password grant tokens when the username changes", async () => {
const ctx = createMockContext();
const aliceArgs: TokenStoreArgs = {
contextId: "request-1",
clientId: "client-123",
accessTokenUrl: "https://auth.example.com/token",
authorizationUrl: null,
username: "alice@example.com",
};
const bobArgs: TokenStoreArgs = { ...aliceArgs, username: "bob@example.com" };
await storeToken(ctx, aliceArgs, { access_token: "alice-token" });
expect((await getToken(ctx, aliceArgs))?.response.access_token).toBe("alice-token");
expect(await getToken(ctx, bobArgs)).toBeUndefined();
});
test("keeps the same key for grants without a username", async () => {
const ctx = createMockContext();
const args: TokenStoreArgs = {
contextId: "request-1",
clientId: "client-123",
accessTokenUrl: "https://auth.example.com/token",
authorizationUrl: null,
};
await storeToken(ctx, args, { access_token: "cc-token" });
expect((await getToken(ctx, { ...args, username: null }))?.response.access_token).toBe(
"cc-token",
);
expect((await getToken(ctx, { ...args, username: "" }))?.response.access_token).toBe("cc-token");
});
});