Skip to main content

codex_auth_manager/
status.rs

1use std::{fmt, path::PathBuf};
2
3use super::IdentityName;
4
5/// Current auth status.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum AuthStatus {
8    /// Codex home does not exist.
9    CodexHomeMissing {
10        /// Missing Codex home path.
11        path: PathBuf,
12    },
13    /// No auth file exists.
14    None,
15    /// `auth.json` is a regular native Codex auth file.
16    Native,
17    /// `auth.json` points to a usable managed identity.
18    Managed {
19        /// Active identity.
20        identity: IdentityName,
21    },
22    /// `auth.json` points to a managed identity that is missing or unusable.
23    BrokenManaged {
24        /// Broken active identity.
25        identity: IdentityName,
26    },
27    /// CAM cannot safely classify the auth state.
28    Unknown {
29        /// Reason the state is unknown.
30        reason: UnknownAuthReason,
31    },
32}
33
34impl fmt::Display for AuthStatus {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Self::CodexHomeMissing { path } => write!(f, "Codex home missing: {}", path.display()),
38            Self::None => write!(f, "No auth file"),
39            Self::Native => write!(f, "Native auth file"),
40            Self::Managed { identity } => write!(f, "Active identity: {identity}"),
41            Self::BrokenManaged { identity } => {
42                write!(f, "Active identity is broken: {identity}")
43            }
44            Self::Unknown { reason } => write!(f, "Unknown auth state: {reason}"),
45        }
46    }
47}
48
49/// Reason CAM cannot safely classify the auth state.
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub enum UnknownAuthReason {
52    /// `auth.json` is neither a regular file nor a symlink.
53    AuthPathIsNotFileOrSymlink,
54    /// The `auth.json` symlink points outside CAM's manager directory.
55    SymlinkTargetOutsideManagerDir,
56    /// The `auth.json` symlink target does not map to a valid identity name.
57    SymlinkTargetHasInvalidIdentityName,
58}
59
60impl fmt::Display for UnknownAuthReason {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match self {
63            Self::AuthPathIsNotFileOrSymlink => write!(f, "auth.json is not a file or symlink"),
64            Self::SymlinkTargetOutsideManagerDir => {
65                write!(f, "symlink target is outside codex-auth-manager")
66            }
67            Self::SymlinkTargetHasInvalidIdentityName => {
68                write!(f, "symlink target has invalid identity name")
69            }
70        }
71    }
72}