Skip to content

Device login: the protocol, and the machine token's columns #122

Closed

Description

@allisonmahmood

Question

patchy login is decided at the product level; this locks the protocol and the token, with the RFC 8628 research in hand.

  1. Routes. POST /api/login/device unauthenticated — the guard admits it as it admitted the mint, per-address limited — answering the device code, the user code, the verification URL (with and without the code), the interval and the expiry; POST /api/login/device/token polled with the device code, answering pending, slow down, expired, denied, or the machine token once. The browser: GET /login/device?code=… behind the session shows the code, a name field on a first login, confirm and deny; POST completes it. ➡️ That shape; names are the spec's.
  2. The user code. Eight characters from a confusable-free alphabet in two groups, ten-minute expiry, five-second interval, the code shown for confirmation rather than typed. ➡️ That.
  3. Naming and re-login. The CLI keeps the machine token's id beside the token in credentials.json; a re-login sends it, the new token inherits the name and the old is revoked in the same act. Consequences: "re-authenticating keeps the name" without a machine identity of its own. ➡️ That.
  4. Expiry enforcement. expires_at at 90 days and last_used_at for the 30 idle; last_used_at stamped at most once an hour so a publish stays one read; expired answers the same 401 as revoked. ➡️ That.
  5. Identity on the wire. /api/me becomes the user (id, email, name), the company (id, handle, name), the role, and the machine (token id, name). Scopes go: the operator's bootstrap token is the one non-user credential and carries an operator scope, or is a distinct kind. ➡️ Drop scopes; one operator flag on the bootstrap token.
  6. Your machines. The page (list, revoke one, revoke all) plus patchy logout; or a patchy machines command instead of the page. Consequences: revoking a lost laptop needs a browser. ➡️ The page and logout; no machines command yet.
  7. CI. PATCHY_API_TOKEN and auth set stay for a token minted on Your machines; the operator path in the skill is rewritten around it. ➡️ That; the patchy-mint-token skill is rewritten or deleted by the build.
  8. Limits. Start per address per minute; poll per device code at the interval, with slow-down; a denied or expired code is gone.

From the prototype (#119, resolved 2026-09-03)

GET /login/device?code=… behind the session works as shaped in question 1: the code survives the door, the Account Portal and both handshakes, and the page can name the company and the person from the session's email and name claims. Two facts change the POST half:

  1. A POST is neither refresh- nor handshake-eligible in @clerk/backend: Confirm pressed more than ~65 s after the page loaded arrives as signed-out, gets the door, and the door page's Sign in link has lost the code. ➡️ A signed-out POST answers 303 to GET /login/device?code=<code from the body> so the person re-enters through the door with the code intact.
  2. The 401 for non-document clients (The login door on a tier 0 page #120 item 12) is where the CLI's own instruction belongs: what patchy prints when a page refuses it, and whether /d/* ever accepts the machine token is Write the auth spec and file the build tickets #125's call.

From the door (The login door on a tier 0 page, resolved 2026-09-03)

  • Item 10 is answered: /d/* never accepts the machine token. A machine token builds and manages patches; reading a company patch is the user's signed-in browser. So the 401 carries no WWW-Authenticate, and what patchy prints when a page refuses it is "sign in in your browser; only public patches fetch by URL". The skill text is the spec's.
  • Sign-out is a POST on Your machines (question 6) that revokes the Clerk session and clears Clerk's cookies; patchy logout stays the CLI's own act on the machine token.

From company and user (Company and user: the minimum that ships, and where the company lives, resolved 2026-09-03)

  • Question 5 confirmed from the company side: /api/me carries the user, the company (id, handle, name) and the role; no CLI company commands exist, so this is how an agent learns where its user is.
  • Deactivation revokes every machine token of the user in the same act, and the door refuses a deactivated user's session without a Clerk call; the bearer middleware refuses the same way, by the user row.

Activity

  1. added
    wayfinder:grilling
    on Sep 2, 2026
  2. allisonmahmood commented on Sep 3, 2026

    @allisonmahmood
    OwnerAuthor

    Grilling, round one (2026-09-03)

    Decided, pending the two tickets below:

    • Name field (Q3): always on the confirm page, prefilled and editable: from the old token on a re-login, from the CLI's hostname (os.hostname(), sent with the start request) on a first one. Required, 1–64 characters. A re-login is the rename path; Your machines gets none.
    • Re-login edges (Q4): an expired or revoked old token still lends its name and revoking it again is a no-op; an old token that belongs to another user is ignored and the login is treated as a first one, never an error; the old token dies in confirm's transaction, so a poll that fails after confirm leaves the machine logged out until the next patchy login.
    • Identity on the wire (Q5): /api/me is a union of two kinds, not one shape with nullable fields: { kind: "user", user: { id, email, name }, company: { id, handle, name }, role, machine: { id, name } } and { kind: "operator", machine: { id, name } }. The machine field's id is the token's id; there is no machine entity, a machine is known only through its token. POST /api/tokens goes (the bootstrap token comes from the environment); POST /api/tokens/:id/revoke stays as the operator's last step in moderation. patchy whoami prints user, company, role and machine, or "Operator".
    • Expiry (Q8): expires_at is a real column set at confirm (so item 9's 303 dance never eats a token's life), last_used_at starts equal to created_at and is stamped at most hourly on a successful bearer lookup; Your machines lists live tokens only, dead rows stay for provenance and name inheritance.
    • Limits (Q9): the guard admits POST /api/login/device as it admits the mint; the poll is per device code with slow_down; the confirm page's lookup is limited per user; finished device_logins rows are deleted by the poll that reports them and never-polled rows go with the retention sweep.
    • upload with no token (Q10): exit 1 local, "Run: patchy login". It never starts a login. The first run is login then upload; one product line adjusts.
    • Taken as-is from the ticket: the user code (Q2's alphabet, XXXX-XXXX, 10 min, 5 s), the signed-out POST answering 303 to the GET with the code (item 9), expired answering the same 401 as revoked, item 10 as the door answered it.

    Still open, now blocking this ticket:

  3. allisonmahmood commented on Sep 3, 2026

    @allisonmahmood
    OwnerAuthor

    Grilling, round two (2026-09-03)

    • patchy logout (Q6): deletes the local credential first, then asks the instance to revoke the token as a courtesy. A 401 is success; unreachable is still exit 0 with a warning ("Logged out on this machine. The token could not be revoked; it expires on its own after 30 idle days, or revoke it now on Your machines"). ADR-0004 gains one line: a command whose local act succeeded reports a failed courtesy call as a warning, never as an exit code. The route is the bearer revoking itself, name to the spec.
    • CI (Q7): not this slice. The only way to a machine token is patchy login; Your machines is list, revoke one, revoke all, with no create. CI joins the map's Out of scope list beside the company-owned token, and the docs/product.md line "CI holds a token from the same page" moves to future tense with the spec. PATCHY_API_TOKEN and auth set stay because the dev loop and the packed-CLI e2e run on the seeded token. The patchy-mint-token skill is deleted.

    Nothing else is open on this ticket but Q1 (does patchy login block) and Q2 (what the URL carries), which resolve through How agent-driven CLIs hand off a device login and patchy login end to end: the handoff, the confirm page, the token. The session that closes this ticket after the prototype writes the resolution comment from these two rounds plus that answer.

  4. allisonmahmood commented on Sep 3, 2026

    @allisonmahmood
    OwnerAuthor

    From the research (How agent-driven CLIs hand off a device login, resolved 2026-09-04)

    Q1 narrows to two shapes for the prototype: Stripe's (block for a human at a TTY; one JSON document and patchy login --complete <code> under --json, non-TTY or a detected agent) or the always-two-step shape from round one. Either way one JSON document per run names the next command, the bare code prints beside the URL, and the device code persists in the state dir between runs. Q2's confirm-not-type has RFC 8628 §5.4 and Vercel, Wrangler and Heroku behind it; nothing in the survey argues for a bare verification page.

  5. allisonmahmood commented on Sep 3, 2026

    @allisonmahmood
    OwnerAuthor

    Resolution (2026-09-04)

    Two grilling rounds (above) plus the research on How agent-driven CLIs hand off a device login and the prototype on patchy login end to end: the handoff, the confirm page, the token, which settled Q1 and Q2. Everything the spec needs on device login and the machine token, in one place:

    Routes (1). POST /api/login/device unauthenticated, admitted by the guard beside the mint, per-address limited like it; takes the CLI's hostname as a machine-name hint and, on a re-login, the stored key's id; answers the device code, the user code, the confirm URL with and without the code, the interval (5 s) and the expiry (10 min). POST /api/login/device/token polled with the device code: pending / slow_down, complete with the machine token exactly once, or a 410 saying expired, denied or unknown; the poll that reports a finished login deletes its row, never-polled rows go with the retention sweep. Browser: GET /login/device?code=… behind the session; POST confirms or denies.

    The user code (2). Eight characters from BCDFGHJKLMNPQRSTVWXZ as XXXX-XXXX, ten-minute expiry, five-second interval, shown for confirmation and never typed. The URL carries it (/login/device?code=XXXX-XXXX, the user code as the lookup key), and the CLI prints the bare code beside the URL (RFC 8628 §3.3.1). A bare /login/device exists only as a message page with no input.

    Does patchy login block (Q1). Only for a human at a TTY: stdin a terminal, no --json, none of the agent environment variables set (Stripe's gate; isatty alone is wrong because an agent's shell tool has a TTY and still sees nothing before exit, measured on #131). Otherwise one JSON document on stdout and exit 0, naming next and agentNextSteps; patchy login again while a live login is pending resumes it, patchy login --complete [code] is the explicit form, waiting up to --wait seconds (default 60) before answering pending. Denied, expired and unknown are the server's answers and exit 2 rejected; the CLI polls even when its own record has expired, so the server deletes the row. The pending login persists in the state dir.

    Naming and re-login (3, round one Q3/Q4). The name field is always on the confirm page, prefilled and editable: the old key's name on a re-login, the CLI's hostname on a first login; required, 1–64 characters. A re-login is the rename path. The CLI sends the stored key's id; confirm revokes it in the same transaction and the new token inherits the name; an expired or revoked old token still lends its name; an old token belonging to another user is ignored and the login is a first one, never an error. The confirm page is #131's variant A, copy and reverse states quoted there.

    Expiry (4, round one Q8). expires_at set at confirm to 90 days; last_used_at starts equal to created_at and is stamped at most hourly on a successful bearer lookup, 30 idle days; expired answers the same 401 as revoked. Your machines lists live tokens only; dead rows stay for provenance and name inheritance.

    Identity on the wire (5). /api/me is a union of two kinds: { kind: "user", user: { id, email, name }, company: { id, handle, name }, role, machine: { id, name } } and { kind: "operator", machine: { id, name } }. The machine's id is the token's id; there is no machine entity. Scopes go: the bootstrap token is the one non-user credential, with one operator flag. POST /api/tokens goes; POST /api/tokens/:id/revoke stays as the operator's last step in moderation. patchy whoami prints user, company, role and machine, or Operator.

    Your machines (6) and logout. The page (list, revoke one, revoke all, sign-out) plus patchy logout; no patchy machines command. logout deletes the local credential first, then asks the instance to revoke the token as a courtesy: a 401 is success, unreachable is exit 0 with a warning naming the 30-day idle expiry and Your machines; ADR-0004 gains the line that a command whose local act succeeded reports a failed courtesy call as a warning, never as an exit code. The route is the bearer revoking itself.

    CI (7). Not this slice: the only way to a machine token is patchy login, Your machines has no create; PATCHY_API_TOKEN and auth set stay for the dev loop and the packed-CLI e2e; the patchy-mint-token skill is deleted; docs/product.md's CI line moves to future tense.

    Limits (8, round one Q9). Start per address per minute as the mint; the poll per device code with slow_down; the confirm page's lookup limited per user; a denied or expired code is gone.

    Items 9 and 10. A signed-out POST answers 303 to GET /login/device?code=<code from the body>; the door refreshes the session and the page returns with a "press Confirm again" notice (proven on #131). /d/* never accepts the machine token; the 401 for non-document clients carries no WWW-Authenticate, and patchy says "sign in in your browser; only public patches fetch by URL" (from #120).

    upload with no token (round one Q10). Exit 1 local, Run: patchy login; it never starts a login.

  6. allisonmahmood commented on Sep 3, 2026

    @allisonmahmood
    OwnerAuthor

    Resolved above: every question closed, Q1 and Q2 through #131.

  7. allisonmahmood commented on Sep 4, 2026

    @allisonmahmood
    OwnerAuthor

    Addendum (2026-09-04, from Data model, packages and migration). Minting moves from confirm to the poll: confirm records the user, the machine name and the old key's id and sets the login confirmed; the poll that finds that state mints the token, revokes the old key and deletes the row in one transaction, returning the plaintext from memory. So device_logins has no token column, no plaintext ever sits in a row, and an abandoned confirm mints nothing. The "confirm revokes it in the same transaction" line under Naming and re-login reads "the poll's transaction" from here on; everything else in the resolution stands. Stale rows are deleted on the next start, not by the retention sweep.

  8. allisonmahmood commented on Sep 4, 2026

    @allisonmahmood
    OwnerAuthor

    Addendum, 2026-09-04. The operator-as-token parts of this resolution (the operator kind of /api/me, the tok_operator row with user_id NULL and the operator flag, PATCHY_OPERATOR_TOKEN in the dev loop) are reopened on The operator is a company, not a token. Read that ticket's resolution over these lines; the rest of this resolution stands.

  9. allisonmahmood commented on Sep 4, 2026

    @allisonmahmood
    OwnerAuthor

    Addendum, 2026-09-04. Resolved on The operator is Patchy, not a token: the operator is Patchy with its own later login and dashboards, so every operator-as-token part of this resolution is gone — the operator kind of /api/me (one shape now), tok_operator, the nullable user_id and operator flag (user_id NOT NULL), PATCHY_BOOTSTRAP_API_TOKEN and PATCHY_OPERATOR_TOKEN, the admin scope, and the moderation and pin routes. revoke stays only as the bearer revoking itself. The rest stands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

wayfinder:grilling

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions