refactor(tokens-repo): use token repo for graphql use_recovery_api_key

redis/token-repo
Houkime 2022-12-21 10:24:32 +00:00
parent a97705ef25
commit 009a89fa02
1 changed files with 19 additions and 9 deletions

View File

@ -22,10 +22,19 @@ from selfprivacy_api.utils.auth import (
delete_new_device_auth_token,
get_new_device_auth_token,
refresh_token,
use_mnemonic_recoverery_token,
use_new_device_auth_token,
)
from selfprivacy_api.repositories.tokens.json_tokens_repository import (
JsonTokensRepository,
)
from selfprivacy_api.repositories.tokens.exceptions import (
RecoveryKeyNotFound,
InvalidMnemonic,
)
TOKEN_REPO = JsonTokensRepository()
@strawberry.type
class ApiKeyMutationReturn(MutationReturnInterface):
@ -98,20 +107,21 @@ class ApiMutations:
self, input: UseRecoveryKeyInput
) -> DeviceApiTokenMutationReturn:
"""Use recovery key"""
token = use_mnemonic_recoverery_token(input.key, input.deviceName)
if token is None:
try:
token = TOKEN_REPO.use_mnemonic_recovery_key(input.key, input.deviceName)
return DeviceApiTokenMutationReturn(
success=True,
message="Recovery key used",
code=200,
token=token.token,
)
except (RecoveryKeyNotFound, InvalidMnemonic):
return DeviceApiTokenMutationReturn(
success=False,
message="Recovery key not found",
code=404,
token=None,
)
return DeviceApiTokenMutationReturn(
success=True,
message="Recovery key used",
code=200,
token=token,
)
@strawberry.mutation(permission_classes=[IsAuthenticated])
def refresh_device_api_token(self, info: Info) -> DeviceApiTokenMutationReturn: