Skip to content

feat(grok): add device-code login for headless hosts (grok auth device) - #38

Merged
raine merged 3 commits into
raine:mainfrom
wtfsayo:grok-auth-device
Jul 12, 2026
Merged

feat(grok): add device-code login for headless hosts (grok auth device)#38
raine merged 3 commits into
raine:mainfrom
wtfsayo:grok-auth-device

Conversation

@wtfsayo

@wtfsayo wtfsayo commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Problem

Grok auth today is browser-only (grok auth login), which opens a system browser and waits on an ephemeral loopback callback. On headless hosts (SSH sessions, containers, CI shells) there is no browser and no reachable loopback, so there is no way to authenticate. The CLI already exposes grok auth device, but it just errors with Grok device login is unavailable; use grok auth login.

Fix

Implement the OAuth device-code grant against the same issuer and public client as browser login (auth.x.ai, same CLIENT_ID / SCOPES):

  • auth/device.rs: request a device + user code from /oauth2/device/code, print the verification URL and code, then poll /oauth2/token with grant_type=urn:ietf:params:oauth:grant-type:device_code, handling authorization_pending and slow_down until tokens arrive.
  • Store the resulting access/refresh tokens through the existing GrokTokenStore exactly like browser login (same StoredAuth, so status / logout / refresh all work unchanged).
  • Wire it into the existing grok auth device CLI handler; document it in the README.

No changes to the request/inference path.

Testing

  • 4 new unit tests against a local mock issuer: pending→slow_down→success, access-denied, malformed init, and token persistence via the in-memory store. The polling sleep is injected so tests run instantly.
  • cargo test --all, cargo fmt --check, and clippy clean.

@wtfsayo
wtfsayo force-pushed the grok-auth-device branch from 1634a29 to 56ef242 Compare July 12, 2026 09:51
@wtfsayo
wtfsayo marked this pull request as draft July 12, 2026 09:55
Adds an OAuth device-code flow alongside the existing browser PKCE login,
using the same public client and issuer (auth.x.ai). 'grok auth device' prints
a verification URL and user code, polls the token endpoint (handling
authorization_pending / slow_down), and stores the same access/refresh tokens
as browser login. Useful on headless machines where a loopback callback is
awkward.
@wtfsayo
wtfsayo force-pushed the grok-auth-device branch from 56ef242 to 1b0d312 Compare July 12, 2026 09:58
@wtfsayo
wtfsayo marked this pull request as ready for review July 12, 2026 09:58
raine added 2 commits July 12, 2026 13:22
Honor the OAuth device authorization server's polling interval before the
first token request and preserve the required five-second increase after a
slow_down response.

Use the server-provided device code lifetime without extending short values.
Inject time and sleep behavior so protocol timing and the production token
persistence path have deterministic regression coverage.
Describe each authentication subcommand in generated CLI help so users can
choose the appropriate login flow and understand status and logout behavior
without consulting separate documentation.
@raine

raine commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Looks good. Thank you

@raine
raine merged commit b045bfb into raine:main Jul 12, 2026
1 check passed
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.

2 participants