Skip to content

Phase 1: Backend device authorization flow - #1287

Merged
neuromechanist merged 12 commits into
feature/issue-1272-epic-orcid-cli-signinfrom
feature/issue-1281-phase1-device-flow
Sep 7, 2026
Merged

neuromechanist merged 12 commits into
feature/issue-1272-epic-orcid-cli-signinfrom
feature/issue-1281-phase1-device-flow

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Backend half of ORCID-first CLI sign-in: the device authorization grant (RFC 8628), the pattern gh auth login uses, so nemar auth login can obtain its API key through a browser ORCID sign-in on any device without a redirect reaching the terminal.

New: migration 0081 device_codes; POST /auth/device/start and POST /auth/device/token for the CLI; GET /auth/device/lookup, POST /auth/device/confirm, POST /auth/device/deny for the website page (phase 2) behind the existing web session; named per-machine keys with GET/POST /auth/keys and DELETE /auth/keys/:id (current on the bearer path) for the paste-key fallback and phase 3's logout; the shared vocabulary in shared/contract/device-auth.ts; ADR 0047.

Design points the review should check (ADR 0047): the key is minted only when the CLI collects it, never at confirm, inside one db.batch() gated on EXISTS rather than last_insert_rowid(); the polling secret is stored hashed; every timestamp is written and compared in SQL; confirm grants a 120 s collection grace; /auth/device/token is deliberately outside AUTH_PATHS and carries its own per-row 5 s floor; identity_conflict refuses new sign-ins only; account_revoked is unreachable at confirm because revoked accounts hold no web session.

Closes #1281
Part of epic #1272

Test plan

  • backend/test/device-codes-migration.test.ts runs the exported SQL against bun:sqlite: PK, UNIQUE, CHECKs, SQL-side expiry format, confirm-once with grace, poll floor, stamp-once, prune, the mint pair for verified/pending/revoked/flagged/at-cap accounts, cascade and SET NULL
  • backend/test/device-auth-routes.test.ts (41 cases at the HTTP entry points, ORCID stand-in via Bun.serve): start, poll states, expiry audited once, lookup, confirm, deny, mint after confirm, account changes between confirm and collect, wrong account, prune, and a brand-new ORCID iD going pending, verify, confirm, token
  • backend/test/api-keys-routes.test.ts (12 cases): list, current marker, cap, revoke one and keep the others, current on both credential paths
  • backend/test/rate-limit-buckets.test.ts: the five strict paths and the deliberate exclusion of token
  • test/contract-device-auth.unit.test.ts: alphabet, format/normalize, every code has a two-sentence message
  • bun run lint, bun run typecheck, bun run migrations:d1-check (real local D1 replay) clean
  • Prove-the-test-fails: six predicates mutated one at a time (confirm guard, mint join, key cap, poll floor, consume gate, Origin gate), each named test failed for the expected reason, each reverted
  • test/auth-device-flow.test.ts (live tier) skips loudly until the routes reach the dev worker, which deploys only from dev; runs once the epic lands or the branch is deployed by hand
  • Staging walk-through with a real ORCID account, recorded on Phase 1: Backend device authorization flow #1281, after deployment

Follow-up filed: #1286 (pre-existing ISO-versus-SQL timestamp comparisons in auth_codes, web_sessions, orcid_link_intents).

@github-actions

github-actions Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1287

Version: 0.9.17-PR1287.90b2710
Commit: 90b2710


🤖 Updated on each push.

This branch was previously deployed

1 inactive deployment
npm — 90b27108 Deployed Sep 7, 2026 by neuromechanist via publish #2350
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant