From cb1906144c22be4b363e9ca874558e3d1b5d214d Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 26 Dec 2022 10:27:10 +0000 Subject: [PATCH] refactor(tokens-repo): delete get_new_device_auth_token from auth utils --- selfprivacy_api/actions/api_tokens.py | 8 ++++++++ .../graphql/mutations/api_mutations.py | 2 +- selfprivacy_api/rest/api_auth.py | 2 +- selfprivacy_api/utils/auth.py | 16 ---------------- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/selfprivacy_api/actions/api_tokens.py b/selfprivacy_api/actions/api_tokens.py index a90aa125..b13c13ee 100644 --- a/selfprivacy_api/actions/api_tokens.py +++ b/selfprivacy_api/actions/api_tokens.py @@ -114,3 +114,11 @@ def get_new_api_recovery_key( def delete_new_device_auth_token() -> None: TOKEN_REPO.delete_new_device_key() + + +def get_new_device_auth_token() -> str: + """Generate and store a new device auth token which is valid for 10 minutes + and return a mnemonic phrase representation + """ + key = TOKEN_REPO.get_new_device_key() + return Mnemonic(language="english").to_mnemonic(bytes.fromhex(key.key)) diff --git a/selfprivacy_api/graphql/mutations/api_mutations.py b/selfprivacy_api/graphql/mutations/api_mutations.py index 0c83eab5..c2075c38 100644 --- a/selfprivacy_api/graphql/mutations/api_mutations.py +++ b/selfprivacy_api/graphql/mutations/api_mutations.py @@ -13,6 +13,7 @@ from selfprivacy_api.actions.api_tokens import ( get_new_api_recovery_key, refresh_api_token, delete_new_device_auth_token, + get_new_device_auth_token, ) from selfprivacy_api.graphql import IsAuthenticated from selfprivacy_api.graphql.mutations.mutation_interface import ( @@ -21,7 +22,6 @@ from selfprivacy_api.graphql.mutations.mutation_interface import ( ) from selfprivacy_api.utils.auth import ( - get_new_device_auth_token, use_new_device_auth_token, ) diff --git a/selfprivacy_api/rest/api_auth.py b/selfprivacy_api/rest/api_auth.py index 8209ef67..a860798c 100644 --- a/selfprivacy_api/rest/api_auth.py +++ b/selfprivacy_api/rest/api_auth.py @@ -13,12 +13,12 @@ from selfprivacy_api.actions.api_tokens import ( get_new_api_recovery_key, refresh_api_token, delete_new_device_auth_token, + get_new_device_auth_token, ) from selfprivacy_api.dependencies import TokenHeader, get_token_header from selfprivacy_api.utils.auth import ( - get_new_device_auth_token, use_mnemonic_recoverery_token, use_new_device_auth_token, ) diff --git a/selfprivacy_api/utils/auth.py b/selfprivacy_api/utils/auth.py index 847cd30a..f27a8262 100644 --- a/selfprivacy_api/utils/auth.py +++ b/selfprivacy_api/utils/auth.py @@ -227,22 +227,6 @@ def use_mnemonic_recoverery_token(mnemonic_phrase, name): return token -def get_new_device_auth_token() -> str: - """Generate a new device auth token which is valid for 10 minutes - and return a mnemonic phrase representation - Write token to the new_device of the tokens.json file. - """ - token = secrets.token_bytes(16) - token_str = token.hex() - with WriteUserData(UserDataFiles.TOKENS) as tokens: - tokens["new_device"] = { - "token": token_str, - "date": str(datetime.now()), - "expiration": str(datetime.now() + timedelta(minutes=10)), - } - return Mnemonic(language="english").to_mnemonic(token) - - def _get_new_device_auth_token(): """Get new device auth token. If it is expired, return None""" with ReadUserData(UserDataFiles.TOKENS) as tokens: