ai-usagebar 1.21.1

Omarchy/Waybar widgets + TUI for tracking multi-provider AI plan usage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Antigravity's saved Google session, read from the OS keyring or CLI file.
//!
//! Antigravity normally stores the OAuth token it obtained at sign-in through
//! Go's `go-keyring`, under service `gemini` and account `antigravity`. On
//! Windows that is a generic credential named `gemini:antigravity` in the
//! Credential Manager, on macOS a generic password in the login Keychain, and
//! on Linux a Secret Service item. The `agy` CLI can instead use the
//! JSON file `~/.gemini/antigravity-cli/antigravity-oauth-token`. Both shapes
//! are accepted; the keyring is tried first.
//!
//! This module only *reads* the blob and pulls out the tokens; the network
//! side (refresh, quota) lives in `cloud.rs`. The keyring and CLI file are
//! never written.
//!
//! [`read`] is deliberately forgiving: any keyring hiccup — no backend, a
//! locked store, a denied ACL — falls through to the CLI file. If neither
//! source is available, the caller gets `Ok(None)` and can produce its own
//! "no session" message.

use std::fmt::Write as _;
use std::path::Path;

use base64::Engine as _;
use chrono::{DateTime, Utc};
use sha2::{Digest, Sha256};

use crate::error::{AppError, Result};

/// `go-keyring` service name Antigravity registers its session under.
pub const KEYRING_SERVICE: &str = "gemini";
/// `go-keyring` account (user) name paired with [`KEYRING_SERVICE`].
pub const KEYRING_ACCOUNT: &str = "antigravity";
/// Prefix `go-keyring` adds when it had to base64-wrap the value.
pub const GO_KEYRING_PREFIX: &str = "go-keyring-base64:";

/// Upper bound on the blob a keyring may hand back. A real token blob is
/// well under 4 KiB; anything larger is not something this module parses.
const MAX_BLOB_BYTES: usize = 64 * 1024;

const ACCESS_TOKEN_KEYS: [&str; 8] = [
    "access_token",
    "accessToken",
    "token",
    "id_token",
    "idToken",
    "bearerToken",
    "auth_token",
    "authToken",
];
const REFRESH_TOKEN_KEYS: [&str; 2] = ["refresh_token", "refreshToken"];
const EXPIRY_KEYS: [&str; 3] = ["expiry", "expires_at", "expiresAt"];

/// Any value at or above this is an epoch in milliseconds, not seconds
/// (10^11 s is the year 5138; 10^11 ms is 1973).
const EPOCH_MILLIS_THRESHOLD: f64 = 1e11;

/// The tokens Antigravity saved, plus a non-secret identity for them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StoredToken {
    pub access_token: String,
    pub refresh_token: Option<String>,
    pub expires_at: Option<DateTime<Utc>>,
    /// First 16 hex chars of the SHA-256 of the refresh token (or of the
    /// access token when there is none). Scopes ai-usagebar's own persisted
    /// refresh to this sign-in, so a re-login never gets the old session's
    /// cached access token.
    pub fingerprint: String,
}

/// Decode the raw keyring value into a [`StoredToken`].
///
/// Error messages are fixed strings: the input is a credential and must never
/// end up in a tooltip or a log line.
pub fn parse_keyring_blob(raw: &str) -> Result<StoredToken> {
    let raw = raw.trim();
    let json = match raw.strip_prefix(GO_KEYRING_PREFIX) {
        Some(encoded) => {
            let bytes = base64::engine::general_purpose::STANDARD
                .decode(encoded.trim())
                .map_err(|_| {
                    AppError::Credentials(
                        "Antigravity's saved Google session is not valid base64; open Antigravity and sign in again"
                            .into(),
                    )
                })?;
            String::from_utf8(bytes).map_err(|_| {
                AppError::Credentials(
                    "Antigravity's saved Google session is not UTF-8; open Antigravity and sign in again"
                        .into(),
                )
            })?
        }
        None => raw.to_string(),
    };
    let root: serde_json::Value = serde_json::from_str(&json).map_err(|_| {
        AppError::Credentials(
            "Antigravity's saved Google session is not valid JSON; open Antigravity and sign in again"
                .into(),
        )
    })?;
    let token = match root.get("token") {
        Some(nested) if nested.is_object() => nested,
        _ => &root,
    };

    let access_token = first_string(token, &ACCESS_TOKEN_KEYS).ok_or_else(|| {
        AppError::Credentials(
            "Antigravity's saved Google session has no access token; open Antigravity and sign in"
                .into(),
        )
    })?;
    let refresh_token = first_string(token, &REFRESH_TOKEN_KEYS);
    let expires_at = EXPIRY_KEYS
        .iter()
        .filter_map(|key| token.get(key))
        .find_map(parse_expiry);
    let fingerprint = fingerprint_of(refresh_token.as_deref().unwrap_or(&access_token));

    Ok(StoredToken {
        access_token,
        refresh_token,
        expires_at,
        fingerprint,
    })
}

fn first_string(object: &serde_json::Value, keys: &[&str]) -> Option<String> {
    keys.iter()
        .filter_map(|key| object.get(key).and_then(serde_json::Value::as_str))
        .map(str::trim)
        .find(|value| !value.is_empty())
        .map(str::to_string)
}

fn parse_expiry(value: &serde_json::Value) -> Option<DateTime<Utc>> {
    match value {
        serde_json::Value::String(text) => {
            let text = text.trim();
            if let Ok(parsed) = DateTime::parse_from_rfc3339(text) {
                return Some(parsed.with_timezone(&Utc));
            }
            text.parse::<f64>().ok().and_then(epoch_to_datetime)
        }
        serde_json::Value::Number(number) => number.as_f64().and_then(epoch_to_datetime),
        _ => None,
    }
}

fn epoch_to_datetime(epoch: f64) -> Option<DateTime<Utc>> {
    if !epoch.is_finite() || epoch <= 0.0 {
        return None;
    }
    let millis = if epoch >= EPOCH_MILLIS_THRESHOLD {
        epoch
    } else {
        epoch * 1000.0
    };
    if millis > i64::MAX as f64 {
        return None;
    }
    DateTime::from_timestamp_millis(millis as i64)
}

fn fingerprint_of(secret: &str) -> String {
    let digest = Sha256::digest(secret.as_bytes());
    let mut hex = String::with_capacity(16);
    for byte in digest.iter().take(8) {
        let _ = write!(hex, "{byte:02x}");
    }
    hex
}

/// The raw blob from the OS keyring or Antigravity CLI token file, or `None`
/// when no saved session is available. Never errors on a backend failure — see
/// the module docs.
pub fn read() -> Result<Option<String>> {
    let cli_path = crate::cache::home_dir().ok().map(|home| {
        home.join(".gemini")
            .join("antigravity-cli")
            .join("antigravity-oauth-token")
    });
    Ok(read_saved_session(read_platform(), cli_path.as_deref()))
}

fn read_saved_session(keyring: Option<String>, cli_path: Option<&Path>) -> Option<String> {
    keyring.or_else(|| cli_path.and_then(read_cli_token_file))
}

/// Read the read-only OAuth file used by the Antigravity CLI when it cannot
/// use the OS keyring. The file has the same JSON credential shape as the
/// keyring blob, including the nested `token` object.
///
/// Held to the keyring path's limits rather than trusted because it is a file:
/// only a regular file is opened — checked before `open`, since opening a FIFO
/// blocks until a writer appears and this runs inside the widget's fetch — and
/// at most `MAX_BLOB_BYTES + 1` bytes are read, so a runaway file or a symlink
/// to a device is rejected by `decode_blob_bytes` instead of filling memory.
fn read_cli_token_file(path: &Path) -> Option<String> {
    use std::io::Read;
    if !std::fs::metadata(path).ok()?.is_file() {
        return None;
    }
    let mut bytes = Vec::new();
    std::fs::File::open(path)
        .ok()?
        .take(MAX_BLOB_BYTES as u64 + 1)
        .read_to_end(&mut bytes)
        .ok()?;
    decode_blob_bytes(&bytes)
}

/// A keyring value as bytes → text. `go-keyring` writes UTF-8; a credential
/// written by something else on Windows may be UTF-16LE.
///
/// UTF-16LE of ASCII text is byte-valid UTF-8 (NUL is a legal code point), so
/// "does it parse as UTF-8" cannot tell them apart. A JSON blob never carries
/// a NUL, so an even-length value with a NUL byte (or a UTF-16 BOM) is UTF-16.
fn decode_blob_bytes(bytes: &[u8]) -> Option<String> {
    if bytes.is_empty() || bytes.len() > MAX_BLOB_BYTES {
        return None;
    }
    let looks_utf16 =
        bytes.len().is_multiple_of(2) && (bytes.starts_with(&[0xff, 0xfe]) || bytes.contains(&0));
    let text = if looks_utf16 {
        let units: Vec<u16> = bytes
            .as_chunks::<2>()
            .0
            .iter()
            .map(|pair| u16::from_le_bytes(*pair))
            .collect();
        let units = units.strip_prefix(&[0xfeff]).unwrap_or(&units);
        String::from_utf16(units).ok()?
    } else {
        std::str::from_utf8(bytes).ok()?.to_string()
    };
    let text = text.trim_matches(['\0', '\n', '\r', ' ']);
    if text.is_empty() {
        None
    } else {
        Some(text.to_string())
    }
}

#[cfg(windows)]
fn read_platform() -> Option<String> {
    use windows_sys::Win32::Security::Credentials::{
        CRED_TYPE_GENERIC, CREDENTIALW, CredFree, CredReadW,
    };

    let target: Vec<u16> = format!("{KEYRING_SERVICE}:{KEYRING_ACCOUNT}")
        .encode_utf16()
        .chain(std::iter::once(0))
        .collect();
    let mut credential: *mut CREDENTIALW = std::ptr::null_mut();
    // SAFETY: `target` is a NUL-terminated UTF-16 string that outlives the
    // call; `credential` is a local out-slot the API fills on success.
    let ok = unsafe { CredReadW(target.as_ptr(), CRED_TYPE_GENERIC, 0, &mut credential) };
    // `ERROR_NOT_FOUND` is the expected "never signed in" case; any other
    // failure (locked store, denied access) is treated the same way rather
    // than failing the vendor on a keyring hiccup.
    if ok == 0 || credential.is_null() {
        return None;
    }
    // SAFETY: `CredReadW` succeeded, so `credential` points at a CREDENTIALW
    // owned by the API that stays valid until `CredFree`. The blob pointer
    // and size describe one allocation of exactly `CredentialBlobSize` bytes.
    let bytes = unsafe {
        let cred = &*credential;
        let len = cred.CredentialBlobSize as usize;
        if cred.CredentialBlob.is_null() || len == 0 || len > MAX_BLOB_BYTES {
            Vec::new()
        } else {
            std::slice::from_raw_parts(cred.CredentialBlob, len).to_vec()
        }
    };
    // SAFETY: `credential` came from `CredReadW` and is freed exactly once.
    unsafe { CredFree(credential.cast()) };
    decode_blob_bytes(&bytes)
}

#[cfg(target_os = "macos")]
fn read_platform() -> Option<String> {
    let out = std::process::Command::new("/usr/bin/security")
        .args([
            "find-generic-password",
            "-s",
            KEYRING_SERVICE,
            "-a",
            KEYRING_ACCOUNT,
            "-w",
        ])
        .stdin(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    decode_blob_bytes(&out.stdout)
}

#[cfg(not(any(windows, target_os = "macos")))]
fn read_platform() -> Option<String> {
    // `secret-tool` (libsecret) speaks to whichever Secret Service is running.
    // A missing binary or no running daemon both exit non-zero / fail to
    // spawn, and both mean "no session available here".
    let out = std::process::Command::new("secret-tool")
        .args([
            "lookup",
            "service",
            KEYRING_SERVICE,
            "username",
            KEYRING_ACCOUNT,
        ])
        .stdin(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    decode_blob_bytes(&out.stdout)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn encode(json: &str) -> String {
        format!(
            "{GO_KEYRING_PREFIX}{}",
            base64::engine::general_purpose::STANDARD.encode(json)
        )
    }

    #[test]
    fn bare_json_blob_parses() {
        let token = parse_keyring_blob(
            r#"{"access_token":"at","refresh_token":"rt","expiry":"2030-01-02T03:04:05Z"}"#,
        )
        .unwrap();
        assert_eq!(token.access_token, "at");
        assert_eq!(token.refresh_token.as_deref(), Some("rt"));
        assert_eq!(
            token.expires_at.unwrap().to_rfc3339(),
            "2030-01-02T03:04:05+00:00"
        );
        assert_eq!(token.fingerprint.len(), 16);
    }

    #[test]
    fn go_keyring_prefixed_blob_parses_to_the_same_token() {
        let json = r#"{"access_token":"at","refresh_token":"rt"}"#;
        let bare = parse_keyring_blob(json).unwrap();
        let wrapped = parse_keyring_blob(&format!("  {}\n", encode(json))).unwrap();
        assert_eq!(bare, wrapped);
    }

    #[test]
    fn nested_token_object_is_unwrapped() {
        let token = parse_keyring_blob(
            r#"{"email":"someone","token":{"accessToken":"nested-at","refreshToken":"nested-rt"}}"#,
        )
        .unwrap();
        assert_eq!(token.access_token, "nested-at");
        assert_eq!(token.refresh_token.as_deref(), Some("nested-rt"));
    }

    #[test]
    fn every_access_token_alias_is_accepted() {
        for key in ACCESS_TOKEN_KEYS {
            let token = parse_keyring_blob(&format!(r#"{{"{key}":"value-{key}"}}"#)).unwrap();
            assert_eq!(token.access_token, format!("value-{key}"), "{key}");
            assert_eq!(token.refresh_token, None);
        }
    }

    #[test]
    fn every_refresh_token_alias_is_accepted() {
        for key in REFRESH_TOKEN_KEYS {
            let token =
                parse_keyring_blob(&format!(r#"{{"access_token":"at","{key}":"rt-{key}"}}"#))
                    .unwrap();
            assert_eq!(
                token.refresh_token.as_deref(),
                Some(format!("rt-{key}")).as_deref()
            );
        }
    }

    #[test]
    fn empty_aliases_are_skipped_in_favour_of_later_ones() {
        let token =
            parse_keyring_blob(r#"{"access_token":"  ","accessToken":"","token":"real"}"#).unwrap();
        assert_eq!(token.access_token, "real");
    }

    #[test]
    fn expiry_accepts_rfc3339_epoch_seconds_and_epoch_millis() {
        let expected = "2030-01-02T03:04:05+00:00";
        let cases = [
            r#"{"access_token":"at","expiry":"2030-01-02T04:04:05+01:00"}"#,
            r#"{"access_token":"at","expires_at":1893553445}"#,
            r#"{"access_token":"at","expiresAt":1893553445000}"#,
            r#"{"access_token":"at","expires_at":"1893553445"}"#,
        ];
        for case in cases {
            let token = parse_keyring_blob(case).unwrap();
            assert_eq!(token.expires_at.unwrap().to_rfc3339(), expected, "{case}");
        }
    }

    #[test]
    fn unparseable_or_missing_expiry_is_none() {
        for case in [
            r#"{"access_token":"at"}"#,
            r#"{"access_token":"at","expiry":"soon"}"#,
            r#"{"access_token":"at","expiry":-5}"#,
            r#"{"access_token":"at","expiry":true}"#,
        ] {
            assert_eq!(parse_keyring_blob(case).unwrap().expires_at, None, "{case}");
        }
    }

    #[test]
    fn fingerprint_is_stable_and_tracks_the_refresh_token() {
        let a = parse_keyring_blob(r#"{"access_token":"at-1","refresh_token":"rt"}"#).unwrap();
        let b = parse_keyring_blob(r#"{"access_token":"at-2","refresh_token":"rt"}"#).unwrap();
        let c = parse_keyring_blob(r#"{"access_token":"at-1","refresh_token":"other"}"#).unwrap();
        assert_eq!(a.fingerprint, b.fingerprint);
        assert_ne!(a.fingerprint, c.fingerprint);
        assert!(a.fingerprint.chars().all(|c| c.is_ascii_hexdigit()));
        assert!(!a.fingerprint.contains("rt"));
    }

    #[test]
    fn fingerprint_falls_back_to_the_access_token() {
        let a = parse_keyring_blob(r#"{"access_token":"at-1"}"#).unwrap();
        let b = parse_keyring_blob(r#"{"access_token":"at-2"}"#).unwrap();
        assert_ne!(a.fingerprint, b.fingerprint);
        assert_eq!(a.fingerprint, fingerprint_of("at-1"));
    }

    #[test]
    fn missing_access_token_is_a_credentials_error() {
        let err = parse_keyring_blob(r#"{"refresh_token":"rt"}"#).unwrap_err();
        match err {
            AppError::Credentials(msg) => assert!(msg.contains("no access token"), "{msg}"),
            other => panic!("expected Credentials, got {other:?}"),
        }
    }

    #[test]
    fn garbage_base64_never_leaks_the_input() {
        let raw = format!("{GO_KEYRING_PREFIX}!!not-base64-SECRETMARKER!!");
        let err = parse_keyring_blob(&raw).unwrap_err();
        assert!(matches!(err, AppError::Credentials(_)));
        assert!(!err.to_string().contains("SECRETMARKER"));
    }

    #[test]
    fn garbage_json_never_leaks_the_input() {
        for raw in [
            "{not json SECRETMARKER".to_string(),
            encode("{still not json SECRETMARKER"),
        ] {
            let err = parse_keyring_blob(&raw).unwrap_err();
            assert!(matches!(err, AppError::Credentials(_)));
            assert!(!err.to_string().contains("SECRETMARKER"));
        }
    }

    #[test]
    fn blob_bytes_decode_utf8_and_utf16le() {
        assert_eq!(
            decode_blob_bytes(
                "{\"a\":\"é\"}
"
                .as_bytes()
            )
            .as_deref(),
            Some("{\"a\":\"é\"}")
        );
        let utf16: Vec<u8> = "{\"a\":\"é\"}"
            .encode_utf16()
            .flat_map(u16::to_le_bytes)
            .collect();
        assert_eq!(decode_blob_bytes(&utf16).as_deref(), Some("{\"a\":\"é\"}"));
        let with_bom: Vec<u8> = [0xff, 0xfe].into_iter().chain(utf16).collect();
        assert_eq!(
            decode_blob_bytes(&with_bom).as_deref(),
            Some("{\"a\":\"é\"}")
        );
        assert_eq!(decode_blob_bytes(b""), None);
        assert_eq!(decode_blob_bytes(&[0xff, 0xfe, 0xfd]), None);
        assert_eq!(decode_blob_bytes(&[0xc3, 0x28]), None);
        assert_eq!(decode_blob_bytes(&vec![b'a'; MAX_BLOB_BYTES + 1]), None);
    }

    #[test]
    fn cli_oauth_token_file_rejects_oversized_and_non_regular_paths() {
        let dir = tempfile::tempdir().unwrap();

        let big = dir.path().join("big");
        std::fs::write(&big, vec![b'a'; MAX_BLOB_BYTES + 1]).unwrap();
        assert_eq!(read_cli_token_file(&big), None, "over the blob cap");

        let not_a_file = dir.path().join("a-directory");
        std::fs::create_dir(&not_a_file).unwrap();
        assert_eq!(read_cli_token_file(&not_a_file), None, "not a regular file");

        assert_eq!(read_cli_token_file(&dir.path().join("missing")), None);
    }

    #[test]
    fn cli_oauth_token_file_is_read_from_the_nested_token_shape() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir
            .path()
            .join(".gemini")
            .join("antigravity-cli")
            .join("antigravity-oauth-token");
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(
            &path,
            r#"{"token":{"access_token":"file-at","refresh_token":"file-rt","expiry":"2030-01-02T03:04:05Z"}}"#,
        )
        .unwrap();

        let raw = read_saved_session(None, Some(&path)).expect("CLI OAuth file should be readable");
        let token = parse_keyring_blob(&raw).unwrap();
        assert_eq!(token.access_token, "file-at");
        assert_eq!(token.refresh_token.as_deref(), Some("file-rt"));

        assert_eq!(
            read_saved_session(Some("keyring".into()), Some(&path)).as_deref(),
            Some("keyring")
        );
    }

    /// Touches the real Credential Manager; asserts only that the read does
    /// not error, and never inspects or prints the value.
    #[cfg(windows)]
    #[test]
    #[ignore = "reads the real Windows credential store"]
    fn reading_the_real_windows_credential_never_errors() {
        assert!(read().is_ok());
    }
}