@@ -267,7 +267,7 @@ async fn _sso_login(
267
267
}
268
268
Some ( ( mut user, sso_user) ) => {
269
269
let mut device = get_device ( & data, conn, & user) . await ?;
270
- let twofactor_token = twofactor_auth ( & user, & data, & mut device, ip, client_version, conn) . await ?;
270
+ let twofactor_token = twofactor_auth ( & mut user, & data, & mut device, ip, client_version, conn) . await ?;
271
271
272
272
if user. private_key . is_none ( ) {
273
273
// User was invited a stub was created
@@ -431,7 +431,7 @@ async fn _password_login(
431
431
432
432
let mut device = get_device ( & data, conn, & user) . await ?;
433
433
434
- let twofactor_token = twofactor_auth ( & user, & data, & mut device, ip, client_version, conn) . await ?;
434
+ let twofactor_token = twofactor_auth ( & mut user, & data, & mut device, ip, client_version, conn) . await ?;
435
435
436
436
let auth_tokens = auth:: AuthTokens :: new ( & device, & user, AuthMethod :: Password , data. client_id ) ;
437
437
@@ -658,7 +658,7 @@ async fn get_device(data: &ConnectData, conn: &mut DbConn, user: &User) -> ApiRe
658
658
}
659
659
660
660
async fn twofactor_auth (
661
- user : & User ,
661
+ user : & mut User ,
662
662
data : & ConnectData ,
663
663
device : & mut Device ,
664
664
ip : & ClientIp ,
@@ -723,7 +723,6 @@ async fn twofactor_auth(
723
723
Some ( TwoFactorType :: Email ) => {
724
724
email:: validate_email_code_str ( & user. uuid , twofactor_code, & selected_data?, & ip. ip , conn) . await ?
725
725
}
726
-
727
726
Some ( TwoFactorType :: Remember ) => {
728
727
match device. twofactor_remember {
729
728
Some ( ref code) if !CONFIG . disable_2fa_remember ( ) && ct_eq ( code, twofactor_code) => {
@@ -737,6 +736,22 @@ async fn twofactor_auth(
737
736
}
738
737
}
739
738
}
739
+ Some ( TwoFactorType :: RecoveryCode ) => {
740
+ // Check if recovery code is correct
741
+ if !user. check_valid_recovery_code ( twofactor_code) {
742
+ err ! ( "Recovery code is incorrect. Try again." )
743
+ }
744
+
745
+ // Remove all twofactors from the user
746
+ TwoFactor :: delete_all_by_user ( & user. uuid , conn) . await ?;
747
+ enforce_2fa_policy ( user, & user. uuid , device. atype , & ip. ip , conn) . await ?;
748
+
749
+ log_user_event ( EventType :: UserRecovered2fa as i32 , & user. uuid , device. atype , & ip. ip , conn) . await ;
750
+
751
+ // Remove the recovery code, not needed without twofactors
752
+ user. totp_recover = None ;
753
+ user. save ( conn) . await ?;
754
+ }
740
755
_ => err ! (
741
756
"Invalid two factor provider" ,
742
757
ErrorEvent {
0 commit comments