Skip to content

Commit b79876a

Browse files
committed
Add captcha challenge on admin login page
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent c7a2630 commit b79876a

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

fedcode/forms.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#
99

1010
from django import forms
11+
from django.contrib.admin.forms import AdminAuthenticationForm
1112
from django.contrib.auth.forms import AuthenticationForm
1213
from django.contrib.auth.forms import UserCreationForm
1314
from django.contrib.auth.models import User
@@ -156,3 +157,12 @@ class UserLoginForm(AuthenticationForm):
156157
},
157158
widget=ReCaptchaV2Checkbox,
158159
)
160+
161+
162+
class AdminLoginForm(AdminAuthenticationForm):
163+
captcha = ReCaptchaField(
164+
error_messages={
165+
"required": ("Captcha is required"),
166+
},
167+
widget=ReCaptchaV2Checkbox(),
168+
)

fedcode/templates/admin_login.html

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{% extends "admin/base_site.html" %}
2+
{% load i18n static %}
3+
4+
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" href="{% static "admin/css/login.css" %}">
5+
{{ form.media }}
6+
{% endblock %}
7+
8+
{% block bodyclass %}{{ block.super }} login{% endblock %}
9+
10+
{% block usertools %}{% endblock %}
11+
12+
{% block nav-global %}{% endblock %}
13+
14+
{% block nav-sidebar %}{% endblock %}
15+
16+
{% block content_title %}{% endblock %}
17+
18+
{% block nav-breadcrumbs %}{% endblock %}
19+
20+
{% block content %}
21+
22+
{% if form.errors %}
23+
{% for error in form.errors.values %}
24+
<p>{{ error }}</p>
25+
{% endfor %}
26+
{% endif %}
27+
28+
29+
<div id="content-main">
30+
31+
{% if user.is_authenticated %}
32+
<p class="errornote">
33+
{% blocktranslate trimmed %}
34+
You are authenticated as {{ username }}, but are not authorized to
35+
access this page. Would you like to login to a different account?
36+
{% endblocktranslate %}
37+
</p>
38+
{% endif %}
39+
40+
<form action="{{ app_path }}" method="post" id="login-form">{% csrf_token %}
41+
<div class="form-row">
42+
{{ form.username.errors }}
43+
{{ form.username.label_tag }} {{ form.username }}
44+
</div>
45+
<div class="form-row">
46+
{{ form.password.errors }}
47+
{{ form.password.label_tag }} {{ form.password }}
48+
<input type="hidden" name="next" value="{{ next }}">
49+
</div>
50+
{% url 'admin_password_reset' as password_reset_url %}
51+
{% if password_reset_url %}
52+
<div class="password-reset-link">
53+
<a href="{{ password_reset_url }}">{% translate 'Forgotten your password or username?' %}</a>
54+
</div>
55+
{% endif %}
56+
<div class="field">
57+
<div class="control">
58+
{{ form.captcha }}
59+
</div>
60+
</div>
61+
<div class="submit-row">
62+
<input type="submit" value="{% translate 'Log in' %}">
63+
</div>
64+
</form>
65+
</div>
66+
{% endblock %}

fedcode/views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import json
1111
import logging
1212
import os.path
13-
from urllib.parse import urlparse
1413

1514
import requests
1615
from django.contrib import messages
@@ -47,6 +46,7 @@
4746
from fedcode.activitypub import AP_CONTEXT
4847
from fedcode.activitypub import create_activity_obj
4948
from fedcode.activitypub import has_valid_header
49+
from fedcode.forms import AdminLoginForm
5050
from fedcode.forms import CreateGitRepoForm
5151
from fedcode.forms import CreateNoteForm
5252
from fedcode.forms import CreateReviewForm
@@ -889,3 +889,8 @@ def revoke_token(request):
889889
},
890890
)
891891
return JsonResponse(json.loads(r.content), status=r.status_code, content_type=AP_CONTENT_TYPE)
892+
893+
894+
class AdminLoginView(LoginView):
895+
template_name = "admin_login.html"
896+
authentication_form = AdminLoginForm

federatedcode/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
FEDERATEDCODE_CLIENT_ID = env.str("FEDERATEDCODE_CLIENT_ID")
6262
FEDERATEDCODE_CLIENT_SECRET = env.str("FEDERATEDCODE_CLIENT_SECRET")
6363

64-
RECAPTCHA_PUBLIC_KEY = env.str("RECAPTCHA_PUBLIC_KEY")
65-
RECAPTCHA_PRIVATE_KEY = env.str("RECAPTCHA_PRIVATE_KEY")
64+
RECAPTCHA_PUBLIC_KEY = env.str("RECAPTCHA_PUBLIC_KEY", "")
65+
RECAPTCHA_PRIVATE_KEY = env.str("RECAPTCHA_PRIVATE_KEY", "")
6666
SILENCED_SYSTEM_CHECKS = ["captcha.recaptcha_test_key_error"]
6767
RECAPTCHA_DOMAIN = env.str("RECAPTCHA_DOMAIN", "www.recaptcha.net")
6868

federatedcode/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.urls import path
1414

1515
from fedcode import views
16+
from fedcode.views import AdminLoginView
1617
from fedcode.views import CreateReview
1718
from fedcode.views import CreateSync
1819
from fedcode.views import CreatGitView
@@ -45,6 +46,7 @@
4546
from fedcode.views import redirect_vulnerability
4647

4748
urlpatterns = [
49+
path("admin/login/", AdminLoginView.as_view(), name="admin-login"),
4850
path("admin/", admin.site.urls),
4951
path(".well-known/webfinger", WebfingerView.as_view(), name="web-finger"),
5052
path("", HomeView.as_view(), name="home-page"),

0 commit comments

Comments
 (0)