refactor: fix rp issues

- add raise errors
- fix names confusion
- add use_mnemonic_new_device_key()
pull/18/head
def 2022-11-03 05:43:15 +04:00
parent 4a09d360ac
commit 80a3750d92
3 changed files with 66 additions and 20 deletions

View File

@ -66,7 +66,9 @@ class AbstractTokensRepository(ABC):
"""Create the recovery key"""
@abstractmethod
def use_mnemonic_recovery_key(self, mnemonic_phrase: str, name: str) -> Token:
def use_mnemonic_recovery_key(
self, mnemonic_phrase: str, device_name: str
) -> Token:
"""Use the mnemonic recovery key and create a new token with the given name"""
def is_recovery_key_valid(self) -> bool:
@ -85,5 +87,7 @@ class AbstractTokensRepository(ABC):
"""Delete the new device key"""
@abstractmethod
def use_mnemonic_new_device_key(self, mnemonic_phrase: str, name: str) -> None:
def use_mnemonic_new_device_key(
self, mnemonic_phrase: str, device_name: str
) -> Token:
"""Use the mnemonic new device key"""

View File

@ -1,2 +1,18 @@
class TokenNotFoundError(Exception):
"""Token not found!"""
class RecoveryKeyNotFoundError(Exception):
"""Recovery key not found!"""
class MnemonicError(Exception):
"""Phrase is not mnemonic!"""
class RecoveryKeyIsNotValidError(Exception):
"""Recovery key is not valid!"""
class RecoveryTokenError(Exception):
"""Error ???"""

View File

@ -8,7 +8,13 @@ from selfprivacy_api.utils import UserDataFiles, WriteUserData, ReadUserData
from selfprivacy_api.models.tokens.token import Token
from selfprivacy_api.models.tokens.recovery_key import RecoveryKey
from selfprivacy_api.models.tokens.new_device_key import NewDeviceKey
from selfprivacy_api.repositories.tokens.exceptions import TokenNotFoundError
from selfprivacy_api.repositories.tokens.exceptions import (
TokenNotFoundError,
RecoveryKeyNotFoundError,
MnemonicError,
RecoveryKeyIsNotValidError,
RecoveryTokenError,
)
from selfprivacy_api.repositories.tokens.abstract_tokens_repository import (
AbstractTokensRepository,
)
@ -81,7 +87,7 @@ class JsonTokensRepository(AbstractTokensRepository):
if userdata_token["token"] == input_token:
tokens_file["tokens"].remove(
userdata_token
) # Allah, i pray it works
) # Naiji, i pray it works
def refresh_token(self, input_token: Token) -> Token:
"""Change the token field of the existing token"""
@ -133,45 +139,45 @@ class JsonTokensRepository(AbstractTokensRepository):
return recovery_key
def use_mnemonic_recovery_key(self, mnemonic_phrase: str, name: str) -> Token:
def use_mnemonic_recovery_key(
self, mnemonic_phrase: str, device_name: str
) -> Token:
"""Use the mnemonic recovery key and create a new token with the given name"""
recovery_key = self.get_recovery_key() # self ?
recovery_key = self.get_recovery_key()
if recovery_key is None:
return None
raise RecoveryKeyNotFoundError("Recovery key is None!")
if not recovery_key.is_valid():
return None
if recovery_key is None:
return None
raise RecoveryKeyIsNotValidError("Recovery key is not valid!")
recovery_token = bytes.fromhex(recovery_key.key)
if not Mnemonic(language="english").check(mnemonic_phrase):
return None
raise MnemonicError("Phrase is not mnemonic!")
phrase_bytes = Mnemonic(language="english").to_entropy(mnemonic_phrase)
if phrase_bytes != recovery_token:
return None
raise RecoveryTokenError("Phrase is not (?) recovery token")
new_recovery_key = RecoveryKey.generate()
new_token = Token.generate(device_name=device_name)
with WriteUserData(UserDataFiles.TOKENS) as tokens:
tokens["tokens"].append(
{
"token": new_recovery_key.key,
# "name": new_recovery_key.name, what???? there is no name
"date": str(datetime.now()),
"token": new_token.token,
"name": new_token.device_name,
"date": new_token.created_at,
}
)
if "recovery_token" in tokens:
if (
"uses_left" in tokens["recovery_token"]
and tokens["recovery_token"]["uses_left"] is not None
):
tokens["recovery_token"]["uses_left"] -= 1
return new_recovery_key
return new_token
def get_new_device_key(self) -> NewDeviceKey:
"""Creates and returns the new device key"""
@ -192,6 +198,26 @@ class JsonTokensRepository(AbstractTokensRepository):
if "new_device" in tokens_file:
del tokens_file["new_device"]
def use_mnemonic_new_device_key(self, mnemonic_phrase: str, name: str) -> None:
def use_mnemonic_new_device_key(
self, mnemonic_phrase: str, device_name: str
) -> Token:
"""Use the mnemonic new device key"""
...
new_device_key = NewDeviceKey.generate()
if new_device_key.key is None:
raise TokenNotFoundError("Device key is None!")
token = bytes.fromhex(new_device_key.key)
if not Mnemonic(language="english").check(mnemonic_phrase):
raise MnemonicError("Phrase is not mnemonic!")
phrase_bytes = Mnemonic(language="english").to_entropy(mnemonic_phrase)
if phrase_bytes != token:
raise MnemonicError("Phrase is not token!")
new_token = Token.generate(device_name=device_name)
with WriteUserData(UserDataFiles.TOKENS) as tokens:
if "new_device" in tokens:
del tokens["new_device"]
return new_token