diff --git a/selfprivacy_api/actions/api_tokens.py b/selfprivacy_api/actions/api_tokens.py index 394d3d98..3b180e84 100644 --- a/selfprivacy_api/actions/api_tokens.py +++ b/selfprivacy_api/actions/api_tokens.py @@ -11,6 +11,7 @@ from selfprivacy_api.repositories.tokens.exceptions import ( TokenNotFound, RecoveryKeyNotFound, InvalidMnemonic, + NewDeviceKeyNotFound, ) TOKEN_REPO = JsonTokensRepository() @@ -142,3 +143,15 @@ def get_new_device_auth_token() -> str: """ key = TOKEN_REPO.get_new_device_key() return Mnemonic(language="english").to_mnemonic(bytes.fromhex(key.key)) + + +def use_new_device_auth_token(mnemonic_phrase, name) -> str: + """Use the new device auth token by converting the mnemonic string to a byte array. + If the mnemonic phrase is valid then generate a device token and return it. + New device auth token must be deleted. + """ + try: + token = TOKEN_REPO.use_mnemonic_new_device_key(mnemonic_phrase, name) + return token.token + except (NewDeviceKeyNotFound, InvalidMnemonic): + return None diff --git a/selfprivacy_api/graphql/mutations/api_mutations.py b/selfprivacy_api/graphql/mutations/api_mutations.py index 45961c81..0c413fbc 100644 --- a/selfprivacy_api/graphql/mutations/api_mutations.py +++ b/selfprivacy_api/graphql/mutations/api_mutations.py @@ -15,6 +15,7 @@ from selfprivacy_api.actions.api_tokens import ( refresh_api_token, delete_new_device_auth_token, get_new_device_auth_token, + use_new_device_auth_token, ) from selfprivacy_api.graphql import IsAuthenticated from selfprivacy_api.graphql.mutations.mutation_interface import ( @@ -22,9 +23,6 @@ from selfprivacy_api.graphql.mutations.mutation_interface import ( MutationReturnInterface, ) -from selfprivacy_api.utils.auth import ( - use_new_device_auth_token, -) from selfprivacy_api.repositories.tokens.json_tokens_repository import ( JsonTokensRepository, diff --git a/selfprivacy_api/rest/api_auth.py b/selfprivacy_api/rest/api_auth.py index ab96bee5..275dac36 100644 --- a/selfprivacy_api/rest/api_auth.py +++ b/selfprivacy_api/rest/api_auth.py @@ -15,13 +15,11 @@ from selfprivacy_api.actions.api_tokens import ( use_mnemonic_recovery_token, delete_new_device_auth_token, get_new_device_auth_token, + use_new_device_auth_token, ) from selfprivacy_api.dependencies import TokenHeader, get_token_header -from selfprivacy_api.utils.auth import ( - use_new_device_auth_token, -) router = APIRouter( prefix="/auth", diff --git a/selfprivacy_api/utils/auth.py b/selfprivacy_api/utils/auth.py index 11836b17..53dffd7e 100644 --- a/selfprivacy_api/utils/auth.py +++ b/selfprivacy_api/utils/auth.py @@ -200,24 +200,3 @@ def _get_new_device_auth_token(): if datetime.now() > expiration: return None return new_device["token"] - - -def use_new_device_auth_token(mnemonic_phrase, name): - """Use the new device auth token by converting the mnemonic string to a byte array. - If the mnemonic phrase is valid then generate a device token and return it. - New device auth token must be deleted. - """ - token_str = _get_new_device_auth_token() - if token_str is None: - return None - token = bytes.fromhex(token_str) - if not Mnemonic(language="english").check(mnemonic_phrase): - return None - phrase_bytes = Mnemonic(language="english").to_entropy(mnemonic_phrase) - if phrase_bytes != token: - return None - token = create_token(name) - with WriteUserData(UserDataFiles.TOKENS) as tokens: - if "new_device" in tokens: - del tokens["new_device"] - return token