mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-27 11:16:35 +00:00
refactor(tokens-repository): move use_mnemonic_new_device_key() to abstract class
This commit is contained in:
parent
2797c6f88f
commit
ca822cdf6f
|
@ -8,6 +8,7 @@ from selfprivacy_api.repositories.tokens.exceptions import (
|
||||||
TokenNotFound,
|
TokenNotFound,
|
||||||
InvalidMnemonic,
|
InvalidMnemonic,
|
||||||
RecoveryKeyNotFound,
|
RecoveryKeyNotFound,
|
||||||
|
NewDeviceKeyNotFound,
|
||||||
)
|
)
|
||||||
from selfprivacy_api.models.tokens.recovery_key import RecoveryKey
|
from selfprivacy_api.models.tokens.recovery_key import RecoveryKey
|
||||||
from selfprivacy_api.models.tokens.new_device_key import NewDeviceKey
|
from selfprivacy_api.models.tokens.new_device_key import NewDeviceKey
|
||||||
|
@ -124,11 +125,21 @@ class AbstractTokensRepository(ABC):
|
||||||
def delete_new_device_key(self) -> None:
|
def delete_new_device_key(self) -> None:
|
||||||
"""Delete the new device key"""
|
"""Delete the new device key"""
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def use_mnemonic_new_device_key(
|
def use_mnemonic_new_device_key(
|
||||||
self, mnemonic_phrase: str, device_name: str
|
self, mnemonic_phrase: str, device_name: str
|
||||||
) -> Token:
|
) -> Token:
|
||||||
"""Use the mnemonic new device key"""
|
"""Use the mnemonic new device key"""
|
||||||
|
new_device_key = self._get_stored_new_device_key()
|
||||||
|
if not new_device_key:
|
||||||
|
raise NewDeviceKeyNotFound
|
||||||
|
|
||||||
|
if not self._assert_mnemonic(new_device_key.key, mnemonic_phrase):
|
||||||
|
raise NewDeviceKeyNotFound("Phrase is not token!")
|
||||||
|
|
||||||
|
new_token = self.create_token(device_name=device_name)
|
||||||
|
self.delete_new_device_key()
|
||||||
|
|
||||||
|
return new_token
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def _store_token(self, new_token: Token):
|
def _store_token(self, new_token: Token):
|
||||||
|
@ -138,6 +149,10 @@ class AbstractTokensRepository(ABC):
|
||||||
def _decrement_recovery_token(self):
|
def _decrement_recovery_token(self):
|
||||||
"""Decrement recovery key use count by one"""
|
"""Decrement recovery key use count by one"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def _get_stored_new_device_key(self) -> Optional[NewDeviceKey]:
|
||||||
|
"""Retrieves new device key that is already stored."""
|
||||||
|
|
||||||
# TODO: find a proper place for it
|
# TODO: find a proper place for it
|
||||||
def _assert_mnemonic(self, hex_key: str, mnemonic_phrase: str):
|
def _assert_mnemonic(self, hex_key: str, mnemonic_phrase: str):
|
||||||
"""Return true if hex string matches the phrase, false otherwise
|
"""Return true if hex string matches the phrase, false otherwise
|
||||||
|
|
|
@ -3,7 +3,6 @@ temporary legacy
|
||||||
"""
|
"""
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from mnemonic import Mnemonic
|
|
||||||
|
|
||||||
from selfprivacy_api.utils import UserDataFiles, WriteUserData, ReadUserData
|
from selfprivacy_api.utils import UserDataFiles, WriteUserData, ReadUserData
|
||||||
from selfprivacy_api.models.tokens.token import Token
|
from selfprivacy_api.models.tokens.token import Token
|
||||||
|
@ -11,8 +10,6 @@ from selfprivacy_api.models.tokens.recovery_key import RecoveryKey
|
||||||
from selfprivacy_api.models.tokens.new_device_key import NewDeviceKey
|
from selfprivacy_api.models.tokens.new_device_key import NewDeviceKey
|
||||||
from selfprivacy_api.repositories.tokens.exceptions import (
|
from selfprivacy_api.repositories.tokens.exceptions import (
|
||||||
TokenNotFound,
|
TokenNotFound,
|
||||||
InvalidMnemonic,
|
|
||||||
NewDeviceKeyNotFound,
|
|
||||||
)
|
)
|
||||||
from selfprivacy_api.repositories.tokens.abstract_tokens_repository import (
|
from selfprivacy_api.repositories.tokens.abstract_tokens_repository import (
|
||||||
AbstractTokensRepository,
|
AbstractTokensRepository,
|
||||||
|
@ -135,19 +132,3 @@ class JsonTokensRepository(AbstractTokensRepository):
|
||||||
expires_at=tokens_file["new_device"]["expiration"],
|
expires_at=tokens_file["new_device"]["expiration"],
|
||||||
)
|
)
|
||||||
return new_device_key
|
return new_device_key
|
||||||
|
|
||||||
def use_mnemonic_new_device_key(
|
|
||||||
self, mnemonic_phrase: str, device_name: str
|
|
||||||
) -> Token:
|
|
||||||
"""Use the mnemonic new device key"""
|
|
||||||
new_device_key = self._get_stored_new_device_key()
|
|
||||||
if not new_device_key:
|
|
||||||
raise NewDeviceKeyNotFound
|
|
||||||
|
|
||||||
if not self._assert_mnemonic(new_device_key.key, mnemonic_phrase):
|
|
||||||
raise NewDeviceKeyNotFound("Phrase is not token!")
|
|
||||||
|
|
||||||
new_token = self.create_token(device_name=device_name)
|
|
||||||
self.delete_new_device_key()
|
|
||||||
|
|
||||||
return new_token
|
|
||||||
|
|
Loading…
Reference in a new issue