Skip to content

Commit 8e07fbb

Browse files
authored
Merge pull request #14 from nuke-ops/ops
auth fix + secret pages
2 parents fbd02c4 + 47e6253 commit 8e07fbb

8 files changed

Lines changed: 93 additions & 21 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ __pycache__/
1111
.venv/
1212

1313
# app
14+
/secrets.json
1415
/static/CACHE/
1516
/media/*
1617
!/media/robots.txt
1718
!/media/sitemap.xml
1819
config.json
1920
*.log
21+
/ops/*
22+
/static/ops/*
23+

auth_app/templates/auth/login.html

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@
1010
<link rel="icon" href="{% static "shared/images/nstrat.ico" %}">
1111
<link rel="stylesheet" href="{% static 'shared/css/bulma.css' %}">
1212
<link rel="stylesheet" href="{% static 'auth_app/css/login.css' %}">
13-
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback"
14-
defer></script>
15-
<script>
16-
window.onloadTurnstileCallback = function () {
17-
turnstile.render("#login_box", {
18-
sitekey: "0x4AAAAAAAOs7enSVbUYlAD-",
19-
callback: function(token) {
20-
console.log(`Challenge Success ${token}`);
21-
},
22-
});
23-
};
24-
</script>
2513
</head>
2614
<body>
2715
<div id="login_box" class="box">
@@ -33,8 +21,11 @@
3321
<form method="post" action="{% url 'login' %}">
3422
{% csrf_token %}
3523
{{ form }}
36-
<button type="submit" class="button is-link">Login</button>
37-
</form>
38-
</div>
39-
</body>
24+
</br>
25+
<div class="cf-turnstile" data-sitekey="{{ TURNSTILE_SITE_KEY }}"></div>
26+
<button type="submit" class="button is-link">Login</button>
27+
</form>
28+
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" defer></script>
29+
</div>
30+
</body>
4031
</html>

auth_app/views.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
from django.shortcuts import render, redirect
2-
from django.contrib.auth import authenticate, login, logout
1+
import requests
2+
from django.conf import settings
33
from django.contrib import messages
4+
from django.contrib.auth import authenticate, login, logout
5+
from django.shortcuts import redirect, render
6+
47
from .forms import LoginForm
58

9+
TURNSTILE_VERIFY_URL = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
10+
11+
12+
def verify_turnstile(token, ip=None):
13+
data = {
14+
"secret": settings.CLOUDFLARE_TURNSTILE_SECRET_KEY,
15+
"response": token,
16+
}
17+
if ip:
18+
data["remoteip"] = ip
19+
20+
r = requests.post(TURNSTILE_VERIFY_URL, data=data, timeout=5)
21+
return r.json()
22+
623

724
def user_login(request):
825
if request.user.is_authenticated:
@@ -12,6 +29,16 @@ def user_login(request):
1229
form = LoginForm(initial={"next": initial_next})
1330

1431
if request.method == "POST":
32+
# cf turnstile
33+
token = request.POST.get("cf-turnstile-response")
34+
if not token:
35+
messages.error(request, "Captcha missing.")
36+
return redirect("login")
37+
result = verify_turnstile(token, request.META.get("REMOTE_ADDR"))
38+
if not result.get("success"):
39+
messages.error(request, "Captcha failed. Try again.")
40+
41+
# auth
1542
form = LoginForm(request.POST)
1643
if form.is_valid():
1744
username = form.cleaned_data["username"]
@@ -24,7 +51,11 @@ def user_login(request):
2451
else:
2552
messages.error(request, "Invalid username or password.")
2653

27-
return render(request, "auth/login.html", {"form": form})
54+
return render(
55+
request,
56+
"auth/login.html",
57+
{"form": form, "TURNSTILE_SITE_KEY": settings.CLOUDFLARE_TURNSTILE_SITE_KEY},
58+
)
2859

2960

3061
def user_logout(request):

error_handlers/urls.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from django.urls import path
2+
from . import views
3+
4+
app_name = "error_handlers"

error_handlers/views.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ def error_400(request, exception):
1212
)
1313

1414

15+
def error_401(request, exception):
16+
status_code = 401
17+
message = "UNAUTHORIZED"
18+
return render(
19+
request,
20+
"error.html",
21+
{"status_code": " ".join(str(status_code)), "message": message},
22+
status=status_code,
23+
)
24+
25+
1526
def error_403(request, exception):
1627
status_code = 403
1728
message = "PERMISSION DENIED"

nukeops/settings.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,24 @@
180180
"compressor.finders.CompressorFinder",
181181
]
182182
COMPRESS_ROOT = STATIC_ROOT if conf["static_path"] else "static/"
183+
184+
try:
185+
from ops.overwrite import app_overwrite
186+
187+
INSTALLED_APPS = app_overwrite(INSTALLED_APPS)
188+
except ImportError:
189+
pass
190+
191+
# Secrets
192+
BASE_DIR = Path(__file__).resolve().parent.parent
193+
194+
SECRETS_FILE = BASE_DIR / "secrets.json"
195+
196+
if SECRETS_FILE.exists():
197+
with open(SECRETS_FILE) as f:
198+
secrets = json.load(f)
199+
else:
200+
secrets = {}
201+
202+
CLOUDFLARE_TURNSTILE_SITE_KEY = secrets.get("TURNSTILE_SITE_KEY", "")
203+
CLOUDFLARE_TURNSTILE_SECRET_KEY = secrets.get("TURNSTILE_SECRET_KEY", "")

nukeops/urls.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
1. Import the include() function: from django.urls import include, path
1515
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1616
"""
17-
from django.conf.urls import handler404, handler500
17+
18+
# from django.conf.urls import handler404, handler500
1819
from django.contrib import admin
1920
from django.urls import include, path, re_path
2021
from django.views.static import serve
2122

2223
from auth_app.views import user_login
23-
from error_handlers.views import error_400, error_403, error_404, error_500
24+
from error_handlers.views import error_400, error_401, error_403, error_404, error_500
2425

2526
from .settings import MEDIA_ROOT
2627
from .views import media_access
@@ -38,6 +39,14 @@
3839
]
3940

4041
handler400 = error_400
42+
handler401 = error_401
4143
handler403 = error_403
4244
handler404 = error_404
4345
handler500 = error_500
46+
47+
try:
48+
from ops.overwrite import urls_overwrite
49+
50+
urlpatterns = urls_overwrite(urlpatterns)
51+
except ImportError:
52+
pass

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
wheel==0.42.0
22
channels==4.0.0
33
Django==5.0.2
4+
requests==2.32.5
45
mysqlclient==2.2.0
56
daphne==4.0.0
67
channels-redis==4.1.0

0 commit comments

Comments
 (0)