tsk-41: Adding auth #43

Merged
phoenix merged 17 commits from tsk-41 into main 2025-08-14 23:11:42 +00:00
2 changed files with 59 additions and 11 deletions
Showing only changes of commit b5f7943938 - Show all commits

View File

@@ -2,7 +2,16 @@ pub async fn fetch_next_queue_item(app: &crate::config::App) -> Result<reqwest::
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let fetch_endpoint = String::from("api/v2/song/queue/next"); let fetch_endpoint = String::from("api/v2/song/queue/next");
let api_url = format!("{}/{fetch_endpoint}", app.uri); let api_url = format!("{}/{fetch_endpoint}", app.uri);
client.get(api_url).send().await let (key, header) = auth_header(app).await;
println!("Header: {header:?}");
client.get(api_url).header(key, header)
.send().await
}
pub async fn auth_header(app: &crate::config::App) -> (reqwest::header::HeaderName, reqwest::header::HeaderValue) {
let bearer = format!("Bearer {}", app.token.token);
let header_value = reqwest::header::HeaderValue::from_str(&bearer).unwrap();
(reqwest::header::AUTHORIZATION, header_value)
} }
pub mod parsing { pub mod parsing {
@@ -143,3 +152,13 @@ pub mod service_token {
} }
} }
} }
pub mod refresh_token {
pub mod response {
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Response {
pub message: String,
pub data: Vec<icarus_models::login_result::LoginResult>,
}
}
}

View File

@@ -33,11 +33,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Base URL: {:?}", app.uri); println!("Base URL: {:?}", app.uri);
println!("Token: {:?}", app.token); println!("Token: {:?}", app.token);
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"); println!("Token did expire");
app.token = auth::get_refresh_token(&app, &app.token).await; app.token = match auth::get_refresh_token(&app, &app.token).await {
Ok(login_result) => {
login_result
}
Err(err) => {
eprintln!("Error: {err:?}");
continue;
}
};
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).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);
@@ -46,14 +53,14 @@ 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;
// TODO: For now, focus on making sure the token is valid before you get here
continue;
match is_queue_empty(&app).await { match is_queue_empty(&app).await {
Ok((empty, song_queue_item)) => { Ok((empty, song_queue_item)) => {
if !empty { if !empty {
println!("Queue is not empty"); println!("Queue is not empty");
println!("SongQueueItem: {song_queue_item:?}"); println!("SongQueueItem: {song_queue_item:?}");
// TODO: For now, focus on making sure the token is valid before you get here
continue;
let song_queue_id = song_queue_item.data[0].id; let song_queue_id = song_queue_item.data[0].id;
let user_id = song_queue_item.data[0].user_id; let user_id = song_queue_item.data[0].user_id;
@@ -137,18 +144,40 @@ mod auth {
} }
} }
// TODO: Implement function. Might want to put the functionality within icarus_models // TODO: 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 {
let current_time = time::OffsetDateTime::now_utc(); let current_time = time::OffsetDateTime::now_utc();
let expire_time = time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap(); let expire_time = time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap();
current_time > expire_time current_time > expire_time
// true
} }
// TODO: Implement function // TODO: Refactor this to only have one function parameter
pub async fn get_refresh_token(app: &crate::config::App, login_result: &icarus_models::login_result::LoginResult) -> icarus_models::login_result::LoginResult { pub async fn get_refresh_token(app: &crate::config::App, login_result: &icarus_models::login_result::LoginResult) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
icarus_models::login_result::LoginResult::default() let client = reqwest::Client::new();
let endpoint = String::from("api/v2/token/refresh");
let api_url = format!("{}/{endpoint}", app.uri);
let payload = serde_json::json!({
"access_token": login_result.token
});
match client.post(api_url).json(&payload).send().await {
Ok(response) => match response.json::<crate::api::refresh_token::response::Response>().await {
Ok(resp) => {
if resp.data.is_empty() {
Err(std::io::Error::other(String::from("No token returned")))
} else {
Ok(resp.data[0].clone())
}
}
Err(err) => {
Err(std::io::Error::other(err.to_string()))
}
}
Err(err) => {
Err(std::io::Error::other(err.to_string()))
}
}
} }
} }