use std::fmt;
use chrono::{DateTime, TimeZone, Utc};
use serde::Serialize;
use serde_json::Value;
use sha2::{Digest, Sha256};
use super::harness::Harness;
use super::ids::AccountId;
#[derive(Clone, PartialEq)]
pub struct Secret {
value: Value,
harness: Harness,
}
impl Secret {
pub fn new(v: Value) -> Self {
Secret::of(Harness::Claude, v)
}
pub fn of(harness: Harness, value: Value) -> Self {
Secret { value, harness }
}
pub fn expose(&self) -> &Value {
&self.value
}
pub fn harness(&self) -> Harness {
self.harness
}
pub fn meta(&self) -> LoginMeta {
LoginMeta::of(self.harness, &self.value)
}
pub fn has_token(&self) -> bool {
self.meta().fingerprint != NO_TOKEN
}
}
pub const NO_TOKEN: &str = "none";
impl fmt::Debug for Secret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Secret({} {})", self.harness, self.meta().fingerprint)
}
}
impl Drop for Secret {
fn drop(&mut self) {
wipe_strings(&mut self.value);
}
}
fn wipe_strings(v: &mut Value) {
match v {
Value::String(s) => wipe(std::mem::take(s)),
Value::Object(m) => m.values_mut().for_each(wipe_strings),
Value::Array(a) => a.iter_mut().for_each(wipe_strings),
Value::Null | Value::Bool(_) | Value::Number(_) => {}
}
}
fn wipe(s: String) {
let mut b = s.into_bytes();
b.iter_mut().for_each(|x| *x = 0);
std::hint::black_box(&b);
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, serde::Deserialize)]
pub struct LoginMeta {
pub fingerprint: String,
pub subscription_type: Option<String>,
pub rate_limit_tier: Option<String>,
pub access_expires_at: Option<DateTime<Utc>>,
pub refresh_expires_at: Option<DateTime<Utc>>,
pub scopes: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub account: Option<String>,
}
impl LoginMeta {
pub fn of(harness: Harness, v: &Value) -> LoginMeta {
match harness {
Harness::Claude => claude_meta(v),
Harness::Agy => agy_meta(v),
Harness::Codex => codex_meta(v),
}
}
}
fn claude_meta(v: &Value) -> LoginMeta {
let s = |k: &str| v.get(k).and_then(Value::as_str).map(str::to_string);
let nonempty = |k: &str| v.get(k).and_then(Value::as_str).filter(|x| !x.is_empty());
let t = |k: &str| {
v.get(k)
.and_then(Value::as_i64)
.and_then(|ms| Utc.timestamp_millis_opt(ms).single())
};
let token = nonempty("refreshToken").or_else(|| nonempty("accessToken"));
LoginMeta {
fingerprint: token.map(fingerprint).unwrap_or_else(|| NO_TOKEN.into()),
subscription_type: s("subscriptionType"),
rate_limit_tier: s("rateLimitTier"),
access_expires_at: t("expiresAt"),
refresh_expires_at: t("refreshTokenExpiresAt"),
scopes: v
.get("scopes")
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(|x| x.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default(),
account: nonempty("accountUuid").map(|a| {
AccountId::of(a, nonempty("organizationUuid"))
.as_str()
.to_string()
}),
}
}
fn agy_meta(v: &Value) -> LoginMeta {
let token = v.get("token");
let s = |k: &str| {
token
.and_then(|t| t.get(k))
.and_then(Value::as_str)
.filter(|x| !x.is_empty())
};
LoginMeta {
fingerprint: s("refresh_token")
.or_else(|| s("access_token"))
.map(fingerprint)
.unwrap_or_else(|| NO_TOKEN.into()),
subscription_type: v
.get("auth_method")
.and_then(Value::as_str)
.filter(|x| !x.is_empty())
.map(str::to_string),
rate_limit_tier: None,
access_expires_at: s("expiry")
.and_then(|e| DateTime::parse_from_rfc3339(e).ok())
.map(|t| t.with_timezone(&Utc)),
refresh_expires_at: None,
scopes: Vec::new(),
account: None,
}
}
fn codex_meta(v: &Value) -> LoginMeta {
let tokens = v.get("tokens");
let s = |k: &str| {
tokens
.and_then(|t| t.get(k))
.and_then(Value::as_str)
.filter(|x| !x.is_empty())
};
let claims = |jwt: Option<&str>| -> Value {
jwt.and_then(super::jwt::payload)
.and_then(|b| serde_json::from_slice(&b).ok())
.unwrap_or(Value::Null)
};
let id = claims(s("id_token"));
let access = claims(s("access_token"));
let api_key = v
.get("OPENAI_API_KEY")
.and_then(Value::as_str)
.filter(|x| !x.is_empty());
LoginMeta {
fingerprint: s("refresh_token")
.or_else(|| s("access_token"))
.or(api_key)
.map(fingerprint)
.unwrap_or_else(|| NO_TOKEN.into()),
subscription_type: id
.get("https://api.openai.com/auth")
.and_then(|a| a.get("chatgpt_plan_type"))
.and_then(Value::as_str)
.map(str::to_string),
rate_limit_tier: None,
access_expires_at: access
.get("exp")
.and_then(Value::as_i64)
.and_then(|t| Utc.timestamp_opt(t, 0).single()),
refresh_expires_at: None,
scopes: Vec::new(),
account: None,
}
}
pub fn fingerprint(token: &str) -> String {
let d = Sha256::digest(token.as_bytes());
hex::encode(&d[..8])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn agy_meta_names_the_grant_without_exposing_it() {
let s = Secret::of(
Harness::Agy,
serde_json::json!({
"token": {"access_token": "ya-a", "refresh_token": "ya-r",
"expiry": "2026-11-24T10:00:00Z", "expires_in": 3599},
"auth_method": "consumer",
"id_token_email": "sam@example.com"
}),
);
let m = s.meta();
assert_eq!(m.fingerprint, fingerprint("ya-r"));
assert_eq!(m.subscription_type.as_deref(), Some("consumer"));
assert_eq!(
m.access_expires_at.map(|t| t.to_rfc3339()).as_deref(),
Some("2026-11-24T10:00:00+00:00")
);
let dbg = format!("{s:?}");
assert!(!dbg.contains("ya-a") && !dbg.contains("ya-r"), "{dbg}");
}
#[test]
fn codex_meta_reads_the_plan_and_the_access_expiry_from_the_tokens_claims() {
use crate::core::jwt::b64url;
let jwt = |claims: &str| format!("{}.{}.sig", b64url(b"{}"), b64url(claims.as_bytes()));
let s = Secret::of(
Harness::Codex,
serde_json::json!({
"auth_mode": "chatgpt",
"tokens": {
"id_token": jwt(r#"{"https://api.openai.com/auth":{"chatgpt_plan_type":"plus"}}"#),
"access_token": jwt(r#"{"exp":1795600000}"#),
"refresh_token": "rt-secret",
}
}),
);
let m = s.meta();
assert_eq!(m.fingerprint, fingerprint("rt-secret"));
assert_eq!(m.subscription_type.as_deref(), Some("plus"));
assert_eq!(
m.access_expires_at.map(|t| t.timestamp()),
Some(1_795_600_000)
);
}
}