2023-10-12 00:49:07 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-10-12 00:49:07 +00:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-10-12 00:49:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import fetchMock from "fetch-mock-jest";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import { TokenRefresher } from "../../../../src/utils/oidc/TokenRefresher";
|
|
|
|
import { persistAccessTokenInStorage, persistRefreshTokenInStorage } from "../../../../src/utils/tokens/tokens";
|
|
|
|
import { mockPlatformPeg } from "../../../test-utils";
|
|
|
|
import { makeDelegatedAuthConfig } from "../../../test-utils/oidc";
|
2023-10-12 00:49:07 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../src/utils/tokens/tokens", () => ({
|
2023-10-12 00:49:07 +00:00
|
|
|
persistAccessTokenInStorage: jest.fn(),
|
|
|
|
persistRefreshTokenInStorage: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe("TokenRefresher", () => {
|
|
|
|
const clientId = "test-client-id";
|
|
|
|
const issuer = "https://auth.com/";
|
|
|
|
const redirectUri = "https://test.com";
|
|
|
|
const deviceId = "test-device-id";
|
|
|
|
const userId = "@alice:server.org";
|
|
|
|
const accessToken = "test-access-token";
|
|
|
|
const refreshToken = "test-refresh-token";
|
|
|
|
|
|
|
|
const authConfig = makeDelegatedAuthConfig(issuer);
|
|
|
|
const idTokenClaims = {
|
|
|
|
aud: "123",
|
|
|
|
iss: issuer,
|
|
|
|
sub: "123",
|
|
|
|
exp: 123,
|
|
|
|
iat: 456,
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2024-02-23 16:43:14 +00:00
|
|
|
fetchMock.get(`${issuer}.well-known/openid-configuration`, authConfig.metadata);
|
|
|
|
fetchMock.get(`${issuer}jwks`, {
|
2023-10-12 00:49:07 +00:00
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
keys: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
mocked(persistAccessTokenInStorage).mockResolvedValue(undefined);
|
|
|
|
mocked(persistRefreshTokenInStorage).mockResolvedValue(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should persist tokens with a pickle key", async () => {
|
|
|
|
const pickleKey = "test-pickle-key";
|
|
|
|
const getPickleKey = jest.fn().mockResolvedValue(pickleKey);
|
|
|
|
mockPlatformPeg({ getPickleKey });
|
|
|
|
|
2024-02-23 16:43:14 +00:00
|
|
|
const refresher = new TokenRefresher(issuer, clientId, redirectUri, deviceId, idTokenClaims, userId);
|
2023-10-12 00:49:07 +00:00
|
|
|
|
|
|
|
await refresher.oidcClientReady;
|
|
|
|
|
|
|
|
await refresher.persistTokens({ accessToken, refreshToken });
|
|
|
|
|
|
|
|
expect(getPickleKey).toHaveBeenCalledWith(userId, deviceId);
|
|
|
|
expect(persistAccessTokenInStorage).toHaveBeenCalledWith(accessToken, pickleKey);
|
|
|
|
expect(persistRefreshTokenInStorage).toHaveBeenCalledWith(refreshToken, pickleKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should persist tokens without a pickle key", async () => {
|
|
|
|
const getPickleKey = jest.fn().mockResolvedValue(null);
|
|
|
|
mockPlatformPeg({ getPickleKey });
|
|
|
|
|
2024-02-23 16:43:14 +00:00
|
|
|
const refresher = new TokenRefresher(issuer, clientId, redirectUri, deviceId, idTokenClaims, userId);
|
2023-10-12 00:49:07 +00:00
|
|
|
|
|
|
|
await refresher.oidcClientReady;
|
|
|
|
|
|
|
|
await refresher.persistTokens({ accessToken, refreshToken });
|
|
|
|
|
|
|
|
expect(getPickleKey).toHaveBeenCalledWith(userId, deviceId);
|
|
|
|
expect(persistAccessTokenInStorage).toHaveBeenCalledWith(accessToken, undefined);
|
|
|
|
expect(persistRefreshTokenInStorage).toHaveBeenCalledWith(refreshToken, undefined);
|
|
|
|
});
|
|
|
|
});
|