File tree Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Expand file tree Collapse file tree 2 files changed +23
-3
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 1
- # -*- coding: utf-8 -*-
2
-
3
1
"""Endpoints integrating with Github, Bitbucket, and other webhooks."""
4
2
5
3
import json
23
21
from readthedocs .integrations .utils import normalize_request_payload
24
22
from readthedocs .projects .models import Project
25
23
24
+ from ..authentication import CsrfExemptSessionAuthentication
25
+
26
26
27
27
log = logging .getLogger (__name__ )
28
28
@@ -349,7 +349,7 @@ class IsAuthenticatedOrHasToken(permissions.IsAuthenticated):
349
349
"""
350
350
351
351
def has_permission (self , request , view ):
352
- has_perm = ( super ().has_permission (request , view ) )
352
+ has_perm = super ().has_permission (request , view )
353
353
return has_perm or 'token' in request .data
354
354
355
355
@@ -422,6 +422,11 @@ class WebhookView(APIView):
422
422
be.
423
423
"""
424
424
425
+ # We want to avoid CSRF checking when authenticating by user/password on
426
+ # this API endpoint so we can make a request like:
427
+ # curl -X POST -d "branches=branch" -u user:pass -e URL /api/v2/webhook/test-builds/{pk}/
428
+ authentication_classes = [CsrfExemptSessionAuthentication ]
429
+
425
430
VIEW_MAP = {
426
431
Integration .GITHUB_WEBHOOK : GitHubWebhookView ,
427
432
Integration .GITLAB_WEBHOOK : GitLabWebhookView ,
You can’t perform that action at this time.
0 commit comments