File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change
1
+ # -*- coding: utf-8 -*-
2
+ from rest_framework .authentication import SessionAuthentication
3
+
4
+
5
+ class CsrfExemptSessionAuthentication (SessionAuthentication ):
6
+ """
7
+ Session authentication class exempt of CSRF.
8
+
9
+ DRF by default when using a ``SessionAuthentication`` it enforces CSRF.
10
+
11
+ See: https://github.com/encode/django-rest-framework/blob/3.9.0/rest_framework/authentication.py#L134-L144
12
+ """
13
+
14
+ def enforce_csrf (self , request ):
15
+ return
Original file line number Diff line number Diff line change 29
29
from readthedocs .integrations .utils import normalize_request_payload
30
30
from readthedocs .projects .models import Project
31
31
32
+ from ..authentication import CsrfExemptSessionAuthentication
33
+
34
+
32
35
log = logging .getLogger (__name__ )
33
36
34
37
GITHUB_EVENT_HEADER = 'HTTP_X_GITHUB_EVENT'
@@ -418,6 +421,11 @@ class WebhookView(APIView):
418
421
be.
419
422
"""
420
423
424
+ # We want to avoid CSRF checking when authenticating by user/password on
425
+ # this API endpoint so we can make a request like:
426
+ # curl -X POST -d "branches=branch" -u user:pass -e URL /api/v2/webhook/test-builds/{pk}/
427
+ authentication_classes = [CsrfExemptSessionAuthentication ]
428
+
421
429
VIEW_MAP = {
422
430
Integration .GITHUB_WEBHOOK : GitHubWebhookView ,
423
431
Integration .GITLAB_WEBHOOK : GitLabWebhookView ,
You can’t perform that action at this time.
0 commit comments