2022-06-24 17:08:58 +00:00
|
|
|
"""GraphQL API for SelfPrivacy."""
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
import typing
|
2023-04-12 13:59:23 +00:00
|
|
|
|
2022-06-24 17:08:58 +00:00
|
|
|
from strawberry.permission import BasePermission
|
|
|
|
from strawberry.types import Info
|
2023-04-12 13:59:23 +00:00
|
|
|
from strawberry.extensions import Extension
|
2022-06-24 17:08:58 +00:00
|
|
|
|
2022-12-26 15:20:58 +00:00
|
|
|
from selfprivacy_api.actions.api_tokens import is_token_valid
|
2023-04-12 13:59:23 +00:00
|
|
|
from selfprivacy_api.utils.localization import Localization
|
2022-06-24 17:08:58 +00:00
|
|
|
|
2022-06-24 18:14:20 +00:00
|
|
|
|
2022-06-24 17:08:58 +00:00
|
|
|
class IsAuthenticated(BasePermission):
|
|
|
|
"""Is authenticated permission"""
|
2022-06-24 18:14:20 +00:00
|
|
|
|
2022-06-24 17:08:58 +00:00
|
|
|
message = "You must be authenticated to access this resource."
|
|
|
|
|
|
|
|
def has_permission(self, source: typing.Any, info: Info, **kwargs) -> bool:
|
2022-08-25 17:03:56 +00:00
|
|
|
token = info.context["request"].headers.get("Authorization")
|
|
|
|
if token is None:
|
|
|
|
token = info.context["request"].query_params.get("token")
|
|
|
|
if token is None:
|
2022-06-24 17:08:58 +00:00
|
|
|
return False
|
2022-08-25 17:03:56 +00:00
|
|
|
return is_token_valid(token.replace("Bearer ", ""))
|
2023-04-12 13:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LocaleExtension(Extension):
|
|
|
|
"""Parse the Accept-Language header and set the locale in the context as one of the supported locales."""
|
|
|
|
|
|
|
|
def resolve(self, _next, root, info: Info, *args, **kwargs):
|
|
|
|
locale = Localization().get_locale(
|
|
|
|
info.context["request"].headers.get("Accept-Language")
|
|
|
|
)
|
|
|
|
info.context["locale"] = locale
|
|
|
|
return _next(root, info, *args, **kwargs)
|