diff --git a/src/auth.rs b/src/auth.rs index 104182a..f3bf405 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -2,19 +2,18 @@ // use std::sync::Arc; use axum::{ + Json, http::{Request, StatusCode}, middleware::Next, - response::{IntoResponse}, - Json, + response::IntoResponse, }; use axum_extra::extract::cookie::CookieJar; -use jsonwebtoken::{decode, DecodingKey, Validation}; +use jsonwebtoken::{DecodingKey, Validation, decode}; use serde::{Deserialize, Serialize}; // use serde_json::{json, Value}; use thiserror::Error; // use time::OffsetDateTime; - fn deserialize_i64_from_f64<'de, D>(deserializer: D) -> Result where D: serde::Deserializer<'de>, @@ -36,19 +35,18 @@ where #[derive(Debug, Serialize, Deserialize)] pub struct UserClaims { pub iss: String, - pub aud: String, // Audience - pub sub: String, // Subject (user ID) + pub aud: String, // Audience + pub sub: String, // Subject (user ID) #[serde(deserialize_with = "deserialize_i64_from_f64")] - pub exp: i64, // Expiration time (UTC timestamp) + pub exp: i64, // Expiration time (UTC timestamp) #[serde(deserialize_with = "deserialize_i64_from_f64")] - pub iat: i64, // Issued at (UTC timestamp) + pub iat: i64, // Issued at (UTC timestamp) // pub azp: String, // pub gty: String, #[serde(skip_serializing_if = "Option::is_none")] - pub roles: Option>, // Optional roles + pub roles: Option>, // Optional roles } - #[derive(Error, Debug)] pub enum JwtError { #[error("Token creation failed")] @@ -63,7 +61,6 @@ pub enum JwtError { InvalidKey, } - #[derive(Debug, Serialize)] pub struct ErrorResponse { pub status: &'static str, diff --git a/src/main.rs b/src/main.rs index a06dc19..1d08116 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,8 +50,8 @@ pub mod init { use tower_http::timeout::TimeoutLayer; use axum::http::{ - header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE}, HeaderValue, Method, + header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE}, }; pub async fn routes() -> axum::Router { @@ -60,8 +60,10 @@ pub mod init { .route( crate::callers::endpoints::QUEUESONG, post(crate::callers::song::endpoint::queue_song) - // .route_layer(axum::middleware::from_fn_with_state(app_state.clone(), crate::auth::auth)), - .route_layer(axum::middleware::from_fn(crate::auth::auth::)), + // .route_layer(axum::middleware::from_fn_with_state(app_state.clone(), crate::auth::auth)), + .route_layer(axum::middleware::from_fn( + crate::auth::auth::, + )), ) .route( crate::callers::endpoints::QUEUESONG, @@ -157,10 +159,10 @@ pub mod init { crate::db::migrations(&pool).await; let cors = tower_http::cors::CorsLayer::new() - .allow_origin("http://localhost:3000".parse::().unwrap()) - .allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE]) - .allow_credentials(true) - .allow_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE]); + .allow_origin("http://localhost:3000".parse::().unwrap()) + .allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE]) + .allow_credentials(true) + .allow_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE]); routes() .await @@ -171,7 +173,6 @@ pub mod init { } } - // TODO: Move elsewhere fn get_full() -> String { get_address() + ":" + &get_port() @@ -292,7 +293,11 @@ mod tests { } pub fn token_fields() -> (String, String, String) { - (String::from("What a twist!"), String::from("icarus_test"), String::from("icarus")) + ( + String::from("What a twist!"), + String::from("icarus_test"), + String::from("icarus"), + ) } pub async fn test_token() -> Result { @@ -300,20 +305,14 @@ mod tests { let (message, issuer, audience) = token_fields(); match icarus_models::token::create_token(&key, &message, &issuer, &audience) { - Ok((access_token, _some_time)) => { - Ok(access_token) - } - Err(err) => { - Err(err) - } + Ok((access_token, _some_time)) => Ok(access_token), + Err(err) => Err(err), } } pub async fn bearer_auth() -> String { - let token = match test_token().await { - Ok(access_token) => { - access_token - } + let token = match test_token().await { + Ok(access_token) => access_token, Err(err) => { assert!(false, "Error: {err:?}"); String::new() @@ -331,7 +330,6 @@ mod tests { let mut form = MultipartForm::default(); let _ = form.add_file("flac", "tests/I/track01.flac"); - // Create request let content_type = form.content_type(); let body = MultipartBody::from(form); @@ -1011,7 +1009,10 @@ mod tests { .method(axum::http::Method::PATCH) .uri(uri) .header(axum::http::header::CONTENT_TYPE, content_type) - .header(axum::http::header::AUTHORIZATION, bearer_auth().await) + .header( + axum::http::header::AUTHORIZATION, + bearer_auth().await, + ) .body(axum::body::Body::from_stream(body)) .unwrap(), ) @@ -1474,7 +1475,10 @@ mod tests { .method(axum::http::Method::GET) .uri(uri) .header(axum::http::header::CONTENT_TYPE, "image/jpeg") - .header(axum::http::header::AUTHORIZATION, bearer_auth().await) + .header( + axum::http::header::AUTHORIZATION, + bearer_auth().await, + ) .body(axum::body::Body::empty()) .unwrap(), ) @@ -1864,7 +1868,10 @@ mod tests { axum::http::header::CONTENT_TYPE, "application/json", ) - .header(axum::http::header::AUTHORIZATION, bearer_auth().await) + .header( + axum::http::header::AUTHORIZATION, + bearer_auth().await, + ) .body(axum::body::Body::from(payload.to_string())) .unwrap(), ) @@ -1938,7 +1945,10 @@ mod tests { .method(axum::http::Method::GET) .uri(uri) .header(axum::http::header::CONTENT_TYPE, "application/json") - .header(axum::http::header::AUTHORIZATION, super::bearer_auth().await) + .header( + axum::http::header::AUTHORIZATION, + super::bearer_auth().await, + ) .body(axum::body::Body::empty()) .unwrap(), ) @@ -1992,7 +2002,10 @@ mod tests { .method(axum::http::Method::GET) .uri(uri) .header(axum::http::header::CONTENT_TYPE, "application/json") - .header(axum::http::header::AUTHORIZATION, super::bearer_auth().await) + .header( + axum::http::header::AUTHORIZATION, + super::bearer_auth().await, + ) .body(axum::body::Body::empty()) .unwrap(), ) @@ -2045,7 +2058,6 @@ mod tests { pub async fn coverart_id() -> Result { uuid::Uuid::parse_str("996122cd-5ae9-4013-9934-60768d3006ed") } - } #[tokio::test] @@ -2079,7 +2091,10 @@ mod tests { axum::http::Request::builder() .method(axum::http::Method::GET) .uri(&uri) - .header(axum::http::header::AUTHORIZATION, super::bearer_auth().await) + .header( + axum::http::header::AUTHORIZATION, + super::bearer_auth().await, + ) .body(axum::body::Body::empty()) .unwrap(), ) @@ -2135,7 +2150,10 @@ mod tests { axum::http::Request::builder() .method(axum::http::Method::GET) .uri(&uri) - .header(axum::http::header::AUTHORIZATION, super::bearer_auth().await) + .header( + axum::http::header::AUTHORIZATION, + super::bearer_auth().await, + ) .body(axum::body::Body::empty()) .unwrap(), ) @@ -2192,7 +2210,10 @@ mod tests { axum::http::Request::builder() .method(axum::http::Method::GET) .uri(&uri) - .header(axum::http::header::AUTHORIZATION, super::bearer_auth().await) + .header( + axum::http::header::AUTHORIZATION, + super::bearer_auth().await, + ) .body(axum::body::Body::empty()) .unwrap(), ) @@ -2328,7 +2349,10 @@ mod tests { axum::http::Request::builder() .method(axum::http::Method::DELETE) .uri(&uri) - .header(axum::http::header::AUTHORIZATION, super::bearer_auth().await) + .header( + axum::http::header::AUTHORIZATION, + super::bearer_auth().await, + ) .body(axum::body::Body::empty()) .unwrap(), )