-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathauth-helpers.test.js
More file actions
193 lines (166 loc) · 6.93 KB
/
Copy pathauth-helpers.test.js
File metadata and controls
193 lines (166 loc) · 6.93 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
// Unit tests for auth.js helpers: JWT minting/verification and CSRF tokens.
// Uses real jose and real HMAC — no mocks needed for pure crypto functions.
import { describe, it, expect, vi } from 'vitest';
process.env.PUBLIC_APP_ORIGIN ||= 'https://app.test';
process.env.JWT_SECRET ||= 'test-auth-helpers-secret-at-least-32ch';
// db.js is lazy (Proxy), but mock it to prevent accidental real connections.
vi.mock('../../api/_lib/db.js', () => ({
sql: vi.fn(async () => []),
}));
const { mintAccessToken, authenticateBearer, csrfTokenFor, verifyCsrfToken } =
await import('../../api/_lib/auth.js');
const AUDIENCE = 'https://app.test/api/mcp';
const ISSUER = 'https://app.test';
// ── mintAccessToken ───────────────────────────────────────────────────────────
describe('mintAccessToken', () => {
it('returns a signed JWT string', async () => {
const token = await mintAccessToken({
userId: 'user-1',
clientId: 'mcp_test',
scope: 'avatars:read',
resource: AUDIENCE,
});
expect(typeof token).toBe('string');
expect(token.split('.').length).toBe(3);
});
it('embeds correct sub, scope, aud, and exp claims', async () => {
const token = await mintAccessToken({
userId: 'user-42',
clientId: 'mcp_test',
scope: 'avatars:read profile',
resource: AUDIENCE,
});
const [, payloadB64] = token.split('.');
const payload = JSON.parse(Buffer.from(payloadB64, 'base64url').toString());
expect(payload.sub).toBe('user-42');
expect(payload.scope).toBe('avatars:read profile');
expect(payload.aud).toBe(AUDIENCE);
expect(payload.iss).toBe(ISSUER);
expect(typeof payload.exp).toBe('number');
expect(payload.exp).toBeGreaterThan(Math.floor(Date.now() / 1000));
expect(payload.exp - payload.iat).toBe(3600);
});
it('uses env.MCP_RESOURCE as audience when resource is omitted', async () => {
const token = await mintAccessToken({
userId: 'user-3',
clientId: 'mcp_test',
scope: 'profile',
});
const [, payloadB64] = token.split('.');
const payload = JSON.parse(Buffer.from(payloadB64, 'base64url').toString());
expect(payload.aud).toBe(AUDIENCE);
});
});
// ── authenticateBearer ────────────────────────────────────────────────────────
describe('authenticateBearer', () => {
it('returns { userId, scope, source, clientId } for a valid JWT', async () => {
const token = await mintAccessToken({
userId: 'user-abc',
clientId: 'mcp_pub',
scope: 'avatars:read',
resource: AUDIENCE,
});
const result = await authenticateBearer(token);
expect(result).not.toBeNull();
expect(result.userId).toBe('user-abc');
expect(result.scope).toBe('avatars:read');
expect(result.source).toBe('oauth');
expect(result.clientId).toBe('mcp_pub');
});
it('returns null for null token', async () => {
expect(await authenticateBearer(null)).toBeNull();
});
it('returns null for undefined token', async () => {
expect(await authenticateBearer(undefined)).toBeNull();
});
it('returns null for a tampered signature', async () => {
const token = await mintAccessToken({
userId: 'user-abc',
clientId: 'mcp_pub',
scope: 'avatars:read',
resource: AUDIENCE,
});
const parts = token.split('.');
// Flip the last 4 chars of the signature segment.
parts[2] = parts[2].slice(0, -4) + 'XXXX';
const tampered = parts.join('.');
expect(await authenticateBearer(tampered)).toBeNull();
});
it('returns null for an expired JWT', async () => {
// Mint a token with past expiry using jose directly.
const { SignJWT } = await import('jose');
const key = new TextEncoder().encode(process.env.JWT_SECRET);
const now = Math.floor(Date.now() / 1000);
const expired = await new SignJWT({ scope: 'avatars:read', client_id: 'mcp_test' })
.setProtectedHeader({ alg: 'HS256', typ: 'JWT' })
.setIssuer(ISSUER)
.setSubject('user-expired')
.setAudience(AUDIENCE)
.setIssuedAt(now - 7200)
.setExpirationTime(now - 3600)
.sign(key);
expect(await authenticateBearer(expired)).toBeNull();
});
it('returns null for a JWT signed with a different secret', async () => {
const { SignJWT } = await import('jose');
const wrongKey = new TextEncoder().encode('wrong-secret-totally-different-key!!');
const now = Math.floor(Date.now() / 1000);
const badToken = await new SignJWT({ scope: 'avatars:read', client_id: 'mcp_test' })
.setProtectedHeader({ alg: 'HS256', typ: 'JWT' })
.setIssuer(ISSUER)
.setSubject('user-bad')
.setAudience(AUDIENCE)
.setIssuedAt(now)
.setExpirationTime(now + 3600)
.sign(wrongKey);
expect(await authenticateBearer(badToken)).toBeNull();
});
});
// ── csrfTokenFor + verifyCsrfToken ────────────────────────────────────────────
describe('csrfTokenFor', () => {
it('returns an HMAC string when session cookie is present', async () => {
const req = { headers: { cookie: '__Host-sid=my-session-abc' } };
const csrf = await csrfTokenFor(req);
expect(typeof csrf).toBe('string');
expect(csrf.length).toBeGreaterThan(0);
});
it('returns null when no session cookie is present', async () => {
const req = { headers: { cookie: '' } };
expect(await csrfTokenFor(req)).toBeNull();
});
it('returns null when cookie header is missing entirely', async () => {
const req = { headers: {} };
expect(await csrfTokenFor(req)).toBeNull();
});
});
describe('verifyCsrfToken', () => {
it('round-trip: generated token passes verification against the same session', async () => {
const req = { headers: { cookie: '__Host-sid=session-token-xyz' } };
const csrf = await csrfTokenFor(req);
expect(await verifyCsrfToken(req, csrf)).toBe(true);
});
it('returns false when submitted token is for a different session', async () => {
const req1 = { headers: { cookie: '__Host-sid=session-one' } };
const csrf1 = await csrfTokenFor(req1);
const req2 = { headers: { cookie: '__Host-sid=session-two' } };
expect(await verifyCsrfToken(req2, csrf1)).toBe(false);
});
it('returns false for null submitted token', async () => {
const req = { headers: { cookie: '__Host-sid=some-session' } };
expect(await verifyCsrfToken(req, null)).toBe(false);
});
it('returns false for empty submitted token', async () => {
const req = { headers: { cookie: '__Host-sid=some-session' } };
expect(await verifyCsrfToken(req, '')).toBe(false);
});
it('returns false when no session cookie exists on the request', async () => {
const req = { headers: {} };
expect(await verifyCsrfToken(req, 'some-csrf-token')).toBe(false);
});
it('accepts the legacy "sid" cookie name', async () => {
const req = { headers: { cookie: 'sid=legacy-session-token' } };
const csrf = await csrfTokenFor(req);
expect(typeof csrf).toBe('string');
expect(await verifyCsrfToken(req, csrf)).toBe(true);
});
});