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
......@@ -144,4 +144,5 @@ cython_debug/
# Misc
.DS_Store
.vscode
\ No newline at end of file
.vscode
dev/
\ No newline at end of file
[flake8]
# https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/#coding-style
max-line-length=119
exclude=migrations
exclude=*/migrations/*
import unicodedata
from django.conf import settings
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):
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.urls import include
from django.urls import include, path
from oidc_auth.auth import using_oidc_auth
urlpatterns = [
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 = [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'oidc_auth.context_processors.login_url',
'oidc_auth.context_processors.logout_url',
],
},
},
......@@ -108,12 +110,10 @@ AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
]
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = os.getenv('LOGOUT_REDIRECT_URL', '/')
if DEBUG is True:
LOGIN_URL = '/admin'
# Mozilla OIDC
# https://mozilla-django-oidc.readthedocs.io/en/stable/
......
......@@ -30,15 +30,15 @@
<a class="nav-link dropdown-toggle" href="#" id="userMenu" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ request.user.username }}
</a>
<div class="dropdown-menu" aria-labelledby="userMenu">
<form action="{% url 'oidc_logout' %}" method="post">
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="userMenu">
<form action="{{ LOGOUT_URL }}" method="post">
{% csrf_token %}
<input type="submit" value="logout" style="background-color: white" class="dropdown-item">
</form>
</div>
</li>
{% else %}
<a href="{% url 'oidc_authentication_init' %}" class="nav-link">Login</a>
<a href="{{ LOGIN_URL }}" class="nav-link">Login</a>
{% endif %}
</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