2023-01-11 17:18:23 +00:00
|
|
|
"""
|
2024-07-26 19:59:44 +00:00
|
|
|
App tokens actions.
|
2023-01-11 17:18:23 +00:00
|
|
|
The only actions on tokens that are accessible from APIs
|
|
|
|
"""
|
2024-07-26 19:59:44 +00:00
|
|
|
|
2023-01-11 17:02:01 +00:00
|
|
|
from datetime import datetime, timezone
|
2022-08-25 17:03:56 +00:00
|
|
|
from typing import Optional
|
|
|
|
from pydantic import BaseModel
|
2022-12-21 13:51:39 +00:00
|
|
|
from mnemonic import Mnemonic
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2023-11-10 17:10:01 +00:00
|
|
|
from selfprivacy_api.utils.timeutils import ensure_tz_aware, ensure_tz_aware_strict
|
2024-11-02 23:15:51 +00:00
|
|
|
from selfprivacy_api.repositories.tokens import ACTIVE_TOKEN_PROVIDER
|
2022-12-26 10:53:27 +00:00
|
|
|
from selfprivacy_api.repositories.tokens.exceptions import (
|
|
|
|
TokenNotFound,
|
|
|
|
RecoveryKeyNotFound,
|
|
|
|
InvalidMnemonic,
|
2022-12-26 14:03:55 +00:00
|
|
|
NewDeviceKeyNotFound,
|
2022-12-26 10:53:27 +00:00
|
|
|
)
|
2022-12-21 13:51:39 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
class TokenInfoWithIsCaller(BaseModel):
|
|
|
|
"""Token info"""
|
|
|
|
|
|
|
|
name: str
|
|
|
|
date: datetime
|
|
|
|
is_caller: bool
|
|
|
|
|
2023-06-14 11:01:15 +00:00
|
|
|
|
2023-01-11 17:18:23 +00:00
|
|
|
def _naive(date_time: datetime) -> datetime:
|
|
|
|
if date_time is None:
|
|
|
|
return None
|
|
|
|
if date_time.tzinfo is not None:
|
|
|
|
date_time.astimezone(timezone.utc)
|
|
|
|
return date_time.replace(tzinfo=None)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2023-06-14 11:01:15 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
def get_api_tokens_with_caller_flag(caller_token: str) -> list[TokenInfoWithIsCaller]:
|
|
|
|
"""Get the tokens info"""
|
2024-11-02 23:15:51 +00:00
|
|
|
caller_name = ACTIVE_TOKEN_PROVIDER.get_token_by_token_string(
|
|
|
|
caller_token
|
|
|
|
).device_name
|
|
|
|
tokens = ACTIVE_TOKEN_PROVIDER.get_tokens()
|
2022-08-25 17:03:56 +00:00
|
|
|
return [
|
|
|
|
TokenInfoWithIsCaller(
|
2022-12-23 12:29:32 +00:00
|
|
|
name=token.device_name,
|
|
|
|
date=token.created_at,
|
|
|
|
is_caller=token.device_name == caller_name,
|
2022-08-25 17:03:56 +00:00
|
|
|
)
|
|
|
|
for token in tokens
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-12-26 15:20:58 +00:00
|
|
|
def is_token_valid(token) -> bool:
|
|
|
|
"""Check if token is valid"""
|
2024-11-02 23:15:51 +00:00
|
|
|
return ACTIVE_TOKEN_PROVIDER.is_token_valid(token)
|
2022-12-26 15:20:58 +00:00
|
|
|
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
class NotFoundException(Exception):
|
|
|
|
"""Not found exception"""
|
|
|
|
|
|
|
|
|
|
|
|
class CannotDeleteCallerException(Exception):
|
|
|
|
"""Cannot delete caller exception"""
|
|
|
|
|
|
|
|
|
|
|
|
def delete_api_token(caller_token: str, token_name: str) -> None:
|
|
|
|
"""Delete the token"""
|
2024-11-02 23:15:51 +00:00
|
|
|
if ACTIVE_TOKEN_PROVIDER.is_token_name_pair_valid(token_name, caller_token):
|
2022-08-25 17:03:56 +00:00
|
|
|
raise CannotDeleteCallerException("Cannot delete caller's token")
|
2024-11-02 23:15:51 +00:00
|
|
|
if not ACTIVE_TOKEN_PROVIDER.is_token_name_exists(token_name):
|
2022-08-25 17:03:56 +00:00
|
|
|
raise NotFoundException("Token not found")
|
2024-11-02 23:15:51 +00:00
|
|
|
token = ACTIVE_TOKEN_PROVIDER.get_token_by_name(token_name)
|
|
|
|
ACTIVE_TOKEN_PROVIDER.delete_token(token)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def refresh_api_token(caller_token: str) -> str:
|
|
|
|
"""Refresh the token"""
|
2022-12-21 15:23:42 +00:00
|
|
|
try:
|
2024-11-02 23:15:51 +00:00
|
|
|
old_token = ACTIVE_TOKEN_PROVIDER.get_token_by_token_string(caller_token)
|
|
|
|
new_token = ACTIVE_TOKEN_PROVIDER.refresh_token(old_token)
|
2022-12-21 15:23:42 +00:00
|
|
|
except TokenNotFound:
|
2022-08-25 17:03:56 +00:00
|
|
|
raise NotFoundException("Token not found")
|
2022-12-21 15:23:42 +00:00
|
|
|
return new_token.token
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RecoveryTokenStatus(BaseModel):
|
|
|
|
"""Recovery token status"""
|
|
|
|
|
|
|
|
exists: bool
|
|
|
|
valid: bool
|
|
|
|
date: Optional[datetime] = None
|
|
|
|
expiration: Optional[datetime] = None
|
|
|
|
uses_left: Optional[int] = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_api_recovery_token_status() -> RecoveryTokenStatus:
|
2023-11-10 17:10:01 +00:00
|
|
|
"""Get the recovery token status, timezone-aware"""
|
2024-11-02 23:15:51 +00:00
|
|
|
token = ACTIVE_TOKEN_PROVIDER.get_recovery_key()
|
2022-12-23 12:00:35 +00:00
|
|
|
if token is None:
|
2022-08-25 17:03:56 +00:00
|
|
|
return RecoveryTokenStatus(exists=False, valid=False)
|
2024-11-02 23:15:51 +00:00
|
|
|
is_valid = ACTIVE_TOKEN_PROVIDER.is_recovery_key_valid()
|
2023-11-10 17:10:01 +00:00
|
|
|
|
|
|
|
# New tokens are tz-aware, but older ones might not be
|
|
|
|
expiry_date = token.expires_at
|
|
|
|
if expiry_date is not None:
|
|
|
|
expiry_date = ensure_tz_aware_strict(expiry_date)
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
return RecoveryTokenStatus(
|
|
|
|
exists=True,
|
|
|
|
valid=is_valid,
|
2023-11-10 17:10:01 +00:00
|
|
|
date=ensure_tz_aware_strict(token.created_at),
|
|
|
|
expiration=expiry_date,
|
2022-12-23 12:00:35 +00:00
|
|
|
uses_left=token.uses_left,
|
2022-08-25 17:03:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidExpirationDate(Exception):
|
|
|
|
"""Invalid expiration date exception"""
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidUsesLeft(Exception):
|
|
|
|
"""Invalid uses left exception"""
|
|
|
|
|
|
|
|
|
|
|
|
def get_new_api_recovery_key(
|
|
|
|
expiration_date: Optional[datetime] = None, uses_left: Optional[int] = None
|
|
|
|
) -> str:
|
|
|
|
"""Get new recovery key"""
|
|
|
|
if expiration_date is not None:
|
2023-11-10 13:31:12 +00:00
|
|
|
expiration_date = ensure_tz_aware(expiration_date)
|
|
|
|
current_time = datetime.now(timezone.utc)
|
|
|
|
if expiration_date < current_time:
|
2022-08-25 17:03:56 +00:00
|
|
|
raise InvalidExpirationDate("Expiration date is in the past")
|
|
|
|
if uses_left is not None:
|
|
|
|
if uses_left <= 0:
|
|
|
|
raise InvalidUsesLeft("Uses must be greater than 0")
|
|
|
|
|
2024-11-02 23:15:51 +00:00
|
|
|
key = ACTIVE_TOKEN_PROVIDER.create_recovery_key(expiration_date, uses_left)
|
2022-12-21 13:51:39 +00:00
|
|
|
mnemonic_phrase = Mnemonic(language="english").to_mnemonic(bytes.fromhex(key.key))
|
|
|
|
return mnemonic_phrase
|
2022-12-26 10:13:37 +00:00
|
|
|
|
|
|
|
|
2022-12-26 10:53:27 +00:00
|
|
|
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:
|
2024-11-02 23:15:51 +00:00
|
|
|
token = ACTIVE_TOKEN_PROVIDER.use_mnemonic_recovery_key(mnemonic_phrase, name)
|
2022-12-26 10:53:27 +00:00
|
|
|
return token.token
|
|
|
|
except (RecoveryKeyNotFound, InvalidMnemonic):
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2022-12-26 10:13:37 +00:00
|
|
|
def delete_new_device_auth_token() -> None:
|
2024-11-02 23:15:51 +00:00
|
|
|
ACTIVE_TOKEN_PROVIDER.delete_new_device_key()
|
2022-12-26 10:27:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
2024-11-02 23:15:51 +00:00
|
|
|
key = ACTIVE_TOKEN_PROVIDER.get_new_device_key()
|
2022-12-26 10:27:10 +00:00
|
|
|
return Mnemonic(language="english").to_mnemonic(bytes.fromhex(key.key))
|
2022-12-26 14:03:55 +00:00
|
|
|
|
|
|
|
|
2022-12-30 18:06:16 +00:00
|
|
|
def use_new_device_auth_token(mnemonic_phrase, name) -> Optional[str]:
|
2022-12-26 14:03:55 +00:00
|
|
|
"""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:
|
2024-11-02 23:15:51 +00:00
|
|
|
token = ACTIVE_TOKEN_PROVIDER.use_mnemonic_new_device_key(mnemonic_phrase, name)
|
2022-12-26 14:03:55 +00:00
|
|
|
return token.token
|
|
|
|
except (NewDeviceKeyNotFound, InvalidMnemonic):
|
|
|
|
return None
|