selfprivacy-rest-api/selfprivacy_api/graphql/queries/api_queries.py

106 lines
2.7 KiB
Python
Raw Normal View History

2022-06-24 12:26:51 +00:00
"""API access status"""
2022-06-24 13:05:18 +00:00
# pylint: disable=too-few-public-methods
2022-06-24 17:08:58 +00:00
import datetime
2022-06-24 12:26:51 +00:00
import typing
2022-06-24 17:08:58 +00:00
from flask import request
2022-06-24 12:26:51 +00:00
import strawberry
2022-06-24 17:12:32 +00:00
from selfprivacy_api.graphql import IsAuthenticated
2022-06-24 17:08:58 +00:00
from selfprivacy_api.utils import parse_date
2022-06-24 12:26:51 +00:00
2022-06-24 17:08:58 +00:00
from selfprivacy_api.utils.auth import (
get_recovery_token_status,
get_tokens_info,
is_recovery_token_exists,
is_recovery_token_valid,
is_token_name_exists,
is_token_name_pair_valid,
refresh_token,
get_token_name,
)
2022-06-24 13:05:18 +00:00
2022-06-24 18:14:20 +00:00
2022-06-24 17:08:58 +00:00
def get_api_version() -> str:
"""Get API version"""
return "1.2.7"
2022-06-24 18:14:20 +00:00
2022-06-24 17:08:58 +00:00
@strawberry.type
class ApiDevice:
"""A single device with SelfPrivacy app installed"""
2022-06-24 18:14:20 +00:00
2022-06-24 17:08:58 +00:00
name: str
creation_date: datetime.datetime
is_caller: bool
2022-06-24 18:14:20 +00:00
2022-06-24 17:08:58 +00:00
def get_devices() -> typing.List[ApiDevice]:
"""Get list of devices"""
2022-06-24 18:14:20 +00:00
caller_name = get_token_name(
request.headers.get("Authorization").split(" ")[1]
if request.headers.get("Authorization") is not None
else None
)
2022-06-24 17:08:58 +00:00
tokens = get_tokens_info()
return [
ApiDevice(
name=token["name"],
creation_date=parse_date(token["date"]),
is_caller=token["name"] == caller_name,
)
for token in tokens
]
@strawberry.type
class ApiRecoveryKeyStatus:
"""Recovery key status"""
2022-06-24 18:14:20 +00:00
2022-06-24 17:08:58 +00:00
exists: bool
valid: bool
creation_date: typing.Optional[datetime.datetime]
expiration_date: typing.Optional[datetime.datetime]
uses_left: typing.Optional[int]
2022-06-24 18:14:20 +00:00
2022-06-24 17:08:58 +00:00
def get_recovery_key_status() -> ApiRecoveryKeyStatus:
"""Get recovery key status"""
if not is_recovery_token_exists():
return ApiRecoveryKeyStatus(
2022-06-24 18:14:20 +00:00
exists=False,
valid=False,
creation_date=None,
expiration_date=None,
uses_left=None,
2022-06-24 17:08:58 +00:00
)
status = get_recovery_token_status()
if status is None:
return ApiRecoveryKeyStatus(
2022-06-24 18:14:20 +00:00
exists=False,
valid=False,
creation_date=None,
expiration_date=None,
uses_left=None,
2022-06-24 17:08:58 +00:00
)
return ApiRecoveryKeyStatus(
exists=True,
valid=is_recovery_token_valid(),
creation_date=parse_date(status["date"]),
2022-06-24 18:14:20 +00:00
expiration_date=parse_date(status["expiration"])
if status["expiration"] is not None
else None,
2022-06-24 17:08:58 +00:00
uses_left=status["uses_left"] if status["uses_left"] is not None else None,
)
2022-06-24 12:26:51 +00:00
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
@strawberry.type
2022-06-24 16:28:58 +00:00
class Api:
"""API access status"""
2022-06-24 18:14:20 +00:00
2022-06-24 16:28:58 +00:00
version: str = strawberry.field(resolver=get_api_version)
2022-06-24 18:14:20 +00:00
devices: typing.List[ApiDevice] = strawberry.field(
resolver=get_devices, permission_classes=[IsAuthenticated]
)
recovery_key: ApiRecoveryKeyStatus = strawberry.field(
resolver=get_recovery_key_status, permission_classes=[IsAuthenticated]
)