@@ -165,27 +165,46 @@ async fn _password_login(
165165 // Set the user_uuid here to be passed back used for event logging.
166166 * user_uuid = Some ( user. uuid . clone ( ) ) ;
167167
168- // Check password
169- let password = data. password . as_ref ( ) . unwrap ( ) ;
170- if let Some ( auth_request_uuid) = data. auth_request . clone ( ) {
171- if let Some ( auth_request) = AuthRequest :: find_by_uuid ( auth_request_uuid. as_str ( ) , conn) . await {
172- if !auth_request. check_access_code ( password) {
173- err ! (
174- "Username or access code is incorrect. Try again" ,
175- format!( "IP: {}. Username: {}." , ip. ip, username) ,
176- ErrorEvent {
177- event: EventType :: UserFailedLogIn ,
178- }
179- )
168+ // Check if the user is disabled
169+ if !user. enabled {
170+ err ! (
171+ "This user has been disabled" ,
172+ format!( "IP: {}. Username: {}." , ip. ip, username) ,
173+ ErrorEvent {
174+ event: EventType :: UserFailedLogIn
180175 }
181- } else {
176+ )
177+ }
178+
179+ let password = data. password . as_ref ( ) . unwrap ( ) ;
180+
181+ // If we get an auth request, we don't check the user's password, but the access code of the auth request
182+ if let Some ( ref auth_request_uuid) = data. auth_request {
183+ let Some ( auth_request) = AuthRequest :: find_by_uuid ( auth_request_uuid. as_str ( ) , conn) . await else {
182184 err ! (
183185 "Auth request not found. Try again." ,
184186 format!( "IP: {}. Username: {}." , ip. ip, username) ,
185187 ErrorEvent {
186188 event: EventType :: UserFailedLogIn ,
187189 }
188190 )
191+ } ;
192+
193+ // Delete the request after we used it
194+ auth_request. delete ( conn) . await ?;
195+
196+ if auth_request. user_uuid != user. uuid
197+ || !auth_request. approved . unwrap_or ( false )
198+ || ip. ip . to_string ( ) != auth_request. request_ip
199+ || !auth_request. check_access_code ( password)
200+ {
201+ err ! (
202+ "Username or access code is incorrect. Try again" ,
203+ format!( "IP: {}. Username: {}." , ip. ip, username) ,
204+ ErrorEvent {
205+ event: EventType :: UserFailedLogIn ,
206+ }
207+ )
189208 }
190209 } else if !user. check_valid_password ( password) {
191210 err ! (
@@ -197,8 +216,8 @@ async fn _password_login(
197216 )
198217 }
199218
200- // Change the KDF Iterations
201- if user. password_iterations != CONFIG . password_iterations ( ) {
219+ // Change the KDF Iterations (only when not logging in with an auth request)
220+ if data . auth_request . is_none ( ) && user. password_iterations != CONFIG . password_iterations ( ) {
202221 user. password_iterations = CONFIG . password_iterations ( ) ;
203222 user. set_password ( password, None , false , None ) ;
204223
@@ -207,17 +226,6 @@ async fn _password_login(
207226 }
208227 }
209228
210- // Check if the user is disabled
211- if !user. enabled {
212- err ! (
213- "This user has been disabled" ,
214- format!( "IP: {}. Username: {}." , ip. ip, username) ,
215- ErrorEvent {
216- event: EventType :: UserFailedLogIn
217- }
218- )
219- }
220-
221229 let now = Utc :: now ( ) . naive_utc ( ) ;
222230
223231 if user. verified_at . is_none ( ) && CONFIG . mail_enabled ( ) && CONFIG . signups_verify ( ) {
0 commit comments