Skip to content

Commit

Permalink
feat: implement AuthError trait for custom error handling in authenti…
Browse files Browse the repository at this point in the history
…cation module
  • Loading branch information
RWDai committed Jan 6, 2025
1 parent 224e1d8 commit e429cc1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
11 changes: 11 additions & 0 deletions backend/supports/auth/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use tardis::basic::error::TardisError;

pub trait AuthError {
// 410: The request signature is incorrect.
fn signature_error(msg: &str, locale_code: &str) -> TardisError {
TardisError::custom("410", msg, locale_code)
}
}

impl AuthError for TardisError {}

1 change: 1 addition & 0 deletions backend/supports/auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod auth_initializer;
pub mod dto;
pub mod helper;
pub mod serv;
mod error;
6 changes: 3 additions & 3 deletions backend/supports/auth/src/serv/auth_crypto_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use lazy_static::lazy_static;
use crate::{
auth_config::AuthConfig,
auth_constants::DOMAIN_CODE,
dto::auth_crypto_dto::{AuthEncryptReq, AuthEncryptResp},
dto::auth_crypto_dto::{AuthEncryptReq, AuthEncryptResp}, error::AuthError,
};

lazy_static! {
Expand Down Expand Up @@ -94,7 +94,7 @@ pub async fn decrypt_req(
"[Auth] input_keys have four key,and body:{body},input_sm3_digest:{input_sm3_digest},sm3(body):{}",
TardisFuns::crypto.digest.sm3(body)?
);
return Err(TardisError::bad_request("[Auth] Encrypted request: body digest error.", "401-auth-req-crypto-error"));
return Err(TardisError::signature_error("[Auth] Encrypted request: body digest error.", "401-auth-req-crypto-error"));
}

let data = TardisFuns::crypto
Expand All @@ -121,7 +121,7 @@ pub async fn decrypt_req(
"[Auth] input_keys have three key,and body:{body},input_sm3_digest:{input_sm3_digest},sm3(body):{}",
TardisFuns::crypto.digest.sm3(body)?
);
return Err(TardisError::bad_request("[Auth] Encrypted request: body digest error.", "401-auth-req-crypto-error"));
return Err(TardisError::signature_error("[Auth] Encrypted request: body digest error.", "401-auth-req-crypto-error"));
}

let data = TardisFuns::crypto
Expand Down

0 comments on commit e429cc1

Please sign in to comment.