# Subscription sign-in

Status: implemented on the oauth-login branch (ticket 30); the credential seam awaits the owner's
review

Tier: bundled (`src/oauth.c`, `src/oauth_openai.c`, `src/oauth_openrouter.c`). Clock: vendor.
Record: a `picu_credential` row of kind `oauth`. Default: on.

## The seam

Core's credential store (`cred.h`) gains `pic_cred_oauth_ops`: a sign-in label per provider, a login
that returns what to seal, a refresh, and the origin its token may be sent to. The composition root
registers the bundled table; core calls only through it, so core names no provider and links without
the flows (tiers checks 4 and 5). It is a new seam, which [keel](keel.md) sends to the owner. The
lighter alternative, the flows in core with the provider names behind an allowance, was refused: the
allowance only shrinks.

## Decisions

- Providers: OpenAI's Sign in with ChatGPT (OpenAI's published program for open-source agents, Pi a
  launch partner) and OpenRouter (documented PKCE for third-party apps). Pi's Anthropic sign-in is
  refused by Anthropic's terms (February 2026); Codex, Copilot, xAI, Meta and Kimi reuse first-party
  client ids with no published permission. None of them is built.
- Refresh runs under a lock file beside `pic.sqlite`, not a `BEGIN IMMEDIATE` around the network
  call, so other writers never wait on a token endpoint. The row is read again inside the lock.
- RPC: Pi has no credential commands. pic's `login` and `logout`, which used to refuse, now work,
  and `get_auth_status` is pic's own.
- The device-code poll (RFC 8628) is built and tested, but no provider pic signs in to offers it.
- A subscription's token is sent only to the origin its flow names (`https://api.openai.com/` for
  OpenAI). A models file that points the provider elsewhere, or at http, gets a refusal naming the
  override, before the token is read.
- The terminal interface runs a sign-in as a child (`pic auth login PROVIDER --pipe`: JSON lines
  out, a pasted address in), so the screen stays its own; Esc or Ctrl-C sends the child SIGTERM, its
  wait returns, it closes the listener and exits (SIGKILL after 3 s). The CLI keeps Ctrl-C ending
  pic.
- OpenRouter sends no state; its redirect's random path binds a code to the sign-in, so a pasted
  code must come inside the whole redirect address, never alone.

## What stays open

- Login fixation on a shared machine. The authorize URL, with its state and PKCE challenge, is in
  the browser opener's argv (`/usr/bin/open URL`), which another local user can read with `ps` or
  `/proc`. That user could sign in with an account of their own and race the code to 127.0.0.1:1455
  before the real redirect, so pic would store their account's tokens, not the victim's: the
  victim's requests would bill the attacker, and the attacker could read them on the provider's
  side. The state does not help, since it is the leaked value. Pi and the Codex CLI have the same
  window. A mitigation was considered and not built: open a 0600 file holding a page that redirects
  to the URL, so argv carries only a path. It changes how every browser receives the sign-in (a file
  URL, a refresh) and cannot be tested here, for a threat that needs a hostile local user who is
  watching at that moment. The callback answers only one sign-in, once, and drops a connection with
  no request line within a second.
- A rotated refresh token lost to a failed write means signing in again; see
  [durability](../dev/durability.md#a-refreshed-sign-in).

