From 7764da9c642d1783549bb16eb642ba796c563ccf Mon Sep 17 00:00:00 2001 From: elitonzky Date: Fri, 29 Sep 2023 20:14:44 -0300 Subject: [PATCH] feature: create authentication class for flows user-api-token --- marketplace/accounts/authorizations.py | 26 ++++++++++++++++++++++++++ marketplace/wpp_templates/views.py | 3 +++ 2 files changed, 29 insertions(+) create mode 100644 marketplace/accounts/authorizations.py diff --git a/marketplace/accounts/authorizations.py b/marketplace/accounts/authorizations.py new file mode 100644 index 00000000..b7e165dc --- /dev/null +++ b/marketplace/accounts/authorizations.py @@ -0,0 +1,26 @@ +from mozilla_django_oidc.contrib.drf import OIDCAuthentication +from marketplace.connect.client import ConnectProjectClient + + +class FlowsOIDCAuthentication(OIDCAuthentication): # pragma: no cover + def is_flows_token(self, token): + """ + Check if the token is likely a Flows token based on its length. + """ + return len(token) == 40 + + def authenticate(self, request): + access_token = self.get_access_token(request) + if not access_token: + return None + + if self.is_flows_token(access_token): + client = ConnectProjectClient() + response = client.get_user_api_token( + request.data["user_email"], access_token + ) + + if response: + return (request.user, access_token) + + return super().authenticate(request) diff --git a/marketplace/wpp_templates/views.py b/marketplace/wpp_templates/views.py index cde8388f..c56e102e 100644 --- a/marketplace/wpp_templates/views.py +++ b/marketplace/wpp_templates/views.py @@ -29,6 +29,8 @@ from .requests import TemplateMessageRequest from .languages import LANGUAGES +from marketplace.accounts.authorizations import FlowsOIDCAuthentication + WHATSAPP_VERSION = settings.WHATSAPP_VERSION @@ -49,6 +51,7 @@ class TemplateMessageViewSet(viewsets.ModelViewSet): lookup_field = "uuid" serializer_class = TemplateMessageSerializer pagination_class = CustomResultsPagination + authentication_classes = [FlowsOIDCAuthentication] def filter_queryset(self, queryset): params = self.request.query_params