account_factory = $account_factory; $this->api_factory = $api_factory; $this->options_storage = $options_storage; $this->integration_name = $integration_name; } /** * @return API */ private function api() { if ( ! $this->api ) { $this->api = $this->api_factory->create(); } return $this->api; } /** * @return Account */ private function account() { if ( ! $this->account ) { $this->account = $this->account_factory->create(); } return $this->account; } /** * @param string $email * @param string $password * @param string $password_confirmation * * @throws Account_Validation_Exception * @throws Account_Authorization_Exception * @throws Account_Exception */ public function create_account( $email, $password, $password_confirmation ) { $this->account()->createClient( $email, $password, $password_confirmation, 'wordpress' ); $this->login( $email, $password ); } /** * @param string $email * @param string $password * * @throws Account_Validation_Exception * @throws Account_Authorization_Exception * @throws Account_Exception */ public function login( $email, $password ) { $integration = $this->create_integration( $email, $password ); $key = $this->create_key( $email, $password, $integration->getId() ); // Set encryption key $this->options_storage->save_aes_key(); // Save parameters in wp db $this->options_storage->set_twofas_integration_login( $integration->getLogin() ); $this->options_storage->set_twofas_key_token( $key->getToken() ); // Set 2FA globally for WP users $this->options_storage->set_twofas_email( $email ); $this->options_storage->enable_plugin(); $this->options_storage->set_basic_plan(); } /** * @param $email * * @throws Account_Exception * @throws NotFoundException * @throws Account_Validation_Exception * @throws PasswordResetAttemptsRemainingIsReachedException */ public function reset_password( $email ) { $this->account()->resetPassword( $email ); } /** * @return Client * * @throws Account_Exception */ public function get_client() { return $this->account()->getClient(); } /** * @param int $integration_id * * @return Integration * * @throws NotFoundException * @throws Account_Exception */ public function get_integration( $integration_id ) { return $this->account()->getIntegration( $integration_id ); } /** * @param Integration $integration * * @throws Account_Validation_Exception * @throws Account_Exception */ public function update_integration( Integration $integration ) { $this->account()->updateIntegration( $integration ); } /** * @param int $integration_id * * @throws Account_Exception * @throws NotFoundException */ public function delete_integration( $integration_id ) { $this->account()->deleteIntegration( $this->get_integration( $integration_id ) ); } /** * @param Client $client * * @return Card * * @throws Account_Exception * @throws NotFoundException */ public function get_primary_card( Client $client ) { return $this->account()->getPrimaryCard( $client ); } /** * @param int $user_id * * @return IntegrationUser * * @throws API_Validation_Exception * @throws API_Authorization_Exception * @throws API_Exception */ public function create_integration_user( $user_id ) { $integration_user = new IntegrationUser(); $integration_user->setExternalId( (string) $user_id ); $integration_user->setMobileSecret( MobileSecretGenerator::generate() ); $this->api()->addIntegrationUser( $this->options_storage, $integration_user ); return $integration_user; } /** * @param string $user_id * * @return IntegrationUser|null * * @throws API_Authorization_Exception * @throws API_Exception */ public function get_integration_user( $user_id ) { try { return $this->api()->getIntegrationUser( $this->options_storage, $user_id ); } catch ( IntegrationUserNotFoundException $e ) { return null; } } /** * @param int $user_id * * @return IntegrationUser|null * * @throws API_Authorization_Exception * @throws API_Exception */ public function get_integration_user_by_external_id( $user_id ) { try { return $this->api()->getIntegrationUserByExternalId( $this->options_storage, (string) $user_id ); } catch ( IntegrationUserNotFoundException $e ) { return null; } } /** * @param ReadKey $key_storage * @param int $user_id * * @return IntegrationUser|null * * @throws API_Authorization_Exception * @throws API_Exception */ public function get_integration_user_with_empty_key( ReadKey $key_storage, $user_id ) { try { return $this->api()->getIntegrationUserByExternalId( $key_storage, (string) $user_id ); } catch ( IntegrationUserNotFoundException $e ) { return null; } } /** * @param IntegrationUser $integration_user * * @return IntegrationUser * * @throws API_Validation_Exception * @throws API_Authorization_Exception * @throws API_Exception */ public function update_integration_user( IntegrationUser $integration_user ) { return $this->api()->updateIntegrationUser( $this->options_storage, $integration_user ); } /** * @param string $totp_secret * * @return Authentication * * @throws API_Authorization_Exception * @throws ChannelNotActiveException * @throws InvalidDateException * @throws API_Validation_Exception * @throws API_Exception */ public function request_auth_via_totp( $totp_secret ) { return $this->api()->requestAuthViaTotp( $totp_secret ); } /** * @param string $totp_secret * @param string $mobile_secret * @param string $session_id * @param string $browser_version * * @return Authentication * * @throws API_Authorization_Exception * @throws ChannelNotActiveException * @throws InvalidDateException * @throws API_Validation_Exception * @throws API_Exception */ public function request_auth_via_totp_with_mobile_support( $totp_secret, $mobile_secret, $session_id, $browser_version ) { return $this->api()->requestAuthViaTotpWithMobileSupport( $totp_secret, $mobile_secret, $session_id, $browser_version ); } /** * @param string $phone_number * * @return Authentication * * @throws API_Authorization_Exception * @throws AuthenticationLimitationException * @throws ChannelNotActiveException * @throws CountryIsBlockedException * @throws InvalidDateException * @throws InvalidNumberException * @throws NumberLimitationException * @throws PaymentException * @throws API_Validation_Exception * @throws API_Exception */ public function request_auth_via_sms( $phone_number ) { return $this->api()->requestAuthViaSms( $phone_number ); } /** * @param string $phone_number * * @return Authentication * * @throws ChannelNotActiveException * @throws CountryIsBlockedException * @throws InvalidDateException * @throws AuthenticationLimitationException * @throws InvalidNumberException * @throws NumberLimitationException * @throws PaymentException * @throws API_Authorization_Exception * @throws API_Validation_Exception * @throws API_Exception */ public function request_auth_via_call( $phone_number ) { return $this->api()->requestAuthViaCall( $phone_number ); } /** * @param AuthenticationCollection $authentications * @param string $code * * @return Code * * @throws API_Authorization_Exception * @throws API_Validation_Exception * @throws API_Exception */ public function check_code( AuthenticationCollection $authentications, $code ) { return $this->api()->checkCode( $authentications, $code ); } /** * @param IntegrationUser $integration_user * @param AuthenticationCollection $authentications * @param string $code * * @return Code * * @throws API_Authorization_Exception * @throws API_Validation_Exception * @throws API_Exception */ public function check_backup_code( IntegrationUser $integration_user, AuthenticationCollection $authentications, $code ) { return $this->api()->checkBackupCode( $integration_user, $authentications, $code ); } /** * @param string $integration_id * @param string $session_id * @param string $socket_id * * @return array * * @throws API_Authorization_Exception * @throws API_Validation_Exception * @throws API_Exception */ public function authenticate_channel( $integration_id, $session_id, $socket_id ) { return $this->api()->authenticateChannel( $integration_id, $session_id, $socket_id ); } /** * @param string $channel_name * @param int $status_id * @param string $status * * @throws API_Authorization_Exception * @throws API_Validation_Exception * @throws InvalidArgumentException * @throws API_Exception */ public function update_channel_status( $channel_name, $status_id, $status ) { $this->api()->updateChannelStatus( $channel_name, $status_id, $status ); } /** * @param IntegrationUser $integration_user * * @return BackupCodesCollection * * @throws API_Authorization_Exception * @throws API_Exception */ public function regenerate_backup_codes( IntegrationUser $integration_user ) { return $this->api()->regenerateBackupCodes( $integration_user ); } /** * @param Integration $integration * * @return Integration * * @throws Account_Exception * @throws Account_Validation_Exception */ public function reset_integration_encryption_keys( Integration $integration ) { return $this->account()->resetIntegrationEncryptionKeys( $integration ); } /** * @param string $email * @param string $password * * @throws Account_Authorization_Exception * @throws Account_Exception * @throws Account_Validation_Exception */ public function generate_oauth_setup_token( $email, $password ) { $this->account()->generateOAuthSetupToken( $email, $password ); } /** * @param string $email * @param string $password * @param int $integration_id * * @throws Account_Authorization_Exception * @throws Account_Exception * @throws Account_Validation_Exception */ public function generate_integration_specific_token( $email, $password, $integration_id ) { $this->account()->generateIntegrationSpecificToken( $email, $password, $integration_id ); } /** * @param string $email * @param string $password * * @return Integration * * @throws Account_Validation_Exception * @throws Account_Authorization_Exception * @throws Account_Exception */ private function create_integration( $email, $password ) { $this->generate_oauth_setup_token( $email, $password ); return $this->account()->createIntegration( $this->integration_name->get_name() ); } /** * @param string $email * @param string $password * @param int $integration_id * * @return Key * * @throws Account_Validation_Exception * @throws Account_Authorization_Exception * @throws Account_Exception */ private function create_key( $email, $password, $integration_id ) { $this->generate_integration_specific_token( $email, $password, $integration_id ); return $this->account()->createKey( $integration_id, 'WordPress' ); } }