Commit b9ff851b authored by Kris Steinhoff's avatar Kris Steinhoff
Browse files

Merge branch 'login-views' into 'master'

Login views

Closes #1

See merge request !3
parents 72fd877e 160a6a7e
Pipeline #17843 passed with stage
in 32 seconds
...@@ -145,3 +145,4 @@ cython_debug/ ...@@ -145,3 +145,4 @@ cython_debug/
# Misc # Misc
.DS_Store .DS_Store
.vscode .vscode
dev/
\ No newline at end of file
[flake8] [flake8]
# https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/#coding-style # https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/#coding-style
max-line-length=119 max-line-length=119
exclude=migrations exclude=*/migrations/*
import unicodedata import unicodedata
from django.conf import settings
from mozilla_django_oidc.auth import OIDCAuthenticationBackend from mozilla_django_oidc.auth import OIDCAuthenticationBackend
def using_oidc_auth():
return (
settings.OIDC_RP_CLIENT_ID and
settings.OIDC_RP_CLIENT_SECRET and
settings.OIDC_OP_AUTHORIZATION_ENDPOINT and
settings.OIDC_OP_TOKEN_ENDPOINT and
settings.OIDC_OP_USER_ENDPOINT
)
def generate_username(email): def generate_username(email):
return unicodedata.normalize('NFKC', email).split('@')[0] return unicodedata.normalize('NFKC', email).split('@')[0]
......
from django.urls import reverse
from django.conf import settings
from oidc_auth.auth import using_oidc_auth
def login_url(request):
return {'LOGIN_URL': settings.LOGIN_URL}
def logout_url(request):
return {'LOGOUT_URL': reverse('oidc_logout') if using_oidc_auth() else reverse('logout')}
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<div class="card">
<div class="card-body">
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-6">
<label for="username">Username</label>
<input class="form-control" type="text" placeholder="Username" autofocus autocapitalize="none" autocomplete="username" maxlength="150" required id="username">
</div>
<div class="form-group col-md-6">
<label for="password">Password</label>
<input class="form-control" type="password" placeholder="Password" autofocus autocomplete="current-password" required id="password">
</div>
</div>
<button type="submit" class="btn btn-primary">Login</button>
<input type="hidden" name="next" value="{{ next }}">
</form>
</div>
<div class="card-footer">
<a href="{% url 'password_reset' %}">Lost password?</a>
</div>
</div>
{% endblock %}
from django.conf.urls import url from django.conf.urls import url
from django.urls import include from django.urls import include, path
from oidc_auth.auth import using_oidc_auth
urlpatterns = [ urlpatterns = [
url(r'^oidc/', include('mozilla_django_oidc.urls')), url(r'^oidc/', include('mozilla_django_oidc.urls')),
] ]
if not using_oidc_auth():
urlpatterns += [path('accounts/', include('django.contrib.auth.urls'))]
...@@ -92,6 +92,8 @@ TEMPLATES = [ ...@@ -92,6 +92,8 @@ TEMPLATES = [
'django.template.context_processors.request', 'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth', 'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages', 'django.contrib.messages.context_processors.messages',
'oidc_auth.context_processors.login_url',
'oidc_auth.context_processors.logout_url',
], ],
}, },
}, },
...@@ -108,12 +110,10 @@ AUTHENTICATION_BACKENDS = [ ...@@ -108,12 +110,10 @@ AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend', 'django.contrib.auth.backends.ModelBackend',
] ]
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/' LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = os.getenv('LOGOUT_REDIRECT_URL', '/') LOGOUT_REDIRECT_URL = os.getenv('LOGOUT_REDIRECT_URL', '/')
if DEBUG is True:
LOGIN_URL = '/admin'
# Mozilla OIDC # Mozilla OIDC
# https://mozilla-django-oidc.readthedocs.io/en/stable/ # https://mozilla-django-oidc.readthedocs.io/en/stable/
......
...@@ -30,15 +30,15 @@ ...@@ -30,15 +30,15 @@
<a class="nav-link dropdown-toggle" href="#" id="userMenu" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <a class="nav-link dropdown-toggle" href="#" id="userMenu" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ request.user.username }} {{ request.user.username }}
</a> </a>
<div class="dropdown-menu" aria-labelledby="userMenu"> <div class="dropdown-menu dropdown-menu-right" aria-labelledby="userMenu">
<form action="{% url 'oidc_logout' %}" method="post"> <form action="{{ LOGOUT_URL }}" method="post">
{% csrf_token %} {% csrf_token %}
<input type="submit" value="logout" style="background-color: white" class="dropdown-item"> <input type="submit" value="logout" style="background-color: white" class="dropdown-item">
</form> </form>
</div> </div>
</li> </li>
{% else %} {% else %}
<a href="{% url 'oidc_authentication_init' %}" class="nav-link">Login</a> <a href="{{ LOGIN_URL }}" class="nav-link">Login</a>
{% endif %} {% endif %}
</div> </div>
</div> </div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment