import { encodeBase64 } from '@std/encoding/base64'; import { escape } from 'entities'; import { nip19 } from 'nostr-tools'; import { z } from 'zod'; import { AppController } from '@/app.ts'; import { nostrNow } from '@/utils.ts'; import { parseBody } from '@/utils/api.ts'; import { getClientConnectUri } from '@/utils/connect.ts'; const passwordGrantSchema = z.object({ grant_type: z.literal('password'), password: z.string(), }); const codeGrantSchema = z.object({ grant_type: z.literal('authorization_code'), code: z.string(), }); const credentialsGrantSchema = z.object({ grant_type: z.literal('client_credentials'), }); const createTokenSchema = z.discriminatedUnion('grant_type', [ passwordGrantSchema, codeGrantSchema, credentialsGrantSchema, ]); const createTokenController: AppController = async (c) => { const body = await parseBody(c.req.raw); const result = createTokenSchema.safeParse(body); if (!result.success) { return c.json({ error: 'Invalid request', issues: result.error.issues }, 400); } switch (result.data.grant_type) { case 'password': return c.json({ access_token: result.data.password, token_type: 'Bearer', scope: 'read write follow push', created_at: nostrNow(), }); case 'authorization_code': return c.json({ access_token: result.data.code, token_type: 'Bearer', scope: 'read write follow push', created_at: nostrNow(), }); case 'client_credentials': return c.json({ access_token: '_', token_type: 'Bearer', scope: 'read write follow push', created_at: nostrNow(), }); } }; /** Display the OAuth form. */ const oauthController: AppController = async (c) => { const encodedUri = c.req.query('redirect_uri'); if (!encodedUri) { return c.text('Missing `redirect_uri` query param.', 422); } const redirectUri = maybeDecodeUri(encodedUri); const connectUri = await getClientConnectUri(c.req.raw.signal); const script = ` window.addEventListener('load', function() { if ('nostr' in window) { nostr.getPublicKey().then(function(pubkey) { document.getElementById('pubkey').value = pubkey; document.getElementById('oauth_form').submit(); }); } }); `; const hash = encodeBase64(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(script))); c.res.headers.set( 'content-security-policy', `default-src 'self' 'sha256-${hash}'`, ); return c.html(`