mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-26 05:51:29 +00:00
refactor(tokens-repo): cleanup actions/api_tokens.py
This commit is contained in:
parent
158c1f13a6
commit
51018dd6c2
|
@ -1,4 +1,7 @@
|
||||||
"""App tokens actions"""
|
"""
|
||||||
|
App tokens actions.
|
||||||
|
The only actions on tokens that are accessible from APIs
|
||||||
|
"""
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
@ -24,6 +27,12 @@ class TokenInfoWithIsCaller(BaseModel):
|
||||||
date: datetime
|
date: datetime
|
||||||
is_caller: bool
|
is_caller: bool
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
def get_api_tokens_with_caller_flag(caller_token: str) -> list[TokenInfoWithIsCaller]:
|
def get_api_tokens_with_caller_flag(caller_token: str) -> list[TokenInfoWithIsCaller]:
|
||||||
"""Get the tokens info"""
|
"""Get the tokens info"""
|
||||||
|
@ -82,12 +91,7 @@ class RecoveryTokenStatus(BaseModel):
|
||||||
uses_left: Optional[int] = None
|
uses_left: Optional[int] = None
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def get_api_recovery_token_status() -> RecoveryTokenStatus:
|
def get_api_recovery_token_status() -> RecoveryTokenStatus:
|
||||||
|
@ -99,8 +103,8 @@ def get_api_recovery_token_status() -> RecoveryTokenStatus:
|
||||||
return RecoveryTokenStatus(
|
return RecoveryTokenStatus(
|
||||||
exists=True,
|
exists=True,
|
||||||
valid=is_valid,
|
valid=is_valid,
|
||||||
date=naive(token.created_at),
|
date=_naive(token.created_at),
|
||||||
expiration=naive(token.expires_at),
|
expiration=_naive(token.expires_at),
|
||||||
uses_left=token.uses_left,
|
uses_left=token.uses_left,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue