|
| 1 | +/* |
| 2 | +Copyright 2026 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import fetchMock from "@fetch-mock/vitest"; |
| 18 | +import { type Mocked } from "vitest"; |
| 19 | + |
| 20 | +import { type Logger } from "../../../src/logger"; |
| 21 | +import { OAuth2, startDeviceAuthorization } from "../../../src/oauth"; |
| 22 | +import { fetchWithLogging } from "../../../src/oauth/fetch"; |
| 23 | +import { makeDelegatedAuthMetadata } from "../../test-utils/auth"; |
| 24 | +import { OAuthGrantType } from "../../../src/oauth/register"; |
| 25 | + |
| 26 | +describe("fetchWithLogging()", () => { |
| 27 | + let mockLogger: Mocked<Logger>; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + mockLogger = { |
| 31 | + debug: vi.fn(), |
| 32 | + } as unknown as Mocked<Logger>; |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + vi.useRealTimers(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should log the request and the response", async () => { |
| 40 | + vi.useFakeTimers(); |
| 41 | + const responseResolvers = Promise.withResolvers<Response>(); |
| 42 | + fetchMock.post("https://auth.org/token", responseResolvers.promise); |
| 43 | + |
| 44 | + const prom = fetchWithLogging(mockLogger, "https://auth.org/token", { method: "POST" }); |
| 45 | + vi.advanceTimersByTime(1234); |
| 46 | + responseResolvers.resolve(new Response("{}", { status: 200 })); |
| 47 | + await prom; |
| 48 | + |
| 49 | + expect(mockLogger.debug).toHaveBeenCalledTimes(2); |
| 50 | + expect(mockLogger.debug.mock.calls[0]).toEqual(["OAuth2: --> POST https://auth.org/token"]); |
| 51 | + expect(mockLogger.debug.mock.calls[1]).toEqual(["OAuth2: <-- POST https://auth.org/token [1234ms 200]"]); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should not log the values of query parameters", async () => { |
| 55 | + fetchMock.get("https://auth.org/whatever?token=super-secret", { status: 200, body: "{}" }); |
| 56 | + |
| 57 | + await fetchWithLogging(mockLogger, "https://auth.org/whatever?token=super-secret"); |
| 58 | + |
| 59 | + for (const call of mockLogger.debug.mock.calls) { |
| 60 | + expect(call[0]).not.toContain("super-secret"); |
| 61 | + } |
| 62 | + expect(mockLogger.debug.mock.calls[0]).toEqual(["OAuth2: --> GET https://auth.org/whatever?token=xxx"]); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should log the response status even when it is an error", async () => { |
| 66 | + fetchMock.post("https://auth.org/token", { status: 400, body: "{}" }); |
| 67 | + |
| 68 | + await fetchWithLogging(mockLogger, "https://auth.org/token", { method: "POST" }); |
| 69 | + |
| 70 | + expect(mockLogger.debug.mock.calls[1]).toEqual([ |
| 71 | + expect.stringMatching(/^OAuth2: <-- POST https:\/\/auth\.org\/token \[\d+ms 400\]$/), |
| 72 | + ]); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should log and rethrow network errors", async () => { |
| 76 | + const error = new Error("Network error"); |
| 77 | + fetchMock.post("https://auth.org/token", { throws: error }); |
| 78 | + |
| 79 | + await expect(fetchWithLogging(mockLogger, "https://auth.org/token", { method: "POST" })).rejects.toThrow(error); |
| 80 | + |
| 81 | + expect(mockLogger.debug.mock.calls[1]).toEqual([ |
| 82 | + expect.stringMatching(/^OAuth2: <-- POST https:\/\/auth\.org\/token \[\d+ms Error: Network error\]$/), |
| 83 | + ]); |
| 84 | + }); |
| 85 | + |
| 86 | + describe("integration", () => { |
| 87 | + const delegatedAuthConfig = makeDelegatedAuthMetadata("https://auth.org/", [ |
| 88 | + OAuthGrantType.DeviceAuthorization, |
| 89 | + ]); |
| 90 | + |
| 91 | + it("should log token endpoint requests made by OAuth2", async () => { |
| 92 | + fetchMock.post(delegatedAuthConfig.token_endpoint, { |
| 93 | + status: 200, |
| 94 | + body: { access_token: "abc123", token_type: "Bearer", expires_in: 300 }, |
| 95 | + }); |
| 96 | + |
| 97 | + const auth = new OAuth2( |
| 98 | + delegatedAuthConfig, |
| 99 | + { clientId: "test-client-id", redirectUri: "https://test.com" }, |
| 100 | + mockLogger, |
| 101 | + ); |
| 102 | + await auth.completeAuthorizationCodeGrant("code123"); |
| 103 | + |
| 104 | + expect(mockLogger.debug.mock.calls).toEqual([ |
| 105 | + ["OAuth2: --> POST https://auth.org/token"], |
| 106 | + [expect.stringMatching(/^OAuth2: <-- POST https:\/\/auth\.org\/token \[\d+ms 200\]$/)], |
| 107 | + ]); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should log revocation endpoint requests made by OAuth2", async () => { |
| 111 | + fetchMock.post(delegatedAuthConfig.revocation_endpoint, { status: 200, body: "{}" }); |
| 112 | + |
| 113 | + const auth = new OAuth2( |
| 114 | + delegatedAuthConfig, |
| 115 | + { clientId: "test-client-id", redirectUri: "https://test.com" }, |
| 116 | + mockLogger, |
| 117 | + ); |
| 118 | + await auth.revokeToken("abc123", "access_token"); |
| 119 | + |
| 120 | + expect(mockLogger.debug.mock.calls[0]).toEqual(["OAuth2: --> POST https://auth.org/revoke"]); |
| 121 | + }); |
| 122 | + |
| 123 | + it("should log registration endpoint requests made by registerClient", async () => { |
| 124 | + fetchMock.post(delegatedAuthConfig.registration_endpoint, { |
| 125 | + status: 200, |
| 126 | + body: { client_id: "xyz789" }, |
| 127 | + }); |
| 128 | + |
| 129 | + await OAuth2.registerClient( |
| 130 | + delegatedAuthConfig, |
| 131 | + { |
| 132 | + client_uri: "https://just.testing", |
| 133 | + redirect_uris: ["https://just.testing"], |
| 134 | + client_name: "Element", |
| 135 | + application_type: "web", |
| 136 | + }, |
| 137 | + mockLogger, |
| 138 | + ); |
| 139 | + |
| 140 | + expect(mockLogger.debug.mock.calls[0]).toEqual(["OAuth2: --> POST https://auth.org/registration"]); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should log device authorization endpoint requests", async () => { |
| 144 | + fetchMock.post(delegatedAuthConfig.device_authorization_endpoint!, { |
| 145 | + status: 200, |
| 146 | + body: { |
| 147 | + device_code: "device123", |
| 148 | + user_code: "USER123", |
| 149 | + verification_uri: "https://auth.org/link", |
| 150 | + expires_in: 300, |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + await startDeviceAuthorization({ |
| 155 | + clientId: "test-client-id", |
| 156 | + scope: "test-scope", |
| 157 | + metadata: delegatedAuthConfig, |
| 158 | + logger: mockLogger, |
| 159 | + }); |
| 160 | + |
| 161 | + expect(mockLogger.debug.mock.calls[0]).toEqual(["OAuth2: --> POST https://auth.org/device"]); |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments