Skip to main content

codex_cli/auth/
mod.rs

1pub mod auto_refresh;
2pub mod current;
3pub mod login;
4pub mod output;
5pub mod refresh;
6pub mod remove;
7pub mod save;
8pub mod sync;
9pub mod use_secret;
10
11use anyhow::Result;
12use std::path::Path;
13
14use crate::json;
15use crate::jwt;
16
17pub fn identity_from_auth_file(path: &Path) -> Result<Option<String>> {
18    let value = json::read_json(path)?;
19    let token = token_from_auth_json(&value);
20    let payload = token
21        .and_then(|tok| jwt::decode_payload_json(&tok))
22        .and_then(|payload| jwt::identity_from_payload(&payload));
23    Ok(payload)
24}
25
26pub fn email_from_auth_file(path: &Path) -> Result<Option<String>> {
27    let value = json::read_json(path)?;
28    let token = token_from_auth_json(&value);
29    let payload = token
30        .and_then(|tok| jwt::decode_payload_json(&tok))
31        .and_then(|payload| jwt::email_from_payload(&payload));
32    Ok(payload)
33}
34
35pub fn account_id_from_auth_file(path: &Path) -> Result<Option<String>> {
36    let value = json::read_json(path)?;
37    let account = json::string_at(&value, &["tokens", "account_id"])
38        .or_else(|| json::string_at(&value, &["account_id"]));
39    Ok(account)
40}
41
42pub fn last_refresh_from_auth_file(path: &Path) -> Result<Option<String>> {
43    let value = json::read_json(path)?;
44    Ok(json::string_at(&value, &["last_refresh"]))
45}
46
47pub fn identity_key_from_auth_file(path: &Path) -> Result<Option<String>> {
48    let identity = identity_from_auth_file(path)?;
49    let identity = match identity {
50        Some(value) => value,
51        None => return Ok(None),
52    };
53    let account_id = account_id_from_auth_file(path)?;
54    let key = match account_id {
55        Some(account) => format!("{}::{}", identity, account),
56        None => identity,
57    };
58    Ok(Some(key))
59}
60
61fn token_from_auth_json(value: &serde_json::Value) -> Option<String> {
62    json::string_at(value, &["tokens", "id_token"])
63        .or_else(|| json::string_at(value, &["tokens", "access_token"]))
64}