// 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(`Atlassian${body}`, { status, headers: { 'content-type': 'text/html; charset=utf-8' } }); const PARAMS = ['audience', 'client_id', 'scope', 'redirect_uri', 'state', 'response_type', 'prompt']; // Where the documentation stops: a code's lifetime is Atlassian's; the twin gives it ten minutes const CODE_TTL = 600; export async function screen(ctx: HandlerContext): Promise { const request = ctx.call.request; const q = request.method === 'POST' ? new URLSearchParams(ctx.text) : new URL(request.url).searchParams; // source: https://developer.atlassian.com/cloud/jira/platform/oauth-2-3lo-apps/ "audience=api.atlassian.com" if (q.get('audience') !== 'api.atlassian.com') return page('

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): Response => { const u = new URL(redirect); for (const [k, v] of Object.entries(params)) u.searchParams.set(k, v); if (q.get('state') !== null) u.searchParams.set('state', q.get('state')!); return new Response(null, { status: 302, headers: { location: u.toString() } }); }; const hidden = PARAMS.filter((p) => q.get(p) !== null).map((p) => ``).join(''); const login = (error = ''): Response => page(`

Log in to continue

${error ? `

${esc(error)}

` : ''}
${hidden}
`, 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(`

${esc(app.name)} is requesting access to your Atlassian account.

${hidden}${creds}
`); } const site = sites.find((s) => s.id === q.get('site')); if (!site) 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 }); }