Code formatting

This commit is contained in:
kdeng00
2025-07-31 23:08:23 -04:00
parent 78be4363ed
commit 30522b3a49
2 changed files with 62 additions and 41 deletions
+8 -11
View File
@@ -2,19 +2,18 @@
// use std::sync::Arc; // use std::sync::Arc;
use axum::{ use axum::{
Json,
http::{Request, StatusCode}, http::{Request, StatusCode},
middleware::Next, middleware::Next,
response::{IntoResponse}, response::IntoResponse,
Json,
}; };
use axum_extra::extract::cookie::CookieJar; use axum_extra::extract::cookie::CookieJar;
use jsonwebtoken::{decode, DecodingKey, Validation}; use jsonwebtoken::{DecodingKey, Validation, decode};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
// use serde_json::{json, Value}; // use serde_json::{json, Value};
use thiserror::Error; use thiserror::Error;
// use time::OffsetDateTime; // use time::OffsetDateTime;
fn deserialize_i64_from_f64<'de, D>(deserializer: D) -> Result<i64, D::Error> fn deserialize_i64_from_f64<'de, D>(deserializer: D) -> Result<i64, D::Error>
where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
@@ -36,19 +35,18 @@ where
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct UserClaims { pub struct UserClaims {
pub iss: String, pub iss: String,
pub aud: String, // Audience pub aud: String, // Audience
pub sub: String, // Subject (user ID) pub sub: String, // Subject (user ID)
#[serde(deserialize_with = "deserialize_i64_from_f64")] #[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")] #[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 azp: String,
// pub gty: String, // pub gty: String,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub roles: Option<Vec<String>>, // Optional roles pub roles: Option<Vec<String>>, // Optional roles
} }
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum JwtError { pub enum JwtError {
#[error("Token creation failed")] #[error("Token creation failed")]
@@ -63,7 +61,6 @@ pub enum JwtError {
InvalidKey, InvalidKey,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct ErrorResponse { pub struct ErrorResponse {
pub status: &'static str, pub status: &'static str,
+54 -30
View File
@@ -50,8 +50,8 @@ pub mod init {
use tower_http::timeout::TimeoutLayer; use tower_http::timeout::TimeoutLayer;
use axum::http::{ use axum::http::{
header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
HeaderValue, Method, HeaderValue, Method,
header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
}; };
pub async fn routes() -> axum::Router { pub async fn routes() -> axum::Router {
@@ -60,8 +60,10 @@ pub mod init {
.route( .route(
crate::callers::endpoints::QUEUESONG, crate::callers::endpoints::QUEUESONG,
post(crate::callers::song::endpoint::queue_song) 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_with_state(app_state.clone(), crate::auth::auth)),
.route_layer(axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>)), .route_layer(axum::middleware::from_fn(
crate::auth::auth::<axum::body::Body>,
)),
) )
.route( .route(
crate::callers::endpoints::QUEUESONG, crate::callers::endpoints::QUEUESONG,
@@ -157,10 +159,10 @@ pub mod init {
crate::db::migrations(&pool).await; crate::db::migrations(&pool).await;
let cors = tower_http::cors::CorsLayer::new() let cors = tower_http::cors::CorsLayer::new()
.allow_origin("http://localhost:3000".parse::<HeaderValue>().unwrap()) .allow_origin("http://localhost:3000".parse::<HeaderValue>().unwrap())
.allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE]) .allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE])
.allow_credentials(true) .allow_credentials(true)
.allow_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE]); .allow_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE]);
routes() routes()
.await .await
@@ -171,7 +173,6 @@ pub mod init {
} }
} }
// TODO: Move elsewhere // TODO: Move elsewhere
fn get_full() -> String { fn get_full() -> String {
get_address() + ":" + &get_port() get_address() + ":" + &get_port()
@@ -292,7 +293,11 @@ mod tests {
} }
pub fn token_fields() -> (String, String, String) { 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<String, josekit::JoseError> { pub async fn test_token() -> Result<String, josekit::JoseError> {
@@ -300,20 +305,14 @@ mod tests {
let (message, issuer, audience) = token_fields(); let (message, issuer, audience) = token_fields();
match icarus_models::token::create_token(&key, &message, &issuer, &audience) { match icarus_models::token::create_token(&key, &message, &issuer, &audience) {
Ok((access_token, _some_time)) => { Ok((access_token, _some_time)) => Ok(access_token),
Ok(access_token) Err(err) => Err(err),
}
Err(err) => {
Err(err)
}
} }
} }
pub async fn bearer_auth() -> String { pub async fn bearer_auth() -> String {
let token = match test_token().await { let token = match test_token().await {
Ok(access_token) => { Ok(access_token) => access_token,
access_token
}
Err(err) => { Err(err) => {
assert!(false, "Error: {err:?}"); assert!(false, "Error: {err:?}");
String::new() String::new()
@@ -331,7 +330,6 @@ mod tests {
let mut form = MultipartForm::default(); let mut form = MultipartForm::default();
let _ = form.add_file("flac", "tests/I/track01.flac"); let _ = form.add_file("flac", "tests/I/track01.flac");
// Create request // Create request
let content_type = form.content_type(); let content_type = form.content_type();
let body = MultipartBody::from(form); let body = MultipartBody::from(form);
@@ -1011,7 +1009,10 @@ mod tests {
.method(axum::http::Method::PATCH) .method(axum::http::Method::PATCH)
.uri(uri) .uri(uri)
.header(axum::http::header::CONTENT_TYPE, content_type) .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)) .body(axum::body::Body::from_stream(body))
.unwrap(), .unwrap(),
) )
@@ -1474,7 +1475,10 @@ mod tests {
.method(axum::http::Method::GET) .method(axum::http::Method::GET)
.uri(uri) .uri(uri)
.header(axum::http::header::CONTENT_TYPE, "image/jpeg") .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()) .body(axum::body::Body::empty())
.unwrap(), .unwrap(),
) )
@@ -1864,7 +1868,10 @@ mod tests {
axum::http::header::CONTENT_TYPE, axum::http::header::CONTENT_TYPE,
"application/json", "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())) .body(axum::body::Body::from(payload.to_string()))
.unwrap(), .unwrap(),
) )
@@ -1938,7 +1945,10 @@ mod tests {
.method(axum::http::Method::GET) .method(axum::http::Method::GET)
.uri(uri) .uri(uri)
.header(axum::http::header::CONTENT_TYPE, "application/json") .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()) .body(axum::body::Body::empty())
.unwrap(), .unwrap(),
) )
@@ -1992,7 +2002,10 @@ mod tests {
.method(axum::http::Method::GET) .method(axum::http::Method::GET)
.uri(uri) .uri(uri)
.header(axum::http::header::CONTENT_TYPE, "application/json") .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()) .body(axum::body::Body::empty())
.unwrap(), .unwrap(),
) )
@@ -2045,7 +2058,6 @@ mod tests {
pub async fn coverart_id() -> Result<uuid::Uuid, uuid::Error> { pub async fn coverart_id() -> Result<uuid::Uuid, uuid::Error> {
uuid::Uuid::parse_str("996122cd-5ae9-4013-9934-60768d3006ed") uuid::Uuid::parse_str("996122cd-5ae9-4013-9934-60768d3006ed")
} }
} }
#[tokio::test] #[tokio::test]
@@ -2079,7 +2091,10 @@ mod tests {
axum::http::Request::builder() axum::http::Request::builder()
.method(axum::http::Method::GET) .method(axum::http::Method::GET)
.uri(&uri) .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()) .body(axum::body::Body::empty())
.unwrap(), .unwrap(),
) )
@@ -2135,7 +2150,10 @@ mod tests {
axum::http::Request::builder() axum::http::Request::builder()
.method(axum::http::Method::GET) .method(axum::http::Method::GET)
.uri(&uri) .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()) .body(axum::body::Body::empty())
.unwrap(), .unwrap(),
) )
@@ -2192,7 +2210,10 @@ mod tests {
axum::http::Request::builder() axum::http::Request::builder()
.method(axum::http::Method::GET) .method(axum::http::Method::GET)
.uri(&uri) .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()) .body(axum::body::Body::empty())
.unwrap(), .unwrap(),
) )
@@ -2328,7 +2349,10 @@ mod tests {
axum::http::Request::builder() axum::http::Request::builder()
.method(axum::http::Method::DELETE) .method(axum::http::Method::DELETE)
.uri(&uri) .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()) .body(axum::body::Body::empty())
.unwrap(), .unwrap(),
) )