request = $request; $this->session = $session; $this->flash = $flash; $this->api_wrapper = $api_wrapper; $this->qr_code_generator = $qr_code_generator; $this->authentication_storage = $storage->get_authentication_storage(); $this->trusted_devices_storage = $storage->get_trusted_devices_storage(); $this->legacy_mode_checker = $legacy_mode_checker; } /** * @param null|WP_Error|WP_User $user * * @return bool */ public function supports( $user ) { if ( $this->is_wp_user( $user ) ) { return false; } try { $this->get_wp_user(); } catch ( User_Not_Found_Exception $e ) { return false; } return $this->legacy_mode_checker->totp_is_obligatory_or_legacy_mode_is_active() && $this->request->is_login_action_equal_to( Login_Action::CONFIGURE ); } /** * @param null|WP_Error|WP_User $user * * @return bool|JSON_Response|Redirection_Response|View_Response */ protected function handle( $user ) { $code = $this->request->post( Authenticate_Filter::TWOFAS_CODE_KEY ); $secret = $this->request->post( Authenticate_Filter::SECRET_FIELD ); if ( empty( $code ) ) { $this->login_support->set_error( Errors::EMPTY_CODE ); return $this->fallback( $user ); } if ( empty( $secret ) ) { $this->login_support->set_error( Errors::EMPTY_PRIVATE_KEY ); return $this->fallback( $user ); } try { $user_id = $this->get_user_id(); $integration_user = $this->get_integration_user(); if ( is_null( $integration_user ) ) { $final_response = $this->json_error( Errors::INTEGRATION_USER_ERROR, 404 ); $this->login_support->set_final_response( $final_response ); return $this->fallback( $user ); } if ( $this->authentication_storage->is_authentication_expired() ) { $final_response = $this->json_error( Errors::AUTHENTICATION_EXPIRED_ERROR, 403 ); $this->login_support->set_final_response( $final_response ); return $this->fallback( $user ); } $result = $this->api_wrapper->check_code( $this->authentication_storage->get_authentications(), $code ); if ( $result->accepted() ) { $integration_user->setTotpSecret( $secret ); $this->api_wrapper->update_integration_user( $integration_user ); $this->user_storage->enable_totp(); $this->user_storage->enable_2fa(); $this->flash->add_message( 'success', 'totp-enabled' ); $this->trusted_devices_storage->delete_trusted_devices( $user_id ); $this->session->log_out_on_other_devices( $user_id ); $response = $this->json( array( 'user_id' => $user_id ), 200 ); $this->login_support->set_final_response( $response ); return $this->fallback( $user ); } elseif ( $result->canRetry() ) { $qr_code_message = $this->request->post( 'qr_code_message' ); $qr_code = $this->qr_code_generator->generateBase64( $qr_code_message ); $final_response = $this->view( 'login/configuration.html.twig', array( 'qr_code' => $qr_code, 'qr_code_message' => $qr_code_message, 'totp_secret' => $secret ) ); $this->login_support->set_final_response( $final_response ); $this->login_support->set_error( Errors::INVALID_CODE ); return $this->fallback( $user ); } else { $this->user_storage->block_user(); $final_response = $this->json_error( Errors::AUTHENTICATION_LIMIT_ERROR, 403 ); $this->login_support->set_final_response( $final_response ); return $this->fallback( $user ); } } catch ( API_Validation_Exception $e ) { $qr_code_message = $this->request->post( 'qr_code_message' ); $qr_code = $this->qr_code_generator->generateBase64( $qr_code_message ); $final_response = $this->view( 'login/configuration.html.twig', array( 'qr_code' => $qr_code, 'qr_code_message' => $qr_code_message, 'totp_secret' => $secret ) ); $this->login_support->set_final_response( $final_response ); $this->login_support->set_error( Errors::VALIDATION_ERROR ); } catch ( Exception $e ) { $this->capture_exception( $e ); } return $this->fallback( $user ); } }