Skip to content

Commit

Permalink
Add logging to log file says when are where oauth token is used
Browse files Browse the repository at this point in the history
  • Loading branch information
damienrj committed Feb 3, 2025
1 parent 8adfbcd commit c288b41
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions crates/goose/src/providers/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use sha2::Digest;
use std::{collections::HashMap, fs, net::SocketAddr, path::PathBuf, sync::Arc};
use tokio::sync::{oneshot, Mutex as TokioMutex};
use url::Url;
use tracing::{info};



lazy_static! {
static ref OAUTH_MUTEX: TokioMutex<()> = TokioMutex::new(());
Expand Down Expand Up @@ -50,31 +53,38 @@ fn get_cache_path(client_id: &str, scopes: &[String]) -> PathBuf {

fn load_cached_token(client_id: &str, scopes: &[String]) -> Option<String> {
let cache_path = get_cache_path(client_id, scopes);
if let Ok(contents) = fs::read_to_string(cache_path) {
if let Ok(contents) = fs::read_to_string(&cache_path) {
if let Ok(cache) = serde_json::from_str::<TokenCache>(&contents) {
if let Some(expires_at) = cache.expires_at {
if expires_at > Utc::now() {
info!("Using cached OAuth token from {} valid until {}", cache_path.display(), expires_at);
return Some(cache.access_token);
}
}
}
}
info!("No valid cached OAuth token found at {}", cache_path.display());
None
}

fn save_token_cache(client_id: &str, scopes: &[String], token: &str, expires_in: Option<u64>) {
let expires_at = expires_in.map(|secs| Utc::now() + Duration::seconds(secs as i64));
let cache_path = get_cache_path(client_id, scopes);

info!("Saving new OAuth token to {}{}",
cache_path.display(),
expires_at.map_or(String::new(), |exp| format!(" valid until {}", exp))
);

let token_cache = TokenCache {
access_token: token.to_string(),
expires_at,
};

let cache_path = get_cache_path(client_id, scopes);
if let Ok(contents) = serde_json::to_string(&token_cache) {
fs::write(cache_path, contents)
fs::write(&cache_path, contents)
.map_err(|e| anyhow::anyhow!("Failed to write token cache: {}", e))
.unwrap_or_else(|e| tracing::warn!("{}", e));
.unwrap_or_else(|e| tracing::warn!("Failed to write to {}: {}", cache_path.display(), e));
}
}

Expand Down

0 comments on commit c288b41

Please sign in to comment.