refactor(tokens-repo): break out _store_new_device_key()

This commit is contained in:
Houkime 2022-12-26 12:31:09 +00:00
parent 29723b9f3a
commit c037a12f4d
3 changed files with 10 additions and 11 deletions

View file

@ -118,9 +118,15 @@ class AbstractTokensRepository(ABC):
return False return False
return recovery_key.is_valid() return recovery_key.is_valid()
@abstractmethod
def get_new_device_key(self) -> NewDeviceKey: def get_new_device_key(self) -> NewDeviceKey:
"""Creates and returns the new device key""" """Creates and returns the new device key"""
new_device_key = NewDeviceKey.generate()
self._store_new_device_key(new_device_key)
return new_device_key
def _store_new_device_key(self, new_device_key: NewDeviceKey) -> None:
"""Store new device key directly"""
@abstractmethod @abstractmethod
def delete_new_device_key(self) -> None: def delete_new_device_key(self) -> None:

View file

@ -104,10 +104,7 @@ class JsonTokensRepository(AbstractTokensRepository):
if tokens["recovery_token"]["uses_left"] is not None: if tokens["recovery_token"]["uses_left"] is not None:
tokens["recovery_token"]["uses_left"] -= 1 tokens["recovery_token"]["uses_left"] -= 1
def get_new_device_key(self) -> NewDeviceKey: def _store_new_device_key(self, new_device_key: NewDeviceKey) -> None:
"""Creates and returns the new device key"""
new_device_key = NewDeviceKey.generate()
with WriteUserData(UserDataFiles.TOKENS) as tokens_file: with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
tokens_file["new_device"] = { tokens_file["new_device"] = {
"token": new_device_key.key, "token": new_device_key.key,
@ -115,8 +112,6 @@ class JsonTokensRepository(AbstractTokensRepository):
"expiration": new_device_key.expires_at.strftime(DATETIME_FORMAT), "expiration": new_device_key.expires_at.strftime(DATETIME_FORMAT),
} }
return new_device_key
def delete_new_device_key(self) -> None: def delete_new_device_key(self) -> None:
"""Delete the new device key""" """Delete the new device key"""
with WriteUserData(UserDataFiles.TOKENS) as tokens_file: with WriteUserData(UserDataFiles.TOKENS) as tokens_file:

View file

@ -68,11 +68,9 @@ class RedisTokensRepository(AbstractTokensRepository):
self._store_model_as_hash(RECOVERY_KEY_REDIS_KEY, recovery_key) self._store_model_as_hash(RECOVERY_KEY_REDIS_KEY, recovery_key)
return recovery_key return recovery_key
def get_new_device_key(self) -> NewDeviceKey: def _store_new_device_key(self, new_device_key: NewDeviceKey) -> None:
"""Creates and returns the new device key""" """Store new device key directly"""
new_device_key = NewDeviceKey.generate()
self._store_model_as_hash(NEW_DEVICE_KEY_REDIS_KEY, new_device_key) self._store_model_as_hash(NEW_DEVICE_KEY_REDIS_KEY, new_device_key)
return new_device_key
def delete_new_device_key(self) -> None: def delete_new_device_key(self) -> None:
"""Delete the new device key""" """Delete the new device key"""