feat(providers): add xAI Grok subscription OAuth (device-code) + per-provider session auth

First subscription-OAuth provider beyond Kimi Code (26th registry variant).
Log in with a Grok/SuperGrok/X subscription via RFC-8628 device-code OAuth
(auth.x.ai), then use it against api.x.ai/v1 — reusing the existing xai wire
(ChatCompletions + OpenAI listing + Passthrough + restrict + models_dev_id
xai). Sourced from Pi (earendil-works/pi auth/oauth/xai.ts): client
b1a00492..., scope 'openid profile email offline_access grok-cli:access
api:access', standard Bearer (no x-xai-token-auth).

Foundation (generalizes Kigi's Kimi-singleton OAuth to per-provider, root
cause, not a patch):
- Registry: OAuthConfig on PlatformSpec (client_id/host/device+token
  paths/scope/scope_key); XAI_OAUTH_CONFIG + XAI_GROK_SPEC (uses_oauth, method
  id 'xai-grok', an interactive login after kimi-code).
- Generic device-code wire (auth/oauth_device.rs) + GenericDeviceRefresher,
  sharing the RFC-8628 core with Kimi; Kimi's bespoke flow is byte-identical
  (X-Msh headers, KIMI_CODE_OAUTH_SCOPE, keyring gating unchanged).
- Per-provider AuthManager via a process-global pool (auth/oauth_registry.rs):
  build-on-demand with start_proactive_refresh, keyed by scope. The session
  resolves the AuthManager for the ACTIVE model's platform for bearer/refresh/
  401-recovery/api_key — an oauth-platform model always uses its OWN token,
  never the primary.
- Live /models under OAuth; base routes oauth().is_some() -> platform.base_url()
  (kimi-code stays on proxy_url).

Security: adversarial review + a systematic token-leak audit found and closed
FIVE channels where the primary Kimi token could reach api.x.ai (bearer
resolver, api_key stamping, aux summary/classifier/image-describe models, and
subagent model-override). Each fix routes through the platform-aware resolver
(the oauth model's pooled token or None, NEVER the primary) and is revert-to-red
verified. No access/refresh token is ever logged.

Registry at 26; picker updated (xai-grok interactive login row); TUI
context-window already auto-updates per model. Full gate green (234 suites,
fmt, clippy -D warnings, deny). GPT/Claude/Grok officially permit third-party
subscription use.
This commit is contained in:
ZacharyZhang-NY committed 2026-07-22 01:36:29 -04:00
1 parent 8a26460251
commit dbce6bf305
26 files changed
+2359 -161

No files matched your search

@@ -385,6 +385,59 @@ impl AuthManager {
)
}
/// Build a manager for a GENERIC device-code OAuth provider (xai-grok),
/// scoped to `oauth.scope_key`. Unlike [`Self::new`] this path is
/// file-store only (no keyring — that is gated to the default Kimi install)
/// and ignores the Kimi-specific `KIGI_AUTH` inline-credential env; it
/// otherwise shares the same multi-scope `auth.json` (honoring
/// `KIGI_AUTH_PATH`). The refresher is selected from the scope by
/// [`super::refresh::build_refresher`].
pub(crate) fn new_oauth_provider(
kigi_home: &Path,
oauth: &'static kigi_models::OAuthConfig,
) -> Self {
let scope = oauth.scope_key.to_owned();
let path = std::env::var("KIGI_AUTH_PATH")
.map(PathBuf::from)
.unwrap_or_else(|_| kigi_home.join("auth.json"));
let (auth, disk_state) = match read_auth_json(&path) {
Ok(map) => {
let found = lookup_auth(&map, &scope);
let state = if found.is_some() {
DiskAuthState::Ok
} else {
DiskAuthState::EntryMissing
};
(found, state)
}
Err(e) => {
let state = if e.kind() == std::io::ErrorKind::NotFound {
DiskAuthState::FileMissing
} else {
DiskAuthState::Unreadable
};
(None, state)
}
};
kigi_log::unified_log::info(
"AuthManager::new_oauth_provider",
None,
Some(serde_json::json!({
"scope": &scope,
"found": auth.is_some(),
"is_expired": auth.as_ref().map(is_expired),
})),
);
Self::assemble(
auth,
path,
scope,
KimiCodeConfig::default(),
Some(disk_state),
)
}
/// Single field-assembly point for [`Self::new`]'s two construction paths
/// (inline `KIGI_AUTH` vs. on-disk `auth.json`), which differ only in the
/// threaded fields. One literal means a newly added field can't be silently
@@ -789,6 +842,13 @@ impl AuthManager {
&self.kimi_code_config
}
/// The auth.json / keyring scope key this manager persists under
/// (`oauth/kimi-code` for Kimi, `oauth/xai` for xai-grok, …). Drives the
/// refresher selection in [`super::refresh::build_refresher`].
pub(crate) fn scope(&self) -> &str {
&self.scope
}
/// Handle notified after every successful token refresh.
///
/// Used by [`ModelsManager`] to trigger model catalog recovery