feat(cloud): provider-specific onUnauthorized hook (default: revoke)

This commit is contained in:
Neil
2026-11-10 04:39:25 -08:00
parent 956ddeb626
commit 0832550290
2 changed files with 10 additions and 1 deletions
@@ -20,6 +20,11 @@ export type AccountProvider = {
format(credential: string): string
/** Extra headers derived from the real credential (e.g. Codex's ChatGPT account id). */
extraHeaders?(credential: string): Record<string, string>
/**
* The provider answered 401 to this credential. Default (absent): mark it revoked. Codex instead
* kicks a single-flight lease refresh, which marks it expired/revoked itself if the refresh token is dead.
*/
onUnauthorized?(ctx: { db: D1Database; keyring: Keyring; userId: string; now: number; waitUntil: (work: Promise<unknown>) => void }): Promise<void>
/** The real value for this person, or null (none, expired, revoked). */
credential(db: D1Database, keyring: Keyring, userId: string, now: number): Promise<string | null>
}
+5 -1
View File
@@ -347,7 +347,11 @@ function egressRoutes(env: Env, deps: Deps): (row: SandboxRow, host: string) =>
if (status === 401) {
// The provider no longer accepts it (revoked at the source): stop using it until renewed.
const now = (deps.now ?? Date.now)()
const work = markAgentCredentialRevoked(env.SANDBOX_DB, row.user_id, agent, now).then(() => {
const waitUntil = (work: Promise<unknown>) => (deps.waitUntil ? deps.waitUntil(work.catch(() => undefined)) : void work.catch(() => undefined))
const handled = provider.onUnauthorized
? keyring.then((ring) => provider.onUnauthorized!({ db: env.SANDBOX_DB, keyring: ring, userId: row.user_id, now, waitUntil }))
: markAgentCredentialRevoked(env.SANDBOX_DB, row.user_id, agent, now)
const work = handled.then(() => {
forgetAccountCredential(row.user_id, agent)
scheduleEnvRefresh(env, deps, row)
})