Skip to content

Commit 1d496b4

Browse files
authored
Actually make the contract attach API not require CSRF (#2895)
1 parent 64f8a2c commit 1d496b4

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

b2b/views/v0/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
)
2525
from courses.models import CourseRun
2626
from ecommerce.models import Discount, Product
27+
from main.authentication import CsrfExemptSessionAuthentication
2728
from main.constants import USER_MSG_TYPE_B2B_ENROLL_SUCCESS
2829

2930

@@ -86,12 +87,14 @@ class AttachContractApi(APIView):
8687
"""View for attaching a user to a B2B contract."""
8788

8889
permission_classes = [IsAuthenticated]
90+
authentication_classes = [
91+
CsrfExemptSessionAuthentication,
92+
]
8993

9094
@extend_schema(
9195
request=None,
9296
responses=ContractPageSerializer(many=True),
9397
)
94-
@csrf_exempt
9598
def post(self, request, enrollment_code: str, format=None): # noqa: A002, ARG002
9699
"""
97100
Use the provided enrollment code to attach the user to a B2B contract.

main/authentication.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Custom authentication handlers for DRF."""
2+
3+
from rest_framework.authentication import SessionAuthentication
4+
5+
6+
class CsrfExemptSessionAuthentication(SessionAuthentication):
7+
"""
8+
Authentication handler that ignores CSRF.
9+
10+
Otherwise, SessionAuthentication will enforce CSRF check, even if you tell
11+
it not to.
12+
"""
13+
14+
def enforce_csrf(self, request): # noqa: ARG002
15+
"""No-op CSRF enforcement"""
16+
17+
return

0 commit comments

Comments
 (0)