refactor: fix rp issues
- add raise errors - fix names confusion - add use_mnemonic_new_device_key()pull/18/head
parent
4a09d360ac
commit
80a3750d92
|
@ -66,7 +66,9 @@ class AbstractTokensRepository(ABC):
|
||||||
"""Create the recovery key"""
|
"""Create the recovery key"""
|
||||||
|
|
||||||
@abstractmethod
|
@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"""
|
"""Use the mnemonic recovery key and create a new token with the given name"""
|
||||||
|
|
||||||
def is_recovery_key_valid(self) -> bool:
|
def is_recovery_key_valid(self) -> bool:
|
||||||
|
@ -85,5 +87,7 @@ class AbstractTokensRepository(ABC):
|
||||||
"""Delete the new device key"""
|
"""Delete the new device key"""
|
||||||
|
|
||||||
@abstractmethod
|
@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"""
|
"""Use the mnemonic new device key"""
|
||||||
|
|
|
@ -1,2 +1,18 @@
|
||||||
class TokenNotFoundError(Exception):
|
class TokenNotFoundError(Exception):
|
||||||
"""Token not found!"""
|
"""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 ???"""
|
||||||
|
|
|
@ -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.token import Token
|
||||||
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
|
||||||
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 (
|
from selfprivacy_api.repositories.tokens.abstract_tokens_repository import (
|
||||||
AbstractTokensRepository,
|
AbstractTokensRepository,
|
||||||
)
|
)
|
||||||
|
@ -81,7 +87,7 @@ class JsonTokensRepository(AbstractTokensRepository):
|
||||||
if userdata_token["token"] == input_token:
|
if userdata_token["token"] == input_token:
|
||||||
tokens_file["tokens"].remove(
|
tokens_file["tokens"].remove(
|
||||||
userdata_token
|
userdata_token
|
||||||
) # Allah, i pray it works
|
) # Naiji, i pray it works
|
||||||
|
|
||||||
def refresh_token(self, input_token: Token) -> Token:
|
def refresh_token(self, input_token: Token) -> Token:
|
||||||
"""Change the token field of the existing token"""
|
"""Change the token field of the existing token"""
|
||||||
|
@ -133,45 +139,45 @@ class JsonTokensRepository(AbstractTokensRepository):
|
||||||
|
|
||||||
return recovery_key
|
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"""
|
"""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:
|
if recovery_key is None:
|
||||||
return None
|
raise RecoveryKeyNotFoundError("Recovery key is None!")
|
||||||
|
|
||||||
if not recovery_key.is_valid():
|
if not recovery_key.is_valid():
|
||||||
return None
|
raise RecoveryKeyIsNotValidError("Recovery key is not valid!")
|
||||||
|
|
||||||
if recovery_key is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
recovery_token = bytes.fromhex(recovery_key.key)
|
recovery_token = bytes.fromhex(recovery_key.key)
|
||||||
|
|
||||||
if not Mnemonic(language="english").check(mnemonic_phrase):
|
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)
|
phrase_bytes = Mnemonic(language="english").to_entropy(mnemonic_phrase)
|
||||||
if phrase_bytes != recovery_token:
|
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:
|
with WriteUserData(UserDataFiles.TOKENS) as tokens:
|
||||||
tokens["tokens"].append(
|
tokens["tokens"].append(
|
||||||
{
|
{
|
||||||
"token": new_recovery_key.key,
|
"token": new_token.token,
|
||||||
# "name": new_recovery_key.name, what???? there is no name
|
"name": new_token.device_name,
|
||||||
"date": str(datetime.now()),
|
"date": new_token.created_at,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if "recovery_token" in tokens:
|
if "recovery_token" in tokens:
|
||||||
if (
|
if (
|
||||||
"uses_left" in tokens["recovery_token"]
|
"uses_left" in tokens["recovery_token"]
|
||||||
and tokens["recovery_token"]["uses_left"] is not None
|
and tokens["recovery_token"]["uses_left"] is not None
|
||||||
):
|
):
|
||||||
tokens["recovery_token"]["uses_left"] -= 1
|
tokens["recovery_token"]["uses_left"] -= 1
|
||||||
return new_recovery_key
|
return new_token
|
||||||
|
|
||||||
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"""
|
||||||
|
@ -192,6 +198,26 @@ class JsonTokensRepository(AbstractTokensRepository):
|
||||||
if "new_device" in tokens_file:
|
if "new_device" in tokens_file:
|
||||||
del tokens_file["new_device"]
|
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"""
|
"""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
|
||||||
|
|
Loading…
Reference in New Issue