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, delete_new_device_auth_token,
get_new_device_auth_token, get_new_device_auth_token,
refresh_token, refresh_token,
use_mnemonic_recoverery_token,
use_new_device_auth_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 @strawberry.type
class ApiKeyMutationReturn(MutationReturnInterface): class ApiKeyMutationReturn(MutationReturnInterface):
@ -98,20 +107,21 @@ class ApiMutations:
self, input: UseRecoveryKeyInput self, input: UseRecoveryKeyInput
) -> DeviceApiTokenMutationReturn: ) -> DeviceApiTokenMutationReturn:
"""Use recovery key""" """Use recovery key"""
token = use_mnemonic_recoverery_token(input.key, input.deviceName) try:
if token is None: 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( return DeviceApiTokenMutationReturn(
success=False, success=False,
message="Recovery key not found", message="Recovery key not found",
code=404, code=404,
token=None, token=None,
) )
return DeviceApiTokenMutationReturn(
success=True,
message="Recovery key used",
code=200,
token=token,
)
@strawberry.mutation(permission_classes=[IsAuthenticated]) @strawberry.mutation(permission_classes=[IsAuthenticated])
def refresh_device_api_token(self, info: Info) -> DeviceApiTokenMutationReturn: def refresh_device_api_token(self, info: Info) -> DeviceApiTokenMutationReturn: