Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { env } from 'cloudflare:test'
3import { SignJWT, exportJWK, generateKeyPair } from 'jose'
4
5import { getDb } from '../db/index.ts'
6import { createAuth, publicCapabilityNames } from '../lib/auth.ts'
7import {
8 authMd,
9 oauthAuthorizationServer,
10 oauthProtectedResource,
11} from './auth-discovery.ts'
12
13const BASE = 'http://agents.test'
14
15describe('auth discovery (task 10.4)', () => {
16 it('protected-resource metadata exposes real scopes and docs', () => {
17 const doc = oauthProtectedResource(BASE) as {
18 resource: string
19 authorization_servers: Array<string>
20 scopes_supported: Array<string>
21 resource_documentation: string
22 }
23 expect(doc.resource).toBe(BASE)
24 expect(doc.authorization_servers).toEqual([BASE])
25 expect(doc.scopes_supported).toEqual(publicCapabilityNames())
26 expect(doc.scopes_supported.length).toBeGreaterThan(0)
27 expect(doc.resource_documentation).toBe(`${BASE}/auth.md`)
28 })
29
30 it('authorization-server metadata is honest — no fake OAuth endpoints', () => {
31 const doc = oauthAuthorizationServer(BASE)
32 expect(doc.issuer).toBe(`${BASE}/api/auth`)
33 expect(doc.authorization_endpoint).toBeUndefined()
34 expect(doc.token_endpoint).toBeUndefined()
35 expect(doc.grant_types_supported).toEqual([])
36 })
37
38 it('auth.md documents both registration paths and every scope', () => {
39 const md = authMd(BASE)
40 expect(md).toContain('/v1/agents/register-key')
41 expect(md).toContain(`${BASE}/api/auth/agent/register`)
42 expect(md).toContain('.well-known/agent-configuration')
43 for (const name of publicCapabilityNames()) {
44 expect(md).toContain(`\`${name}\``)
45 }
46 })
47
48 it('agent_auth.register_uri round-trips against the live register endpoint', async () => {
49 const auth = createAuth(getDb(env), {
50 secret: 'vitest-only-better-auth-secret-0123456789',
51 baseUrl: BASE,
52 })
53 const doc = oauthAuthorizationServer(BASE) as {
54 agent_auth: { register_uri: string }
55 }
56 const registerUri = doc.agent_auth.register_uri
57 expect(registerUri).toBe(`${BASE}/api/auth/agent/register`)
58
59 const hostKeys = await generateKeyPair('Ed25519', { extractable: true })
60 const agentKeys = await generateKeyPair('Ed25519', { extractable: true })
61 const hostId = crypto.randomUUID()
62 const hostJwt = await new SignJWT({
63 aud: `${BASE}/api/auth`,
64 host_public_key: await exportJWK(hostKeys.publicKey),
65 agent_public_key: await exportJWK(agentKeys.publicKey),
66 })
67 .setProtectedHeader({ alg: 'EdDSA', typ: 'host+jwt' })
68 .setIssuer(hostId)
69 .setIssuedAt()
70 .setJti(crypto.randomUUID())
71 .setExpirationTime('5m')
72 .sign(hostKeys.privateKey)
73
74 const response = await auth.handler(
75 new Request(registerUri, {
76 method: 'POST',
77 headers: {
78 'Content-Type': 'application/json',
79 Authorization: `Bearer ${hostJwt}`,
80 },
81 body: JSON.stringify({
82 name: 'auth-discovery-test',
83 mode: 'autonomous',
84 capabilities: ['getSchema'],
85 }),
86 }),
87 )
88 expect(response.ok).toBe(true)
89 const registration = (await response.json()) as { status?: string }
90 expect(registration.status).toBe('active')
91 })
92})