Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ First, install it with

$ pip install ctlsettings

or add `ctlsettings==0.3.3` to your `requirements.txt`.
or add `ctlsettings==0.4.8` to your `requirements.txt`.

### Dependencies

Expand All @@ -26,6 +26,7 @@ The following libraries are used in some way, so they'll need to be installed:
* boto3
* sentry-sdk
* gunicorn
* djangorestframework

### Use it

Expand Down
51 changes: 51 additions & 0 deletions ctlsettings/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import requests
from django.conf import settings
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status


def verify_turnstile_token(token, remote_ip=None):
url = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
data = {
"secret": settings.TURNSTILE_SECRET_KEY,
"response": token,
}
if remote_ip:
data["remoteip"] = remote_ip
response = requests.post(url, data=data)
result = response.json()
return result.get("success", False)


class LoginAPIView(APIView):
def post(self, request, *args, **kwargs):

print("Request data:", request.data)
username = request.data.get('username')
password = request.data.get('password')
if not username or not password:
return Response(
{'error': 'Username and password must be provided'},
status=status.HTTP_400_BAD_REQUEST
)
# Skip Turnstile in local development
if settings.TURNSTILE_ENABLE and "localhost" not in request.META.get(
"HTTP_ORIGIN"):
token = request.data.get("turnstile_token")
if not token:
return Response(
{"error": "Missing Turnstile token"},
status=status.HTTP_400_BAD_REQUEST
)
is_valid = verify_turnstile_token(
token,
request.META.get("REMOTE_ADDR")
)
if not is_valid:
return Response(
{"error": "Invalid Turnstile token"},
status=status.HTTP_403_FORBIDDEN
)
# Continue with authentication logic here
return Response({"success": True})
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='ctlsettings',
version='0.4.7',
version='0.4.8',
author='Columbia University Center for Teaching and Learning',
author_email='ctl-dev@columbia.edu',
url='https://github.com/ccnmtl/ctlsettings',
Expand All @@ -20,6 +20,7 @@
'requests',
'gunicorn',
'django-impersonate',
'djangorestframework'
],
scripts=[],
license='GPL-3.0-or-later',
Expand Down