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
|
|
|
|
import strawberry
|
2022-08-25 17:03:56 +00:00
|
|
|
from strawberry.types import Info
|
2022-12-26 14:29:05 +00:00
|
|
|
from selfprivacy_api.actions.api_tokens import (
|
|
|
|
get_api_tokens_with_caller_flag,
|
|
|
|
get_api_recovery_token_status,
|
|
|
|
)
|
2022-06-24 17:12:32 +00:00
|
|
|
from selfprivacy_api.graphql import IsAuthenticated
|
2022-08-25 17:03:56 +00:00
|
|
|
from selfprivacy_api.dependencies import get_api_version as get_api_version_dependency
|
2022-06-24 12:26:51 +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"""
|
2022-08-25 17:03:56 +00:00
|
|
|
return get_api_version_dependency()
|
2022-06-24 17:08:58 +00:00
|
|
|
|
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
|
|
|
@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:
|
2023-11-10 17:10:01 +00:00
|
|
|
"""Get recovery key status, times are timezone-aware"""
|
2022-12-26 14:29:05 +00:00
|
|
|
status = get_api_recovery_token_status()
|
2022-12-26 14:51:37 +00:00
|
|
|
if status is None or not status.exists:
|
2022-06-24 17:08:58 +00:00
|
|
|
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,
|
2022-12-26 14:51:37 +00:00
|
|
|
valid=status.valid,
|
2022-12-26 14:29:05 +00:00
|
|
|
creation_date=status.date,
|
|
|
|
expiration_date=status.expiration,
|
|
|
|
uses_left=status.uses_left,
|
2022-06-24 17:08:58 +00:00
|
|
|
)
|
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-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
@strawberry.field(permission_classes=[IsAuthenticated])
|
|
|
|
def devices(self, info: Info) -> typing.List[ApiDevice]:
|
|
|
|
return [
|
|
|
|
ApiDevice(
|
|
|
|
name=device.name,
|
|
|
|
creation_date=device.date,
|
|
|
|
is_caller=device.is_caller,
|
|
|
|
)
|
|
|
|
for device in get_api_tokens_with_caller_flag(
|
|
|
|
info.context["request"]
|
|
|
|
.headers.get("Authorization", "")
|
|
|
|
.replace("Bearer ", "")
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2022-06-24 18:14:20 +00:00
|
|
|
recovery_key: ApiRecoveryKeyStatus = strawberry.field(
|
|
|
|
resolver=get_recovery_key_status, permission_classes=[IsAuthenticated]
|
|
|
|
)
|