mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-11 18:39:30 +00:00
refactor(tokens-repository): dissect use_mnemonic_new_device_key()
This commit is contained in:
parent
772c0dfc64
commit
4498003aca
|
@ -123,33 +123,31 @@ class JsonTokensRepository(AbstractTokensRepository):
|
||||||
del tokens_file["new_device"]
|
del tokens_file["new_device"]
|
||||||
return
|
return
|
||||||
|
|
||||||
def use_mnemonic_new_device_key(
|
def _get_stored_new_device_key(self) -> Optional[NewDeviceKey]:
|
||||||
self, mnemonic_phrase: str, device_name: str
|
"""Retrieves new device key that is already stored."""
|
||||||
) -> Token:
|
|
||||||
"""Use the mnemonic new device key"""
|
|
||||||
|
|
||||||
with ReadUserData(UserDataFiles.TOKENS) as tokens_file:
|
with ReadUserData(UserDataFiles.TOKENS) as tokens_file:
|
||||||
if "new_device" not in tokens_file or tokens_file["new_device"] is None:
|
if "new_device" not in tokens_file or tokens_file["new_device"] is None:
|
||||||
raise NewDeviceKeyNotFound("New device key not found")
|
return
|
||||||
|
|
||||||
new_device_key = NewDeviceKey(
|
new_device_key = NewDeviceKey(
|
||||||
key=tokens_file["new_device"]["token"],
|
key=tokens_file["new_device"]["token"],
|
||||||
created_at=tokens_file["new_device"]["date"],
|
created_at=tokens_file["new_device"]["date"],
|
||||||
expires_at=tokens_file["new_device"]["expiration"],
|
expires_at=tokens_file["new_device"]["expiration"],
|
||||||
)
|
)
|
||||||
|
return new_device_key
|
||||||
|
|
||||||
token = bytes.fromhex(new_device_key.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 Mnemonic(language="english").check(mnemonic_phrase):
|
if not self._assert_mnemonic(new_device_key.key, mnemonic_phrase):
|
||||||
raise InvalidMnemonic("Phrase is not mnemonic!")
|
|
||||||
|
|
||||||
phrase_bytes = Mnemonic(language="english").to_entropy(mnemonic_phrase)
|
|
||||||
if bytes(phrase_bytes) != bytes(token):
|
|
||||||
raise NewDeviceKeyNotFound("Phrase is not token!")
|
raise NewDeviceKeyNotFound("Phrase is not token!")
|
||||||
|
|
||||||
new_token = Token.generate(device_name=device_name)
|
new_token = Token.generate(device_name=device_name)
|
||||||
with WriteUserData(UserDataFiles.TOKENS) as tokens:
|
self.delete_new_device_key()
|
||||||
if "new_device" in tokens:
|
|
||||||
del tokens["new_device"]
|
|
||||||
|
|
||||||
return new_token
|
return new_token
|
||||||
|
|
Loading…
Reference in a new issue