Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
//! Provider authentication for the CLI: a typed capability catalog, credential
//! contracts, and (in submodules) API-key validation, registered OAuth flows,
//! and secure persistence. This module is the security boundary between what a
//! user can sign in with and what the agent runtime trusts.
//!
//! Capability metadata is non-secret by construction: keys, tokens, and
//! verifier/state values never live here. They move through
//! [`crate::core::server::provider_secrets`] only.
pub mod account;
pub mod credentials;
pub mod providers;
use std::path::PathBuf;
pub use credentials::{Credential, CredentialStore, OAuthToken};
/// Wire transport a provider speaks, chosen at credential resolution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Transport {
OpenAi,
Anthropic,
}
/// Non-secret guidance for minting an API key.
#[derive(Debug, Clone)]
pub struct ApiKeyMetadata {
/// Human-facing page where the user creates/views keys. Owned because
/// Tokamak's follows the deployment this run signs in to.
pub keys_url: String,
/// Short hint shown above the masked input.
pub hint: &'static str,
/// Whether the TUI should open the keys page automatically for this provider.
pub open_in_browser: bool,
}
#[derive(Debug, Clone)]
pub struct ProviderDefinition {
/// Provider id used in `~/.jan/config.toml` and in `<id>/<model>` ids.
pub id: &'static str,
/// Display name shown in pickers.
pub name: &'static str,
/// Default API endpoint (an OpenAI-compatible root or the Anthropic `/v1`).
pub default_base_url: String,
pub transport: Transport,
pub api_key: ApiKeyMetadata,
}
/// The providers the TUI `/login` flow offers, in picker order.
pub fn provider_catalog() -> Vec<ProviderDefinition> {
let catalog = vec![
ProviderDefinition {
id: "openai",
name: "Codex",
default_base_url: "https://api.openai.com/v1".to_string(),
transport: Transport::OpenAi,
api_key: ApiKeyMetadata {
keys_url: "https://platform.openai.com/account/api-keys".to_string(),
hint: "get a key at platform.openai.com/account/api-keys",
open_in_browser: false,
},
},
ProviderDefinition {
id: "anthropic",
name: "Claude",
default_base_url: "https://api.anthropic.com/v1".to_string(),
transport: Transport::Anthropic,
api_key: ApiKeyMetadata {
keys_url: "https://console.anthropic.com/settings/keys".to_string(),
hint: "get a key at console.anthropic.com/settings/keys",
open_in_browser: false,
},
},
ProviderDefinition {
id: "opencode",
name: "OpenCode",
default_base_url: "https://opencode.ai/zen/v1".to_string(),
transport: Transport::OpenAi,
api_key: ApiKeyMetadata {
keys_url: "https://opencode.ai/auth".to_string(),
hint: "sign in at opencode.ai/auth to copy your OpenCode API key",
open_in_browser: true,
},
},
ProviderDefinition {
id: "deepseek",
name: "DeepSeek",
default_base_url: "https://api.deepseek.com/v1".to_string(),
transport: Transport::OpenAi,
api_key: ApiKeyMetadata {
keys_url: "https://platform.deepseek.com/api_keys".to_string(),
hint: "get a key at platform.deepseek.com/api_keys",
open_in_browser: false,
},
},
// Not a fixed endpoint: the deployment this run signs in to (a dev
// stack's `TOKAMAK_BASE_URL`, or where the last sign-in went), so a
// paste sign-in verifies and stores the key against the same host the
// browser flow would, and sends the user to that host's keys page.
ProviderDefinition {
id: "tokamak",
name: "Tokamak",
default_base_url: super::tokamak::base_url(),
transport: Transport::OpenAi,
api_key: ApiKeyMetadata {
keys_url: super::tokamak::api_keys_url(),
hint: "get a key at tokamak.sh/settings/api-keys",
open_in_browser: false,
},
},
];
catalog
}
/// The catalog entry for `id`, if any.
pub fn provider_by_id(id: &str) -> Option<ProviderDefinition> {
provider_catalog().into_iter().find(|p| p.id == id)
}
/// Non-secret outcome of a successful login, safe to render and report.
#[derive(Debug, Clone, PartialEq)]
pub struct LoginResult {
pub provider: String,
pub models: Vec<String>,
/// Where the non-secret provider configuration was written.
pub config_path: PathBuf,
/// The model adopted as `default_model`, or `None` when the user already
/// had one (or the account has no models).
pub default_model: Option<String>,
}
/// Why a login attempt failed. Every variant carries only safe, non-secret
/// text; errors never include the credential.
#[derive(Debug, Clone, PartialEq)]
pub enum LoginError {
/// The entered key is empty or malformed; retryable without I/O.
InvalidKey(String),
/// The provider rejected the credential (HTTP 401/403).
Unauthorized,
/// The provider is rate limiting this credential (HTTP 429).
RateLimited,
/// Network, protocol, or provider 5xx failure, with a safe message.
Unavailable(String),
/// A validated credential could not be persisted; nothing was changed.
Persist(String),
/// The OAuth interaction failed; no credential was persisted.
OAuth(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn catalog_lists_the_five_providers_in_picker_order() {
let ids: Vec<&str> = provider_catalog().iter().map(|p| p.id).collect();
assert_eq!(
ids,
vec!["openai", "anthropic", "opencode", "deepseek", "tokamak"]
);
assert_eq!(provider_by_id("deepseek").unwrap().name, "DeepSeek");
assert!(provider_by_id("nope").is_none());
}
#[test]
fn only_opencode_api_key_prompt_opens_keys_page_in_browser() {
let enabled: Vec<&str> = provider_catalog()
.iter()
.filter(|provider| provider.api_key.open_in_browser)
.map(|provider| provider.id)
.collect();
assert_eq!(enabled, vec!["opencode"]);
}
}