Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tr/gh/authinfo fixes #76

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vibi-dpu/src/core/github/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SetupInfo> = Vec::new();
let repos_opt = get_github_app_installed_repos(&access_token).await;
Expand Down
5 changes: 0 additions & 5 deletions vibi-dpu/src/db/github/auth.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use sled::IVec;

use crate::db::config::get_db;
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());
avikalpg marked this conversation as resolved.
Show resolved Hide resolved
println!("auth info = {:?}", &auth_info);
let json = serde_json::to_string(&auth_info).expect("Failed to serialize auth info");
// Convert JSON string to bytes
Expand Down
30 changes: 22 additions & 8 deletions vibi-dpu/src/github/auth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::DateTime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional?
I haven't seen the chrono crate being imported anywhere else.

use jsonwebtoken::{encode, Header, EncodingKey, Algorithm};
use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -89,27 +90,40 @@ pub async fn fetch_access_token(installation_id: &str) -> Option<GithubAuthInfo>
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<GithubAuthInfo> {
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;
Expand All @@ -128,7 +142,7 @@ pub async fn refresh_git_auth(clone_url: &str, directory: &str) -> Option<String
return None;
}
let latest_authinfo = authinfo_opt.expect("Empty authinfo_opt");
let access_token = latest_authinfo.access_token().to_string();
let access_token = latest_authinfo.token().to_string();
return Some(access_token);
}

Expand Down
35 changes: 14 additions & 21 deletions vibi-dpu/src/utils/github_auth_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,35 @@ use serde::Serialize;

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct GithubAuthInfo {
access_token: String,
installation_id: String,
expires_at: u64,
timestamp: Option<u64>,
token: String,
expires_at: String,
installation_id: Option<String>
}

impl GithubAuthInfo {
// Constructor
pub fn new(access_token: String, installation_id: String, expires_at: u64, timestamp: Option<u64>) -> Self {
pub fn new(token: String, expires_at: String, installation_id: Option<String>) -> 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<u64> {
&self.timestamp
pub fn installation_id(&self) -> &Option<String> {
&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());
}
}