authentication_storage = $storage->get_authentication_storage(); $this->trusted_devices_storage = $storage->get_trusted_devices_storage(); $this->api_wrapper = $api_wrapper; $this->qr_code_message = $qr_code_message; $this->qr_code_generator = $qr_code_generator; $this->request = $request; $this->session = $session; $this->legacy_mode_checker = $legacy_mode_checker; $this->integration_user = $integration_user; } /** * @param WP_Error|WP_User $user * * @return bool */ public function supports( $user ) { if ( ! $this->is_wp_user_set() ) { return false; } if ( ! $this->legacy_mode_checker->totp_is_obligatory_or_legacy_mode_is_active() && ! $this->session->exists( Authenticate_Filter::LOGIN_CONFIGURATION_IN_PROGRESS_KEY ) ) { return false; } if ( ! $this->is_wp_user( $user ) && $this->request->is_login_action_equal_to( Login_Action::TOTP_RESET ) ) { return ! $this->user_storage->is_totp_enabled() && $this->user_storage->is_totp_configured(); } return ! $this->user_storage->is_totp_configured(); } /** * @param WP_Error|WP_User $user * * @return bool|JSON_Response|Redirect_Response|View_Response * * @throws TokenNotFoundException * @throws User_Not_Found_Exception * @throws API_EXception */ protected function handle( $user ) { $totp_secret = TotpSecretGenerator::generate(); $user_id = $this->get_user_id(); if ( ! $this->integration_user->exists() ) { $integration_user = $this->api_wrapper->create_integration_user( $user_id ); $this->integration_user->set_user( $integration_user ); } else { $this->integration_user->set_totp_secret( null ); $this->api_wrapper->update_integration_user( $this->integration_user->get_user() ); } $this->user_storage->delete_totp_configuration(); $this->trusted_devices_storage->delete_trusted_devices( $user_id ); $this->session->set( Authenticate_Filter::LOGIN_CONFIGURATION_IN_PROGRESS_KEY, '1' ); $message = $this->qr_code_message->create( $totp_secret ); $qr_code = $this->qr_code_generator->generateBase64( $message ); $this->authentication_storage->close_authentication( $this->get_user_id() ); $authentication = $this->api_wrapper->request_auth_via_totp( $totp_secret ); $this->authentication_storage->open_authentication( $this->get_user_id(), $authentication ); return $this->view( 'login/configuration.html.twig', [ 'qr_code' => $qr_code, 'qr_code_message' => $message, 'totp_secret' => $totp_secret, ] ); } }