5
5
use crate :: errors:: Error ;
6
6
use crate :: tokio:: io:: AsyncReadExt ;
7
7
use crate :: {
8
+ auth_passthrough:: AuthPassthrough ,
8
9
config:: get_config,
9
- messages:: { error_response, md5_hash_password, write_all, wrong_password, md5_hash_second_pass} ,
10
+ messages:: {
11
+ error_response, md5_hash_password, md5_hash_second_pass, write_all, wrong_password,
12
+ } ,
10
13
pool:: { get_pool, ConnectionPool } ,
11
- auth_passthrough:: AuthPassthrough ,
12
14
} ;
13
15
use bytes:: { BufMut , BytesMut } ;
14
16
use log:: debug;
15
17
16
- async fn refetch_auth_hash < S > ( pool : & ConnectionPool , stream : & mut S , username : & str , pool_name : & str ) -> Result < String , Error >
17
- where S : tokio:: io:: AsyncWrite + std:: marker:: Unpin + std:: marker:: Send
18
+ async fn refetch_auth_hash < S > (
19
+ pool : & ConnectionPool ,
20
+ stream : & mut S ,
21
+ username : & str ,
22
+ pool_name : & str ,
23
+ ) -> Result < String , Error >
24
+ where
25
+ S : tokio:: io:: AsyncWrite + std:: marker:: Unpin + std:: marker:: Send ,
18
26
{
19
27
let address = pool. address ( 0 , 0 ) ;
20
28
if let Some ( apt) = AuthPassthrough :: from_pool_settings ( & pool. settings ) {
@@ -29,7 +37,8 @@ where S: tokio::io::AsyncWrite + std::marker::Unpin + std::marker::Send
29
37
"No password set and auth passthrough failed for database: {}, user: {}" ,
30
38
pool_name, username
31
39
) ,
32
- ) . await ?;
40
+ )
41
+ . await ?;
33
42
34
43
Err ( Error :: ClientError ( format ! (
35
44
"Could not obtain hash for {{ username: {:?}, database: {:?} }}. Auth passthrough not enabled." ,
@@ -174,8 +183,7 @@ impl ClearText {
174
183
"Invalid password {{ username: {:?}, pool_name: {:?}, application_name: {:?} }}" ,
175
184
self . username, self . pool_name, self . application_name
176
185
) ) )
177
- }
178
- else {
186
+ } else {
179
187
validate_pool ( write, pool, & self . username , & self . pool_name ) . await ?;
180
188
181
189
Ok ( None )
@@ -305,7 +313,7 @@ impl Md5 {
305
313
Some ( ref password) => {
306
314
let our_hash = md5_hash_password ( & self . username , password, & self . salt ) ;
307
315
308
- if our_hash != password_hash {
316
+ if our_hash != password_hash {
309
317
wrong_password ( write, & self . username ) . await ?;
310
318
311
319
Err ( Error :: ClientError ( format ! (
@@ -324,15 +332,24 @@ impl Md5 {
324
332
325
333
let hash = match hash {
326
334
Some ( hash) => hash. to_string ( ) ,
327
- None => refetch_auth_hash ( & pool, write, & self . username , & self . pool_name ) . await ?,
335
+ None => {
336
+ refetch_auth_hash ( & pool, write, & self . username , & self . pool_name )
337
+ . await ?
338
+ }
328
339
} ;
329
340
330
341
let our_hash = md5_hash_second_pass ( & hash, & self . salt ) ;
331
342
332
343
// Compare hashes
333
344
if our_hash != password_hash {
334
345
// Server hash maybe changed
335
- let hash = refetch_auth_hash ( & pool, write, & self . username , & self . pool_name ) . await ?;
346
+ let hash = refetch_auth_hash (
347
+ & pool,
348
+ write,
349
+ & self . username ,
350
+ & self . pool_name ,
351
+ )
352
+ . await ?;
336
353
let our_hash = md5_hash_second_pass ( & hash, & self . salt ) ;
337
354
338
355
if our_hash != password_hash {
@@ -345,7 +362,13 @@ impl Md5 {
345
362
} else {
346
363
( * pool. auth_hash . write ( ) ) = Some ( hash) ;
347
364
348
- validate_pool ( write, pool. clone ( ) , & self . username , & self . pool_name ) . await ?;
365
+ validate_pool (
366
+ write,
367
+ pool. clone ( ) ,
368
+ & self . username ,
369
+ & self . pool_name ,
370
+ )
371
+ . await ?;
349
372
350
373
Ok ( ( ) )
351
374
}
0 commit comments