11use argon2:: { password_hash, Argon2 , PasswordHasher , PasswordVerifier } ;
2- use password_hash:: PasswordHashString ;
3- use rand:: distributions :: { Alphanumeric , DistString } ;
2+ use password_hash:: phc :: PasswordHash ;
3+ use rand:: distr :: { Alphanumeric , SampleString } ;
44use sqlx:: { Acquire , Executor , PgTransaction , Postgres } ;
55use std:: sync:: Arc ;
66use uuid:: Uuid ;
@@ -118,7 +118,7 @@ impl AccountsManager {
118118 } )
119119 }
120120
121- async fn hash_password ( & self , password : String ) -> Result < PasswordHashString , GeneralError > {
121+ async fn hash_password ( & self , password : String ) -> Result < PasswordHash , GeneralError > {
122122 let guard = self
123123 . hashing_semaphore
124124 . clone ( )
@@ -129,13 +129,7 @@ impl AccountsManager {
129129 // We transfer ownership to the blocking task and back to ensure Tokio doesn't spawn
130130 // excess threads.
131131 let ( _guard, res) = tokio:: task:: spawn_blocking ( move || {
132- let salt = password_hash:: SaltString :: generate ( rand:: thread_rng ( ) ) ;
133- (
134- guard,
135- Argon2 :: default ( )
136- . hash_password ( password. as_bytes ( ) , & salt)
137- . map ( |hash| hash. serialize ( ) ) ,
138- )
132+ ( guard, Argon2 :: default ( ) . hash_password ( password. as_bytes ( ) ) )
139133 } )
140134 . await ?;
141135
@@ -145,7 +139,7 @@ impl AccountsManager {
145139 async fn verify_password (
146140 & self ,
147141 password : String ,
148- hash : PasswordHashString ,
142+ hash : PasswordHash ,
149143 ) -> Result < ( ) , CreateSessionError > {
150144 let guard = self
151145 . hashing_semaphore
@@ -157,13 +151,13 @@ impl AccountsManager {
157151 let ( _guard, res) = tokio:: task:: spawn_blocking ( move || {
158152 (
159153 guard,
160- Argon2 :: default ( ) . verify_password ( password. as_bytes ( ) , & hash. password_hash ( ) ) ,
154+ Argon2 :: default ( ) . verify_password ( password. as_bytes ( ) , & hash) ,
161155 )
162156 } )
163157 . await
164158 . map_err ( GeneralError :: from) ?;
165159
166- if let Err ( password_hash:: Error :: Password ) = res {
160+ if let Err ( password_hash:: Error :: PasswordInvalid ) = res {
167161 return Err ( CreateSessionError :: InvalidPassword ) ;
168162 }
169163
@@ -189,7 +183,9 @@ impl AccountsManager {
189183 values ($1, $2) \
190184 returning account_id",
191185 email,
192- hash. as_str( ) ,
186+ // However, since arguments don't link back to the target column,
187+ // SQLx doesn't know that `PasswordHash` would be a valid argument here.
188+ hash. to_string( ) ,
193189 )
194190 . fetch_one ( & mut * * txn)
195191 . await
@@ -220,7 +216,7 @@ impl AccountsManager {
220216
221217 // Thanks to `sqlx.toml`:
222218 // * `account_id` maps to `AccountId`
223- // * `password_hash` maps to `Text<PasswordHashString >`
219+ // * `password_hash` maps to `Text<PasswordHash >`
224220 // * `session_token` maps to `SessionToken`
225221 let maybe_account = sqlx:: query!(
226222 // language=PostgreSQL
@@ -279,6 +275,6 @@ impl SessionToken {
279275 const LEN : usize = 32 ;
280276
281277 fn generate ( ) -> Self {
282- SessionToken ( Alphanumeric . sample_string ( & mut rand:: thread_rng ( ) , Self :: LEN ) )
278+ SessionToken ( Alphanumeric . sample_string ( & mut rand:: rng ( ) , Self :: LEN ) )
283279 }
284280}
0 commit comments