From 29723b9f3a1d028bbe10da26b6694d36ce4c6aec Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 26 Dec 2022 10:53:27 +0000 Subject: [PATCH] refactor(tokens-repo): delete use_mnemonic_recoverery_token from auth utils --- selfprivacy_api/actions/api_tokens.py | 22 ++++++++++- .../graphql/mutations/api_mutations.py | 9 +++-- selfprivacy_api/rest/api_auth.py | 6 +-- selfprivacy_api/utils/auth.py | 39 ------------------- 4 files changed, 29 insertions(+), 47 deletions(-) diff --git a/selfprivacy_api/actions/api_tokens.py b/selfprivacy_api/actions/api_tokens.py index b13c13e..394d3d9 100644 --- a/selfprivacy_api/actions/api_tokens.py +++ b/selfprivacy_api/actions/api_tokens.py @@ -7,7 +7,11 @@ from mnemonic import Mnemonic from selfprivacy_api.repositories.tokens.json_tokens_repository import ( JsonTokensRepository, ) -from selfprivacy_api.repositories.tokens.exceptions import TokenNotFound +from selfprivacy_api.repositories.tokens.exceptions import ( + TokenNotFound, + RecoveryKeyNotFound, + InvalidMnemonic, +) TOKEN_REPO = JsonTokensRepository() @@ -112,6 +116,22 @@ def get_new_api_recovery_key( return mnemonic_phrase +def use_mnemonic_recovery_token(mnemonic_phrase, name): + """Use the recovery token by converting the mnemonic word list to a byte array. + If the recovery token if invalid itself, return None + If the binary representation of phrase not matches + the byte array of the recovery token, return None. + If the mnemonic phrase is valid then generate a device token and return it. + Substract 1 from uses_left if it exists. + mnemonic_phrase is a string representation of the mnemonic word list. + """ + try: + token = TOKEN_REPO.use_mnemonic_recovery_key(mnemonic_phrase, name) + return token.token + except (RecoveryKeyNotFound, InvalidMnemonic): + return None + + def delete_new_device_auth_token() -> None: TOKEN_REPO.delete_new_device_key() diff --git a/selfprivacy_api/graphql/mutations/api_mutations.py b/selfprivacy_api/graphql/mutations/api_mutations.py index c2075c3..45961c8 100644 --- a/selfprivacy_api/graphql/mutations/api_mutations.py +++ b/selfprivacy_api/graphql/mutations/api_mutations.py @@ -11,6 +11,7 @@ from selfprivacy_api.actions.api_tokens import ( NotFoundException, delete_api_token, get_new_api_recovery_key, + use_mnemonic_recovery_token, refresh_api_token, delete_new_device_auth_token, get_new_device_auth_token, @@ -107,15 +108,15 @@ class ApiMutations: self, input: UseRecoveryKeyInput ) -> DeviceApiTokenMutationReturn: """Use recovery key""" - try: - token = TOKEN_REPO.use_mnemonic_recovery_key(input.key, input.deviceName) + token = use_mnemonic_recovery_token(input.key, input.deviceName) + if token is not None: return DeviceApiTokenMutationReturn( success=True, message="Recovery key used", code=200, - token=token.token, + token=token, ) - except (RecoveryKeyNotFound, InvalidMnemonic): + else: return DeviceApiTokenMutationReturn( success=False, message="Recovery key not found", diff --git a/selfprivacy_api/rest/api_auth.py b/selfprivacy_api/rest/api_auth.py index a860798..ab96bee 100644 --- a/selfprivacy_api/rest/api_auth.py +++ b/selfprivacy_api/rest/api_auth.py @@ -8,10 +8,11 @@ from selfprivacy_api.actions.api_tokens import ( InvalidUsesLeft, NotFoundException, delete_api_token, + refresh_api_token, get_api_recovery_token_status, get_api_tokens_with_caller_flag, get_new_api_recovery_key, - refresh_api_token, + use_mnemonic_recovery_token, delete_new_device_auth_token, get_new_device_auth_token, ) @@ -19,7 +20,6 @@ from selfprivacy_api.actions.api_tokens import ( from selfprivacy_api.dependencies import TokenHeader, get_token_header from selfprivacy_api.utils.auth import ( - use_mnemonic_recoverery_token, use_new_device_auth_token, ) @@ -99,7 +99,7 @@ class UseTokenInput(BaseModel): @router.post("/recovery_token/use") async def rest_use_recovery_token(input: UseTokenInput): - token = use_mnemonic_recoverery_token(input.token, input.device) + token = use_mnemonic_recovery_token(input.token, input.device) if token is None: raise HTTPException(status_code=404, detail="Token not found") return {"token": token} diff --git a/selfprivacy_api/utils/auth.py b/selfprivacy_api/utils/auth.py index f27a826..11836b1 100644 --- a/selfprivacy_api/utils/auth.py +++ b/selfprivacy_api/utils/auth.py @@ -188,45 +188,6 @@ def generate_recovery_token( return Mnemonic(language="english").to_mnemonic(recovery_token) -def use_mnemonic_recoverery_token(mnemonic_phrase, name): - """Use the recovery token by converting the mnemonic word list to a byte array. - If the recovery token if invalid itself, return None - If the binary representation of phrase not matches - the byte array of the recovery token, return None. - If the mnemonic phrase is valid then generate a device token and return it. - Substract 1 from uses_left if it exists. - mnemonic_phrase is a string representation of the mnemonic word list. - """ - if not is_recovery_token_valid(): - return None - recovery_token_str = _get_recovery_token() - if recovery_token_str is None: - return None - recovery_token = bytes.fromhex(recovery_token_str) - if not Mnemonic(language="english").check(mnemonic_phrase): - return None - phrase_bytes = Mnemonic(language="english").to_entropy(mnemonic_phrase) - if phrase_bytes != recovery_token: - return None - token = _generate_token() - name = _validate_token_name(name) - with WriteUserData(UserDataFiles.TOKENS) as tokens: - tokens["tokens"].append( - { - "token": token, - "name": name, - "date": str(datetime.now()), - } - ) - if "recovery_token" in tokens: - if ( - "uses_left" in tokens["recovery_token"] - and tokens["recovery_token"]["uses_left"] is not None - ): - tokens["recovery_token"]["uses_left"] -= 1 - return token - - def _get_new_device_auth_token(): """Get new device auth token. If it is expired, return None""" with ReadUserData(UserDataFiles.TOKENS) as tokens: