Skip to content

Commit

Permalink
feature: create authentication class for flows user-api-token
Browse files Browse the repository at this point in the history
  • Loading branch information
elitonzky committed Sep 29, 2023
1 parent 9a4a246 commit 7764da9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
26 changes: 26 additions & 0 deletions marketplace/accounts/authorizations.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions marketplace/wpp_templates/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from .requests import TemplateMessageRequest
from .languages import LANGUAGES

from marketplace.accounts.authorizations import FlowsOIDCAuthentication


WHATSAPP_VERSION = settings.WHATSAPP_VERSION

Expand All @@ -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
Expand Down

0 comments on commit 7764da9

Please sign in to comment.