|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
1 | 5 | /*
|
2 | 6 | Copyright 2023 The Matrix.org Foundation C.I.C.
|
3 | 7 |
|
@@ -25,29 +29,27 @@ import {
|
25 | 29 | completeAuthorizationCodeGrant,
|
26 | 30 | generateAuthorizationParams,
|
27 | 31 | generateAuthorizationUrl,
|
| 32 | + generateOidcAuthorizationUrl, |
28 | 33 | } from "../../../src/oidc/authorize";
|
29 | 34 | import { OidcError } from "../../../src/oidc/error";
|
| 35 | +import { makeDelegatedAuthConfig, mockOpenIdConfiguration } from "../../test-utils/oidc"; |
30 | 36 |
|
31 | 37 | jest.mock("jwt-decode");
|
32 | 38 |
|
33 | 39 | // save for resetting mocks
|
34 | 40 | const realSubtleCrypto = crypto.subtleCrypto;
|
35 | 41 |
|
36 | 42 | describe("oidc authorization", () => {
|
37 |
| - const issuer = "https://auth.com/"; |
38 |
| - const authorizationEndpoint = "https://auth.com/authorization"; |
39 |
| - const tokenEndpoint = "https://auth.com/token"; |
40 |
| - const delegatedAuthConfig = { |
41 |
| - issuer, |
42 |
| - registrationEndpoint: issuer + "registration", |
43 |
| - authorizationEndpoint: issuer + "auth", |
44 |
| - tokenEndpoint, |
45 |
| - }; |
| 43 | + const delegatedAuthConfig = makeDelegatedAuthConfig(); |
| 44 | + const authorizationEndpoint = delegatedAuthConfig.metadata.authorization_endpoint; |
| 45 | + const tokenEndpoint = delegatedAuthConfig.metadata.token_endpoint; |
46 | 46 | const clientId = "xyz789";
|
47 | 47 | const baseUrl = "https://test.com";
|
48 | 48 |
|
49 | 49 | beforeAll(() => {
|
50 | 50 | jest.spyOn(logger, "warn");
|
| 51 | + |
| 52 | + fetchMock.get(delegatedAuthConfig.issuer + ".well-known/openid-configuration", mockOpenIdConfiguration()); |
51 | 53 | });
|
52 | 54 |
|
53 | 55 | afterEach(() => {
|
@@ -97,20 +99,36 @@ describe("oidc authorization", () => {
|
97 | 99 | "A secure context is required to generate code challenge. Using plain text code challenge",
|
98 | 100 | );
|
99 | 101 | });
|
| 102 | + }); |
| 103 | + |
| 104 | + describe("generateOidcAuthorizationUrl()", () => { |
| 105 | + it("should generate url with correct parameters", async () => { |
| 106 | + const nonce = "abc123"; |
| 107 | + |
| 108 | + const metadata = delegatedAuthConfig.metadata; |
100 | 109 |
|
101 |
| - it("uses a s256 code challenge when crypto is available", async () => { |
102 |
| - jest.spyOn(crypto.subtleCrypto, "digest"); |
103 |
| - const authorizationParams = generateAuthorizationParams({ redirectUri: baseUrl }); |
104 | 110 | const authUrl = new URL(
|
105 |
| - await generateAuthorizationUrl(authorizationEndpoint, clientId, authorizationParams), |
| 111 | + await generateOidcAuthorizationUrl({ |
| 112 | + metadata, |
| 113 | + homeserverUrl: baseUrl, |
| 114 | + clientId, |
| 115 | + redirectUri: baseUrl, |
| 116 | + nonce, |
| 117 | + }), |
106 | 118 | );
|
107 | 119 |
|
108 |
| - const codeChallenge = authUrl.searchParams.get("code_challenge"); |
109 |
| - expect(crypto.subtleCrypto.digest).toHaveBeenCalledWith("SHA-256", expect.any(Object)); |
| 120 | + expect(authUrl.searchParams.get("response_mode")).toEqual("query"); |
| 121 | + expect(authUrl.searchParams.get("response_type")).toEqual("code"); |
| 122 | + expect(authUrl.searchParams.get("client_id")).toEqual(clientId); |
| 123 | + expect(authUrl.searchParams.get("code_challenge_method")).toEqual("S256"); |
| 124 | + // scope minus the 10char random device id at the end |
| 125 | + expect(authUrl.searchParams.get("scope")!.slice(0, -10)).toEqual( |
| 126 | + "openid urn:matrix:org.matrix.msc2967.client:api:* urn:matrix:org.matrix.msc2967.client:device:", |
| 127 | + ); |
| 128 | + expect(authUrl.searchParams.get("state")).toBeTruthy(); |
| 129 | + expect(authUrl.searchParams.get("nonce")).toEqual(nonce); |
110 | 130 |
|
111 |
| - // didn't use plain text code challenge |
112 |
| - expect(authorizationParams.codeVerifier).not.toEqual(codeChallenge); |
113 |
| - expect(codeChallenge).toBeTruthy(); |
| 131 | + expect(authUrl.searchParams.get("code_challenge")).toBeTruthy(); |
114 | 132 | });
|
115 | 133 | });
|
116 | 134 |
|
|
0 commit comments