Open History to see the last commit
1/**
2 * Auth discovery surfaces (PLAN.md task 10.4): RFC 9728 protected-resource
3 * metadata, an RFC 8414 authorization-server document restricted to fields
4 * we genuinely implement (agent-auth registration — no classic OAuth flows,
5 * so no fake authorize/token endpoints), and /auth.md — markdown
6 * registration instructions for agents. All derive scopes from the same
7 * capability list the API enforces.
8 */
9import { publicCapabilityNames } from '#/lib/auth.ts'
10
11export function oauthProtectedResource(
12 origin: string,
13): Record<string, unknown> {
14 return {
15 resource: origin,
16 authorization_servers: [origin],
17 bearer_methods_supported: ['header'],
18 scopes_supported: publicCapabilityNames(),
19 resource_documentation: `${origin}/auth.md`,
20 }
21}
22
23export function oauthAuthorizationServer(
24 origin: string,
25): Record<string, unknown> {
26 const issuer = `${origin}/api/auth`
27 return {
28 issuer,
29 registration_endpoint: `${issuer}/agent/register`,
30 service_documentation: `${origin}/auth.md`,
31 scopes_supported: publicCapabilityNames(),
32 // Honest metadata: this server only does agent-auth registration —
33 // there is no authorization-code or token endpoint to advertise.
34 grant_types_supported: [],
35 response_types_supported: [],
36 agent_auth: {
37 register_uri: `${issuer}/agent/register`,
38 configuration: `${origin}/.well-known/agent-configuration`,
39 identity_types: ['host+jwt'],
40 credential_types: ['Ed25519'],
41 token_type: 'agent+jwt',
42 },
43 }
44}
45
46export function authMd(origin: string): string {
47 const issuer = `${origin}/api/auth`
48 return `# Authenticating with modelschemas
49
50All reads are public at 60 requests/hour per IP — you only need credentials
51for higher rate limits (5,000/hour) and webhook subscriptions.
52
53## Option 1 — API key (simplest)
54
55\`\`\`
56POST ${origin}/v1/agents/register-key
57Content-Type: application/json
58
59{ "name": "my-agent" }
60\`\`\`
61
62The response contains your key **once**. Send it on every request:
63
64\`\`\`
65Authorization: Bearer <key>
66\`\`\`
67
68## Option 2 — agent-auth protocol (Ed25519 JWTs)
69
70Discovery document: \`${origin}/.well-known/agent-configuration\`
71
721. Generate Ed25519 keypairs for your host and agent.
732. Register: \`POST ${issuer}/agent/register\` with a \`host+jwt\` bearer
74 token (iss = your host id, \`host_public_key\` + \`agent_public_key\` as
75 JWKs in the claims) and body
76 \`{ "name", "mode": "autonomous", "capabilities": [...] }\`.
773. Execute: \`POST ${issuer}/capability/execute\` with a short-lived
78 \`agent+jwt\` (iss = host id, sub = agent id, fresh \`jti\` per request —
79 tokens are single-use).
80
81Available capabilities (also \`scopes_supported\` in the OAuth metadata):
82
83${publicCapabilityNames()
84 .map((name) => `- \`${name}\``)
85 .join('\n')}
86
87## Not OAuth
88
89There is no authorization-code or token endpoint —
90\`/.well-known/oauth-authorization-server\` advertises registration only.
91Human sign-in (email OTP) lives at ${origin}/login.
92`
93}