Expand description
Meta Muse Code OAuth (RFC 8628 device authorization + Model API key mint).
Experimental. Flow verified against the official Muse Code CLI
(muse 1.4.6-R6000.1, installed via dev.meta.ai/install.sh) by driving
it against local mock servers and reading its embedded constants. The
endpoints are not published as a stable third-party API; do not describe
them as officially supported.
Flow shape (differences from Kimi Code / GitHub Copilot):
- Device authorization at
auth.meta.com/oidc/device/{authorization,token}/with a public client id, no PKCE and noscopeparameter. - The OAuth access token is NOT the inference bearer. It is exchanged once
at
POST api.meta.ai/muse-code/key({"onboard":false}) for a Model API key, and that key is theAuthorization: Bearerforapi.meta.ai/v1/responses(OpenAI Responses wire). An API key created at dev.meta.ai is the same kind of credential (META_API_KEY). - The CLI never refreshes the OAuth token and never proactively re-mints: the key is used until the API answers 401, then re-minted ONCE with the stored OAuth access token and the request retried once. A mint 401 means the login itself is dead — the user must log in again. We mirror this.
Credential mapping:
OAuthCredentials.refresh= Meta OAuth access token (broker-owned; used only to re-mint; never vended, logged, or sent to the inference host)OAuthCredentials.access= minted Model API key (the inference bearer)OAuthCredentials.expires= nominal re-validation horizon (seeAPI_KEY_NOMINAL_LIFETIME_MS); real invalidation is 401-driven.
The OAuth refresh token is deliberately not stored: the official client never uses it, so keeping it would only widen what a leaked auth.json exposes.
Structs§
- Device
Authorization - Login
Hooks - Minted
Key - A minted Model API key plus the non-secret account facts the mint echoes.
- Muse
Http Response - Minimal HTTP response used by the device-flow / mint state machine.
Enums§
- Device
Poll Outcome - Outcome of one device-token poll (secret-safe; token only on Authorized).
- Muse
Auth Error - Failures from the Muse auth chain. Variants store no device codes, user codes, tokens, keys, or upstream bodies, so Display/Debug cannot leak them.
Constants§
- API_
BASE_ URL - OpenAI Responses base (
{API_BASE_URL}/responses). - API_
HOST - Inference/API host (
TBH_MINT_BASE_URLdefault). - API_
KEY_ ENV - Environment override honoured by the official CLI: a Model API key here takes precedence over the account login.
- API_
KEY_ NOMINAL_ LIFETIME_ MS - Minted keys carry no expiry. The official CLI uses a key until the API answers 401 and never re-mints proactively; this horizon only keeps the repo-wide expiry check from ever forcing a needless re-mint (each mint is a server-side side effect). Invalidation is 401-driven via the broker’s rejected-token recovery.
- API_
ORIGIN - Origin the broker pins for every Muse request. Paths are allowlisted
broker-side (
/v1/responses,/muse-code/models). - CLIENT_
ID - Public native-client id shipped in the official Muse Code CLI. Not a client secret.
- CONNECT_
TIMEOUT - Connect timeout for production device/poll/mint HTTP.
- DEVICE_
AUTH_ URL - Device-authorization endpoint (RFC 8628 §3.1). Trailing slash is part of the path the official CLI sends.
- DEVICE_
GRANT_ TYPE - Device-code grant type (RFC 8628).
- MAX_
RESPONSE_ BODY_ BYTES - Maximum response body bytes accepted from device/poll/mint endpoints.
- MINT_
API_ VERSION x-api-versionthe official CLI sends on the mint request.- MINT_
BODY - Exact mint body the official CLI sends for a returning login.
- MINT_
URL - Model-API-key mint endpoint (bearer = OAuth access token).
- OAUTH_
HOST - Meta OAuth host pinned by the official CLI (
TBH_AUTH_BASE_URLdefault). - ONBOARDING_
REQUIRED_ CODE - Upstream error code returned by the mint when the account has not finished Model API onboarding.
- ONBOARDING_
URL - Where a Meta account finishes Model API onboarding / billing.
- PROVIDER
- Canonical storage / broker / model-prefix id.
- REQUEST_
TIMEOUT - Full request timeout for production device/poll/mint HTTP.
- TOKEN_
URL - Device-code token endpoint (RFC 8628 §3.4).
- USER_
AGENT - Honest User-Agent.
Traits§
- Muse
Browser - Injectable browser opener (production uses
open_browser). - Muse
Clock - Injectable clock (now + cancellable sleep) for deterministic poll tests.
- Muse
Http - Injectable HTTP surface so tests never touch the network.
Functions§
- api_
key_ from_ env - Model API key from the environment override, if set and non-blank.
- apply_
slow_ down - credentials_
from_ mint - OAuth access token →
refresh; minted key →access(the bearer). - env_
api_ key_ present - Presence-only check for the
META_API_KEYoverride (the value never leaves this module). Used by pickers: Muse is usable without a stored login when the override is set. - login
- Production login entry (real network + system browser + auth.json).
- login_
into - Production login;
Some(key)persists ONLY into that slot,Nonereturns the credential unsaved. - login_
with_ into - Device start → user prompt → poll → mint → persist into
persist_key(museormuse@<label>) when given;Nonereturns it unsaved. - mint_
api_ key - Exchange the Meta OAuth access token for a Model API key. The OAuth token is sent only to the pinned mint URL. Callers must never log it.
- mint_
credentials - Mint and build the stored credential.
- mint_
request_ headers - Non-secret headers attached to the pinned mint request (bearer is set by the HTTP adapter and can never be overridden through this list).
- parse_
device_ code_ response - parse_
device_ poll_ response - parse_
mint_ response - Classify a mint response. Upstream bytes never enter the error: the onboarding code is matched as a fixed token, everything else maps to a static variant.
- 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: re-mint a Model API key from the stored OAuth access token (no browser). The OAuth token is carried over unchanged, so the compare-and-swap save in the token store matches.
- start_
device_ authorization - Start device authorization (POST device/authorization/).
- validate_
device_ endpoint - Accept only the exact pinned HTTPS
auth.meta.comdevice endpoints. - validate_
mint_ endpoint - Accept only the pinned mint host+path (no query, no alternate host).
- validate_
verification_ uri - Verification destinations must be plain HTTPS pages on a Meta-owned host
(
meta.com/*.meta.com,meta.ai/*.meta.ai). Rejects userinfo, explicit ports, fragments, and look-alike hosts. Path/query are left to the server (the device page shape is not part of the pinned contract). - wait_
for_ device_ authorization - Poll until authorized, denied, expired, cancelled, or deadline. Returns the OAuth access token.