2022-06-29 17:39:46 +00:00
|
|
|
"""API access mutations"""
|
2024-07-26 19:59:44 +00:00
|
|
|
|
2022-06-29 17:39:46 +00:00
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
import datetime
|
2024-11-15 00:35:07 +00:00
|
|
|
from typing import Optional
|
2022-06-29 17:39:46 +00:00
|
|
|
import strawberry
|
2022-08-25 17:03:56 +00:00
|
|
|
from strawberry.types import Info
|
|
|
|
from selfprivacy_api.actions.api_tokens import (
|
|
|
|
CannotDeleteCallerException,
|
|
|
|
InvalidExpirationDate,
|
|
|
|
InvalidUsesLeft,
|
|
|
|
NotFoundException,
|
|
|
|
delete_api_token,
|
|
|
|
get_new_api_recovery_key,
|
2022-12-26 10:53:27 +00:00
|
|
|
use_mnemonic_recovery_token,
|
2022-12-21 15:23:42 +00:00
|
|
|
refresh_api_token,
|
2022-12-26 10:13:37 +00:00
|
|
|
delete_new_device_auth_token,
|
2022-12-26 10:27:10 +00:00
|
|
|
get_new_device_auth_token,
|
2022-12-26 14:03:55 +00:00
|
|
|
use_new_device_auth_token,
|
2022-08-25 17:03:56 +00:00
|
|
|
)
|
2022-06-29 17:39:46 +00:00
|
|
|
from selfprivacy_api.graphql import IsAuthenticated
|
2022-07-07 13:53:19 +00:00
|
|
|
from selfprivacy_api.graphql.mutations.mutation_interface import (
|
|
|
|
GenericMutationReturn,
|
|
|
|
MutationReturnInterface,
|
|
|
|
)
|
2022-06-29 17:39:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class ApiKeyMutationReturn(MutationReturnInterface):
|
2024-11-15 00:35:07 +00:00
|
|
|
key: Optional[str]
|
2022-06-29 17:39:46 +00:00
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-07-05 12:11:41 +00:00
|
|
|
@strawberry.type
|
|
|
|
class DeviceApiTokenMutationReturn(MutationReturnInterface):
|
2024-11-15 00:35:07 +00:00
|
|
|
token: Optional[str]
|
2022-07-05 12:11:41 +00:00
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-06-29 17:39:46 +00:00
|
|
|
@strawberry.input
|
|
|
|
class RecoveryKeyLimitsInput:
|
|
|
|
"""Recovery key limits input"""
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2024-11-15 00:35:07 +00:00
|
|
|
expiration_date: Optional[datetime.datetime] = None
|
|
|
|
uses: Optional[int] = None
|
2022-06-29 17:39:46 +00:00
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-07-05 12:11:41 +00:00
|
|
|
@strawberry.input
|
|
|
|
class UseRecoveryKeyInput:
|
|
|
|
"""Use recovery key input"""
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-07-05 12:11:41 +00:00
|
|
|
key: str
|
|
|
|
deviceName: str
|
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-07-05 12:11:41 +00:00
|
|
|
@strawberry.input
|
|
|
|
class UseNewDeviceKeyInput:
|
|
|
|
"""Use new device key input"""
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-07-05 12:11:41 +00:00
|
|
|
key: str
|
|
|
|
deviceName: str
|
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-06-29 17:39:46 +00:00
|
|
|
@strawberry.type
|
|
|
|
class ApiMutations:
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
2022-07-07 13:53:19 +00:00
|
|
|
def get_new_recovery_api_key(
|
2024-11-15 00:35:07 +00:00
|
|
|
self, limits: Optional[RecoveryKeyLimitsInput] = None
|
2022-07-07 13:53:19 +00:00
|
|
|
) -> ApiKeyMutationReturn:
|
2022-06-29 17:39:46 +00:00
|
|
|
"""Generate recovery key"""
|
2022-08-25 17:03:56 +00:00
|
|
|
if limits is None:
|
|
|
|
limits = RecoveryKeyLimitsInput()
|
|
|
|
try:
|
|
|
|
key = get_new_api_recovery_key(limits.expiration_date, limits.uses)
|
|
|
|
except InvalidExpirationDate:
|
|
|
|
return ApiKeyMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="Expiration date must be in the future",
|
|
|
|
code=400,
|
|
|
|
key=None,
|
|
|
|
)
|
|
|
|
except InvalidUsesLeft:
|
|
|
|
return ApiKeyMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="Uses must be greater than 0",
|
|
|
|
code=400,
|
|
|
|
key=None,
|
|
|
|
)
|
2022-06-29 17:39:46 +00:00
|
|
|
return ApiKeyMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Recovery key generated",
|
|
|
|
code=200,
|
|
|
|
key=key,
|
|
|
|
)
|
2022-07-05 12:11:41 +00:00
|
|
|
|
|
|
|
@strawberry.mutation()
|
2022-07-07 13:53:19 +00:00
|
|
|
def use_recovery_api_key(
|
|
|
|
self, input: UseRecoveryKeyInput
|
|
|
|
) -> DeviceApiTokenMutationReturn:
|
2022-07-05 12:11:41 +00:00
|
|
|
"""Use recovery key"""
|
2022-12-26 10:53:27 +00:00
|
|
|
token = use_mnemonic_recovery_token(input.key, input.deviceName)
|
|
|
|
if token is not None:
|
2022-12-21 10:24:32 +00:00
|
|
|
return DeviceApiTokenMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Recovery key used",
|
|
|
|
code=200,
|
2022-12-26 10:53:27 +00:00
|
|
|
token=token,
|
2022-12-21 10:24:32 +00:00
|
|
|
)
|
2022-12-26 10:53:27 +00:00
|
|
|
else:
|
2022-07-05 12:11:41 +00:00
|
|
|
return DeviceApiTokenMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="Recovery key not found",
|
|
|
|
code=404,
|
|
|
|
token=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
2022-08-25 17:03:56 +00:00
|
|
|
def refresh_device_api_token(self, info: Info) -> DeviceApiTokenMutationReturn:
|
2022-07-05 12:11:41 +00:00
|
|
|
"""Refresh device api token"""
|
2022-12-21 13:09:51 +00:00
|
|
|
token_string = (
|
2022-08-25 17:03:56 +00:00
|
|
|
info.context["request"]
|
|
|
|
.headers.get("Authorization", "")
|
|
|
|
.replace("Bearer ", "")
|
2022-07-07 13:53:19 +00:00
|
|
|
)
|
2022-12-21 13:09:51 +00:00
|
|
|
if token_string is None:
|
2022-07-05 12:11:41 +00:00
|
|
|
return DeviceApiTokenMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="Token not found",
|
|
|
|
code=404,
|
|
|
|
token=None,
|
|
|
|
)
|
2022-12-21 13:09:51 +00:00
|
|
|
|
|
|
|
try:
|
2022-12-21 15:23:42 +00:00
|
|
|
new_token = refresh_api_token(token_string)
|
2022-12-21 13:09:51 +00:00
|
|
|
return DeviceApiTokenMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Token refreshed",
|
|
|
|
code=200,
|
2022-12-21 15:23:42 +00:00
|
|
|
token=new_token,
|
2022-12-21 13:09:51 +00:00
|
|
|
)
|
2022-12-21 15:23:42 +00:00
|
|
|
except NotFoundException:
|
2022-07-05 12:11:41 +00:00
|
|
|
return DeviceApiTokenMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="Token not found",
|
|
|
|
code=404,
|
|
|
|
token=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
2022-08-25 17:03:56 +00:00
|
|
|
def delete_device_api_token(self, device: str, info: Info) -> GenericMutationReturn:
|
2022-07-05 12:11:41 +00:00
|
|
|
"""Delete device api token"""
|
2022-07-07 13:53:19 +00:00
|
|
|
self_token = (
|
2022-08-25 17:03:56 +00:00
|
|
|
info.context["request"]
|
|
|
|
.headers.get("Authorization", "")
|
|
|
|
.replace("Bearer ", "")
|
2022-07-07 13:53:19 +00:00
|
|
|
)
|
2022-08-25 17:03:56 +00:00
|
|
|
try:
|
|
|
|
delete_api_token(self_token, device)
|
|
|
|
except NotFoundException:
|
2022-07-05 12:11:41 +00:00
|
|
|
return GenericMutationReturn(
|
|
|
|
success=False,
|
2022-08-25 17:03:56 +00:00
|
|
|
message="Token not found",
|
|
|
|
code=404,
|
|
|
|
)
|
|
|
|
except CannotDeleteCallerException:
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="Cannot delete caller token",
|
2022-07-05 12:11:41 +00:00
|
|
|
code=400,
|
|
|
|
)
|
2022-08-25 17:03:56 +00:00
|
|
|
except Exception as e:
|
2022-07-05 12:11:41 +00:00
|
|
|
return GenericMutationReturn(
|
|
|
|
success=False,
|
2022-08-25 17:03:56 +00:00
|
|
|
message=str(e),
|
|
|
|
code=500,
|
2022-07-05 12:11:41 +00:00
|
|
|
)
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Token deleted",
|
|
|
|
code=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def get_new_device_api_key(self) -> ApiKeyMutationReturn:
|
|
|
|
"""Generate device api key"""
|
|
|
|
key = get_new_device_auth_token()
|
|
|
|
return ApiKeyMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Device api key generated",
|
|
|
|
code=200,
|
|
|
|
key=key,
|
|
|
|
)
|
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def invalidate_new_device_api_key(self) -> GenericMutationReturn:
|
|
|
|
"""Invalidate new device api key"""
|
|
|
|
delete_new_device_auth_token()
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="New device key deleted",
|
|
|
|
code=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
@strawberry.mutation()
|
2022-07-07 13:53:19 +00:00
|
|
|
def authorize_with_new_device_api_key(
|
|
|
|
self, input: UseNewDeviceKeyInput
|
|
|
|
) -> DeviceApiTokenMutationReturn:
|
2022-07-05 12:11:41 +00:00
|
|
|
"""Authorize with new device api key"""
|
|
|
|
token = use_new_device_auth_token(input.key, input.deviceName)
|
|
|
|
if token is None:
|
|
|
|
return DeviceApiTokenMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message="Token not found",
|
|
|
|
code=404,
|
|
|
|
token=None,
|
|
|
|
)
|
|
|
|
return DeviceApiTokenMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Token used",
|
|
|
|
code=200,
|
|
|
|
token=token,
|
|
|
|
)
|