tsk-41: Adding auth #43

Merged
phoenix merged 17 commits from tsk-41 into main 2025-08-14 23:11:42 +00:00
Showing only changes of commit 62a1f8a003 - Show all commits

View File

@@ -27,7 +27,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
std::process::exit(-1); std::process::exit(-1);
} }
}; };
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
// let mut service_login = auth::get_token().await; // let mut service_login = auth::get_token().await;
loop { 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; tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
if auth::did_token_expire(&app.token).await { if auth::did_token_expire(&app.token).await {
println!("Token did expire");
app.token = auth::get_refresh_token(&app, &app.token).await; 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!("Token refreshed");
println!("Refreshed token: {:?}", app.token); 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 // TODO: For now, focus on making sure the token is valid before you get here
continue; continue;
@@ -104,19 +108,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
mod auth { mod auth {
// TODO: Implement function
pub async fn get_token(app: &crate::config::App) -> Result<icarus_models::login_result::LoginResult, std::io::Error> { 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 client = reqwest::Client::new();
let endpoint = String::from("api/v2/service/login"); let endpoint = String::from("api/v2/service/login");
let api_url = format!("{}/{endpoint}", app.uri); let api_url = format!("{}/{endpoint}", app.uri);
@@ -125,8 +117,6 @@ mod auth {
"passphrase": icarus_envy::environment::get_service_passphrase().await, "passphrase": icarus_envy::environment::get_service_passphrase().await,
}); });
println!("Payload: {payload:?}");
match client.post(api_url).json(&payload) match client.post(api_url).json(&payload)
.send().await { .send().await {
Ok(response) => match response.json::<crate::api::service_token::response::Response>().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())) 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 // TODO: Implement function. Might want to put the functionality within icarus_models
// at some point // at some point
pub async fn did_token_expire(login_result: &icarus_models::login_result::LoginResult) -> bool { 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 // TODO: Implement function