Files
accounted/lib/auth/oauth-codes.ts
T
Jakob Wennberg ec27228a8e style: remove em/en dashes repo-wide, add CLAUDE.md rule against them (#890)
Em dashes (—) and en dashes (–) had spread across comments, docs, tests,
and a few UI strings, reading as AI-generated boilerplate rather than
house style. Replaced each with punctuation matching its context: colon
for explanatory clauses, comma for asides, plain hyphen for numeric/legal
ranges (e.g. "21-23§"), "to"/"till" for date ranges, parentheses for
paired-dash asides. messages/en.json and messages/sv.json were fixed by
hand together to keep sv/en in sync.

Left untouched where the dash is the functional subject rather than
decorative punctuation: date-range-parser.ts's separator regex,
charset-repair.ts's CP1252 byte-mapping table (and its test), the SIE
encoding mojibake docs, generic-csv.ts's minus-sign normalizer, the
agent system-prompt files that already instruct against em dashes, and
a golden iXBRL test fixture compared byte-for-byte.

Also fixes two bugs surfaced along the way: an off-by-one in
ApiKeysPanel's scope-label split (a leftover from an earlier partial
pass), and a charset-repair test that had lost the literal en-dash it
exists to verify.

Regenerated the agent atom seed migration (skills:generate) since 27
SKILL.md files changed. Added a CLAUDE.md rule against em/en dashes,
with an explicit carve-out for the functional-dash cases above.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-04 15:58:06 +02:00

96 lines
2.7 KiB
TypeScript

import crypto from 'crypto'
/**
* Stateless OAuth auth codes.
* The auth code is an AES-256-GCM encrypted JSON payload containing
* the user ID, PKCE code_challenge, and expiry.
*
* The API key is NOT embedded: it gets created at token exchange
* after PKCE verification, preventing orphaned keys.
*/
const ALGORITHM = 'aes-256-gcm'
const CODE_TTL_MS = 5 * 60 * 1000 // 5 minutes
function getEncryptionKey(): Buffer {
const secret = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!secret) throw new Error('SUPABASE_SERVICE_ROLE_KEY is required')
return crypto.createHash('sha256').update(secret).digest()
}
export interface AuthCodePayload {
userId: string
codeChallenge: string
redirectUri: string
/**
* Scopes the user consented to grant the resulting API key. Undefined on
* codes minted before this field was added: the token endpoint falls back
* to ALL_SCOPES so existing Claude flows are unaffected.
*/
scopes?: string[]
exp: number
}
export function createAuthCode(payload: Omit<AuthCodePayload, 'exp'>): string {
const data: AuthCodePayload = {
...payload,
exp: Date.now() + CODE_TTL_MS,
}
const key = getEncryptionKey()
const iv = crypto.randomBytes(12)
const cipher = crypto.createCipheriv(ALGORITHM, key, iv)
const json = JSON.stringify(data)
const encrypted = Buffer.concat([cipher.update(json, 'utf8'), cipher.final()])
const tag = cipher.getAuthTag()
const combined = Buffer.concat([iv, tag, encrypted])
return combined.toString('base64url')
}
export function decryptAuthCode(code: string): AuthCodePayload | null {
try {
const key = getEncryptionKey()
const combined = Buffer.from(code, 'base64url')
const iv = combined.subarray(0, 12)
const tag = combined.subarray(12, 28)
const encrypted = combined.subarray(28)
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv)
decipher.setAuthTag(tag)
const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()])
const payload: AuthCodePayload = JSON.parse(decrypted.toString('utf8'))
if (Date.now() > payload.exp) return null
return payload
} catch {
return null
}
}
/**
* Verify PKCE: SHA256(code_verifier) must equal the stored code_challenge.
* Only S256 is supported (plain is insecure and not advertised).
*/
export function verifyPkce(
codeVerifier: string,
codeChallenge: string
): boolean {
const hash = crypto
.createHash('sha256')
.update(codeVerifier)
.digest('base64url')
return hash === codeChallenge
}
/**
* Hash an auth code for replay tracking.
*/
export function hashAuthCode(code: string): string {
return crypto.createHash('sha256').update(code).digest('hex')
}