|
| 1 | +import test from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import type { AppContext } from '../app-context.ts' |
| 4 | +import type { AuthenticatedRequest } from './request.ts' |
| 5 | +import { authenticateRequest } from './middleware.ts' |
| 6 | +import { hashAuthToken } from './tokens.ts' |
| 7 | + |
| 8 | +function createContext(overrides: Partial<AppContext['config']> = {}, metaStoreOverrides: Partial<AppContext['metaStore']> = {}) { |
| 9 | + return { |
| 10 | + config: { |
| 11 | + port: 3000, |
| 12 | + host: '127.0.0.1', |
| 13 | + publicUrl: undefined, |
| 14 | + serverName: 'Hosted Server', |
| 15 | + mode: 'hosted', |
| 16 | + requestBodyLimit: '10mb', |
| 17 | + auth: { |
| 18 | + enabled: true, |
| 19 | + sessionTtlHours: 720, |
| 20 | + apiKeyTtlDays: 365, |
| 21 | + rateLimitWindowMs: 600000, |
| 22 | + rateLimitMaxAttempts: 10, |
| 23 | + oidcStateTtlMinutes: 15, |
| 24 | + }, |
| 25 | + metaStore: { |
| 26 | + driver: 'sqlite', |
| 27 | + sqlitePath: '/tmp/test.sqlite', |
| 28 | + mysql: { |
| 29 | + host: '127.0.0.1', |
| 30 | + port: 3306, |
| 31 | + user: 'starquery', |
| 32 | + password: 'starquery', |
| 33 | + database: 'starquery', |
| 34 | + }, |
| 35 | + }, |
| 36 | + ...overrides, |
| 37 | + }, |
| 38 | + metaStore: { |
| 39 | + getAuthTokenByHash: async () => null, |
| 40 | + deleteAuthToken: async () => undefined, |
| 41 | + getUserWithRoles: async () => null, |
| 42 | + updateAuthTokenLastUsed: async () => undefined, |
| 43 | + countUsers: async () => 0, |
| 44 | + ...metaStoreOverrides, |
| 45 | + }, |
| 46 | + } as unknown as AppContext |
| 47 | +} |
| 48 | + |
| 49 | +function createRequest(token?: string) { |
| 50 | + return { |
| 51 | + headers: token ? { authorization: `Bearer ${token}` } : {}, |
| 52 | + } as AuthenticatedRequest |
| 53 | +} |
| 54 | + |
| 55 | +test('authenticateRequest grants full local access when auth is disabled', async () => { |
| 56 | + const context = createContext({ |
| 57 | + mode: 'local', |
| 58 | + auth: { |
| 59 | + enabled: false, |
| 60 | + sessionTtlHours: 720, |
| 61 | + apiKeyTtlDays: 365, |
| 62 | + rateLimitWindowMs: 600000, |
| 63 | + rateLimitMaxAttempts: 10, |
| 64 | + oidcStateTtlMinutes: 15, |
| 65 | + }, |
| 66 | + }) |
| 67 | + const req = createRequest() |
| 68 | + |
| 69 | + const auth = await authenticateRequest(context, req) |
| 70 | + |
| 71 | + assert.equal(auth.kind, 'local') |
| 72 | + assert.deepEqual(auth.permissions, ['*']) |
| 73 | +}) |
| 74 | + |
| 75 | +test('authenticateRequest rejects expired tokens and deletes them', async () => { |
| 76 | + const deletions: string[] = [] |
| 77 | + const rawToken = 'expired-token' |
| 78 | + const context = createContext( |
| 79 | + {}, |
| 80 | + { |
| 81 | + getAuthTokenByHash: async (tokenHash: string) => |
| 82 | + tokenHash === hashAuthToken(rawToken) |
| 83 | + ? { |
| 84 | + id: 'token-1', |
| 85 | + userId: 'user-1', |
| 86 | + kind: 'session', |
| 87 | + name: 'Expired', |
| 88 | + tokenPrefix: 'expired', |
| 89 | + tokenHash, |
| 90 | + storage: 'local', |
| 91 | + expiresAt: '2000-01-01T00:00:00.000Z', |
| 92 | + lastUsedAt: null, |
| 93 | + createdAt: '2000-01-01T00:00:00.000Z', |
| 94 | + } |
| 95 | + : null, |
| 96 | + deleteAuthToken: async (tokenId: string) => { |
| 97 | + deletions.push(tokenId) |
| 98 | + }, |
| 99 | + }, |
| 100 | + ) |
| 101 | + const req = createRequest(rawToken) |
| 102 | + |
| 103 | + const auth = await authenticateRequest(context, req) |
| 104 | + |
| 105 | + assert.equal(auth.kind, 'anonymous') |
| 106 | + assert.deepEqual(deletions, ['token-1']) |
| 107 | +}) |
0 commit comments