pull/18/head
def 2022-11-15 16:21:18 +04:00
parent a0963c261c
commit c2f18e9439
2 changed files with 26 additions and 16 deletions

View File

@ -99,7 +99,9 @@ class JsonTokensRepository(AbstractTokensRepository):
if userdata_token["name"] == input_token.device_name:
userdata_token["token"] = new_token.token
userdata_token["data"] = new_token.created_at
userdata_token["date"] = (
new_token.created_at.strftime("%Y-%m-%d %H:%M:%S.%f"),
)
return new_token
@ -128,12 +130,12 @@ class JsonTokensRepository(AbstractTokensRepository):
) -> RecoveryKey:
"""Create the recovery key"""
recovery_key = RecoveryKey.generate(expiration=expiration, uses_left=uses_left)
recovery_key = RecoveryKey.generate(expiration, uses_left)
with ReadUserData(UserDataFiles.TOKENS) as tokens_file:
tokens_file["recovery_key"] = {
with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
tokens_file["recovery_token"] = {
"token": recovery_key.key,
"date": recovery_key.created_at,
"date": recovery_key.created_at.strftime("%Y-%m-%d %H:%M:%S.%f"),
"expiration": recovery_key.expires_at,
"uses_left": recovery_key.uses_left,
}
@ -159,7 +161,7 @@ class JsonTokensRepository(AbstractTokensRepository):
phrase_bytes = Mnemonic(language="english").to_entropy(mnemonic_phrase)
if phrase_bytes != recovery_token:
raise RecoveryTokenError("Phrase is not (?) recovery token")
raise RecoveryTokenError("Phrase is not recovery token")
new_token = Token.generate(device_name=device_name)
@ -168,7 +170,7 @@ class JsonTokensRepository(AbstractTokensRepository):
{
"token": new_token.token,
"name": new_token.device_name,
"date": new_token.created_at,
"date": new_token.created_at.strftime("%Y-%m-%d %H:%M:%S.%f"),
}
)
@ -187,8 +189,10 @@ class JsonTokensRepository(AbstractTokensRepository):
with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
tokens_file["new_device"] = {
"token": new_device_key.key,
"data": new_device_key.created_at,
"expiration": new_device_key.expires_at,
"data": new_device_key.created_at.strftime("%Y-%m-%d %H:%M:%S.%f"),
"expiration": new_device_key.expires_at.strftime(
"%Y-%m-%d %H:%M:%S.%f"
),
}
return new_device_key
@ -206,15 +210,21 @@ class JsonTokensRepository(AbstractTokensRepository):
self, mnemonic_phrase: str, device_name: str
) -> Token:
"""Use the mnemonic new device key"""
new_device_key = NewDeviceKey.generate()
new_device_key = self.get_new_device_key()
if new_device_key is None:
raise TokenNotFoundError("New device key not found!")
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!")
if bytes(phrase_bytes) != bytes(
token
): # idk why, но оно не робит, хотя оригинальную логику я сохранил
raise TokenNotFoundError("Phrase is not token!")
new_token = Token.generate(device_name=device_name)
with WriteUserData(UserDataFiles.TOKENS) as tokens:

View File

@ -245,10 +245,10 @@ def test_create_recovery_key(tokens, mock_recovery_key_generate, datadir):
assert repo.create_recovery_key(uses_left=1, expiration=None) is not None
assert read_json(datadir / "tokens.json")["recovery_token"] == {
"token": "889bf49c1d3199d71a2e704718772bd53a422020334db051",
"date": "2022-11-11T11:48:54.228038",
"date": "2022-07-15 17:41:31.675698",
"expiration": None,
"uses_left": 1,
} # проблемы, файл не изменяется. не представляю причину
}
def test_get_new_device_key(tokens, mock_new_device_key_generate, datadir):
@ -256,7 +256,7 @@ def test_get_new_device_key(tokens, mock_new_device_key_generate, datadir):
assert repo.get_new_device_key() is not None
assert read_json(datadir / "tokens.json")["new_device"] == {
"data": "2022-07-15 17:41:31.675698",
"date": "2022-07-15 17:41:31.675698",
"expiration": "2022-07-15 17:41:31.675698",
"token": "43478d05b35e4781598acd76e33832bb",
}