fix(tokens-repository): fix getting and setting recovery token expiration date

This commit is contained in:
Houkime 2022-12-19 17:37:44 +00:00
parent ab70687c61
commit a97705ef25
3 changed files with 35 additions and 3 deletions

View file

@ -69,7 +69,7 @@ class JsonTokensRepository(AbstractTokensRepository):
recovery_key = RecoveryKey( recovery_key = RecoveryKey(
key=tokens_file["recovery_token"].get("token"), key=tokens_file["recovery_token"].get("token"),
created_at=tokens_file["recovery_token"].get("date"), created_at=tokens_file["recovery_token"].get("date"),
expires_at=tokens_file["recovery_token"].get("expitation"), expires_at=tokens_file["recovery_token"].get("expiration"),
uses_left=tokens_file["recovery_token"].get("uses_left"), uses_left=tokens_file["recovery_token"].get("uses_left"),
) )
@ -85,10 +85,13 @@ class JsonTokensRepository(AbstractTokensRepository):
recovery_key = RecoveryKey.generate(expiration, uses_left) recovery_key = RecoveryKey.generate(expiration, uses_left)
with WriteUserData(UserDataFiles.TOKENS) as tokens_file: with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
expiration = recovery_key.expires_at
if expiration is not None:
expiration = expiration.strftime(DATETIME_FORMAT)
tokens_file["recovery_token"] = { tokens_file["recovery_token"] = {
"token": recovery_key.key, "token": recovery_key.key,
"date": recovery_key.created_at.strftime(DATETIME_FORMAT), "date": recovery_key.created_at.strftime(DATETIME_FORMAT),
"expiration": recovery_key.expires_at, "expiration": expiration,
"uses_left": recovery_key.uses_left, "uses_left": recovery_key.uses_left,
} }

View file

@ -2,7 +2,7 @@
# pylint: disable=unused-argument # pylint: disable=unused-argument
# pylint: disable=missing-function-docstring # pylint: disable=missing-function-docstring
from datetime import datetime from datetime import datetime, timedelta
from mnemonic import Mnemonic from mnemonic import Mnemonic
import pytest import pytest
@ -341,6 +341,25 @@ def test_use_mnemonic_not_valid_recovery_key(
) )
def test_use_mnemonic_expired_recovery_key(
some_tokens_repo,
):
repo = some_tokens_repo
expiration = datetime.now() - timedelta(minutes=5)
assert repo.create_recovery_key(uses_left=2, expiration=expiration) is not None
recovery_key = repo.get_recovery_key()
assert recovery_key.expires_at == expiration
assert not repo.is_recovery_key_valid()
with pytest.raises(RecoveryKeyNotFound):
token = repo.use_mnemonic_recovery_key(
mnemonic_phrase=Mnemonic(language="english").to_mnemonic(
bytes.fromhex(recovery_key.key)
),
device_name="newdevice",
)
def test_use_mnemonic_not_mnemonic_recovery_key(some_tokens_repo): def test_use_mnemonic_not_mnemonic_recovery_key(some_tokens_repo):
repo = some_tokens_repo repo = some_tokens_repo
assert repo.create_recovery_key(uses_left=1, expiration=None) is not None assert repo.create_recovery_key(uses_left=1, expiration=None) is not None

10
tests/test_models.py Normal file
View file

@ -0,0 +1,10 @@
import pytest
from datetime import datetime, timedelta
from selfprivacy_api.models.tokens.recovery_key import RecoveryKey
def test_recovery_key_expired():
expiration = datetime.now() - timedelta(minutes=5)
key = RecoveryKey.generate(expiration=expiration, uses_left=2)
assert not key.is_valid()