Skip to content

Commit cb81358

Browse files
committed
Change verifyToken to match parent interface name
1 parent 476c111 commit cb81358

File tree

3 files changed

+65
-25
lines changed

3 files changed

+65
-25
lines changed

src/server/auth/handlers/token.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ describe('Token Handler', () => {
300300
endpoints: {
301301
tokenUrl: 'https://example.com/token'
302302
},
303-
verifyToken: async (token) => ({
303+
verifyAccessToken: async (token) => ({
304304
token,
305305
clientId: 'valid-client',
306306
scopes: ['read', 'write'],

src/server/auth/proxyProvider.test.ts

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { ProxyOAuthServerProvider, ProxyOptions } from "./proxyProvider.js";
33
import { AuthInfo } from "./types.js";
44
import { OAuthClientInformationFull, OAuthTokens } from "../../shared/auth.js";
55
import { ServerError } from "./errors.js";
6+
import { InvalidTokenError } from "./errors.js";
7+
import { InsufficientScopeError } from "./errors.js";
68

79
describe("Proxy OAuth Server Provider", () => {
810
// Mock client data
@@ -17,6 +19,10 @@ describe("Proxy OAuth Server Provider", () => {
1719
redirect: jest.fn(),
1820
} as unknown as Response;
1921

22+
// Mock provider functions
23+
const mockVerifyToken = jest.fn();
24+
const mockGetClient = jest.fn();
25+
2026
// Base provider options
2127
const baseOptions: ProxyOptions = {
2228
endpoints: {
@@ -25,7 +31,20 @@ describe("Proxy OAuth Server Provider", () => {
2531
revocationUrl: "https://auth.example.com/revoke",
2632
registrationUrl: "https://auth.example.com/register",
2733
},
28-
verifyToken: jest.fn().mockImplementation(async (token: string) => {
34+
verifyAccessToken: mockVerifyToken,
35+
getClient: mockGetClient,
36+
};
37+
38+
let provider: ProxyOAuthServerProvider;
39+
let originalFetch: typeof global.fetch;
40+
41+
beforeEach(() => {
42+
provider = new ProxyOAuthServerProvider(baseOptions);
43+
originalFetch = global.fetch;
44+
global.fetch = jest.fn();
45+
46+
// Setup mock implementations
47+
mockVerifyToken.mockImplementation(async (token: string) => {
2948
if (token === "valid-token") {
3049
return {
3150
token,
@@ -34,23 +53,15 @@ describe("Proxy OAuth Server Provider", () => {
3453
expiresAt: Date.now() / 1000 + 3600,
3554
} as AuthInfo;
3655
}
37-
throw new Error("Invalid token");
38-
}),
39-
getClient: jest.fn().mockImplementation(async (clientId: string) => {
56+
throw new InvalidTokenError("Invalid token");
57+
});
58+
59+
mockGetClient.mockImplementation(async (clientId: string) => {
4060
if (clientId === "test-client") {
4161
return validClient;
4262
}
4363
return undefined;
44-
}),
45-
};
46-
47-
let provider: ProxyOAuthServerProvider;
48-
let originalFetch: typeof global.fetch;
49-
50-
beforeEach(() => {
51-
provider = new ProxyOAuthServerProvider(baseOptions);
52-
originalFetch = global.fetch;
53-
global.fetch = jest.fn();
64+
});
5465
});
5566

5667
afterEach(() => {
@@ -271,15 +282,44 @@ describe("Proxy OAuth Server Provider", () => {
271282

272283
describe("token verification", () => {
273284
it("verifies valid token", async () => {
285+
const validAuthInfo: AuthInfo = {
286+
token: "valid-token",
287+
clientId: "test-client",
288+
scopes: ["read", "write"],
289+
expiresAt: Date.now() / 1000 + 3600,
290+
};
291+
mockVerifyToken.mockResolvedValue(validAuthInfo);
292+
274293
const authInfo = await provider.verifyAccessToken("valid-token");
275-
expect(authInfo.token).toBe("valid-token");
276-
expect(baseOptions.verifyToken).toHaveBeenCalledWith("valid-token");
294+
expect(authInfo).toEqual(validAuthInfo);
295+
expect(mockVerifyToken).toHaveBeenCalledWith("valid-token");
277296
});
278297

279-
it("rejects invalid token", async () => {
280-
await expect(
281-
provider.verifyAccessToken("invalid-token")
282-
).rejects.toThrow("Invalid token");
298+
it("passes through InvalidTokenError", async () => {
299+
const error = new InvalidTokenError("Token expired");
300+
mockVerifyToken.mockRejectedValue(error);
301+
302+
await expect(provider.verifyAccessToken("invalid-token"))
303+
.rejects.toBe(error);
304+
expect(mockVerifyToken).toHaveBeenCalledWith("invalid-token");
305+
});
306+
307+
it("passes through InsufficientScopeError", async () => {
308+
const error = new InsufficientScopeError("Required scopes: read, write");
309+
mockVerifyToken.mockRejectedValue(error);
310+
311+
await expect(provider.verifyAccessToken("token-with-insufficient-scope"))
312+
.rejects.toBe(error);
313+
expect(mockVerifyToken).toHaveBeenCalledWith("token-with-insufficient-scope");
314+
});
315+
316+
it("passes through unexpected errors", async () => {
317+
const error = new Error("Unexpected error");
318+
mockVerifyToken.mockRejectedValue(error);
319+
320+
await expect(provider.verifyAccessToken("valid-token"))
321+
.rejects.toBe(error);
322+
expect(mockVerifyToken).toHaveBeenCalledWith("valid-token");
283323
});
284324
});
285325
});

src/server/auth/proxyProvider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type ProxyOptions = {
2626
/**
2727
* Function to verify access tokens and return auth info
2828
*/
29-
verifyToken: (token: string) => Promise<AuthInfo>;
29+
verifyAccessToken: (token: string) => Promise<AuthInfo>;
3030

3131
/**
3232
* Function to fetch client information from the upstream server
@@ -40,7 +40,7 @@ export type ProxyOptions = {
4040
*/
4141
export class ProxyOAuthServerProvider implements OAuthServerProvider {
4242
private readonly _endpoints: ProxyEndpoints;
43-
private readonly _verifyToken: (token: string) => Promise<AuthInfo>;
43+
private readonly _verifyAccessToken: (token: string) => Promise<AuthInfo>;
4444
private readonly _getClient: (clientId: string) => Promise<OAuthClientInformationFull | undefined>;
4545

4646
public revokeToken?: (
@@ -50,7 +50,7 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
5050

5151
constructor(options: ProxyOptions) {
5252
this._endpoints = options.endpoints;
53-
this._verifyToken = options.verifyToken;
53+
this._verifyAccessToken = options.verifyAccessToken;
5454
this._getClient = options.getClient;
5555
if (options.endpoints?.revocationUrl) {
5656
this.revokeToken = async (
@@ -234,6 +234,6 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
234234
}
235235

236236
async verifyAccessToken(token: string): Promise<AuthInfo> {
237-
return this._verifyToken(token);
237+
return this._verifyAccessToken(token);
238238
}
239239
}

0 commit comments

Comments
 (0)