From fbbe6719b6823435889fe6638c9449fc3ba36f11 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 12 Nov 2024 14:29:27 +0100 Subject: [PATCH] refactor: apply clippy recommendations --- src/controllers/ui/authorize.rs | 9 +++------ src/controllers/ui/login.rs | 2 +- src/database.rs | 2 +- src/main.rs | 8 ++++---- src/middlewares/app_auth.rs | 2 +- src/middlewares/user_auth.rs | 5 ++--- src/renderer.rs | 2 +- src/services/oauth2.rs | 2 +- src/services/session.rs | 11 +++++------ src/utils.rs | 2 +- 10 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/controllers/ui/authorize.rs b/src/controllers/ui/authorize.rs index 66e2ee2..e434a6c 100644 --- a/src/controllers/ui/authorize.rs +++ b/src/controllers/ui/authorize.rs @@ -208,12 +208,9 @@ pub async fn perform_authorize( .bind(authorization.created_at.to_rfc3339_opts(SecondsFormat::Millis, true)) .execute(&app_state.db) .await; - match res { - Err(err) => { - error!("Failed to save authorization in DB. {}", err); - return (StatusCode::INTERNAL_SERVER_ERROR, Html("Internal server error: Failed to process authorization form.")).into_response(); - }, - _ => {} + if let Err(err) = res { + error!("Failed to save authorization in DB. {}", err); + return (StatusCode::INTERNAL_SERVER_ERROR, Html("Internal server error: Failed to process authorization form.")).into_response(); } info!("Created authorization {}", &authorization.id); diff --git a/src/controllers/ui/login.rs b/src/controllers/ui/login.rs index 1a4b57f..886ce5c 100644 --- a/src/controllers/ui/login.rs +++ b/src/controllers/ui/login.rs @@ -98,7 +98,7 @@ pub async fn perform_login( headers.insert("Set-Cookie", HeaderValue::from_str(&jwt_cookie).unwrap()); // TODO: check redirection for arbitrary URL, enforce relative path headers.insert("Location", HeaderValue::from_str( - &query_params.redirect_to.unwrap_or(format!("/me")) + &query_params.redirect_to.unwrap_or("/me".to_string()) ).unwrap()); ( diff --git a/src/database.rs b/src/database.rs index 45ca33a..66077de 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Context, Result}; +use anyhow::{Context, Result}; use sqlx::{sqlite::{SqliteConnectOptions, SqlitePoolOptions}, Pool, Sqlite, ConnectOptions}; use std::str::FromStr; diff --git a/src/main.rs b/src/main.rs index 11cd4ed..cd1836d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,9 +17,9 @@ use log::info; use sqlx::{Pool, Sqlite}; use models::config::{AppSecrets, Config}; -pub const DEFAULT_DB_PATH: &'static str = &"/var/lib/autotasker/autotasker.db"; -pub const DEFAULT_ASSETS_PATH: &'static str = &"/usr/local/lib/autotasker/assets"; -pub const DEFAULT_CONFIG_PATH: &'static str = &"/etc/autotasker/config.yaml"; +pub const DEFAULT_DB_PATH: &str = "/var/lib/autotasker/autotasker.db"; +pub const DEFAULT_ASSETS_PATH: &str = "/usr/local/lib/autotasker/assets"; +pub const DEFAULT_CONFIG_PATH: &str = "/etc/autotasker/config.yaml"; fn get_config(path: String) -> Result { let inp_def_yaml = fs::read_to_string(path) @@ -45,7 +45,7 @@ async fn get_app_context(start_app_config: StartAppConfig) -> Result<(Config, Ap let database_path = &start_app_config.database_path.unwrap_or(DEFAULT_DB_PATH.to_string()); info!("Using database file at {}", database_path); - let pool = prepare_database(&database_path).await.context("Could not prepare db.")?; + let pool = prepare_database(database_path).await.context("Could not prepare db.")?; let config_path = start_app_config.config_path.unwrap_or(DEFAULT_CONFIG_PATH.to_string()); info!("Using config file at {}", &config_path); diff --git a/src/middlewares/app_auth.rs b/src/middlewares/app_auth.rs index 4f50d31..defe63b 100644 --- a/src/middlewares/app_auth.rs +++ b/src/middlewares/app_auth.rs @@ -99,7 +99,7 @@ pub async fn enforce_jwt_auth_middleware( ); } }; - let token_claims: AppUserTokenClaims = match verify_token(&app_state.secrets, &jwt) { + let token_claims: AppUserTokenClaims = match verify_token(&app_state.secrets, jwt) { Ok(val) => val, Err(_e) => { return Err( diff --git a/src/middlewares/user_auth.rs b/src/middlewares/user_auth.rs index 1c11dc3..6004013 100644 --- a/src/middlewares/user_auth.rs +++ b/src/middlewares/user_auth.rs @@ -1,6 +1,5 @@ -use std::collections::HashMap; -use axum::{extract::{OriginalUri, Query, Request, State}, http::{HeaderMap, HeaderValue, StatusCode}, middleware::Next, response::{Html, IntoResponse, Redirect, Response}, Extension}; +use axum::{extract::{OriginalUri, Request, State}, http::StatusCode, middleware::Next, response::{Html, IntoResponse, Redirect, Response}, Extension}; use axum_extra::extract::CookieJar; use crate::{ @@ -24,7 +23,7 @@ pub async fn auth_middleware( return Ok(next.run(req).await) } }; - let token_claims: UserTokenClaims = match verify_token(&app_state.secrets, &jwt) { + let token_claims: UserTokenClaims = match verify_token(&app_state.secrets, jwt) { Ok(val) => val, Err(_e) => { return Err( diff --git a/src/renderer.rs b/src/renderer.rs index 7d35398..f6294c5 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -39,7 +39,7 @@ impl TemplateRenderer { return res } *res.status_mut() = status; - return res; + res } } diff --git a/src/services/oauth2.rs b/src/services/oauth2.rs index 929938c..55b640e 100644 --- a/src/services/oauth2.rs +++ b/src/services/oauth2.rs @@ -6,7 +6,7 @@ use crate::models::{authorization::AuthorizationScope, config::Application}; pub fn verify_redirect_uri(app: &Application, input_redirect_uri: &str) -> bool { app.allowed_redirect_uris .iter() - .find(|uri| **uri == input_redirect_uri).is_some() + .any(|uri| *uri == input_redirect_uri) } pub fn parse_scope(scope_str: &str) -> Result> { diff --git a/src/services/session.rs b/src/services/session.rs index f53fd51..cf55087 100644 --- a/src/services/session.rs +++ b/src/services/session.rs @@ -1,6 +1,5 @@ -use fully_pub::fully_pub; use anyhow::Result; -use serde::{de::DeserializeOwned, Deserialize, Serialize}; +use serde::{de::DeserializeOwned, Serialize}; use jsonwebtoken::{encode, decode, Header, Algorithm, Validation, EncodingKey, DecodingKey}; use crate::models::config::AppSecrets; @@ -10,16 +9,16 @@ pub fn create_token(secrets: &AppSecrets, claims: T) -> String { let token = encode( &Header::default(), &claims, - &EncodingKey::from_secret(&secrets.jwt_secret.as_bytes()) + &EncodingKey::from_secret(secrets.jwt_secret.as_bytes()) ).expect("Create token"); - return token; + token } pub fn verify_token(secrets: &AppSecrets, jwt: &str) -> Result { let token_data = decode::( - &jwt, - &DecodingKey::from_secret(&secrets.jwt_secret.as_bytes()), + jwt, + &DecodingKey::from_secret(secrets.jwt_secret.as_bytes()), &Validation::new(Algorithm::HS256) )?; diff --git a/src/utils.rs b/src/utils.rs index f28b48b..7486772 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -56,7 +56,7 @@ pub fn parse_basic_auth(header_value: &str) -> Result<(String, String)> { let components: Vec<&str> = basic_auth_str.split(':').collect(); Ok(( - components.get(0).ok_or(anyhow!("Expected username in encoded Authorization header value."))?.to_string(), + components.first().ok_or(anyhow!("Expected username in encoded Authorization header value."))?.to_string(), components.get(1).ok_or(anyhow!("Expected password in encoded Authorization header value."))?.to_string() ))