mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-27 11:16:35 +00:00
refactor(tokens-repo): delete use_mnemonic_recoverery_token from auth utils
This commit is contained in:
parent
cb1906144c
commit
29723b9f3a
|
@ -7,7 +7,11 @@ from mnemonic import Mnemonic
|
||||||
from selfprivacy_api.repositories.tokens.json_tokens_repository import (
|
from selfprivacy_api.repositories.tokens.json_tokens_repository import (
|
||||||
JsonTokensRepository,
|
JsonTokensRepository,
|
||||||
)
|
)
|
||||||
from selfprivacy_api.repositories.tokens.exceptions import TokenNotFound
|
from selfprivacy_api.repositories.tokens.exceptions import (
|
||||||
|
TokenNotFound,
|
||||||
|
RecoveryKeyNotFound,
|
||||||
|
InvalidMnemonic,
|
||||||
|
)
|
||||||
|
|
||||||
TOKEN_REPO = JsonTokensRepository()
|
TOKEN_REPO = JsonTokensRepository()
|
||||||
|
|
||||||
|
@ -112,6 +116,22 @@ def get_new_api_recovery_key(
|
||||||
return mnemonic_phrase
|
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:
|
def delete_new_device_auth_token() -> None:
|
||||||
TOKEN_REPO.delete_new_device_key()
|
TOKEN_REPO.delete_new_device_key()
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ from selfprivacy_api.actions.api_tokens import (
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
delete_api_token,
|
delete_api_token,
|
||||||
get_new_api_recovery_key,
|
get_new_api_recovery_key,
|
||||||
|
use_mnemonic_recovery_token,
|
||||||
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,
|
||||||
|
@ -107,15 +108,15 @@ class ApiMutations:
|
||||||
self, input: UseRecoveryKeyInput
|
self, input: UseRecoveryKeyInput
|
||||||
) -> DeviceApiTokenMutationReturn:
|
) -> DeviceApiTokenMutationReturn:
|
||||||
"""Use recovery key"""
|
"""Use recovery key"""
|
||||||
try:
|
token = use_mnemonic_recovery_token(input.key, input.deviceName)
|
||||||
token = TOKEN_REPO.use_mnemonic_recovery_key(input.key, input.deviceName)
|
if token is not None:
|
||||||
return DeviceApiTokenMutationReturn(
|
return DeviceApiTokenMutationReturn(
|
||||||
success=True,
|
success=True,
|
||||||
message="Recovery key used",
|
message="Recovery key used",
|
||||||
code=200,
|
code=200,
|
||||||
token=token.token,
|
token=token,
|
||||||
)
|
)
|
||||||
except (RecoveryKeyNotFound, InvalidMnemonic):
|
else:
|
||||||
return DeviceApiTokenMutationReturn(
|
return DeviceApiTokenMutationReturn(
|
||||||
success=False,
|
success=False,
|
||||||
message="Recovery key not found",
|
message="Recovery key not found",
|
||||||
|
|
|
@ -8,10 +8,11 @@ from selfprivacy_api.actions.api_tokens import (
|
||||||
InvalidUsesLeft,
|
InvalidUsesLeft,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
delete_api_token,
|
delete_api_token,
|
||||||
|
refresh_api_token,
|
||||||
get_api_recovery_token_status,
|
get_api_recovery_token_status,
|
||||||
get_api_tokens_with_caller_flag,
|
get_api_tokens_with_caller_flag,
|
||||||
get_new_api_recovery_key,
|
get_new_api_recovery_key,
|
||||||
refresh_api_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,
|
||||||
)
|
)
|
||||||
|
@ -19,7 +20,6 @@ from selfprivacy_api.actions.api_tokens import (
|
||||||
from selfprivacy_api.dependencies import TokenHeader, get_token_header
|
from selfprivacy_api.dependencies import TokenHeader, get_token_header
|
||||||
|
|
||||||
from selfprivacy_api.utils.auth import (
|
from selfprivacy_api.utils.auth import (
|
||||||
use_mnemonic_recoverery_token,
|
|
||||||
use_new_device_auth_token,
|
use_new_device_auth_token,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ class UseTokenInput(BaseModel):
|
||||||
|
|
||||||
@router.post("/recovery_token/use")
|
@router.post("/recovery_token/use")
|
||||||
async def rest_use_recovery_token(input: UseTokenInput):
|
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:
|
if token is None:
|
||||||
raise HTTPException(status_code=404, detail="Token not found")
|
raise HTTPException(status_code=404, detail="Token not found")
|
||||||
return {"token": token}
|
return {"token": token}
|
||||||
|
|
|
@ -188,45 +188,6 @@ def generate_recovery_token(
|
||||||
return Mnemonic(language="english").to_mnemonic(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():
|
def _get_new_device_auth_token():
|
||||||
"""Get new device auth token. If it is expired, return None"""
|
"""Get new device auth token. If it is expired, return None"""
|
||||||
with ReadUserData(UserDataFiles.TOKENS) as tokens:
|
with ReadUserData(UserDataFiles.TOKENS) as tokens:
|
||||||
|
|
Loading…
Reference in a new issue