Expand description
Kimi Code OAuth (RFC 8628 device authorization; direct bearer, no mint).
Experimental. Flow verified against the official Kimi Code CLI (v0.31.1):
its embedded packages/oauth sources pin the same endpoints, client id,
grant type, error codes, and refresh semantics implemented here. The
endpoints are not published as a stable third-party API; do not describe
them as officially supported.
Flow shape (differences from GitHub Copilot):
- No session mint: the device-flow
access_tokenis used directly as the API bearer forhttps://api.kimi.com/coding/v1. - Access tokens are short-lived (~15 min) and refresh tokens rotate on every refresh — the rotated pair must always be persisted.
- A refresh rejected with HTTP 401/403 or
invalid_grantmeans the stored refresh token is dead; the user must re-login. - Upstream sends
X-Msh-*device-identity headers on every OAuth and API request; we mirror that conventional surface with an honest User-Agent.
Credential mapping:
OAuthCredentials.refresh= rotating Kimi refresh token (broker-owned)OAuthCredentials.access= short-lived Kimi access tokenOAuthCredentials.expires= access expiry (ms, with refresh margin)
Structs§
- Device
Authorization - Injected
Http Response - Minimal HTTP response used by the device-flow / refresh state machine.
- Kimi
Token Set - Successful token grant (poll or refresh). Secret-bearing — never logged.
- Login
Hooks
Enums§
- Device
Poll Outcome - Outcome of one device-token poll (secret-safe; tokens only on Authorized).
- Kimi
Auth Error - Failures from the Kimi auth chain. Variants intentionally store no device codes, user codes, or tokens so Display/Debug cannot leak them.
- Refresh
Disposition - Whether one refresh attempt should be retried or surfaced.
Constants§
- API_
BASE_ URL - Managed Kimi Code API base (bearer = OAuth access token).
- CLIENT_
ID - Public native-client id shipped inside the official Kimi Code CLI
(
KIMI_CODE_FLOW_CONFIG.clientId). Not a client secret. - CONNECT_
TIMEOUT - Connect timeout for production OAuth HTTP.
- DEVICE_
AUTH_ URL - Device-authorization endpoint (RFC 8628 §3.1, Kimi path shape).
- DEVICE_
GRANT_ TYPE - Device-code grant type (RFC 8628).
- MAX_
RESPONSE_ BODY_ BYTES - Maximum response body bytes accepted from OAuth endpoints.
- MSH_
PLATFORM - Conventional platform tag sent by the official CLI (
X-Msh-Platform). - MSH_
SURFACE_ VERSION - Kimi Code surface version this flow was verified against (
X-Msh-Version). - OAUTH_
HOST - OAuth host pinned by the official CLI (
https://auth.kimi.com). - PROVIDER
- Canonical storage / broker / model-prefix id.
- REQUEST_
TIMEOUT - Full request timeout for production OAuth HTTP.
- TOKEN_
URL - Token endpoint — device-code polling AND refresh grants.
- USER_
AGENT - Honest User-Agent (matches the Copilot mint precedent).
Traits§
- Kimi
Browser - Injectable browser opener (production uses
open_browser). - Kimi
Cancel - Injectable cancellation signal for device-flow polling.
- Kimi
Clock - Injectable clock (now + cancellable sleep) for deterministic poll tests.
- Kimi
Http - Injectable HTTP surface so tests never touch the network.
Functions§
- apply_
slow_ down - classify_
refresh_ response - Classify one refresh response. Pure; drives the retry loop.
- credentials_
from_ token - Map a granted token set onto broker credentials.
- device_
id_ path - Path of the persistent Kimi device id (kept beside auth.json, mode 0600).
- identity_
request_ headers - Device-identity headers sent on every OAuth and managed-API request.
- load_
or_ create_ device_ id - Load the stable device id, creating (and best-effort persisting) a fresh
UUIDv4 on first use. Upstream parity:
createKimiDeviceId. - login
- Production login entry (real network + system browser + auth.json).
- login_
with - Device start → user prompt → poll → atomic persist.
- parse_
device_ code_ response - parse_
device_ poll_ response - poll_
device_ token - One poll against the token endpoint.
- read_
body_ capped - Read a response body with a hard byte cap. Fail closed if exceeded.
- refresh_
token - Broker refresh: rotate the token pair (no browser). The caller persists the rotated credentials via the provider single-flight gate.
- refresh_
with - Refresh with upstream retry semantics: up to 3 attempts, exponential
backoff on 429/5xx, immediate failure on 401/403/
invalid_grant. - start_
device_ authorization - Start device authorization (POST /api/oauth/device_authorization).
- validate_
oauth_ endpoint - Accept only the exact pinned HTTPS auth.kimi.com OAuth endpoints.
- validate_
verification_ uri - Accept only an HTTPS kimi.com / *.kimi.com verification destination with no userinfo, no fragment, and no explicit port. The exact path is server-assigned (not a documented constant), so the host is the pin.
- wait_
for_ device_ authorization - Poll until authorized, denied, expired, cancelled, or deadline.