Skip to main content

cpa_exec/
codex_refresh.rs

1//! Codex token refresh: which token partitions refresh (Go codex_executor_auth.go
2//! `Refresh` / `refreshLocked`), and the partition-scoped result that lands on the
3//! credential file as read under its lock (internal/auth/codex/refresh_patch.go).
4
5use std::collections::BTreeMap;
6
7use chrono::Utc;
8use cpa_common::json::GoValue;
9use cpa_core::credential::{Credential, MetadataPatch};
10use cpa_core::exec::ExecError;
11use serde_json::{Map, Value};
12
13use crate::codex_oauth::{self, CodexOAuth};
14
15/// What a Codex refresh was asked for.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum RefreshMode {
18    /// A due refresh ([`Refresher::prepare`]).
19    Due,
20    /// After an upstream rejected the token requests use ([`Refresher::refresh_now`]).
21    Rejected,
22    /// A management refresh ([`Refresher::refresh_all`]).
23    Management,
24}
25
26/// The refresh grants of one credential on the client its proxy selects. It owns
27/// everything it needs, so a refresh can outlive the request that started it.
28#[derive(Clone)]
29pub struct Refresher {
30    oauth: CodexOAuth,
31}
32
33impl Refresher {
34    pub(crate) fn new(oauth: CodexOAuth) -> Self {
35        Self { oauth }
36    }
37
38    /// Refreshes the due partitions. Go refreshes Codex only in the background and never
39    /// blocks a request on it, so a failed refresh while the token requests use is still
40    /// valid keeps the credential usable; the failure is cached for five minutes by the
41    /// OAuth service.
42    pub async fn prepare(&self, credential: &Credential) -> Result<MetadataPatch, ExecError> {
43        let now = Utc::now();
44        let (outer, siwc) = due_partitions(credential, now);
45        self.refresh_due(credential, outer, siwc, now).await
46    }
47
48    /// [`Self::prepare`] of the partitions `outer` and `siwc`.
49    async fn refresh_due(
50        &self,
51        credential: &Credential,
52        outer: bool,
53        siwc: bool,
54        now: chrono::DateTime<Utc>,
55    ) -> Result<MetadataPatch, ExecError> {
56        if !outer && !siwc {
57            return Ok(MetadataPatch::default());
58        }
59        let usable = if crate::codex_siwc::enabled(credential) {
60            crate::codex_siwc::access_usable(credential, now)
61        } else {
62            codex_oauth::access_usable(credential, now)
63        };
64        match self.partitions(credential, outer, siwc).await {
65            Ok(patch) => Ok(patch),
66            Err(error) if usable => {
67                tracing::warn!(
68                    credential = %credential.id,
69                    status = error.status,
70                    "codex token refresh failed; current access token is still valid"
71                );
72                Ok(MetadataPatch::default())
73            }
74            Err(error) => Err(error),
75        }
76    }
77
78    /// Refreshes after an upstream rejected the token requests use (Go marks it
79    /// `RejectedAccessToken`): that partition (the Sign in with ChatGPT one when
80    /// `use_siwc` is on) and any due one. With neither able to refresh, every partition
81    /// that can (Go's explicit unscheduled refresh). Nothing without a refresh token.
82    pub async fn refresh_now(&self, credential: &Credential) -> Result<MetadataPatch, ExecError> {
83        let (outer, siwc) = rejected_partitions(credential, credential, &DiskRotation::default(), Utc::now());
84        self.partitions(credential, outer, siwc).await
85    }
86
87    /// A management refresh (Go `IsForceRefreshContext`): every partition that can refresh.
88    pub async fn refresh_all(&self, credential: &Credential) -> Result<MetadataPatch, ExecError> {
89        let (outer, siwc) = all_partitions(credential, Utc::now());
90        self.partitions(credential, outer, siwc).await
91    }
92
93    /// Go `refreshLocked` with the credential file (`disk`) read under its lock: a token
94    /// partition another process rotated there replaces the `memory` snapshot's before
95    /// anything refreshes, so its refresh token is not spent twice, and the refresh
96    /// `mode` asks for then runs on the result. The answer names each partition adopted
97    /// or refreshed with its baselines, for [`RefreshPatch::apply`], and whether a grant
98    /// was attempted; with one adopted, a failed refresh still answers with it.
99    pub async fn refresh_locked(
100        &self,
101        memory: &Credential,
102        disk: &Map<String, Value>,
103        mode: RefreshMode,
104    ) -> Result<RefreshPatch, ExecError> {
105        let rotation = DiskRotation::read(memory, disk);
106        let mut adopted = memory.clone();
107        rotation.patch.apply(&mut adopted.metadata);
108        adopted.refresh_derived();
109        let now = Utc::now();
110        let (outer, siwc) = match mode {
111            RefreshMode::Due => due_partitions(&adopted, now),
112            RefreshMode::Rejected => rejected_partitions(memory, &adopted, &rotation, now),
113            RefreshMode::Management => all_partitions(&adopted, now),
114        };
115        let granted = outer || siwc;
116        let refreshed = match mode {
117            RefreshMode::Due => self.refresh_due(&adopted, outer, siwc, now).await,
118            _ => self.partitions(&adopted, outer, siwc).await,
119        };
120        let refresh = match refreshed {
121            Ok(refresh) => refresh,
122            Err(error) if rotation.outer || rotation.siwc => {
123                tracing::warn!(
124                    credential = %memory.id,
125                    status = error.status,
126                    "codex token refresh failed; keeping the tokens another process rotated on disk"
127                );
128                MetadataPatch::default()
129            }
130            Err(error) => return Err(error),
131        };
132        let mut after = adopted.metadata.clone();
133        refresh.apply(&mut after);
134        let outer = rotation.outer || fields(&after, &OUTER) != fields(&adopted.metadata, &OUTER);
135        let siwc = rotation.siwc || fields(&siwc_map(&after), &SIWC) != fields(&siwc_map(&adopted.metadata), &SIWC);
136        Ok(RefreshPatch {
137            granted,
138            outer: outer.then(|| Partition::new(&memory.metadata, disk, &after, &OUTER)),
139            siwc: siwc.then(|| Partition::new(&siwc_map(&memory.metadata), &siwc_map(disk), &siwc_map(&after), &SIWC)),
140        })
141    }
142
143    /// Go `refreshLocked`: the outer tokens, then the Sign in with ChatGPT partition, one
144    /// after the other. A partition that fails while the other refreshes is logged and
145    /// the other's patch kept; with neither refreshed, the outer error comes first.
146    async fn partitions(&self, credential: &Credential, outer: bool, siwc: bool) -> Result<MetadataPatch, ExecError> {
147        if !outer && !siwc {
148            return Ok(MetadataPatch::default());
149        }
150        let outer = if outer {
151            Some(self.oauth.refresh_patch(codex_oauth::refresh_token(credential)).await)
152        } else {
153            None
154        };
155        let siwc = if siwc {
156            let (token, client, device) = crate::codex_siwc::refresh_inputs(credential);
157            Some(
158                self.oauth
159                    .refresh_siwc(token, client, device)
160                    .await
161                    .map(|tokens| crate::codex_siwc::refresh_patch(credential, &tokens)),
162            )
163        } else {
164            None
165        };
166        let mut patch = MetadataPatch::default();
167        let (mut refreshed, mut failures) = (false, Vec::new());
168        for (partition, result) in [("outer", outer), ("SIWC", siwc)] {
169            match result {
170                Some(Ok(part)) => {
171                    refreshed = true;
172                    patch.set.extend(part.set);
173                    patch.remove.extend(part.remove);
174                }
175                Some(Err(error)) => failures.push((partition, error)),
176                None => {}
177            }
178        }
179        if !refreshed {
180            return match failures.into_iter().next() {
181                Some((_, error)) => Err(error),
182                None => Ok(patch),
183            };
184        }
185        for (partition, error) in failures {
186            tracing::warn!(
187                credential = %credential.id,
188                partition,
189                status = error.status,
190                "codex token refresh failed for one partition; the other refreshed"
191            );
192        }
193        Ok(patch)
194    }
195}
196
197/// Go `codexOuterCredentialKeys`.
198const OUTER: [&str; 8] = [
199    "id_token",
200    "access_token",
201    "refresh_token",
202    "account_id",
203    "last_refresh",
204    "email",
205    "expired",
206    "plan_type",
207];
208
209/// Go `codexSIWCCredentialKeys`.
210const SIWC: [&str; 9] = [
211    "access_token",
212    "refresh_token",
213    "id_token",
214    "client_id",
215    "device_id",
216    "expired",
217    "last_refresh",
218    "scope",
219    "scopes",
220];
221
222/// Go `credentialFields`.
223fn fields(metadata: &Map<String, Value>, keys: &[&str]) -> Map<String, Value> {
224    keys.iter()
225        .filter_map(|k| metadata.get(*k).map(|v| ((*k).to_owned(), v.clone())))
226        .collect()
227}
228
229/// The `siwc` object, or nothing (Go's failed `map[string]any` assertion).
230fn siwc_map(metadata: &Map<String, Value>) -> Map<String, Value> {
231    metadata
232        .get("siwc")
233        .and_then(Value::as_object)
234        .cloned()
235        .unwrap_or_default()
236}
237
238/// Go `sameCredentialVersion` (`reflect.DeepEqual` on map reads: null reads as missing).
239fn same_version(a: &Map<String, Value>, b: &Map<String, Value>) -> bool {
240    [
241        "access_token",
242        "refresh_token",
243        "id_token",
244        "account_id",
245        "client_id",
246        "device_id",
247    ]
248    .iter()
249    .all(|k| a.get(*k).filter(|v| !v.is_null()) == b.get(*k).filter(|v| !v.is_null()))
250}
251
252/// One partition of a refresh: its fields in the snapshot the refresh started from, in
253/// the file as read under the lock, and after the refresh.
254#[derive(Debug, Clone)]
255struct Partition {
256    before: Map<String, Value>,
257    disk_before: Map<String, Value>,
258    after: Map<String, Value>,
259}
260
261impl Partition {
262    fn new(before: &Map<String, Value>, disk: &Map<String, Value>, after: &Map<String, Value>, keys: &[&str]) -> Self {
263        Self {
264            before: fields(before, keys),
265            disk_before: fields(disk, keys),
266            after: fields(after, keys),
267        }
268    }
269
270    /// Go `matchesBaselineOrAfter`: the baseline `target` still holds (the file's, the
271    /// snapshot's, or the refresh's own result), or `None` when another writer
272    /// replaced the partition meanwhile.
273    fn baseline(&self, target: &Map<String, Value>) -> Option<&Map<String, Value>> {
274        if !self.disk_before.is_empty() && same_version(target, &self.disk_before) {
275            return Some(&self.disk_before);
276        }
277        if same_version(target, &self.before) {
278            return Some(&self.before);
279        }
280        (!self.after.is_empty() && same_version(target, &self.after)).then_some(&self.after)
281    }
282
283    /// Go `applyCredentialFields`: the fields the refresh changed against `base`, as Go
284    /// holds them (its maps decode numbers as float64).
285    fn apply(&self, base: &Map<String, Value>, doc: &mut BTreeMap<String, GoValue>) {
286        for (key, value) in &self.after {
287            if base.get(key) != Some(value) {
288                let go = serde_json::to_vec(value)
289                    .ok()
290                    .and_then(|text| GoValue::parse_f64(&text))
291                    .unwrap_or_else(|| GoValue::from_json(value));
292                doc.insert(key.clone(), go);
293            }
294        }
295    }
296}
297
298/// Go `CredentialRefreshPatch`: the partitions a refresh adopted from disk or refreshed,
299/// each with the versions it may still be applied over.
300#[derive(Debug, Clone, Default)]
301pub struct RefreshPatch {
302    /// A grant was attempted (Go `refreshLocked` then writes the file itself).
303    granted: bool,
304    outer: Option<Partition>,
305    siwc: Option<Partition>,
306}
307
308impl RefreshPatch {
309    /// A grant was attempted for this refresh; otherwise it only adopted from disk.
310    pub fn granted(&self) -> bool {
311        self.granted
312    }
313
314    /// No partition was adopted or refreshed: nothing to write.
315    pub fn is_empty(&self) -> bool {
316        self.outer.is_none() && self.siwc.is_none()
317    }
318
319    /// Go `CredentialRefreshPatch.CanApplyTo`: some partition still holds a version the
320    /// refresh may be applied over in `metadata`.
321    pub fn can_apply_to(&self, metadata: &Map<String, Value>) -> bool {
322        self.outer.as_ref().is_some_and(|p| p.baseline(metadata).is_some())
323            || self
324                .siwc
325                .as_ref()
326                .is_some_and(|p| p.baseline(&siwc_map(metadata)).is_some())
327    }
328
329    /// Go `CredentialRefreshPatch.Apply` on the credential file as read under its lock:
330    /// `target` is its JSON, `doc` the same document as Go decodes it, which takes the
331    /// changes. A partition another writer replaced since the refresh began keeps that
332    /// writer's version, without blocking the other partition; every other field stays
333    /// as the file has it.
334    pub fn apply(&self, target: &Map<String, Value>, doc: &mut BTreeMap<String, GoValue>) {
335        if let Some(outer) = &self.outer
336            && let Some(base) = outer.baseline(target)
337        {
338            outer.apply(base, doc);
339        }
340        if let Some(siwc) = &self.siwc
341            && let Some(base) = siwc.baseline(&siwc_map(target))
342        {
343            let mut merged = doc
344                .remove("siwc")
345                .and_then(|v| v.into_object().ok())
346                .unwrap_or_default();
347            siwc.apply(base, &mut merged);
348            doc.insert("siwc".into(), GoValue::Object(merged));
349        }
350    }
351}
352
353/// The token partitions another process rotated in the credential file (Go
354/// `refreshLocked`'s disk check), and the patch that adopts them.
355#[derive(Debug, Default)]
356struct DiskRotation {
357    patch: MetadataPatch,
358    outer: bool,
359    siwc: bool,
360}
361
362impl DiskRotation {
363    /// A partition is rotated when the file holds a non-blank access or refresh token
364    /// that differs from the snapshot's (trimmed). A rotated outer partition brings
365    /// Go's listed fields the file has, a rotated `siwc` its whole map; `use_siwc` always
366    /// follows the file when the file has it. Go also copies `type`; here the provider
367    /// a credential was loaded as never changes, and what is written is the file's own
368    /// document ([`RefreshPatch::apply`]), so the adopted copy only feeds the grants.
369    fn read(credential: &Credential, disk: &Map<String, Value>) -> Self {
370        let text =
371            |m: &Map<String, Value>, key: &str| m.get(key).and_then(Value::as_str).unwrap_or("").trim().to_owned();
372        let rotated = |disk: &Map<String, Value>, memory: &Map<String, Value>| {
373            ["refresh_token", "access_token"].iter().any(|key| {
374                let on_disk = text(disk, key);
375                !on_disk.is_empty() && on_disk != text(memory, key)
376            })
377        };
378        let mut patch = MetadataPatch::default();
379        let outer = rotated(disk, &credential.metadata);
380        if outer {
381            for key in OUTER {
382                if let Some(value) = disk.get(key) {
383                    patch.set.insert(key.into(), value.clone());
384                }
385            }
386        }
387        let disk_siwc = disk.get("siwc").and_then(Value::as_object).filter(|m| !m.is_empty());
388        let siwc = disk_siwc.is_some_and(|d| rotated(d, &siwc_map(&credential.metadata)));
389        if let Some(d) = disk_siwc.filter(|_| siwc) {
390            patch.set.insert("siwc".into(), Value::Object(d.clone()));
391        }
392        if let Some(value) = disk.get("use_siwc")
393            && credential.metadata.get("use_siwc") != Some(value)
394        {
395            patch.set.insert("use_siwc".into(), value.clone());
396        }
397        Self { patch, outer, siwc }
398    }
399}
400
401/// The partitions a due refresh takes (Go `CodexOuterNeedsRefresh` and
402/// `CodexSIWCNeedsRefresh`).
403fn due_partitions(credential: &Credential, now: chrono::DateTime<Utc>) -> (bool, bool) {
404    (
405        codex_oauth::refresh_due(credential, now),
406        crate::codex_siwc::refresh_due(credential, now) == Some(true),
407    )
408}
409
410/// The partitions a management refresh takes: every one that can refresh.
411fn all_partitions(credential: &Credential, now: chrono::DateTime<Utc>) -> (bool, bool) {
412    (
413        !codex_oauth::refresh_token(credential).is_empty(),
414        crate::codex_siwc::refresh_due(credential, now).is_some(),
415    )
416}
417
418/// Go `authOuterAccessToken`.
419fn outer_access_token(credential: &Credential) -> &str {
420    ["access_token", "accessToken"]
421        .iter()
422        .filter_map(|k| credential.str(k).map(str::trim))
423        .find(|t| !t.is_empty())
424        .unwrap_or_default()
425}
426
427/// The partitions a refresh after a rejected token takes. On the `memory` snapshot whose
428/// token was rejected: the partition requests use (Sign in with ChatGPT when `use_siwc`
429/// is on) and any due one; with neither able to refresh, every partition that can (Go's
430/// explicit unscheduled refresh), except one `rotation` adopted from disk unless it is
431/// due there. In `adopted`, the partition requests use is forced only while it still
432/// holds the rejected access token (Go compares the token with `RejectedAccessToken`):
433/// a rotated refresh token alone does not replace it.
434fn rejected_partitions(
435    memory: &Credential,
436    adopted: &Credential,
437    rotation: &DiskRotation,
438    now: chrono::DateTime<Utc>,
439) -> (bool, bool) {
440    let active_siwc = crate::codex_siwc::enabled(memory);
441    let pick = |c: &Credential, outer_replaced: bool, siwc_replaced: bool| {
442        let outer = !codex_oauth::refresh_token(c).is_empty()
443            && ((!active_siwc && !outer_replaced) || codex_oauth::refresh_due(c, now));
444        let siwc = crate::codex_siwc::refresh_due(c, now).is_some_and(|due| due || (active_siwc && !siwc_replaced));
445        (outer, siwc)
446    };
447    if pick(memory, false, false) != (false, false) {
448        return pick(
449            adopted,
450            outer_access_token(adopted) != outer_access_token(memory),
451            crate::codex_siwc::access_token(adopted) != crate::codex_siwc::access_token(memory),
452        );
453    }
454    let outer =
455        !codex_oauth::refresh_token(adopted).is_empty() && (!rotation.outer || codex_oauth::refresh_due(adopted, now));
456    let siwc = crate::codex_siwc::refresh_due(adopted, now).is_some_and(|due| due || !rotation.siwc);
457    (outer, siwc)
458}