2022-08-25 17:03:56 +00:00
|
|
|
"""App tokens actions"""
|
|
|
|
from datetime import datetime
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
from selfprivacy_api.utils.auth import (
|
|
|
|
get_tokens_info,
|
|
|
|
get_token_name,
|
|
|
|
)
|
|
|
|
|
2022-12-21 13:51:39 +00:00
|
|
|
from selfprivacy_api.repositories.tokens.json_tokens_repository import (
|
|
|
|
JsonTokensRepository,
|
|
|
|
)
|
2022-12-21 15:23:42 +00:00
|
|
|
from selfprivacy_api.repositories.tokens.exceptions import TokenNotFound
|
2022-12-21 13:51:39 +00:00
|
|
|
|
|
|
|
TOKEN_REPO = JsonTokensRepository()
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
class TokenInfoWithIsCaller(BaseModel):
|
|
|
|
"""Token info"""
|
|
|
|
|
|
|
|
name: str
|
|
|
|
date: datetime
|
|
|
|
is_caller: bool
|
|
|
|
|
|
|
|
|
|
|
|
def get_api_tokens_with_caller_flag(caller_token: str) -> list[TokenInfoWithIsCaller]:
|
|
|
|
"""Get the tokens info"""
|
|
|
|
caller_name = get_token_name(caller_token)
|
|
|
|
tokens = get_tokens_info()
|
|
|
|
return [
|
|
|
|
TokenInfoWithIsCaller(
|
|
|
|
name=token.name,
|
|
|
|
date=token.date,
|
|
|
|
is_caller=token.name == caller_name,
|
|
|
|
)
|
|
|
|
for token in tokens
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
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"""
|
2022-12-21 16:17:56 +00:00
|
|
|
if TOKEN_REPO.is_token_name_pair_valid(token_name, caller_token):
|
2022-08-25 17:03:56 +00:00
|
|
|
raise CannotDeleteCallerException("Cannot delete caller's token")
|
2022-12-21 17:09:49 +00:00
|
|
|
if not TOKEN_REPO.is_token_name_exists(token_name):
|
2022-08-25 17:03:56 +00:00
|
|
|
raise NotFoundException("Token not found")
|
2022-12-23 11:14:52 +00:00
|
|
|
token = TOKEN_REPO.get_token_by_name(token_name)
|
|
|
|
TOKEN_REPO.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:
|
|
|
|
old_token = TOKEN_REPO.get_token_by_token_string(caller_token)
|
|
|
|
new_token = TOKEN_REPO.refresh_token(old_token)
|
|
|
|
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:
|
|
|
|
"""Get the recovery token status"""
|
2022-12-23 12:00:35 +00:00
|
|
|
token = TOKEN_REPO.get_recovery_key()
|
|
|
|
if token is None:
|
2022-08-25 17:03:56 +00:00
|
|
|
return RecoveryTokenStatus(exists=False, valid=False)
|
2022-12-23 12:00:35 +00:00
|
|
|
is_valid = TOKEN_REPO.is_recovery_key_valid()
|
2022-08-25 17:03:56 +00:00
|
|
|
return RecoveryTokenStatus(
|
|
|
|
exists=True,
|
|
|
|
valid=is_valid,
|
2022-12-23 12:00:35 +00:00
|
|
|
date=token.created_at,
|
|
|
|
expiration=token.expires_at,
|
|
|
|
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:
|
|
|
|
current_time = datetime.now().timestamp()
|
|
|
|
if expiration_date.timestamp() < current_time:
|
|
|
|
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")
|
|
|
|
|
2022-12-21 13:51:39 +00:00
|
|
|
key = TOKEN_REPO.create_recovery_key(expiration_date, uses_left)
|
|
|
|
mnemonic_phrase = Mnemonic(language="english").to_mnemonic(bytes.fromhex(key.key))
|
|
|
|
return mnemonic_phrase
|