2023-06-28 21:08:56 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-06-28 21:08:56 +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-06-28 21:08:56 +00:00
|
|
|
*/
|
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
import fetchMock from "fetch-mock-jest";
|
|
|
|
import { completeAuthorizationCodeGrant } from "matrix-js-sdk/src/oidc/authorize";
|
2023-06-28 21:08:56 +00:00
|
|
|
import * as randomStringUtils from "matrix-js-sdk/src/randomstring";
|
2023-07-11 04:09:18 +00:00
|
|
|
import { BearerTokenResponse } from "matrix-js-sdk/src/oidc/validate";
|
|
|
|
import { mocked } from "jest-mock";
|
2024-02-19 16:58:59 +00:00
|
|
|
import { Crypto } from "@peculiar/webcrypto";
|
|
|
|
import { getRandomValues } from "node:crypto";
|
2023-06-28 21:08:56 +00:00
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
import { completeOidcLogin, startOidcLogin } from "../../../src/utils/oidc/authorize";
|
|
|
|
import { makeDelegatedAuthConfig } from "../../test-utils/oidc";
|
2023-10-19 02:46:37 +00:00
|
|
|
import { OidcClientError } from "../../../src/utils/oidc/error";
|
2024-02-19 15:10:11 +00:00
|
|
|
import { mockPlatformPeg } from "../../test-utils";
|
2023-06-28 21:08:56 +00:00
|
|
|
|
2023-08-09 08:39:48 +00:00
|
|
|
jest.unmock("matrix-js-sdk/src/randomstring");
|
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
jest.mock("matrix-js-sdk/src/oidc/authorize", () => ({
|
|
|
|
...jest.requireActual("matrix-js-sdk/src/oidc/authorize"),
|
|
|
|
completeAuthorizationCodeGrant: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2024-02-19 16:58:59 +00:00
|
|
|
const webCrypto = new Crypto();
|
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
describe("OIDC authorization", () => {
|
2023-06-28 21:08:56 +00:00
|
|
|
const issuer = "https://auth.com/";
|
2023-07-11 04:09:18 +00:00
|
|
|
const homeserverUrl = "https://matrix.org";
|
|
|
|
const identityServerUrl = "https://is.org";
|
2023-06-28 21:08:56 +00:00
|
|
|
const clientId = "xyz789";
|
|
|
|
const baseUrl = "https://test.com";
|
|
|
|
|
2023-07-10 00:57:16 +00:00
|
|
|
const delegatedAuthConfig = makeDelegatedAuthConfig(issuer);
|
2023-06-28 21:08:56 +00:00
|
|
|
|
|
|
|
// to restore later
|
|
|
|
const realWindowLocation = window.location;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// @ts-ignore allow delete of non-optional prop
|
|
|
|
delete window.location;
|
|
|
|
// @ts-ignore ugly mocking
|
|
|
|
window.location = {
|
|
|
|
href: baseUrl,
|
|
|
|
origin: baseUrl,
|
|
|
|
};
|
|
|
|
|
|
|
|
jest.spyOn(randomStringUtils, "randomString").mockRestore();
|
2024-02-19 15:10:11 +00:00
|
|
|
mockPlatformPeg();
|
2024-02-19 16:58:59 +00:00
|
|
|
Object.defineProperty(window, "crypto", {
|
|
|
|
value: {
|
|
|
|
getRandomValues,
|
|
|
|
randomUUID: jest.fn().mockReturnValue("not-random-uuid"),
|
|
|
|
subtle: webCrypto.subtle,
|
|
|
|
},
|
|
|
|
});
|
2023-06-28 21:08:56 +00:00
|
|
|
});
|
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
beforeAll(() => {
|
2024-02-23 16:43:14 +00:00
|
|
|
fetchMock.get(
|
|
|
|
`${delegatedAuthConfig.metadata.issuer}.well-known/openid-configuration`,
|
|
|
|
delegatedAuthConfig.metadata,
|
|
|
|
);
|
2023-07-11 04:09:18 +00:00
|
|
|
});
|
|
|
|
|
2023-06-28 21:08:56 +00:00
|
|
|
afterAll(() => {
|
|
|
|
window.location = realWindowLocation;
|
|
|
|
});
|
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
describe("startOidcLogin()", () => {
|
|
|
|
it("navigates to authorization endpoint with correct parameters", async () => {
|
|
|
|
await startOidcLogin(delegatedAuthConfig, clientId, homeserverUrl);
|
2023-06-28 21:08:56 +00:00
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
const expectedScopeWithoutDeviceId = `openid urn:matrix:org.matrix.msc2967.client:api:* urn:matrix:org.matrix.msc2967.client:device:`;
|
2023-06-28 21:08:56 +00:00
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
const authUrl = new URL(window.location.href);
|
2023-06-28 21:08:56 +00:00
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
expect(authUrl.searchParams.get("response_mode")).toEqual("query");
|
|
|
|
expect(authUrl.searchParams.get("response_type")).toEqual("code");
|
|
|
|
expect(authUrl.searchParams.get("client_id")).toEqual(clientId);
|
|
|
|
expect(authUrl.searchParams.get("code_challenge_method")).toEqual("S256");
|
2023-06-28 21:08:56 +00:00
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
// scope ends with a 10char randomstring deviceId
|
|
|
|
const scope = authUrl.searchParams.get("scope")!;
|
|
|
|
expect(scope.substring(0, scope.length - 10)).toEqual(expectedScopeWithoutDeviceId);
|
|
|
|
expect(scope.substring(scope.length - 10)).toBeTruthy();
|
|
|
|
|
|
|
|
// random string, just check they are set
|
|
|
|
expect(authUrl.searchParams.has("state")).toBeTruthy();
|
|
|
|
expect(authUrl.searchParams.has("nonce")).toBeTruthy();
|
|
|
|
expect(authUrl.searchParams.has("code_challenge")).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("completeOidcLogin()", () => {
|
|
|
|
const state = "test-state-444";
|
|
|
|
const code = "test-code-777";
|
|
|
|
const queryDict = {
|
|
|
|
code,
|
|
|
|
state: state,
|
|
|
|
};
|
|
|
|
|
|
|
|
const tokenResponse: BearerTokenResponse = {
|
|
|
|
access_token: "abc123",
|
|
|
|
refresh_token: "def456",
|
2024-05-07 11:27:37 +00:00
|
|
|
id_token: "ghi789",
|
2023-07-11 04:09:18 +00:00
|
|
|
scope: "test",
|
|
|
|
token_type: "Bearer",
|
|
|
|
expires_at: 12345,
|
|
|
|
};
|
2023-06-28 21:08:56 +00:00
|
|
|
|
2023-07-11 04:09:18 +00:00
|
|
|
beforeEach(() => {
|
2023-10-04 04:06:04 +00:00
|
|
|
mocked(completeAuthorizationCodeGrant)
|
|
|
|
.mockClear()
|
|
|
|
.mockResolvedValue({
|
|
|
|
oidcClientSettings: {
|
|
|
|
clientId,
|
|
|
|
issuer,
|
|
|
|
},
|
|
|
|
tokenResponse,
|
|
|
|
homeserverUrl,
|
|
|
|
identityServerUrl,
|
|
|
|
idTokenClaims: {
|
|
|
|
aud: "123",
|
|
|
|
iss: issuer,
|
|
|
|
sub: "123",
|
|
|
|
exp: 123,
|
|
|
|
iat: 456,
|
|
|
|
},
|
|
|
|
});
|
2023-07-11 04:09:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should throw when query params do not include state and code", async () => {
|
2023-10-19 02:46:37 +00:00
|
|
|
await expect(async () => await completeOidcLogin({})).rejects.toThrow(
|
|
|
|
OidcClientError.InvalidQueryParameters,
|
2023-07-11 04:09:18 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should make request complete authorization code grant", async () => {
|
|
|
|
await completeOidcLogin(queryDict);
|
|
|
|
|
|
|
|
expect(completeAuthorizationCodeGrant).toHaveBeenCalledWith(code, state);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return accessToken, configured homeserver and identityServer", async () => {
|
|
|
|
const result = await completeOidcLogin(queryDict);
|
|
|
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
accessToken: tokenResponse.access_token,
|
2023-09-19 00:06:19 +00:00
|
|
|
refreshToken: tokenResponse.refresh_token,
|
2023-07-11 04:09:18 +00:00
|
|
|
homeserverUrl,
|
|
|
|
identityServerUrl,
|
2023-07-20 21:30:19 +00:00
|
|
|
issuer,
|
|
|
|
clientId,
|
2024-05-07 11:27:37 +00:00
|
|
|
idToken: "ghi789",
|
2023-10-04 04:06:04 +00:00
|
|
|
idTokenClaims: result.idTokenClaims,
|
2023-07-11 04:09:18 +00:00
|
|
|
});
|
|
|
|
});
|
2023-06-28 21:08:56 +00:00
|
|
|
});
|
|
|
|
});
|