tsk-41: Token expiration check

This commit is contained in:
2025-08-13 17:03:48 -04:00
parent 80c4c7950b
commit 62a1f8a003

View File

@@ -27,7 +27,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
std::process::exit(-1);
}
};
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
// let mut service_login = auth::get_token().await;
loop {
@@ -37,10 +36,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
if auth::did_token_expire(&app.token).await {
println!("Token did expire");
app.token = auth::get_refresh_token(&app, &app.token).await;
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
println!("Token refreshed");
println!("Refreshed token: {:?}", app.token);
} else {
println!("Token did not expire");
}
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
// TODO: For now, focus on making sure the token is valid before you get here
continue;
@@ -104,19 +108,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
mod auth {
// TODO: Implement function
pub async fn get_token(app: &crate::config::App) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
/*
*
) -> Result<reqwest::Response, reqwest::Error> {
let client = reqwest::Client::new();
let endpoint = String::from("api/v2/song/metadata/queue");
let api_url = format!("{}/{endpoint}", app.uri);
client
.get(api_url)
.query(&[("song_queue_id", song_queue_id)])
.send()
*/
let client = reqwest::Client::new();
let endpoint = String::from("api/v2/service/login");
let api_url = format!("{}/{endpoint}", app.uri);
@@ -125,8 +117,6 @@ mod auth {
"passphrase": icarus_envy::environment::get_service_passphrase().await,
});
println!("Payload: {payload:?}");
match client.post(api_url).json(&payload)
.send().await {
Ok(response) => match response.json::<crate::api::service_token::response::Response>().await {
@@ -145,14 +135,15 @@ mod auth {
Err(std::io::Error::other(err.to_string()))
}
}
// icarus_models::login_result::LoginResult::default()
}
// TODO: Implement function. Might want to put the functionality within icarus_models
// at some point
pub async fn did_token_expire(login_result: &icarus_models::login_result::LoginResult) -> bool {
true
let current_time = time::OffsetDateTime::now_utc();
let expire_time = time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap();
current_time > expire_time
// true
}
// TODO: Implement function