// What an OAuth client and a browser do, for checks/oauth.mjs // (spec/OAUTH.md): DPoP keys and proofs, PKCE, requests to the OAuth // endpoints with a proof and a nonce, and a browser without a browser that // loads the authorization page and calls the API its script calls. // // Values that must only be fresh (keys, `jti`s, PKCE verifiers, states) come // from node:crypto, not from lib/random.mjs: they are taken out of what is // compared, and drawing them from the seeded source would make the requests // that follow depend on how many retries each side needed. import { createHash, randomBytes, webcrypto } from 'node:crypto' import http from 'node:http' import { reach } from './front.mjs' import { begin, end, noteSetCookies, noteUrl, now, toServer } from './observe.mjs' const { subtle } = webcrypto export const b64u = (buf) => Buffer.from(buf).toString('base64url') export const sha256 = (s) => b64u(createHash('sha256').update(s).digest()) export const fresh = (bytes = 16) => b64u(randomBytes(bytes)) /// An ES256 key pair: its private key, public JWK, and JWK thumbprint /// (RFC 7638), the `jkt` a server binds tokens to. export async function newKey() { const { privateKey, publicKey } = await subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign', 'verify']) const { kty, crv, x, y } = await subtle.exportKey('jwk', publicKey) const jwk = { kty, crv, x, y } return { privateKey, jwk, jkt: sha256(JSON.stringify({ crv, kty, x, y })), privateJwk: await subtle.exportKey('jwk', privateKey) } } /// A compact JWS of `header` and `payload`, signed with `key` (ES256). export async function signJwt(header, payload, key) { const input = `${b64u(JSON.stringify(header))}.${b64u(JSON.stringify(payload))}` const sig = await subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, key.privateKey, Buffer.from(input)) return `${input}.${b64u(sig)}` } /// A DPoP proof (RFC 9449 §4.2) of `htm` and `htu` by `key`. `over` sets or, /// with `undefined`, deletes claims; `header` sets header fields. export async function proof(key, htm, htu, { nonce, accessToken, over = {}, header = {} } = {}) { const claims = { jti: fresh(), htm, htu, iat: Math.floor(Date.now() / 1000) } if (nonce) claims.nonce = nonce if (accessToken) claims.ath = sha256(accessToken) for (const [k, v] of Object.entries(over)) { if (v === undefined) delete claims[k] else claims[k] = v } const h = { typ: 'dpop+jwt', alg: 'ES256', jwk: key.jwk, ...header } for (const [k, v] of Object.entries(header)) if (v === undefined) delete h[k] return signJwt(h, claims, key) } /// A PKCE verifier and its S256 challenge. export function pkce() { const verifier = fresh(32) return { verifier, challenge: sha256(verifier) } } /// A form body, leaving out `undefined` values; an array value is the /// parameter once for each of its items. export function form(params) { const q = new URLSearchParams() for (const [k, v] of Object.entries(params)) for (const x of Array.isArray(v) ? v : [v]) if (x !== undefined) q.append(k, x) return { headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: q.toString() } } /// An answer: status, headers by lower-case name, and the body as JSON if /// it is, or text. export async function answer(res) { const text = await res.text() let body try { body = text ? JSON.parse(text) : null } catch { body = { unparsed: text.slice(0, 200) } } return { status: res.status, headers: Object.fromEntries(res.headers), body } } /// A client instance's DPoP state on one server: its key, and the last /// nonce that server gave it. export class Dpop { constructor(key) { this.key = key this.nonce = undefined } /// Send `url` with a fresh proof (and `Authorization: ` /// for `accessToken`). A `use_dpop_nonce` refusal is retried once with the /// nonce it gave, as clients do, unless `retry` is false. `proofOpts` /// bends the proof; `dpop` set gives that header instead (`null`: none). async send(url, { method = 'GET', headers = {}, body, accessToken, scheme = 'DPoP', retry = true, proofOpts = {}, dpop } = {}) { const u = new URL(url) for (let attempt = 0; ; attempt++) { const h = { ...headers } const p = dpop !== undefined ? dpop : await proof(this.key, method, u.origin + u.pathname, { nonce: this.nonce, accessToken, ...proofOpts }) if (p !== null) h.dpop = p if (accessToken !== undefined) h.authorization = `${scheme} ${accessToken}` let res try { res = await answer(await fetch(url, { method, headers: h, body })) } catch (err) { return { status: 0, headers: {}, body: { error: `request failed: ${err.message}` }, proof: p } } if (res.headers['dpop-nonce']) this.nonce = res.headers['dpop-nonce'] const wantsNonce = res.body?.error === 'use_dpop_nonce' if (wantsNonce && retry && attempt === 0 && dpop === undefined) continue return { ...res, proof: p } } } } /// The `error` parameter of a `WWW-Authenticate` header, or null. export const wwwError = (h) => (typeof h === 'string' ? (/\berror="([^"]*)"/.exec(h)?.[1] ?? '') : null) /// A browser: its cookies, and the requests a page and its script make. export class Browser { jar = new Map() cookieHeader() { return [...this.jar].map(([k, v]) => `${k}=${v}`).join('; ') } keep(setCookies = []) { for (const c of setCookies) { const [pair, ...attrs] = c.split(';') const i = pair.indexOf('=') const name = pair.slice(0, i).trim() const value = pair.slice(i + 1).trim() const gone = value === '' || attrs.some((a) => /^\s*max-age=0\s*$/i.test(a)) if (gone) this.jar.delete(name) else this.jar.set(name, value) } } /// One request, with this browser's cookies and `headers` (a header given /// as `undefined` is left out). node:http, not fetch: fetch sets /// `Sec-Fetch-Mode` itself. One to the server is noted, with its trace /// (spec/OPERATIONS.md). request(url, { method = 'GET', headers = {}, body } = {}) { return new Promise((resolve) => { const all = Object.fromEntries(Object.entries({ cookie: this.cookieHeader() || undefined, ...headers }).filter(([, v]) => v !== undefined)) const rec = toServer(url) ? begin(method, url, all, body) : null const req = http.request(reach(url), { method, headers: all }, (res) => { const chunks = [] res.on('data', (c) => chunks.push(c)) res.on('end', () => { this.keep(res.headers['set-cookie']) const text = Buffer.concat(chunks).toString('utf8') if (rec) { noteUrl(res.headers.location) noteSetCookies(res.headers['set-cookie']) end(rec, res.statusCode, text) } let json try { json = JSON.parse(text) } catch {} resolve({ status: res.statusCode, headers: res.headers, text, json, data: pageData(text) }) }) }) req.on('error', (err) => { if (rec) rec.ended = now() resolve({ status: 0, headers: {}, text: '', json: { error: `request failed: ${err.message}` }, data: {} }) }) req.end(body) }) } /// A top-level navigation to `url`, with `Sec-Fetch-Site: site`. navigate(url, { site = 'cross-site', referer, headers = {} } = {}) { return this.request(url, { headers: { 'sec-fetch-mode': 'navigate', 'sec-fetch-dest': 'document', 'sec-fetch-site': site, referer, ...headers } }) } /// A call the page at `page` makes to its server's API. api(page, endpoint, input, { method = 'POST', bearer, headers = {}, raw } = {}) { const origin = new URL(page).origin const h = { origin, referer: page, 'sec-fetch-mode': 'same-origin', 'sec-fetch-site': 'same-origin', 'sec-fetch-dest': 'empty', 'x-csrf-token': this.jar.get('csrf-token'), 'content-type': method === 'POST' ? 'application/json' : undefined, authorization: bearer ? `Bearer ${bearer}` : undefined, ...headers, } const body = raw !== undefined ? raw : method === 'POST' ? JSON.stringify(input) : undefined return this.request(`${origin}${API}${endpoint}`, { method, headers: h, body }) } } export const API = '/@atproto/oauth-provider/~api' /// A sign-in call to the API of the server at `origin`, as a new browser's /// page would make it for a request of the form a PAR answers (that the /// request exists is not checked, spec/OAUTH.md), from client address /// `ip`; `raw` sends that body in place of `input`. export function signInCall(origin, input, { ip, raw } = {}) { const b = new Browser() b.jar.set('csrf-token', randomBytes(12).toString('hex')) const page = `${origin}/oauth/authorize?client_id=${encodeURIComponent('http://localhost')}&request_uri=${encodeURIComponent(`urn:ietf:params:oauth:request_uri:req-${randomBytes(16).toString('hex')}`)}` return b.api(page, '/sign-in', input, { headers: ip ? { 'x-forwarded-for': ip } : {}, raw }) } /// What a page declares in its script: `window[""]=JSON.parse("…")`. export function pageData(html) { const out = {} for (const m of html.matchAll(/window\["(__\w+)"\]\s*=\s*JSON\.parse\(("(?:[^"\\]|\\.)*")\)/g)) { try { out[m[1]] = JSON.parse(JSON.parse(m[2])) } catch {} } return out } /// The fields of the form a `form_post` page submits: `{action, method, /// params}`, or null. export function formPost(html) { const f = /]*\bmethod="([^"]*)"[^>]*\baction="([^"]*)"/i.exec(html) if (!f) return null const unescape = (s) => s.replace(/"/g, '"').replace(/'/g, "'").replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&') const params = {} for (const m of html.matchAll(/]*\btype="hidden"[^>]*\bname="([^"]*)"[^>]*\bvalue="([^"]*)"/gi)) params[unescape(m[1])] = unescape(m[2]) return { method: f[1].toLowerCase(), action: unescape(f[2]), params } } /// A redirect target: the URL without its query and fragment, and the /// parameters in its query, or in its fragment if `fragment`. export function target(url, fragment = false) { if (typeof url !== 'string') return null let u try { u = new URL(url) } catch { return { unparsed: url } } const params = Object.fromEntries(new URLSearchParams(fragment ? u.hash.slice(1) : u.search)) return { to: `${u.origin}${u.pathname}`, params, ...(fragment ? {} : u.hash ? { hash: u.hash } : {}) } }