def try_premium_at_start( self, given_premium_credentials: Optional[PremiumCredentials], username: str, create_new: bool, sync_approval: Literal['yes', 'no', 'unknown'], sync_database: bool, ) -> Optional[Premium]: """ Check if new user provided api pair or we already got one in the DB Returns the created premium if user's premium credentials were fine. If not it will raise PremiumAuthenticationError. If no credentials were given it returns None """ if given_premium_credentials is not None: assert create_new, 'We should never get here for an already existing account' try: self.premium = premium_create_and_verify(given_premium_credentials) except PremiumAuthenticationError as e: self._abort_new_syncing_premium_user(username=username, original_exception=e) # else, if we got premium data in the DB initialize it and try to sync with the server db_credentials = self.data.db.get_rotkehlchen_premium() if db_credentials: assert not create_new, 'We should never get here for a new account' try: self.premium = premium_create_and_verify(db_credentials) except PremiumAuthenticationError as e: message = ( f'Could not authenticate with the rotkehlchen server with ' f'the API keys found in the Database. Error: {str(e)}' ) log.error(message) raise PremiumAuthenticationError(message) from e if self.premium is None: return None result = self._can_sync_data_from_server(new_account=create_new) if create_new: # if this is a new account, make sure the api keys are properly stored # in the DB if sync_database: try: self._sync_if_allowed(sync_approval, result) except PremiumAuthenticationError as e: self._abort_new_syncing_premium_user(username=username, original_exception=e) self.data.db.set_rotkehlchen_premium(self.premium.credentials) else: self._sync_if_allowed(sync_approval, result) # Success, return premium return self.premium
def _maybe_check_premium_status(self) -> None: """ Validates the premium status of the account and if the credentials are not valid it deactivates the user's premium status. """ now = ts_now() if now - self.last_premium_status_check < PREMIUM_STATUS_CHECK: return db_credentials = self.database.get_rotkehlchen_premium() if db_credentials: try: premium_create_and_verify(db_credentials) except PremiumAuthenticationError as e: message = ( f'Could not authenticate with the rotkehlchen server with ' f'the API keys found in the Database. Error: {str(e)}. Will ' f'deactivate the premium status.') self.msg_aggregator.add_error(message) self.deactivate_premium() self.last_premium_status_check = now
def set_premium_credentials(self, credentials: PremiumCredentials) -> None: """ Sets the premium credentials for Rotki Raises PremiumAuthenticationError if the given key is rejected by the Rotkehlchen server """ log.info('Setting new premium credentials') if self.premium is not None: self.premium.set_credentials(credentials) else: self.premium = premium_create_and_verify(credentials) self.data.db.set_rotkehlchen_premium(credentials)
def set_premium_credentials(self, api_key: ApiKey, api_secret: ApiSecret) -> None: """ Raises IncorrectApiKeyFormat if the given key is not in a proper format Raises AuthenticationError if the given key is rejected by the Rotkehlchen server """ log.info('Setting new premium credentials') if self.premium is not None: self.premium.set_credentials(api_key, api_secret) else: self.premium = premium_create_and_verify(api_key, api_secret) self.data.set_premium_credentials(api_key, api_secret)
def set_premium_credentials(self, credentials: PremiumCredentials) -> None: """ Sets the premium credentials for Rotki Raises AuthenticationError if the given key is rejected by the Rotkehlchen server """ log.info('Setting new premium credentials') if self.premium is not None: # For some reason mypy does not see that self.premium is set self.premium.set_credentials(credentials) # type: ignore else: self.premium = premium_create_and_verify(credentials) self.data.db.set_rotkehlchen_premium(credentials)
def set_premium_credentials(self, credentials: PremiumCredentials) -> None: """ Sets the premium credentials for rotki Raises PremiumAuthenticationError if the given key is rejected by the Rotkehlchen server """ log.info('Setting new premium credentials') if self.premium is not None: self.premium.set_credentials(credentials) else: self.premium = premium_create_and_verify(credentials) self.premium_sync_manager.premium = self.premium self.accountant.activate_premium_status(self.premium) self.chain_manager.activate_premium_status(self.premium) self.data.db.set_rotkehlchen_premium(credentials)
def try_premium_at_start( self, given_premium_credentials: Optional[PremiumCredentials], username: str, create_new: bool, sync_approval: Literal['yes', 'no', 'unknown'], ) -> Optional[Premium]: """ Check if new user provided api pair or we already got one in the DB Returns the created premium if user's premium credentials were fine. If not it will raise PremiumAuthenticationError. If no credentials were given it returns None """ if given_premium_credentials is not None: assert create_new, 'We should never get here for an already existing account' try: self.premium = premium_create_and_verify( given_premium_credentials) except PremiumAuthenticationError as e: log.error('Given API key is invalid') # At this point we are at a new user trying to create an account with # premium API keys and we failed. But a directory was created. Remove it. # But create a backup of it in case something went really wrong # and the directory contained data we did not want to lose shutil.move( self.data.user_data_dir, # type: ignore self.data.data_directory / f'auto_backup_{username}_{ts_now()}', ) raise PremiumAuthenticationError( 'Could not verify keys for the new account. ' '{}'.format(str(e)), ) # else, if we got premium data in the DB initialize it and try to sync with the server db_credentials = self.data.db.get_rotkehlchen_premium() if db_credentials: assert not create_new, 'We should never get here for a new account' try: self.premium = premium_create_and_verify(db_credentials) except PremiumAuthenticationError as e: message = ( f'Could not authenticate with the rotkehlchen server with ' f'the API keys found in the Database. Error: {str(e)}') log.error(message) raise PremiumAuthenticationError(message) if self.premium is None: return None result = self._can_sync_data_from_server(new_account=create_new) if result.can_sync == CanSync.ASK_USER: if sync_approval == 'unknown': log.info('Remote DB is possibly newer. Ask user.') raise RotkehlchenPermissionError(result.message, result.payload) elif sync_approval == 'yes': log.info('User approved data sync from server') if self._sync_data_from_server_and_replace_local()[0]: if create_new: # if we successfully synced data from the server and this is # a new account, make sure the api keys are properly stored # in the DB self.data.db.set_rotkehlchen_premium( self.premium.credentials) else: log.debug('Could sync data from server but user refused') elif result.can_sync == CanSync.YES: log.info('User approved data sync from server') if self._sync_data_from_server_and_replace_local()[0]: if create_new: # if we successfully synced data from the server and this is # a new account, make sure the api keys are properly stored # in the DB self.data.db.set_rotkehlchen_premium( self.premium.credentials) # else result.can_sync was no, so we do nothing # Success, return premium return self.premium
def try_premium_at_start( self, api_key: ApiKey, api_secret: ApiSecret, username: str, create_new: bool, sync_approval: Literal['yes', 'no', 'unknown'], ) -> Premium: """ Check if new user provided api pair or we already got one in the DB Returns the created premium if user's premium credentials were fine. If not it will raise AuthenticationError. """ if api_key != '': assert create_new, 'We should never get here for an already existing account' try: self.premium = premium_create_and_verify(api_key, api_secret) except (IncorrectApiKeyFormat, AuthenticationError) as e: log.error('Given API key is invalid') # At this point we are at a new user trying to create an account with # premium API keys and we failed. But a directory was created. Remove it. # But create a backup of it in case something went really wrong # and the directory contained data we did not want to lose shutil.move( self.data.user_directory, os.path.join( self.data.data_directory, f'auto_backup_{username}_{ts_now()}', ), ) shutil.rmtree(self.data.user_directory) raise AuthenticationError( 'Could not verify keys for the new account. ' '{}'.format(str(e)), ) # else, if we got premium data in the DB initialize it and try to sync with the server premium_credentials = self.data.db.get_rotkehlchen_premium() if premium_credentials: assert not create_new, 'We should never get here for a new account' api_key = premium_credentials[0] api_secret = premium_credentials[1] try: self.premium = premium_create_and_verify(api_key, api_secret) except (IncorrectApiKeyFormat, AuthenticationError) as e: message = ( f'Could not authenticate with the rotkehlchen server with ' f'the API keys found in the Database. Error: {str(e)}') log.error(message) raise AuthenticationError(message) result = self._can_sync_data_from_server(new_account=create_new) if result.can_sync == CanSync.ASK_USER: if sync_approval == 'unknown': log.info('DB data at server newer than local') raise RotkehlchenPermissionError(result.message) elif sync_approval == 'yes': log.info('User approved data sync from server') if self._sync_data_from_server_and_replace_local(): if create_new: # if we successfully synced data from the server and this is # a new account, make sure the api keys are properly stored # in the DB self.data.db.set_rotkehlchen_premium( api_key, api_secret) else: log.debug('Could sync data from server but user refused') elif result.can_sync == CanSync.YES: log.info('User approved data sync from server') if self._sync_data_from_server_and_replace_local(): if create_new: # if we successfully synced data from the server and this is # a new account, make sure the api keys are properly stored # in the DB self.data.db.set_rotkehlchen_premium(api_key, api_secret) # else result.can_sync was no, so we do nothing # Success, return premium return self.premium