// Atlassian's authorize page, `https://auth.atlassian.com/authorize?audience=api.atlassian.com&client_id=…&scope=…& // redirect_uri=…&state=…&response_type=code&prompt=consent` (the 3LO page): the person logs in, chooses the site the // app may use, and accepts; the browser is sent to the callback URL with `code` and `state`, or `error=access_denied`. // In a World the person is one of the World's (the sites and people doors). import type { HandlerContext } from '@volter/world-core'; import { APP, CODE, PERSON, type Row, sha256, SITE, tokenValue } from '../../../src/semantics/shared.ts'; const esc = (v: unknown): string => String(v ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]!); const page = (body: string, status = 200): Response => new Response(`
Invalid audience
', 400); const app = ctx.rowsRaw(APP).find((a) => a.deleted !== true && a.client_id === q.get('client_id')); if (!app) return page('Invalid client_id
', 400); const redirect = q.get('redirect_uri') ?? ''; if (!(app.callback_urls as string[]).includes(redirect)) return page('redirect_uri does not match the callback URL
', 400); if (q.get('response_type') !== 'code') return page('Invalid response_type
', 400); const scopes = (q.get('scope') ?? '').split(/[ +]/).filter(Boolean); if (!scopes.length) return page('Invalid scope
', 400); const back = (params: Record${esc(error)}
` : ''}`, error ? 400 : 200); if (request.method !== 'POST') return login(); const person = ctx.rowsRaw(PERSON).find((p) => p.deleted !== true && p.email === q.get('email')); if (!person || person.password_sha256 !== sha256(ctx, `${String(person.id)}:${String(q.get('password') ?? '')}`)) return login('Incorrect email address and / or password.'); if (q.get('decision') === 'cancel') return back({ error: 'access_denied' }); const sites = (person.sites as string[]).map((id) => ctx.row(SITE, id)).filter((s): s is Row => Boolean(s)); if (q.get('decision') !== 'accept') { const creds = ``; return page(`Choose a site you can use
', 400); const id = ctx.mint(CODE); const value = await tokenValue(ctx, `jira-code:${id}`); await ctx.record(CODE, { app: app.id, sites: [site.id], person: person.id, scope: scopes.join(' '), redirect_uri: redirect, sha256: sha256(ctx, value), expires: new Date(Date.parse(ctx.occurredAt) + CODE_TTL * 1000).toISOString() }, id); return back({ code: value }); }