mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-25 18:26:34 +00:00
refactor(tokens-repo): delete use_new_device_auth_token from auth utils
This commit is contained in:
parent
5d4ed73435
commit
f33d5155b0
|
@ -11,6 +11,7 @@ from selfprivacy_api.repositories.tokens.exceptions import (
|
||||||
TokenNotFound,
|
TokenNotFound,
|
||||||
RecoveryKeyNotFound,
|
RecoveryKeyNotFound,
|
||||||
InvalidMnemonic,
|
InvalidMnemonic,
|
||||||
|
NewDeviceKeyNotFound,
|
||||||
)
|
)
|
||||||
|
|
||||||
TOKEN_REPO = JsonTokensRepository()
|
TOKEN_REPO = JsonTokensRepository()
|
||||||
|
@ -142,3 +143,15 @@ def get_new_device_auth_token() -> str:
|
||||||
"""
|
"""
|
||||||
key = TOKEN_REPO.get_new_device_key()
|
key = TOKEN_REPO.get_new_device_key()
|
||||||
return Mnemonic(language="english").to_mnemonic(bytes.fromhex(key.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
|
||||||
|
|
|
@ -15,6 +15,7 @@ from selfprivacy_api.actions.api_tokens import (
|
||||||
refresh_api_token,
|
refresh_api_token,
|
||||||
delete_new_device_auth_token,
|
delete_new_device_auth_token,
|
||||||
get_new_device_auth_token,
|
get_new_device_auth_token,
|
||||||
|
use_new_device_auth_token,
|
||||||
)
|
)
|
||||||
from selfprivacy_api.graphql import IsAuthenticated
|
from selfprivacy_api.graphql import IsAuthenticated
|
||||||
from selfprivacy_api.graphql.mutations.mutation_interface import (
|
from selfprivacy_api.graphql.mutations.mutation_interface import (
|
||||||
|
@ -22,9 +23,6 @@ from selfprivacy_api.graphql.mutations.mutation_interface import (
|
||||||
MutationReturnInterface,
|
MutationReturnInterface,
|
||||||
)
|
)
|
||||||
|
|
||||||
from selfprivacy_api.utils.auth import (
|
|
||||||
use_new_device_auth_token,
|
|
||||||
)
|
|
||||||
|
|
||||||
from selfprivacy_api.repositories.tokens.json_tokens_repository import (
|
from selfprivacy_api.repositories.tokens.json_tokens_repository import (
|
||||||
JsonTokensRepository,
|
JsonTokensRepository,
|
||||||
|
|
|
@ -15,13 +15,11 @@ from selfprivacy_api.actions.api_tokens import (
|
||||||
use_mnemonic_recovery_token,
|
use_mnemonic_recovery_token,
|
||||||
delete_new_device_auth_token,
|
delete_new_device_auth_token,
|
||||||
get_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.dependencies import TokenHeader, get_token_header
|
||||||
|
|
||||||
from selfprivacy_api.utils.auth import (
|
|
||||||
use_new_device_auth_token,
|
|
||||||
)
|
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
prefix="/auth",
|
prefix="/auth",
|
||||||
|
|
|
@ -200,24 +200,3 @@ def _get_new_device_auth_token():
|
||||||
if datetime.now() > expiration:
|
if datetime.now() > expiration:
|
||||||
return None
|
return None
|
||||||
return new_device["token"]
|
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
|
|
||||||
|
|
Loading…
Reference in a new issue