Cargo fmt and clippy fix

This commit is contained in:
2025-12-05 23:49:31 +01:00
parent c1fee82ba9
commit 9bccfcf7c3
10 changed files with 39 additions and 67 deletions

View File

@@ -1,12 +1,11 @@
use crate::prelude::*; use crate::prelude::*;
use std::i64;
use anyhow::Result; use anyhow::Result;
use argon2::{ use argon2::{
Argon2, PasswordHash, PasswordVerifier, Argon2, PasswordHash, PasswordVerifier,
password_hash::{PasswordHasher, SaltString, rand_core::OsRng}, password_hash::{PasswordHasher, SaltString, rand_core::OsRng},
}; };
use axum::{extract::State, http::StatusCode}; use axum::http::StatusCode;
use chrono::{Duration, Utc}; use chrono::{Duration, Utc};
use jsonwebtoken::{DecodingKey, EncodingKey, Header, TokenData, Validation, decode, encode}; use jsonwebtoken::{DecodingKey, EncodingKey, Header, TokenData, Validation, decode, encode};
@@ -26,8 +25,8 @@ pub fn verify_password(password: &str, password_hash: &str) -> Result<bool, pass
let parsed_hash = PasswordHash::new(password_hash)?; let parsed_hash = PasswordHash::new(password_hash)?;
let argon2 = Argon2::default(); let argon2 = Argon2::default();
match argon2.verify_password(password.as_bytes(), &parsed_hash) { match argon2.verify_password(password.as_bytes(), &parsed_hash) {
Ok(_) => return Ok(true), Ok(_) => Ok(true),
Err(password_hash::Error::Password) => return Ok(false), Err(password_hash::Error::Password) => Ok(false),
Err(e) => Err(e), Err(e) => Err(e),
} }
} }
@@ -37,8 +36,8 @@ pub fn gen_jwt(username: String) -> Result<String, StatusCode> {
let secret: String = "verysafestring".to_string(); let secret: String = "verysafestring".to_string();
let now = Utc::now(); let now = Utc::now();
let expire: chrono::TimeDelta = Duration::hours(24); let expire: chrono::TimeDelta = Duration::hours(24);
let exp = (now + expire).timestamp() as i64; let exp = (now + expire).timestamp();
let iat = now.timestamp() as i64; let iat = now.timestamp();
let claim = AuthClaims { iat, exp, username }; let claim = AuthClaims { iat, exp, username };
encode( encode(
@@ -48,7 +47,7 @@ pub fn gen_jwt(username: String) -> Result<String, StatusCode> {
) )
.map_err(|e| { .map_err(|e| {
error!(error = %e, username = claim.username, "create jwt failed"); error!(error = %e, username = claim.username, "create jwt failed");
return StatusCode::INTERNAL_SERVER_ERROR; StatusCode::INTERNAL_SERVER_ERROR
}) })
} }
@@ -61,7 +60,7 @@ pub fn verify_jwt(token: String) -> Result<TokenData<AuthClaims>, StatusCode> {
) )
.map_err(|e| { .map_err(|e| {
error!(error = %e, "verify jwt failed"); error!(error = %e, "verify jwt failed");
return StatusCode::INTERNAL_SERVER_ERROR; StatusCode::INTERNAL_SERVER_ERROR
}); });
result result
} }

View File

@@ -1,10 +1,7 @@
use axum::http::Method; use axum::http::Method;
use std::collections::HashMap; use std::collections::HashMap;
use crate::domain::{ use crate::domain::user_prems::{UserActions, UserPermissions};
user::User,
user_prems::{UserActions, UserPermissions},
};
#[derive(Debug, Hash, Clone, PartialEq, Eq)] #[derive(Debug, Hash, Clone, PartialEq, Eq)]
pub struct RouteKey { pub struct RouteKey {
@@ -66,12 +63,8 @@ impl AppCfg {
None => return false, None => return false,
}; };
if req_perms.root == true { if req_perms.root {
if user_perms.root == true { return user_perms.root
return true;
} else {
return false;
}
} }
req_perms req_perms

View File

@@ -1,12 +1,12 @@
use crate::{ use crate::{
auth::{gen_jwt, verify_password}, auth::{gen_jwt, verify_password},
domain::{api::LoginData, user::InternalUser, user_prems::UserPermissions}, domain::{api::LoginData, user::InternalUser},
infra::db, infra::db,
prelude::*, prelude::*,
}; };
use std::sync::Arc; use std::sync::Arc;
use axum::{extract::State, http::StatusCode}; use axum::http::StatusCode;
use uuid::Uuid; use uuid::Uuid;
use validator::Validate; use validator::Validate;
@@ -27,7 +27,7 @@ pub async fn login(
.await .await
.map_err(|e| { .map_err(|e| {
error!(error = %e, username = login_data.username.as_str(), "fetch user during login failed"); error!(error = %e, username = login_data.username.as_str(), "fetch user during login failed");
return StatusCode::INTERNAL_SERVER_ERROR; StatusCode::INTERNAL_SERVER_ERROR
})?; })?;
let user = match user { let user = match user {
Some(value) => value, Some(value) => value,
@@ -36,7 +36,7 @@ pub async fn login(
let verify = verify_password(&login_data.password, &user.password_hash).map_err(|e| { let verify = verify_password(&login_data.password, &user.password_hash).map_err(|e| {
error!(error = %e, username = login_data.username.as_str(), "verify password hash failed"); error!(error = %e, username = login_data.username.as_str(), "verify password hash failed");
return StatusCode::INTERNAL_SERVER_ERROR; StatusCode::INTERNAL_SERVER_ERROR
})?; })?;
if !verify { if !verify {
@@ -73,7 +73,7 @@ pub async fn create(state: Arc<AppState>, new_user: NewUser) -> Result<User, Sta
let perms = db::perms::create( let perms = db::perms::create(
&state.db_pool, &state.db_pool,
created_user.uuid.clone(), created_user.uuid,
internal.permissions, internal.permissions,
) )
.await .await
@@ -151,12 +151,9 @@ pub async fn get_by_uuid(state: Arc<AppState>, uuid: Uuid) -> anyhow::Result<Opt
.await .await
.context("failed to fetch user permissions")?; .context("failed to fetch user permissions")?;
match perms { if let Some(perms) = perms {
Some(perms) => {
user.attach_permissions(perms); user.attach_permissions(perms);
} }
None => {}
}
Ok(Some(user)) Ok(Some(user))
} }
@@ -215,12 +212,9 @@ pub async fn get_by_username(
.await .await
.context("failed to fetch user permissions")?; .context("failed to fetch user permissions")?;
match perms { if let Some(perms) = perms {
Some(perms) => {
user.attach_permissions(perms); user.attach_permissions(perms);
} }
None => {}
}
Ok(Some(user)) Ok(Some(user))
} }

View File

@@ -71,13 +71,13 @@ impl TryFrom<NewUser> for InternalNewUser {
type Error = UserConversionError; type Error = UserConversionError;
fn try_from(value: NewUser) -> Result<Self, Self::Error> { fn try_from(value: NewUser) -> Result<Self, Self::Error> {
let password_hash = let password_hash =
auth::hash_password(&value.password).map_err(|e| UserConversionError::HashFailed(e))?; auth::hash_password(&value.password).map_err(UserConversionError::HashFailed)?;
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
Ok(Self { Ok(Self {
uuid: uuid, uuid,
username: value.username, username: value.username,
email: value.email, email: value.email,
password_hash: password_hash, password_hash,
first_name: value.first_name, first_name: value.first_name,
last_name: value.last_name, last_name: value.last_name,
permissions: value.permissions.clone(), permissions: value.permissions.clone(),
@@ -108,12 +108,12 @@ impl Display for UserConversionError {
impl InternalUser { impl InternalUser {
pub fn attach_permissions(&mut self, permissions: UserPermissions) { pub fn attach_permissions(&mut self, permissions: UserPermissions) {
self.permissions = UserPermissions::from(permissions); self.permissions = permissions;
} }
} }
impl User { impl User {
pub fn attach_permissions(&mut self, permissions: UserPermissions) { pub fn attach_permissions(&mut self, permissions: UserPermissions) {
self.permissions = UserPermissions::from(permissions); self.permissions = permissions;
} }
} }

View File

@@ -2,7 +2,6 @@ use std::collections::HashSet;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{prelude::FromRow, types::Json}; use sqlx::{prelude::FromRow, types::Json};
use uuid::Uuid;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum UserActions { pub enum UserActions {
@@ -10,6 +9,7 @@ pub enum UserActions {
} }
#[derive(Debug, Clone, Deserialize, Serialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Default)]
pub struct UserPermissions { pub struct UserPermissions {
pub root: bool, pub root: bool,
pub permissions: HashSet<UserActions>, pub permissions: HashSet<UserActions>,
@@ -39,14 +39,6 @@ impl From<UserPermissionsRow> for UserPermissions {
} }
} }
impl Default for UserPermissions {
fn default() -> Self {
Self {
root: false,
permissions: HashSet::new(),
}
}
}
impl UserPermissions { impl UserPermissions {
pub fn root() -> Self { pub fn root() -> Self {

View File

@@ -46,8 +46,8 @@ pub async fn get_by_uuid(pool: &PgPool, uuid: Uuid) -> Result<Option<UserPermiss
debug!(user_uuid = %uuid, "fetch user permissions by uuid completed"); debug!(user_uuid = %uuid, "fetch user permissions by uuid completed");
match perms { match perms {
Some(val) => return Ok(Some(UserPermissions::from(val))), Some(val) => Ok(Some(UserPermissions::from(val))),
None => return Ok(None), None => Ok(None),
} }
} }

View File

@@ -2,14 +2,13 @@ use std::{collections::HashMap, sync::Arc};
use anyhow::{Ok, Result}; use anyhow::{Ok, Result};
use axum::http::Method; use axum::http::Method;
use mineguard::instance::InstanceHandle;
use rustymine_daemon::{ use rustymine_daemon::{
config::AppCfg, config::AppCfg,
domain::user_prems::UserActions, domain::user_prems::UserActions,
router, router,
state::{AppState, check_root}, state::{AppState, check_root},
}; };
use tracing::{Level, debug, info}; use tracing::{Level, info};
pub const APP_NAME: &str = env!("CARGO_PKG_NAME"); pub const APP_NAME: &str = env!("CARGO_PKG_NAME");
pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION");

View File

@@ -5,7 +5,7 @@ use axum::{
Extension, Extension,
extract::{MatchedPath, Request, State}, extract::{MatchedPath, Request, State},
http::{self, Method, StatusCode, header::AUTHORIZATION}, http::{self, Method, StatusCode, header::AUTHORIZATION},
middleware::{Next, from_fn_with_state}, middleware::Next,
response::Response, response::Response,
}; };
@@ -13,7 +13,7 @@ use axum_extra::extract::CookieJar;
use tower_http::cors::{Any, CorsLayer}; use tower_http::cors::{Any, CorsLayer};
use tracing::debug; use tracing::debug;
use crate::{auth::verify_jwt, infra::db, state::AppState}; use crate::{auth::verify_jwt, state::AppState};
pub async fn auth( pub async fn auth(
State(state): State<Arc<AppState>>, State(state): State<Arc<AppState>>,
@@ -109,7 +109,7 @@ pub fn cors() -> CorsLayer {
pub async fn permissions( pub async fn permissions(
State(state): State<Arc<AppState>>, State(state): State<Arc<AppState>>,
Extension(user): Extension<InternalUser>, Extension(user): Extension<InternalUser>,
mut req: Request, req: Request,
next: Next, next: Next,
) -> Result<Response, StatusCode> { ) -> Result<Response, StatusCode> {
let request_method = req.method().clone(); let request_method = req.method().clone();
@@ -117,7 +117,7 @@ pub async fn permissions(
debug!(method = ?request_method, path = request_path, "permissions request started"); debug!(method = ?request_method, path = request_path, "permissions request started");
debug!("Calling user {}", user.username.clone()); debug!("Calling user {}", user.username.clone());
if user.permissions.root == true { if user.permissions.root {
return Ok(next.run(req).await); return Ok(next.run(req).await);
} }
@@ -133,7 +133,7 @@ pub async fn permissions(
.config .config
.route_allows(&method, path.as_str(), user.permissions.clone()) .route_allows(&method, path.as_str(), user.permissions.clone())
{ {
true => return Ok(next.run(req).await), true => Ok(next.run(req).await),
false => return Err(StatusCode::UNAUTHORIZED), false => Err(StatusCode::UNAUTHORIZED),
}; }
} }

View File

@@ -4,12 +4,10 @@ pub mod user_routes;
use axum::{ use axum::{
Json, Router, Json, Router,
http::StatusCode, http::StatusCode,
middleware::from_fn_with_state,
routing::{get, post}, routing::{get, post},
}; };
use serde_json::{Value, json}; use serde_json::{Value, json};
use std::sync::Arc; use std::sync::Arc;
use tower::{Layer, ServiceBuilder};
use crate::prelude::*; use crate::prelude::*;
use crate::state::AppState; use crate::state::AppState;

View File

@@ -1,11 +1,8 @@
use std::{collections::HashMap, process::exit, sync::Arc}; use std::{process::exit, sync::Arc};
use crate::{ use crate::{
core, core,
domain::{ domain::user::NewUser,
user::{InternalNewUser, NewUser},
user_prems::UserActions,
},
prelude::*, prelude::*,
}; };
@@ -41,8 +38,8 @@ impl AppState {
info!("database ready after connect and migrate"); info!("database ready after connect and migrate");
Self { Self {
db_pool: db_pool, db_pool,
config: config, config,
} }
} }
} }