diff --git a/vibi-dpu/src/core/github/setup.rs b/vibi-dpu/src/core/github/setup.rs index 8fd7881e..c51aedf3 100644 --- a/vibi-dpu/src/core/github/setup.rs +++ b/vibi-dpu/src/core/github/setup.rs @@ -22,7 +22,7 @@ pub async fn handle_install_github(installation_code: &str) { return; } let auth_info = auth_info_opt.expect("Empty authinfo_opt"); - let access_token = auth_info.access_token().clone(); + let access_token = auth_info.token().clone(); let mut pubreqs: Vec = Vec::new(); let repos_opt = get_github_app_installed_repos(&access_token).await; diff --git a/vibi-dpu/src/db/github/auth.rs b/vibi-dpu/src/db/github/auth.rs index 434053a6..933018f8 100644 --- a/vibi-dpu/src/db/github/auth.rs +++ b/vibi-dpu/src/db/github/auth.rs @@ -1,5 +1,3 @@ -use std::time::SystemTime; -use std::time::UNIX_EPOCH; use sled::IVec; use crate::db::config::get_db; @@ -7,9 +5,6 @@ use crate::utils::github_auth_info::GithubAuthInfo; pub fn save_github_auth_info_to_db(auth_info: &mut GithubAuthInfo) { let db = get_db(); - let now = SystemTime::now(); - let since_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards"); - auth_info.set_timestamp(since_epoch.as_secs()); println!("auth info = {:?}", &auth_info); let json = serde_json::to_string(&auth_info).expect("Failed to serialize auth info"); // Convert JSON string to bytes diff --git a/vibi-dpu/src/github/auth.rs b/vibi-dpu/src/github/auth.rs index c247f925..911dd792 100644 --- a/vibi-dpu/src/github/auth.rs +++ b/vibi-dpu/src/github/auth.rs @@ -1,3 +1,4 @@ +use chrono::DateTime; use jsonwebtoken::{encode, Header, EncodingKey, Algorithm}; use std::time::{SystemTime, UNIX_EPOCH}; use serde::{Deserialize, Serialize}; @@ -89,27 +90,40 @@ pub async fn fetch_access_token(installation_id: &str) -> Option return None; } let mut response_json = parse_res.expect("Uncaught error in parse_res for AuthInfo"); + response_json.set_installation_id(installation_id); save_github_auth_info_to_db(&mut response_json); return Some(response_json); } pub async fn update_access_token(auth_info: &GithubAuthInfo, clone_url: &str, directory: &str) -> Option { let repo_provider = "github".to_string(); - let app_installation_id = auth_info.installation_id(); - let now = SystemTime::now(); - let now_secs = now.duration_since(UNIX_EPOCH).expect("Time went backwards").as_secs(); + let app_installation_id_opt = auth_info.installation_id().to_owned(); + if app_installation_id_opt.is_none() { + eprintln!("[update_access_token] app_installation_id empty"); + return None; + } + let app_installation_id = app_installation_id_opt.expect("Empty app_installation_id_opt"); + let now_ts = Utc::now().timestamp(); let expires_at = auth_info.expires_at(); - if expires_at > now_secs { - eprintln!("Not yet expired, expires_at = {}, now_secs = {}", expires_at, now_secs); + let expires_at_dt_res = DateTime::parse_from_rfc3339(expires_at); + if expires_at_dt_res.is_err() { + let e = expires_at_dt_res.expect_err("No error in expires_at_dt_res"); + eprintln!("[update_access_token] Unable to parse expires_at to datetime: {:?}", e); + return None; + } + let expires_at_dt = expires_at_dt_res.expect("Uncaught error in expires_at_dt_res"); + let expires_at_ts = expires_at_dt.timestamp(); + if expires_at_ts > now_ts { + eprintln!("Not yet expired, expires_at = {}, now_secs = {}", expires_at, now_ts); return Some(auth_info.to_owned()); } // auth info has expired - println!("github auth info expired, expires_at = {}, now_secs = {}", expires_at, now_secs); + println!("github auth info expired, expires_at = {}, now_secs = {}", expires_at, now_ts); let new_auth_info_opt = fetch_access_token(app_installation_id.as_str()).await; let mut new_auth_info = new_auth_info_opt.clone() .expect("empty auhtinfo_opt from update_access_token"); println!("New github auth info = {:?}", &new_auth_info); - let access_token = new_auth_info.access_token().to_string(); + let access_token = new_auth_info.token().to_string(); set_git_remote_url(clone_url, directory, &access_token, &repo_provider); save_github_auth_info_to_db(&mut new_auth_info); return new_auth_info_opt; @@ -128,7 +142,7 @@ pub async fn refresh_git_auth(clone_url: &str, directory: &str) -> Option, + token: String, + expires_at: String, + installation_id: Option } impl GithubAuthInfo { // Constructor - pub fn new(access_token: String, installation_id: String, expires_at: u64, timestamp: Option) -> Self { + pub fn new(token: String, expires_at: String, installation_id: Option) -> Self { Self { - access_token, - installation_id, + token, expires_at, - timestamp, + installation_id, } } // Public getter methods - pub fn access_token(&self) -> &String { - &self.access_token - } - - pub fn installation_id(&self) -> &String { - &self.installation_id + pub fn token(&self) -> &String { + &self.token } - pub fn expires_at(&self) -> u64 { - self.expires_at + pub fn expires_at(&self) -> &String { + &self.expires_at } - pub fn timestamp(&self) -> &Option { - &self.timestamp + pub fn installation_id(&self) -> &Option { + &self.installation_id } - // Public setters - pub fn set_timestamp(&mut self, timestamp: u64) { - self.timestamp = Some(timestamp); + pub fn set_installation_id(&mut self, installation_id: &str) { + self.installation_id = Some(installation_id.to_string()); } } \ No newline at end of file