Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

feat(meta-muse-code): add claudish login muse with Meta device code

Jack Rudenkoc60836f38157
diff --git a/packages/cli/src/auth/auth-commands.ts b/packages/cli/src/auth/auth-commands.ts
index 79d63701..74a0a966 100644
--- a/packages/cli/src/auth/auth-commands.ts
+++ b/packages/cli/src/auth/auth-commands.ts
@@ -9,11 +9,13 @@
*/
import { select } from "@inquirer/prompts";
+import { museCredentialStatusFromFiles } from "../providers/meta-muse/muse-credentials.js";
import { AntigravityOAuth } from "./antigravity-oauth.js";
import { hasSharedAntigravityToken } from "./antigravity-token.js";
import { CodexOAuth } from "./codex-oauth.js";
import { GrokOAuth } from "./grok-oauth.js";
import { KimiOAuth } from "./kimi-oauth.js";
+import { MuseCodeOAuth } from "./meta-muse-oauth.js";
import { hasOAuthCredentials } from "./oauth-registry.js";
interface OAuthInstance {
@@ -58,9 +60,28 @@ const AUTH_PROVIDERS: OAuthProvider[] = [
getInstance: () => GrokOAuth.getInstance(),
registryKeys: ["grok-subscription"],
},
+ {
+ // Meta device code login; logout removes claudish's own login AND its copy
+ // of the Muse Code CLI's login, never the CLI's Keychain item.
+ name: "muse",
+ displayName: "Muse Code (Meta subscription)",
+ prefix: "muse@",
+ getInstance: () => MuseCodeOAuth.getInstance(),
+ registryKeys: ["meta-muse-code"],
+ },
];
function getAuthStatus(provider: OAuthProvider): string {
+ // Muse Code's login is claudish's own file or its borrowed copy of the Muse
+ // Code CLI's login; the status reads both files and asks the Keychain nothing.
+ if (provider.name === "muse") {
+ const status = museCredentialStatusFromFiles();
+ return status === "own-login"
+ ? "logged in"
+ : status === "borrowed-copy"
+ ? "using a copy of the Muse Code CLI login"
+ : "not logged in";
+ }
// Antigravity's token lives in the SHARED keychain, not a ~/.claudish/*-oauth.json
// file, so hasOAuthCredentials can't see it — check the keychain directly.
const hasCredentials =
diff --git a/packages/cli/src/auth/meta-muse-oauth.ts b/packages/cli/src/auth/meta-muse-oauth.ts
new file mode 100644
index 00000000..f5f9e31f
--- /dev/null
+++ b/packages/cli/src/auth/meta-muse-oauth.ts
@@ -0,0 +1,270 @@
+/**
+ * `claudish login muse` — claudish's OWN Muse Code login, by Meta's device code
+ * flow (RFC 8628), so a user needs neither the Muse Code CLI nor a Keychain
+ * approval.
+ *
+ * ## The wire (captured 2026-10-16, muse-wire.md §8)
+ *
+ * ```
+ * POST https://auth.meta.com/oidc/device/authorization/ (form) client_id=1031625952748946
+ * → {device_code, user_code, verification_uri, verification_uri_complete, expires_in: 600, interval: 5}
+ * POST https://auth.meta.com/oidc/device/token/ (form)
+ * grant_type=urn:ietf:params:oauth:grant-type:device_code&device_code=…&client_id=…
+ * → 400 {"error":"authorization_pending"} | 400 {"error":"slow_down"} | 200 {access_token, …}
+ * ```
+ *
+ * No scope parameter. The success body was not captured (approval was not
+ * given), so only `access_token` is required; `token_type`, when present, must
+ * be Bearer; `refresh_token` / `expires_in` are kept if Meta sends them and
+ * never relied on. The durable credential is the `dca:` access token: it is
+ * used until `/muse-code/key` answers 401, then the user logs in again.
+ *
+ * ## After approval
+ *
+ * The token is minted once (`muse-mint.ts`) BEFORE it is stored:
+ * - confirmed → stored in `~/.claudish/meta-muse-oauth.json` (0600, atomic);
+ * - the account has no active subscription → NOT stored, and the subscribe
+ * message is the login's failure (a stored unsubscribed login would be tried
+ * first on every request and block a working borrowed copy);
+ * - the key service is unreachable or busy → stored, with a warning: the login
+ * itself succeeded, and the request path confirms the entitlement later.
+ *
+ * Then the in-process credential state and the authority's memo are dropped.
+ *
+ * ## Logout
+ *
+ * `claudish logout muse` removes BOTH claudish files — the own login and the
+ * borrowed copy of the Muse Code CLI's login. It never touches the Muse Code
+ * CLI's Keychain item, and it does not revoke remotely (the revocation
+ * endpoint's shape was not captured).
+ */
+
+import { execFile } from "node:child_process";
+import { log } from "../logger.js";
+import { resetMuseCredentialState } from "../providers/meta-muse/muse-credentials.js";
+import {
+ type MuseMint,
+ MuseSubscriptionRequiredError,
+ mintMuseApiKey,
+} from "../providers/meta-muse/muse-mint.js";
+import {
+ defaultMuseStoreDir,
+ deleteMuseStores,
+ writeMuseStore,
+} from "../providers/meta-muse/muse-store.js";
+import { credentials } from "./credentials/authority.js";
+
+export const MUSE_OIDC_CLIENT_ID = "1031625952748946";
+const AUTH_BASE = "https://auth.meta.com";
+const DEVICE_AUTHORIZATION_URL = `${AUTH_BASE}/oidc/device/authorization/`;
+const DEVICE_TOKEN_URL = `${AUTH_BASE}/oidc/device/token/`;
+const DEVICE_CODE_GRANT = "urn:ietf:params:oauth:grant-type:device_code";
+/** RFC 8628 defaults when the authorization response omits them. */
+const DEFAULT_INTERVAL_S = 5;
+const DEFAULT_EXPIRES_S = 600;
+/** RFC 8628 §3.5: `slow_down` raises the interval by 5 s, for good. */
+const SLOW_DOWN_STEP_MS = 5000;
+
+interface DeviceAuthorization {
+ device_code: string;
+ user_code: string;
+ verification_uri: string;
+ verification_uri_complete?: string;
+ expires_in?: number;
+ interval?: number;
+}
+
+interface TokenAnswer {
+ access_token?: unknown;
+ token_type?: unknown;
+ refresh_token?: unknown;
+ expires_in?: unknown;
+ error?: unknown;
+ error_description?: unknown;
+}
+
+/** Side effects, injectable so the flow is checkable with no network and no browser. */
+export interface MuseOAuthDeps {
+ fetch: (...args: Parameters<typeof fetch>) => Promise<Response>;
+ sleep: (ms: number) => Promise<void>;
+ now: () => number;
+ openBrowser: (url: string) => Promise<void>;
+ print: (line: string) => void;
+ mint: (accessToken: string) => Promise<MuseMint>;
+ dir: () => string;
+}
+
+function openInBrowser(url: string): Promise<void> {
+ const command =
+ process.platform === "darwin" ? "open" : process.platform === "win32" ? "explorer" : "xdg-open";
+ // execFile, not a shell: the URL is the vendor's and is passed as one argument.
+ return new Promise((resolve) => {
+ execFile(command, [url], () => resolve());
+ });
+}
+
+const defaultDeps: MuseOAuthDeps = {
+ fetch: (...args) => fetch(...args),
+ sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
+ now: () => Date.now(),
+ openBrowser: openInBrowser,
+ print: (line) => console.log(line),
+ mint: (token) => mintMuseApiKey(token),
+ dir: defaultMuseStoreDir,
+};
+
+function describe(answer: TokenAnswer, status: number): string {
+ if (typeof answer.error_description === "string" && answer.error_description) {
+ return answer.error_description;
+ }
+ if (typeof answer.error === "string" && answer.error) return answer.error;
+ return `HTTP ${status}`;
+}
+
+export class MuseCodeOAuth {
+ private static instance: MuseCodeOAuth | null = null;
+
+ static getInstance(): MuseCodeOAuth {
+ if (!MuseCodeOAuth.instance) MuseCodeOAuth.instance = new MuseCodeOAuth();
+ return MuseCodeOAuth.instance;
+ }
+
+ constructor(private readonly deps: MuseOAuthDeps = defaultDeps) {}
+
+ /** The full device-code login; resolves once the login is stored. */
+ async login(): Promise<void> {
+ const auth = await this.requestDeviceCode();
+ const url = auth.verification_uri_complete ?? auth.verification_uri;
+ this.deps.print("\nSign in to Meta for Muse Code (a Muse Code subscription is required).");
+ this.deps.print(`\n Code: ${auth.user_code}`);
+ this.deps.print(` URL: ${url}\n`);
+ await this.deps.openBrowser(url);
+ this.deps.print("Approve the request in your browser; waiting…");
+
+ const token = await this.pollForToken(auth);
+ let tier: string | undefined;
+ try {
+ tier = (await this.deps.mint(token.accessToken)).subsTierName;
+ } catch (err) {
+ if (err instanceof MuseSubscriptionRequiredError) throw err;
+ if ((err as { terminal?: unknown })?.terminal === true) throw err;
+ this.deps.print(
+ `Warning: the Muse Code key service could not confirm the subscription yet (${(err as Error).message}); the login is saved and will be confirmed on first use.`
+ );
+ }
+
+ writeMuseStore(
+ "own-login",
+ {
+ access_token: token.accessToken,
+ obtained_at: new Date(this.deps.now()).toISOString(),
+ client_id: MUSE_OIDC_CLIENT_ID,
+ ...(token.refreshToken ? { refresh_token: token.refreshToken } : {}),
+ ...(token.expiresIn !== undefined ? { token_expires_in: token.expiresIn } : {}),
+ ...(tier ? { subs_tier_name: tier } : {}),
+ },
+ this.deps.dir()
+ );
+ this.invalidate();
+ this.deps.print(
+ `\nSigned in to Muse Code${tier ? ` (${tier})` : ""}. Try it with: claudish --model [email protected] "hello"\n`
+ );
+ }
+
+ /** Remove claudish's own login and its borrowed copy. Never touches the Keychain. */
+ async logout(): Promise<void> {
+ deleteMuseStores(this.deps.dir());
+ this.invalidate();
+ }
+
+ /** Drop the core's minted key and the authority's memo (which also clears the signed arm). */
+ private invalidate(): void {
+ resetMuseCredentialState();
+ try {
+ credentials.invalidate("meta-muse-code");
+ } catch (err) {
+ log(`[MuseCodeOAuth] credential invalidate failed: ${(err as Error).message}`);
+ }
+ }
+
+ private async requestDeviceCode(): Promise<DeviceAuthorization> {
+ const response = await this.deps.fetch(DEVICE_AUTHORIZATION_URL, {
+ method: "POST",
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
+ body: new URLSearchParams({ client_id: MUSE_OIDC_CLIENT_ID }),
+ });
+ const parsed = (await response.json().catch(() => ({}))) as Partial<DeviceAuthorization> &
+ TokenAnswer;
+ if (
+ !response.ok ||
+ typeof parsed.device_code !== "string" ||
+ typeof parsed.user_code !== "string" ||
+ typeof parsed.verification_uri !== "string"
+ ) {
+ throw new Error(
+ `Could not start the Meta device login: ${describe(parsed, response.status)}`
+ );
+ }
+ return parsed as DeviceAuthorization;
+ }
+
+ /**
+ * Poll until approval, refusal or expiry. `authorization_pending` and a
+ * network blip keep waiting; `slow_down` adds 5 s to the interval for the
+ * rest of the wait; anything else ends the login with Meta's own words.
+ */
+ private async pollForToken(
+ auth: DeviceAuthorization
+ ): Promise<{ accessToken: string; refreshToken?: string; expiresIn?: number }> {
+ let intervalMs =
+ (auth.interval && auth.interval > 0 ? auth.interval : DEFAULT_INTERVAL_S) * 1000;
+ const deadline =
+ this.deps.now() +
+ (auth.expires_in && auth.expires_in > 0 ? auth.expires_in : DEFAULT_EXPIRES_S) * 1000;
+
+ while (this.deps.now() < deadline) {
+ await this.deps.sleep(intervalMs);
+ let response: Response;
+ let answer: TokenAnswer;
+ try {
+ response = await this.deps.fetch(DEVICE_TOKEN_URL, {
+ method: "POST",
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
+ body: new URLSearchParams({
+ grant_type: DEVICE_CODE_GRANT,
+ device_code: auth.device_code,
+ client_id: MUSE_OIDC_CLIENT_ID,
+ }),
+ });
+ answer = (await response.json().catch(() => ({}))) as TokenAnswer;
+ } catch (error) {
+ log(`[MuseCodeOAuth] poll failed, retrying: ${(error as Error).message}`);
+ continue;
+ }
+
+ if (typeof answer.access_token === "string" && answer.access_token.length > 0) {
+ if (typeof answer.token_type === "string" && answer.token_type.toLowerCase() !== "bearer") {
+ throw new Error(
+ `Meta sign-in returned a ${answer.token_type} token, not a Bearer grant.`
+ );
+ }
+ return {
+ accessToken: answer.access_token,
+ ...(typeof answer.refresh_token === "string" && answer.refresh_token
+ ? { refreshToken: answer.refresh_token }
+ : {}),
+ ...(typeof answer.expires_in === "number" ? { expiresIn: answer.expires_in } : {}),
+ };
+ }
+ if (answer.error === "authorization_pending") continue;
+ if (answer.error === "slow_down") {
+ intervalMs += SLOW_DOWN_STEP_MS;
+ continue;
+ }
+ throw new Error(`Meta sign-in failed: ${describe(answer, response.status)}`);
+ }
+ throw new Error(
+ "Meta sign-in timed out: the device code expired. Run `claudish login muse` again."
+ );
+ }
+}
diff --git a/packages/cli/src/auth/oauth-registry.ts b/packages/cli/src/auth/oauth-registry.ts
index 4b673675..e4996967 100644
--- a/packages/cli/src/auth/oauth-registry.ts
+++ b/packages/cli/src/auth/oauth-registry.ts
@@ -63,6 +63,17 @@ export const OAUTH_PROVIDERS: Record<string, OAuthProviderDescriptor> = {
expiresAtField: "expires_at",
expiryBufferMs: 5 * 60 * 1000,
},
+ // Muse Code - Meta Device Authorization Grant (RFC 8628)
+ // Login via: claudish login muse
+ //
+ // Only the OWN login is represented here. The borrowed copy and the Muse Code
+ // CLI's Keychain item are other sources, read by `museCredentialStatus()`,
+ // which is what source.ts and the Providers tab ask. No expiry field: the
+ // `dca:` token is used until the key service answers 401.
+ "meta-muse-code": {
+ credentialFile: "meta-muse-oauth.json",
+ validationMode: "check-expiry",
+ },
// NOTE: there is deliberately no `google` / `gemini-codeassist` entry here.
// Both used to point at ~/.claudish/gemini-oauth.json, the Gemini Code Assist
// token. That product was retired by Google for individuals and the provider
diff --git a/packages/cli/src/providers/provider-definitions.ts b/packages/cli/src/providers/provider-definitions.ts
index 1dfc3858..a55a6c7a 100644
--- a/packages/cli/src/providers/provider-definitions.ts
+++ b/packages/cli/src/providers/provider-definitions.ts
@@ -295,7 +295,7 @@ export interface ProviderDefinition {
* Single source of truth: keep this in sync with AUTH_PROVIDERS in
* src/auth/auth-commands.ts.
*/
- oauthLoginSlug?: "codex" | "kimi" | "antigravity" | "grok";
+ oauthLoginSlug?: "codex" | "kimi" | "antigravity" | "grok" | "muse";
/** Whether this is a local provider (no API key needed) */
isLocal?: boolean;
/** Whether this provider supports direct API access (not just via OpenRouter) */
@@ -721,6 +721,8 @@ export const BUILTIN_PROVIDERS: TieredProviderDefinition[] = [
apiKeyDescription:
"Muse Code login (`claudish login muse`, or `muse login` in an interactive session)",
apiKeyUrl: "https://dev.meta.ai",
+ // `claudish login muse`: Meta device code, claudish's own login file.
+ oauthLoginSlug: "muse",
shortcuts: ["muse"],
shortestPrefix: "muse",
legacyPrefixes: [],
diff --git a/packages/cli/src/tui/index.tsx b/packages/cli/src/tui/index.tsx
index c2f72b0d..b75c96f9 100644
--- a/packages/cli/src/tui/index.tsx
+++ b/packages/cli/src/tui/index.tsx
@@ -4,6 +4,7 @@ import { createCliRenderer } from "@opentui/core";
import { createRoot } from "@opentui/react";
import { _resetAntigravityTokenState } from "../auth/antigravity-token.js";
import { getCodexOAuth } from "../auth/codex-oauth.js";
+import { credentials } from "../auth/credentials/authority.js";
import { setCredentialPromptRole } from "../auth/credentials/prompt-role.js";
import { getKimiOAuth } from "../auth/kimi-oauth.js";
import { setStderrQuiet } from "../logger.js";
@@ -13,6 +14,7 @@ import type { ProviderDef } from "./providers.js";
/** The `claudish login <slug>` argument, derived from the provider catalog. */
type LoginSlug = NonNullable<ProviderDef["oauthSlug"]>;
import { ensureEndpointsRegistered } from "../providers/endpoint-registration.js";
+import { resetMuseCredentialState } from "../providers/meta-muse/muse-credentials.js";
import { App } from "./App.js";
import { invalidateProbeProxyHandlers, shutdownProbeProxy } from "./probe-proxy.js";
@@ -120,6 +122,12 @@ export async function startConfigTui(): Promise<void> {
getKimiOAuth().reloadCredentials();
invalidateProbeProxyHandlers("kimi");
invalidateProbeProxyHandlers("kimi-coding");
+ } else if (slug === "muse") {
+ // The child wrote meta-muse-oauth.json. Drop this process's minted key
+ // and the authority's memo so the next test signs with the new login.
+ resetMuseCredentialState();
+ credentials.invalidate("meta-muse-code");
+ invalidateProbeProxyHandlers("meta-muse-code");
}
}
// Re-enter the TUI regardless of login outcome — failed login should