1
1
use crate :: errors:: Error ;
2
+ use crate :: pool:: ConnectionPool ;
2
3
use crate :: server:: Server ;
3
4
use log:: debug;
4
5
@@ -78,19 +79,25 @@ impl AuthPassthrough {
78
79
79
80
let user = & address. username ;
80
81
81
- debug ! ( "Connecting to server to obtain auth hashes." ) ;
82
+ debug ! ( "Connecting to server to obtain auth hashes" ) ;
83
+
82
84
let auth_query = self . query . replace ( "$1" , user) ;
85
+
83
86
match Server :: exec_simple_query ( address, & auth_user, & auth_query) . await {
84
87
Ok ( password_data) => {
85
88
if password_data. len ( ) == 2 && password_data. first ( ) . unwrap ( ) == user {
86
- if let Some ( stripped_hash) = password_data. last ( ) . unwrap ( ) . to_string ( ) . strip_prefix ( "md5" ) {
87
- Ok ( stripped_hash. to_string ( ) )
88
- }
89
- else {
90
- Err ( Error :: AuthPassthroughError (
91
- "Obtained hash from auth_query does not seem to be in md5 format." . to_string ( ) ,
92
- ) )
93
- }
89
+ if let Some ( stripped_hash) = password_data
90
+ . last ( )
91
+ . unwrap ( )
92
+ . to_string ( )
93
+ . strip_prefix ( "md5" ) {
94
+ Ok ( stripped_hash. to_string ( ) )
95
+ }
96
+ else {
97
+ Err ( Error :: AuthPassthroughError (
98
+ "Obtained hash from auth_query does not seem to be in md5 format." . to_string ( ) ,
99
+ ) )
100
+ }
94
101
} else {
95
102
Err ( Error :: AuthPassthroughError (
96
103
"Data obtained from query does not follow the scheme 'user','hash'."
@@ -99,10 +106,25 @@ impl AuthPassthrough {
99
106
}
100
107
}
101
108
Err ( err) => {
102
- Err ( Error :: AuthPassthroughError (
103
- format ! ( "Error trying to obtain password from auth_query, ignoring hash for user '{}'. Error: {:?}" ,
104
- user, err) ) )
109
+ Err ( Error :: AuthPassthroughError (
110
+ format ! ( "Error trying to obtain password from auth_query, ignoring hash for user '{}'. Error: {:?}" ,
111
+ user, err) )
112
+ )
105
113
}
106
- }
114
+ }
115
+ }
116
+ }
117
+
118
+ pub async fn refetch_auth_hash ( pool : & ConnectionPool ) -> Result < String , Error > {
119
+ let address = pool. address ( 0 , 0 ) ;
120
+ if let Some ( apt) = AuthPassthrough :: from_pool_settings ( & pool. settings ) {
121
+ let hash = apt. fetch_hash ( address) . await ?;
122
+
123
+ return Ok ( hash) ;
107
124
}
125
+
126
+ Err ( Error :: ClientError ( format ! (
127
+ "Could not obtain hash for {{ username: {:?}, database: {:?} }}. Auth passthrough not enabled." ,
128
+ address. username, address. database
129
+ ) ) )
108
130
}
0 commit comments