1
- use domain:: entity:: user;
2
- use domain:: vo:: user:: user_info_vo:: UserInfoVO ;
1
+ use dao:: user:: user_repository:: find_user_by_credentials;
3
2
use domain:: dto:: user:: account_login_dto:: AccountLoginDTO ;
4
- use utils:: response:: Response ;
3
+ use domain:: vo:: user:: user_info_vo:: UserInfoVO ;
4
+ use base64:: engine:: general_purpose:: STANDARD ;
5
+ use base64:: Engine as _;
6
+ use serde_json:: json;
7
+ use md5;
8
+ use jsonwebtoken:: { encode, EncodingKey , Header } ;
9
+ use aes:: Aes128 ;
10
+ use cipher:: { KeyIvInit , BlockDecryptMut } ;
11
+ use cbc:: cipher:: block_padding:: Pkcs7 ;
12
+ use cbc:: Decryptor ;
13
+ use std:: error:: Error ;
14
+ use axum:: http:: StatusCode ;
15
+ use axum:: { Json , Router } ;
16
+ use axum:: response:: { IntoResponse , Response } ;
17
+ use axum:: routing:: post;
18
+
19
+ type Aes128CbcDec = Decryptor < Aes128 > ;
20
+
21
+ const LOGIN_SECURITY_KEY : & [ u8 ] = b"QsCdA/3d8CkxZ6k5c6eA61==" ;
22
+ const IV : & [ u8 ; 16 ] = b"1234567890123456" ; // 请根据实际情况调整 IV
23
+
24
+ #[ derive( serde:: Serialize ) ]
25
+ struct Claims {
26
+ sub : String ,
27
+ exp : usize ,
28
+ }
29
+
30
+ #[ derive( Debug ) ]
31
+ pub enum AuthError {
32
+ WrongCredentials ,
33
+ MissingCredentials ,
34
+ TokenCreation ,
35
+ InvalidToken ,
36
+ }
37
+
38
+ impl IntoResponse for AuthError {
39
+ fn into_response ( self ) -> Response {
40
+ let ( status, error_message) = match self {
41
+ AuthError :: WrongCredentials => ( StatusCode :: UNAUTHORIZED , "Wrong credentials" ) ,
42
+ AuthError :: MissingCredentials => ( StatusCode :: BAD_REQUEST , "Missing credentials" ) ,
43
+ AuthError :: TokenCreation => ( StatusCode :: INTERNAL_SERVER_ERROR , "Token creation error" ) ,
44
+ AuthError :: InvalidToken => ( StatusCode :: BAD_REQUEST , "Invalid token" ) ,
45
+ } ;
46
+ let body = Json ( json ! ( {
47
+ "msg" : error_message,
48
+ } ) ) ;
49
+ ( status, body) . into_response ( )
50
+ }
51
+ }
5
52
6
53
pub struct UserService ;
7
54
@@ -10,7 +57,77 @@ impl UserService {
10
57
UserService
11
58
}
12
59
13
- // pub fn user_login(&self, accountLoginDto: AccountLoginDTO) -> Result<Response<UserInfoVO>, Box<dyn std::error::Error>> {
14
- //
15
- // }
60
+ pub async fn user_login ( & self , account_login_dto : AccountLoginDTO ) -> Result < Json < UserInfoVO > , AuthError > {
61
+ // Check if username or password is empty
62
+ if account_login_dto. username . is_empty ( ) || account_login_dto. password . is_empty ( ) {
63
+ return Err ( AuthError :: MissingCredentials ) ;
64
+ }
65
+
66
+ // Decrypt the password
67
+ // let encrypted_password = match STANDARD.decode(&account_login_dto.password) {
68
+ // Ok(pwd) => pwd,
69
+ // Err(_) => {
70
+ // return Err(AuthError::WrongCredentials);
71
+ // }
72
+ // };
73
+ //
74
+ // let cipher = match Aes128CbcDec::new_from_slices(LOGIN_SECURITY_KEY, IV) {
75
+ // Ok(cipher) => cipher,
76
+ // Err(_) => {
77
+ // return Err(AuthError::TokenCreation);
78
+ // }
79
+ // };
80
+
81
+ // let mut buffer = encrypted_password.to_vec();
82
+ // let decrypted_password = match cipher.decrypt_padded_mut::<Pkcs7>(&mut buffer) {
83
+ // Ok(pwd) => pwd,
84
+ // Err(_) => {
85
+ // return Err(AuthError::MissingCredentials);
86
+ // }
87
+ // };
88
+ //
89
+ // let decrypted_password_str = match String::from_utf8(decrypted_password.to_vec()) {
90
+ // Ok(pwd) => pwd,
91
+ // Err(_) => {
92
+ // return Err(AuthError::MissingCredentials);
93
+ // }
94
+ // };
95
+
96
+ // Get user by username and password
97
+ // let hashed_password = format!("{:x}", md5::compute(decrypted_password_str));
98
+ match find_user_by_credentials ( & account_login_dto. username , & * account_login_dto. password ) . await {
99
+ Ok ( Some ( user) ) => {
100
+ let token = match create_token ( & account_login_dto. username ) {
101
+ Ok ( token) => token,
102
+ Err ( _) => {
103
+ return Err ( AuthError :: MissingCredentials ) ;
104
+ }
105
+ } ;
106
+ let user_info = UserInfoVO {
107
+ id : user. id ,
108
+ name : user. name . clone ( ) ,
109
+ position : None ,
110
+ email : user. email . clone ( ) ,
111
+ phone_number : None ,
112
+ description : None ,
113
+ user_name : None ,
114
+ avatar : None ,
115
+ system_language : None ,
116
+ token : Some ( token) ,
117
+ expire : Some ( 1694757956 ) ,
118
+ } ;
119
+ Ok ( Json ( user_info) )
120
+ }
121
+ _ => Err ( AuthError :: MissingCredentials )
122
+ }
123
+ }
124
+ }
125
+
126
+ fn create_token ( username : & str ) -> Result < String , Box < dyn Error > > {
127
+ let my_claims = Claims {
128
+ sub : username. to_owned ( ) ,
129
+ exp : 10000000000 ,
130
+ } ;
131
+ let token = encode ( & Header :: default ( ) , & my_claims, & EncodingKey :: from_secret ( "secret" . as_ref ( ) ) ) ?;
132
+ Ok ( token)
16
133
}
0 commit comments