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 std::i64;
use anyhow::Result;
use argon2::{
Argon2, PasswordHash, PasswordVerifier,
password_hash::{PasswordHasher, SaltString, rand_core::OsRng},
};
use axum::{extract::State, http::StatusCode};
use axum::http::StatusCode;
use chrono::{Duration, Utc};
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 argon2 = Argon2::default();
match argon2.verify_password(password.as_bytes(), &parsed_hash) {
Ok(_) => return Ok(true),
Err(password_hash::Error::Password) => return Ok(false),
Ok(_) => Ok(true),
Err(password_hash::Error::Password) => Ok(false),
Err(e) => Err(e),
}
}
@@ -37,8 +36,8 @@ pub fn gen_jwt(username: String) -> Result<String, StatusCode> {
let secret: String = "verysafestring".to_string();
let now = Utc::now();
let expire: chrono::TimeDelta = Duration::hours(24);
let exp = (now + expire).timestamp() as i64;
let iat = now.timestamp() as i64;
let exp = (now + expire).timestamp();
let iat = now.timestamp();
let claim = AuthClaims { iat, exp, username };
encode(
@@ -48,7 +47,7 @@ pub fn gen_jwt(username: String) -> Result<String, StatusCode> {
)
.map_err(|e| {
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| {
error!(error = %e, "verify jwt failed");
return StatusCode::INTERNAL_SERVER_ERROR;
StatusCode::INTERNAL_SERVER_ERROR
});
result
}

View File

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

View File

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

View File

@@ -71,13 +71,13 @@ impl TryFrom<NewUser> for InternalNewUser {
type Error = UserConversionError;
fn try_from(value: NewUser) -> Result<Self, Self::Error> {
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();
Ok(Self {
uuid: uuid,
uuid,
username: value.username,
email: value.email,
password_hash: password_hash,
password_hash,
first_name: value.first_name,
last_name: value.last_name,
permissions: value.permissions.clone(),
@@ -108,12 +108,12 @@ impl Display for UserConversionError {
impl InternalUser {
pub fn attach_permissions(&mut self, permissions: UserPermissions) {
self.permissions = UserPermissions::from(permissions);
self.permissions = permissions;
}
}
impl User {
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 sqlx::{prelude::FromRow, types::Json};
use uuid::Uuid;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum UserActions {
@@ -10,6 +9,7 @@ pub enum UserActions {
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Default)]
pub struct UserPermissions {
pub root: bool,
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 {
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");
match perms {
Some(val) => return Ok(Some(UserPermissions::from(val))),
None => return Ok(None),
Some(val) => Ok(Some(UserPermissions::from(val))),
None => Ok(None),
}
}

View File

@@ -2,14 +2,13 @@ use std::{collections::HashMap, sync::Arc};
use anyhow::{Ok, Result};
use axum::http::Method;
use mineguard::instance::InstanceHandle;
use rustymine_daemon::{
config::AppCfg,
domain::user_prems::UserActions,
router,
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_VERSION: &str = env!("CARGO_PKG_VERSION");

View File

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

View File

@@ -4,12 +4,10 @@ pub mod user_routes;
use axum::{
Json, Router,
http::StatusCode,
middleware::from_fn_with_state,
routing::{get, post},
};
use serde_json::{Value, json};
use std::sync::Arc;
use tower::{Layer, ServiceBuilder};
use crate::prelude::*;
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::{
core,
domain::{
user::{InternalNewUser, NewUser},
user_prems::UserActions,
},
domain::user::NewUser,
prelude::*,
};
@@ -41,8 +38,8 @@ impl AppState {
info!("database ready after connect and migrate");
Self {
db_pool: db_pool,
config: config,
db_pool,
config,
}
}
}