-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathmcp-auth.test.ts
More file actions
373 lines (312 loc) · 12.3 KB
/
mcp-auth.test.ts
File metadata and controls
373 lines (312 loc) · 12.3 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
/**
* Tests for mcp-auth.ts - Auth storage module
*/
import { describe, it, before, after } from "node:test"
import assert from "node:assert"
import { mkdirSync, rmSync, existsSync } from "fs"
import { join } from "path"
import { tmpdir } from "os"
import { randomBytes } from "crypto"
// Set up isolated temp directory for tests
const TEST_DIR = join(tmpdir(), `mcp-oauth-test-${randomBytes(4).toString('hex')}`)
process.env.MCP_OAUTH_DIR = TEST_DIR
import {
getAuthEntry,
getAuthForUrl,
saveAuthEntry,
removeAuthEntry,
updateTokens,
updateClientInfo,
updateCodeVerifier,
clearCodeVerifier,
updateOAuthState,
getOAuthState,
clearOAuthState,
isTokenExpired,
hasStoredTokens,
clearAllCredentials,
clearClientInfo,
clearTokens,
type AuthEntry,
} from "./mcp-auth.ts"
describe("mcp-auth", () => {
before(() => {
// Ensure clean state
try {
if (existsSync(TEST_DIR)) {
rmSync(TEST_DIR, { recursive: true, force: true })
}
mkdirSync(TEST_DIR, { recursive: true })
} catch {
// Ignore cleanup errors
}
})
after(() => {
// Clean up temp directory
try {
if (existsSync(TEST_DIR)) {
rmSync(TEST_DIR, { recursive: true, force: true })
}
} catch {
// Ignore cleanup errors
}
})
describe("getAuthEntry", () => {
it("should return undefined for non-existent entry", () => {
const entry = getAuthEntry("non-existent")
assert.strictEqual(entry, undefined)
})
})
describe("saveAuthEntry / getAuthEntry", () => {
it("should save and retrieve an auth entry", () => {
const entry: AuthEntry = {
tokens: {
accessToken: "test-token",
refreshToken: "refresh-token",
expiresAt: 1234567890,
scope: "read write",
},
serverUrl: "https://api.example.com",
}
saveAuthEntry("test-server", entry, "https://api.example.com")
const retrieved = getAuthEntry("test-server")
assert.deepStrictEqual(retrieved, entry)
})
it("should update existing entries", () => {
const entry1: AuthEntry = {
tokens: { accessToken: "token1" },
serverUrl: "https://api.example.com",
}
const entry2: AuthEntry = {
tokens: { accessToken: "token2" },
serverUrl: "https://api.example.com",
}
saveAuthEntry("test-server", entry1, "https://api.example.com")
saveAuthEntry("test-server", entry2, "https://api.example.com")
const retrieved = getAuthEntry("test-server")
assert.strictEqual(retrieved?.tokens?.accessToken, "token2")
})
})
describe("getAuthForUrl", () => {
it("should return entry when URL matches", () => {
const entry: AuthEntry = {
tokens: { accessToken: "test-token" },
serverUrl: "https://api.example.com",
}
saveAuthEntry("test-server", entry, "https://api.example.com")
const retrieved = getAuthForUrl("test-server", "https://api.example.com")
assert.deepStrictEqual(retrieved, entry)
})
it("should return undefined when URL doesn't match", () => {
const entry: AuthEntry = {
tokens: { accessToken: "test-token" },
serverUrl: "https://api.example.com",
}
saveAuthEntry("test-server", entry, "https://api.example.com")
const retrieved = getAuthForUrl("test-server", "https://different.com")
assert.strictEqual(retrieved, undefined)
})
it("should return undefined when serverUrl is not stored", () => {
const entry: AuthEntry = {
tokens: { accessToken: "test-token" },
}
saveAuthEntry("test-server", entry)
const retrieved = getAuthForUrl("test-server", "https://api.example.com")
assert.strictEqual(retrieved, undefined)
})
})
describe("removeAuthEntry", () => {
it("should remove an entry", () => {
const entry: AuthEntry = {
tokens: { accessToken: "test-token" },
}
saveAuthEntry("test-server", entry)
removeAuthEntry("test-server")
const retrieved = getAuthEntry("test-server")
assert.strictEqual(retrieved, undefined)
})
})
describe("updateTokens", () => {
it("should update tokens for a server", () => {
updateTokens("test-server", {
accessToken: "new-token",
refreshToken: "new-refresh",
expiresAt: 1234567890,
scope: "read",
})
const entry = getAuthEntry("test-server")
assert.strictEqual(entry?.tokens?.accessToken, "new-token")
})
it("should preserve existing client info", () => {
updateClientInfo("test-server", { clientId: "client-123" })
updateTokens("test-server", { accessToken: "token" })
const entry = getAuthEntry("test-server")
assert.strictEqual(entry?.clientInfo?.clientId, "client-123")
assert.strictEqual(entry?.tokens?.accessToken, "token")
})
it("should clear URL-bound auth state when tokens move to a different server URL", () => {
saveAuthEntry("token-url-change", {
tokens: { accessToken: "old-token", refreshToken: "old-refresh" },
clientInfo: { clientId: "old-client" },
codeVerifier: "old-verifier",
oauthState: "old-state",
serverUrl: "https://old.example.com/mcp",
}, "https://old.example.com/mcp")
updateTokens("token-url-change", { accessToken: "new-token" }, "https://new.example.com/mcp")
assert.strictEqual(getAuthForUrl("token-url-change", "https://old.example.com/mcp"), undefined)
const newEntry = getAuthForUrl("token-url-change", "https://new.example.com/mcp")
assert.strictEqual(newEntry?.tokens?.accessToken, "new-token")
assert.strictEqual(newEntry?.clientInfo, undefined)
assert.strictEqual(newEntry?.codeVerifier, undefined)
assert.strictEqual(newEntry?.oauthState, undefined)
})
it("should clear legacy URL-bound auth state when saving tokens with a server URL", () => {
saveAuthEntry("token-legacy-url-change", {
tokens: { accessToken: "old-token", refreshToken: "old-refresh" },
clientInfo: { clientId: "old-client" },
codeVerifier: "old-verifier",
oauthState: "old-state",
})
updateTokens("token-legacy-url-change", { accessToken: "new-token" }, "https://new.example.com/mcp")
const newEntry = getAuthForUrl("token-legacy-url-change", "https://new.example.com/mcp")
assert.strictEqual(newEntry?.tokens?.accessToken, "new-token")
assert.strictEqual(newEntry?.clientInfo, undefined)
assert.strictEqual(newEntry?.codeVerifier, undefined)
assert.strictEqual(newEntry?.oauthState, undefined)
})
})
describe("updateClientInfo", () => {
it("should update client info for a server", () => {
updateClientInfo("test-server", {
clientId: "client-123",
clientSecret: "secret",
clientIdIssuedAt: 1234567890,
clientSecretExpiresAt: 1234567999,
})
const entry = getAuthEntry("test-server")
assert.strictEqual(entry?.clientInfo?.clientId, "client-123")
assert.strictEqual(entry?.clientInfo?.clientSecret, "secret")
})
it("should clear URL-bound credentials when client info moves to a different server URL", () => {
saveAuthEntry("url-change", {
tokens: { accessToken: "old-token", refreshToken: "old-refresh" },
clientInfo: { clientId: "old-client" },
codeVerifier: "old-verifier",
oauthState: "old-state",
serverUrl: "https://old.example.com/mcp",
}, "https://old.example.com/mcp")
updateClientInfo("url-change", { clientId: "new-client" }, "https://new.example.com/mcp")
assert.strictEqual(getAuthForUrl("url-change", "https://old.example.com/mcp"), undefined)
const newEntry = getAuthForUrl("url-change", "https://new.example.com/mcp")
assert.strictEqual(newEntry?.clientInfo?.clientId, "new-client")
assert.strictEqual(newEntry?.tokens, undefined)
assert.strictEqual(newEntry?.codeVerifier, undefined)
assert.strictEqual(newEntry?.oauthState, undefined)
})
it("should clear stale verifier and state when legacy client info gains a server URL", () => {
saveAuthEntry("legacy-url-change", {
tokens: { accessToken: "old-token", refreshToken: "old-refresh" },
clientInfo: { clientId: "old-client" },
codeVerifier: "old-verifier",
oauthState: "old-state",
})
updateClientInfo("legacy-url-change", { clientId: "new-client" }, "https://new.example.com/mcp")
const newEntry = getAuthForUrl("legacy-url-change", "https://new.example.com/mcp")
assert.strictEqual(newEntry?.clientInfo?.clientId, "new-client")
assert.strictEqual(newEntry?.tokens, undefined)
assert.strictEqual(newEntry?.codeVerifier, undefined)
assert.strictEqual(newEntry?.oauthState, undefined)
})
})
describe("updateCodeVerifier / clearCodeVerifier", () => {
it("should save and retrieve code verifier", () => {
updateCodeVerifier("test-server", "verifier-123")
const entry = getAuthEntry("test-server")
assert.strictEqual(entry?.codeVerifier, "verifier-123")
})
it("should clear code verifier", () => {
updateCodeVerifier("test-server", "verifier-123")
clearCodeVerifier("test-server")
const entry = getAuthEntry("test-server")
assert.strictEqual(entry?.codeVerifier, undefined)
})
})
describe("updateOAuthState / getOAuthState / clearOAuthState", () => {
it("should save and retrieve OAuth state", () => {
updateOAuthState("test-server", "state-abc-123")
const state = getOAuthState("test-server")
assert.strictEqual(state, "state-abc-123")
})
it("should clear OAuth state", () => {
updateOAuthState("test-server", "state-abc-123")
clearOAuthState("test-server")
const state = getOAuthState("test-server")
assert.strictEqual(state, undefined)
})
})
describe("isTokenExpired", () => {
it("should return null if no tokens", () => {
const expired = isTokenExpired("expiry-test-null")
assert.strictEqual(expired, null)
})
it("should return false if no expiry", () => {
updateTokens("expiry-test-no-expiry", { accessToken: "token" })
const expired = isTokenExpired("expiry-test-no-expiry")
assert.strictEqual(expired, false)
})
it("should return true if expired", () => {
updateTokens("expiry-test-expired", {
accessToken: "token",
expiresAt: 1, // Way in the past
})
const expired = isTokenExpired("expiry-test-expired")
assert.strictEqual(expired, true)
})
it("should return false if not expired", () => {
updateTokens("expiry-test-future", {
accessToken: "token",
expiresAt: Date.now() / 1000 + 3600, // 1 hour from now
})
const expired = isTokenExpired("expiry-test-future")
assert.strictEqual(expired, false)
})
})
describe("hasStoredTokens", () => {
it("should return false if no tokens", () => {
assert.strictEqual(hasStoredTokens("has-tokens-test-false"), false)
})
it("should return true if tokens exist", () => {
updateTokens("has-tokens-test-true", { accessToken: "token" })
assert.strictEqual(hasStoredTokens("has-tokens-test-true"), true)
})
})
describe("clearAllCredentials", () => {
it("should remove all credentials", () => {
updateTokens("test-server", { accessToken: "token" })
updateClientInfo("test-server", { clientId: "client" })
updateCodeVerifier("test-server", "verifier")
clearAllCredentials("test-server")
assert.strictEqual(getAuthEntry("test-server"), undefined)
})
})
describe("clearClientInfo", () => {
it("should only remove client info", () => {
updateTokens("test-server", { accessToken: "token" })
updateClientInfo("test-server", { clientId: "client" })
clearClientInfo("test-server")
const entry = getAuthEntry("test-server")
assert.strictEqual(entry?.clientInfo, undefined)
assert.strictEqual(entry?.tokens?.accessToken, "token")
})
})
describe("clearTokens", () => {
it("should only remove tokens", () => {
updateTokens("test-server", { accessToken: "token" })
updateClientInfo("test-server", { clientId: "client" })
clearTokens("test-server")
const entry = getAuthEntry("test-server")
assert.strictEqual(entry?.tokens, undefined)
assert.strictEqual(entry?.clientInfo?.clientId, "client")
})
})
})