@@ -51,7 +51,7 @@ impl<W: std::io::Write> HeaderContext<W> {
51
51
Some ( "redirect" ) => self . redirect ( & data) . map ( PageContext :: Close ) ,
52
52
Some ( "json" ) => self . json ( & data) . map ( PageContext :: Close ) ,
53
53
Some ( "cookie" ) => self . add_cookie ( & data) . map ( PageContext :: Header ) ,
54
- Some ( "authentication" ) => self . authentication ( & data) ,
54
+ Some ( "authentication" ) => self . authentication ( & data) . await ,
55
55
_ => self . start_body ( data) . await ,
56
56
}
57
57
}
@@ -167,18 +167,17 @@ impl<W: std::io::Write> HeaderContext<W> {
167
167
Ok ( self . response . body ( json_response) )
168
168
}
169
169
170
- fn authentication ( mut self , data : & JsonValue ) -> anyhow:: Result < PageContext < W > > {
171
- use argon2:: Argon2 ;
172
- use password_hash:: PasswordHash ;
170
+ async fn authentication ( mut self , data : & JsonValue ) -> anyhow:: Result < PageContext < W > > {
173
171
let password_hash = get_object_str ( data, "password_hash" ) ;
174
172
let password = get_object_str ( data, "password" ) ;
175
173
if let ( Some ( password) , Some ( password_hash) ) = ( password, password_hash) {
176
- match PasswordHash :: new ( password_hash)
177
- . map_err ( |e| {
178
- anyhow:: anyhow!( "invalid value for the password_hash property: {}" , e)
179
- } ) ?
180
- . verify_password ( & [ & Argon2 :: default ( ) ] , password)
181
- {
174
+ log:: debug!(
175
+ "Authenticating user with password_hash = {:?}" ,
176
+ password_hash
177
+ ) ;
178
+ let verif_result =
179
+ tokio:: task:: block_in_place ( move || verify_password_sync ( password_hash, password) ) ?;
180
+ match verif_result {
182
181
Ok ( ( ) ) => return Ok ( PageContext :: Header ( self ) ) ,
183
182
Err ( e) => log:: info!( "User authentication failed: {}" , e) ,
184
183
}
@@ -219,6 +218,16 @@ impl<W: std::io::Write> HeaderContext<W> {
219
218
}
220
219
}
221
220
221
+ fn verify_password_sync (
222
+ password_hash : & str ,
223
+ password : & str ,
224
+ ) -> Result < Result < ( ) , password_hash:: Error > , anyhow:: Error > {
225
+ let hash = password_hash:: PasswordHash :: new ( password_hash)
226
+ . map_err ( |e| anyhow:: anyhow!( "invalid value for the password_hash property: {}" , e) ) ?;
227
+ let phfs = & [ & argon2:: Argon2 :: default ( ) as & dyn password_hash:: PasswordVerifier ] ;
228
+ Ok ( hash. verify_password ( phfs, password) )
229
+ }
230
+
222
231
fn get_backtrace ( error : & anyhow:: Error ) -> Vec < String > {
223
232
let mut backtrace = vec ! [ ] ;
224
233
let mut source = error. source ( ) ;
0 commit comments