diff --git a/src/auth/mod.rs b/src/auth/mod.rs new file mode 100644 index 0000000..164c884 --- /dev/null +++ b/src/auth/mod.rs @@ -0,0 +1,65 @@ +pub async fn get_token( + app: &crate::config::App, +) -> Result { + let client = reqwest::Client::new(); + let endpoint = String::from("api/v2/service/login"); + let api_url = format!("{}/{endpoint}", app.auth_uri); + + let payload = serde_json::json!({ + "passphrase": icarus_envy::environment::get_service_passphrase().await.value, + }); + + match client.post(api_url).json(&payload).send().await { + Ok(response) => match response + .json::() + .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())), + } +} + +// TODO: 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 { + let current_time = time::OffsetDateTime::now_utc(); + let expire_time = + time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap(); + current_time > expire_time +} + +pub async fn get_refresh_token( + app: &crate::config::App, +) -> Result { + let client = reqwest::Client::new(); + let endpoint = String::from("api/v2/token/refresh"); + let api_url = format!("{}/{endpoint}", app.auth_uri); + + let payload = serde_json::json!({ + "access_token": app.token.token + }); + + match client.post(api_url).json(&payload).send().await { + Ok(response) => match response + .json::() + .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())), + } +} diff --git a/src/main.rs b/src/main.rs index 35bc261..debd542 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ pub mod api; +pub mod auth; pub mod config; pub mod util; @@ -98,74 +99,6 @@ async fn main() -> Result<(), Box> { } } -mod auth { - pub async fn get_token( - app: &crate::config::App, - ) -> Result { - let client = reqwest::Client::new(); - let endpoint = String::from("api/v2/service/login"); - let api_url = format!("{}/{endpoint}", app.auth_uri); - - let payload = serde_json::json!({ - "passphrase": icarus_envy::environment::get_service_passphrase().await.value, - }); - - match client.post(api_url).json(&payload).send().await { - Ok(response) => match response - .json::() - .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())), - } - } - - // TODO: 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 { - let current_time = time::OffsetDateTime::now_utc(); - let expire_time = - time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap(); - current_time > expire_time - } - - pub async fn get_refresh_token( - app: &crate::config::App, - ) -> Result { - let client = reqwest::Client::new(); - let endpoint = String::from("api/v2/token/refresh"); - let api_url = format!("{}/{endpoint}", app.auth_uri); - - let payload = serde_json::json!({ - "access_token": app.token.token - }); - - match client.post(api_url).json(&payload).send().await { - Ok(response) => match response - .json::() - .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())), - } - } -} - async fn wipe_data_from_queues( app: &config::App, song_queue_id: &uuid::Uuid,