mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-03-18 20:39:46 +00:00
Add api status resolvers
This commit is contained in:
parent
17b8334c6e
commit
9b25bc0d53
4 changed files with 69 additions and 5 deletions
|
@ -10,6 +10,9 @@ from selfprivacy_api.graphql.queries.api import Api
|
||||||
class Query:
|
class Query:
|
||||||
"""Root schema for queries"""
|
"""Root schema for queries"""
|
||||||
system: System
|
system: System
|
||||||
api: Api
|
@strawberry.field
|
||||||
|
def api(self) -> Api:
|
||||||
|
"""API access status"""
|
||||||
|
return Api()
|
||||||
|
|
||||||
schema = strawberry.Schema(query=Query)
|
schema = strawberry.Schema(query=Query)
|
||||||
|
|
|
@ -4,24 +4,27 @@ import datetime
|
||||||
import typing
|
import typing
|
||||||
import strawberry
|
import strawberry
|
||||||
|
|
||||||
from selfprivacy_api.resolvers.api import get_api_version
|
from selfprivacy_api.resolvers.api import get_api_version, get_devices, get_recovery_key_status
|
||||||
|
|
||||||
@strawberry.type
|
@strawberry.type
|
||||||
class ApiDevice:
|
class ApiDevice:
|
||||||
|
"""A single device with SelfPrivacy app installed"""
|
||||||
name: str
|
name: str
|
||||||
creation_date: datetime.datetime
|
creation_date: datetime.datetime
|
||||||
is_caller: bool
|
is_caller: bool
|
||||||
|
|
||||||
@strawberry.type
|
@strawberry.type
|
||||||
class ApiRecoveryKeyStatus:
|
class ApiRecoveryKeyStatus:
|
||||||
|
"""Recovery key status"""
|
||||||
exists: bool
|
exists: bool
|
||||||
valid: bool
|
valid: bool
|
||||||
creation_date: datetime.datetime
|
creation_date: typing.Optional[datetime.datetime]
|
||||||
expiration_date: typing.Optional[datetime.datetime]
|
expiration_date: typing.Optional[datetime.datetime]
|
||||||
uses_left: typing.Optional[int]
|
uses_left: typing.Optional[int]
|
||||||
|
|
||||||
@strawberry.type
|
@strawberry.type
|
||||||
class Api:
|
class Api:
|
||||||
|
"""API access status"""
|
||||||
version: str = strawberry.field(resolver=get_api_version)
|
version: str = strawberry.field(resolver=get_api_version)
|
||||||
devices: typing.List[ApiDevice]
|
devices: typing.List[ApiDevice] = strawberry.field(resolver=get_devices)
|
||||||
recovery_key: ApiRecoveryKeyStatus
|
recovery_key: ApiRecoveryKeyStatus = strawberry.field(resolver=get_recovery_key_status)
|
||||||
|
|
|
@ -1,5 +1,53 @@
|
||||||
"""Resolvers for API module"""
|
"""Resolvers for API module"""
|
||||||
|
import datetime
|
||||||
|
import typing
|
||||||
|
from flask import request
|
||||||
|
|
||||||
|
from selfprivacy_api.graphql.queries.api import ApiDevice, ApiRecoveryKeyStatus
|
||||||
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
def get_api_version() -> str:
|
def get_api_version() -> str:
|
||||||
"""Get API version"""
|
"""Get API version"""
|
||||||
return "1.2.7"
|
return "1.2.7"
|
||||||
|
|
||||||
|
def get_devices() -> typing.List[ApiDevice]:
|
||||||
|
"""Get list of devices"""
|
||||||
|
caller_name = get_token_name(request.headers.get("Authorization").split(" ")[1])
|
||||||
|
tokens = get_tokens_info()
|
||||||
|
return [
|
||||||
|
ApiDevice(
|
||||||
|
name=token["name"],
|
||||||
|
creation_date=datetime.datetime.strptime(token["date"], "%Y-%m-%dT%H:%M:%S.%fZ"),
|
||||||
|
is_caller=token["name"] == caller_name,
|
||||||
|
)
|
||||||
|
for token in tokens
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_recovery_key_status() -> ApiRecoveryKeyStatus:
|
||||||
|
"""Get recovery key status"""
|
||||||
|
if not is_recovery_token_exists():
|
||||||
|
return ApiRecoveryKeyStatus(
|
||||||
|
exists=False, valid=False, creation_date=None, expiration_date=None, uses_left=None
|
||||||
|
)
|
||||||
|
status = get_recovery_token_status()
|
||||||
|
if status is None:
|
||||||
|
return ApiRecoveryKeyStatus(
|
||||||
|
exists=False, valid=False, creation_date=None, expiration_date=None, uses_left=None
|
||||||
|
)
|
||||||
|
return ApiRecoveryKeyStatus(
|
||||||
|
exists=True,
|
||||||
|
valid=is_recovery_token_valid(),
|
||||||
|
creation_date=datetime.datetime.strptime(status["creation_date"], "%Y-%m-%dT%H:%M:%S.%fZ"),
|
||||||
|
expiration_date=datetime.datetime.strptime(status["expiration_date"], "%Y-%m-%dT%H:%M:%S.%fZ") if status["expiration_date"] is not None else None,
|
||||||
|
uses_left=status["uses_left"] if status["uses_left"] is not None else None,
|
||||||
|
)
|
||||||
|
|
|
@ -60,6 +60,16 @@ class RecoveryToken(Resource):
|
||||||
"uses_left": None,
|
"uses_left": None,
|
||||||
}
|
}
|
||||||
status = get_recovery_token_status()
|
status = get_recovery_token_status()
|
||||||
|
# check if status is None
|
||||||
|
if status is None:
|
||||||
|
return {
|
||||||
|
"exists": False,
|
||||||
|
"valid": False,
|
||||||
|
"date": None,
|
||||||
|
"expiration": None,
|
||||||
|
"uses_left": None,
|
||||||
|
}
|
||||||
|
|
||||||
if not is_recovery_token_valid():
|
if not is_recovery_token_valid():
|
||||||
return {
|
return {
|
||||||
"exists": True,
|
"exists": True,
|
||||||
|
|
Loading…
Add table
Reference in a new issue