-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathoauth-token.test.js
More file actions
458 lines (405 loc) · 15.6 KB
/
Copy pathoauth-token.test.js
File metadata and controls
458 lines (405 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Tests for /api/oauth/token — OAuth 2.1 token endpoint.
// Focus: authorization_code grant + PKCE verification, refresh_token rotation
// with strict scope-narrowing, client authentication, and reuse detection.
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { Readable } from 'node:stream';
process.env.PUBLIC_APP_ORIGIN ||= 'https://app.test';
process.env.JWT_SECRET ||= 'test-secret-token-suite-only';
// ── Mocks ─────────────────────────────────────────────────────────────────
const sqlState = { queue: [], calls: [] };
vi.mock('../../api/_lib/db.js', () => ({
sql: vi.fn(async (strings, ...values) => {
sqlState.calls.push({ query: strings.join('?'), values });
if (sqlState.queue.length === 0) return [];
const next = sqlState.queue.shift();
if (next instanceof Error) throw next;
return next;
}),
}));
const rlState = { success: true };
vi.mock('../../api/_lib/rate-limit.js', () => ({
limits: { oauthToken: vi.fn(async () => ({ success: rlState.success })) },
clientIp: () => '127.0.0.1',
}));
const authState = {
mintAccess: vi.fn(async ({ scope }) => `access.${scope.replaceAll(' ', '+')}.jwt`),
issueRefresh: vi.fn(async ({ scope }) => ({
token: `refresh.${scope.replaceAll(' ', '+')}`,
id: 'rt-id',
})),
rotate: null,
};
vi.mock('../../api/_lib/auth.js', () => ({
mintAccessToken: (...args) => authState.mintAccess(...args),
issueRefreshToken: (...args) => authState.issueRefresh(...args),
rotateRefreshToken: (...args) => authState.rotate(...args),
}));
const cryptoState = {
// PKCE: handler computes sha256Base64Url(verifier) and compares against
// the stored code_challenge. Tests can set what the mock returns.
pkceComputed: 'EXPECTED_CHALLENGE',
sha256: vi.fn(async (s) => `hash:${s}`),
constTimeEqual: true,
};
vi.mock('../../api/_lib/crypto.js', () => ({
sha256: (...args) => cryptoState.sha256(...args),
sha256Base64Url: vi.fn(async () => cryptoState.pkceComputed),
constantTimeEquals: vi.fn(() => cryptoState.constTimeEqual),
}));
const { default: handler } = await import('../../api/oauth/[action].js');
// ── Helpers ───────────────────────────────────────────────────────────────
function makeReq({ method = 'POST', headers = {}, formBody = {} } = {}) {
const base = Readable.from([Buffer.from(new URLSearchParams(formBody).toString())]);
base.method = method;
base.url = '/api/oauth/token';
base.headers = {
host: 'app.test',
'content-type': 'application/x-www-form-urlencoded',
...headers,
};
return base;
}
function makeRes() {
return {
statusCode: 200,
headers: {},
body: '',
writableEnded: false,
setHeader(k, v) {
this.headers[k.toLowerCase()] = v;
},
getHeader(k) {
return this.headers[k.toLowerCase()];
},
end(chunk) {
if (chunk !== undefined) this.body += chunk;
this.writableEnded = true;
},
};
}
async function invoke(opts) {
const req = makeReq(opts);
const res = makeRes();
await handler(req, res);
const json = res.body ? safeJson(res.body) : null;
return { res, status: res.statusCode, body: json };
}
function safeJson(s) {
try {
return JSON.parse(s);
} catch {
return s;
}
}
const PUBLIC_CLIENT = {
client_id: 'mcp_pub',
client_type: 'public',
scope: 'avatars:read avatars:write profile offline_access',
grant_types: ['authorization_code', 'refresh_token'],
};
const CONFIDENTIAL_CLIENT = {
client_id: 'mcp_conf',
client_type: 'confidential',
client_secret_hash: 'hash:secret123',
scope: 'avatars:read profile',
grant_types: ['authorization_code', 'refresh_token'],
};
const FUTURE = new Date(Date.now() + 60_000).toISOString();
const PAST = new Date(Date.now() - 60_000).toISOString();
const AUTH_CODE_ROW = {
code: 'auth-code-1',
client_id: 'mcp_pub',
user_id: 'user-1',
redirect_uri: 'https://client.test/cb',
scope: 'avatars:read profile',
resource: 'https://app.test/api/mcp',
code_challenge: 'EXPECTED_CHALLENGE',
code_challenge_method: 'S256',
expires_at: FUTURE,
consumed_at: null,
};
beforeEach(() => {
sqlState.queue = [];
sqlState.calls = [];
rlState.success = true;
cryptoState.pkceComputed = 'EXPECTED_CHALLENGE';
cryptoState.constTimeEqual = true;
authState.mintAccess.mockClear();
authState.issueRefresh.mockClear();
authState.rotate = vi.fn();
});
// ── Method / preflight ────────────────────────────────────────────────────
describe('/api/oauth/token — method handling', () => {
it('returns 405 for GET', async () => {
const req = makeReq({ method: 'GET' });
const res = makeRes();
await handler(req, res);
expect(res.statusCode).toBe(405);
});
it('returns 204 for OPTIONS preflight', async () => {
const req = makeReq({ method: 'OPTIONS' });
const res = makeRes();
await handler(req, res);
expect(res.statusCode).toBe(204);
});
});
// ── Client identification + authentication ────────────────────────────────
describe('/api/oauth/token — client auth', () => {
it('returns invalid_client when client_id missing', async () => {
const { status, body } = await invoke({ formBody: { grant_type: 'authorization_code' } });
expect(status).toBe(400);
expect(body.error).toBe('invalid_client');
});
it('rate-limits per client_id', async () => {
rlState.success = false;
const { status, body } = await invoke({
formBody: { client_id: 'mcp_pub', grant_type: 'authorization_code' },
});
expect(status).toBe(429);
expect(body.error).toBe('rate_limited');
});
it('returns invalid_client when client unknown in DB', async () => {
sqlState.queue.push([]); // client lookup empty
const { status, body } = await invoke({
formBody: { client_id: 'mcp_unknown', grant_type: 'authorization_code' },
});
expect(status).toBe(400);
expect(body.error).toBe('invalid_client');
});
it('rejects confidential client with bad secret', async () => {
sqlState.queue.push([CONFIDENTIAL_CLIENT]);
cryptoState.constTimeEqual = false;
const { status, body } = await invoke({
formBody: {
client_id: 'mcp_conf',
client_secret: 'wrong',
grant_type: 'authorization_code',
},
});
expect(status).toBe(401);
expect(body.error).toBe('invalid_client');
});
it('accepts confidential client secret via HTTP Basic auth header', async () => {
sqlState.queue.push([CONFIDENTIAL_CLIENT]);
cryptoState.constTimeEqual = true;
// Then the unsupported_grant_type path so we don't have to fully wire auth_code.
const basic = 'Basic ' + Buffer.from('mcp_conf:secret123').toString('base64');
const { status, body } = await invoke({
headers: { authorization: basic },
formBody: { grant_type: 'unsupported_grant' },
});
expect(status).toBe(400);
expect(body.error).toBe('unsupported_grant_type');
});
});
// ── grant_type routing ────────────────────────────────────────────────────
describe('/api/oauth/token — grant_type routing', () => {
it('rejects unknown grant_type', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
const { status, body } = await invoke({
formBody: { client_id: 'mcp_pub', grant_type: 'password' },
});
expect(status).toBe(400);
expect(body.error).toBe('unsupported_grant_type');
});
});
// ── authorization_code grant ──────────────────────────────────────────────
describe('/api/oauth/token — authorization_code grant', () => {
const baseForm = {
client_id: 'mcp_pub',
grant_type: 'authorization_code',
code: 'auth-code-1',
redirect_uri: 'https://client.test/cb',
code_verifier: 'verifier-string',
};
it('rejects when required fields missing', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
const { code_verifier: _drop, ...withoutVerifier } = baseForm;
const { status, body } = await invoke({ formBody: withoutVerifier });
expect(status).toBe(400);
expect(body.error).toBe('invalid_request');
});
it('rejects unknown auth code', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
sqlState.queue.push([]); // no code row
const { status, body } = await invoke({ formBody: baseForm });
expect(status).toBe(400);
expect(body.error).toBe('invalid_grant');
expect(body.error_description).toMatch(/unknown code/);
});
it('rejects already-consumed code AND revokes refresh tokens (replay defence)', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
sqlState.queue.push([{ ...AUTH_CODE_ROW, consumed_at: PAST }]);
sqlState.queue.push([]); // revoke update
const { status, body } = await invoke({ formBody: baseForm });
expect(status).toBe(400);
expect(body.error).toBe('invalid_grant');
const revoke = sqlState.calls.find((c) =>
/update oauth_refresh_tokens set revoked_at/i.test(c.query),
);
expect(revoke).toBeTruthy();
});
it('rejects expired code', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
sqlState.queue.push([{ ...AUTH_CODE_ROW, expires_at: PAST }]);
const { status, body } = await invoke({ formBody: baseForm });
expect(status).toBe(400);
expect(body.error_description).toMatch(/expired/);
});
it('rejects code issued to different client', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
sqlState.queue.push([{ ...AUTH_CODE_ROW, client_id: 'mcp_other' }]);
const { status, body } = await invoke({ formBody: baseForm });
expect(status).toBe(400);
expect(body.error_description).toMatch(/client mismatch/);
});
it('rejects redirect_uri mismatch', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
sqlState.queue.push([AUTH_CODE_ROW]);
const { status, body } = await invoke({
formBody: { ...baseForm, redirect_uri: 'https://client.test/other' },
});
expect(status).toBe(400);
expect(body.error_description).toMatch(/redirect_uri mismatch/);
});
it('rejects when PKCE verifier does not hash to stored challenge', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
sqlState.queue.push([AUTH_CODE_ROW]);
cryptoState.pkceComputed = 'WRONG_HASH';
const { status, body } = await invoke({ formBody: baseForm });
expect(status).toBe(400);
expect(body.error_description).toMatch(/PKCE verification failed/);
});
it('issues access + refresh on success and consumes the auth code', async () => {
sqlState.queue.push([PUBLIC_CLIENT]); // client
sqlState.queue.push([AUTH_CODE_ROW]); // code lookup
sqlState.queue.push([{ code: 'authcode' }]); // atomic consume returns the row
const { status, body } = await invoke({ formBody: baseForm });
expect(status).toBe(200);
expect(body.token_type).toBe('Bearer');
expect(body.expires_in).toBe(3600);
expect(body.scope).toBe('avatars:read profile');
expect(body.access_token).toContain('access.');
expect(body.refresh_token).toContain('refresh.');
// Code marked consumed.
const consume = sqlState.calls.find((c) =>
/update oauth_auth_codes set consumed_at/i.test(c.query),
);
expect(consume).toBeTruthy();
// Access token minted with the code's scope and resource.
expect(authState.mintAccess).toHaveBeenCalledWith(
expect.objectContaining({
userId: 'user-1',
clientId: 'mcp_pub',
scope: 'avatars:read profile',
}),
);
});
it('omits refresh_token when client lacks refresh_token grant', async () => {
const noRefresh = { ...PUBLIC_CLIENT, grant_types: ['authorization_code'] };
sqlState.queue.push([noRefresh]);
sqlState.queue.push([AUTH_CODE_ROW]);
sqlState.queue.push([{ code: 'authcode' }]); // atomic consume returns the row
const { status, body } = await invoke({ formBody: baseForm });
expect(status).toBe(200);
expect(body.refresh_token).toBeUndefined();
expect(authState.issueRefresh).not.toHaveBeenCalled();
});
});
// ── refresh_token grant ───────────────────────────────────────────────────
describe('/api/oauth/token — refresh_token grant', () => {
const baseForm = {
client_id: 'mcp_pub',
grant_type: 'refresh_token',
refresh_token: 'old-refresh-secret',
};
it('rejects missing refresh_token', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
const { status, body } = await invoke({
formBody: { client_id: 'mcp_pub', grant_type: 'refresh_token' },
});
expect(status).toBe(400);
expect(body.error).toBe('invalid_request');
});
it('issues a new access+refresh on successful rotation (no scope narrowing)', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
authState.rotate = vi.fn(async ({ narrowScope }) => {
// Caller did not pass scope → narrowScope("stored") returns stored unchanged.
const eff = narrowScope('avatars:read profile');
return {
userId: 'user-1',
scope: eff,
resource: 'https://app.test/api/mcp',
next: { token: 'rotated-refresh', id: 'rt-2' },
};
});
const { status, body } = await invoke({ formBody: baseForm });
expect(status).toBe(200);
expect(body.scope).toBe('avatars:read profile');
expect(body.refresh_token).toBe('rotated-refresh');
expect(body.access_token).toContain('access.');
});
it('narrows scope when caller requests a strict subset', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
authState.rotate = vi.fn(async ({ narrowScope }) => {
const eff = narrowScope('avatars:read profile');
return {
userId: 'user-1',
scope: eff,
resource: null,
next: { token: 'narrowed-refresh', id: 'rt-3' },
};
});
const { status, body } = await invoke({
formBody: { ...baseForm, scope: 'avatars:read' },
});
expect(status).toBe(200);
expect(body.scope).toBe('avatars:read');
});
it('rejects scope-widening attempt (requested scope not subset of stored)', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
// Simulate the handler calling narrowScope and the function throwing
// invalid_scope, which the handler converts into the JSON error below.
authState.rotate = vi.fn(async ({ narrowScope }) => {
narrowScope('avatars:read profile'); // throws
});
const { status, body } = await invoke({
formBody: { ...baseForm, scope: 'avatars:read avatars:delete' },
});
expect(status).toBe(400);
expect(body.error).toBe('invalid_scope');
expect(body.error_description).toMatch(/exceeds granted scope/);
});
it('rejects empty scope subset (asks for nothing while sending scope param)', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
authState.rotate = vi.fn(async ({ narrowScope }) => {
narrowScope('avatars:read profile'); // throws — '' is not a subset
});
const { status, body } = await invoke({
formBody: { ...baseForm, scope: 'unknown:scope' },
});
expect(status).toBe(400);
expect(body.error).toBe('invalid_scope');
});
it('propagates invalid_grant from rotation (e.g. revoked / unknown token)', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
authState.rotate = vi.fn(async () => {
throw Object.assign(new Error('invalid_grant'), { status: 400 });
});
const { status, body } = await invoke({ formBody: baseForm });
expect(status).toBe(400);
expect(body.error).toBe('invalid_grant');
});
it('propagates refresh_reuse_detected (chain revoked) from rotation', async () => {
sqlState.queue.push([PUBLIC_CLIENT]);
authState.rotate = vi.fn(async () => {
throw Object.assign(new Error('reuse'), {
status: 400,
code: 'refresh_reuse_detected',
});
});
const { status, body } = await invoke({ formBody: baseForm });
expect(status).toBe(400);
expect(body.error).toBe('refresh_reuse_detected');
});
});