Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/928-endpoint-to-set-csrf-token-c…
Browse files Browse the repository at this point in the history
…ookie' into 741-graphql-api
  • Loading branch information
Kurocon committed Feb 17, 2025
2 parents a7a4f2d + 09cbfdf commit 5347b42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions amelie/api/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from amelie.api.decorators import authentication_optional, authentication_required
from amelie.api.exceptions import NotLoggedInError

from django.http import JsonResponse
from django.views.decorators.csrf import ensure_csrf_cookie
from django.middleware.csrf import get_token
from django.views.decorators.http import require_GET

from modernrpc.core import rpc_method, REQUEST_KEY


Expand Down Expand Up @@ -129,3 +134,9 @@ def get_authenticated_apps(**kwargs) -> Union[List[Dict], None]:
else:
return None

@require_GET
@ensure_csrf_cookie # Ensures the CSRF cookie is set
def get_csrf_token(request):
response = JsonResponse({"message": "CSRF cookie set"})
response["X-CSRFToken"] = get_token(request) # Send CSRF token in headers
return response
2 changes: 2 additions & 0 deletions amelie/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from modernrpc.views import RPCEntryPoint

from amelie.companies.views import vivatbanner_get
from amelie.api.authentication import get_csrf_token

app_name = 'api'

Expand All @@ -12,4 +13,5 @@
path('', RPCEntryPoint.as_view(protocol=Protocol.JSON_RPC), name="jsonrpc_mountpoint"),
path('docs/', RPCEntryPoint.as_view(enable_doc=True, enable_rpc=False, template_name="api/doc_index.html")),
path('vivat_banners/', vivatbanner_get, name='vivatbanner_get'),
path('csrf-token/', get_csrf_token, name='get_csrf_token'),
]

0 comments on commit 5347b42

Please sign in to comment.