/
login_view.py
53 lines (40 loc) · 2.14 KB
/
login_view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from django.contrib.auth import login as auth_login
from django.contrib.auth import views as auth_views
from django.shortcuts import redirect
if TYPE_CHECKING:
from django.contrib.auth.forms import AuthenticationForm
from django.http.response import HttpResponseRedirect
logger = logging.getLogger(__name__)
class LoginView(auth_views.LoginView):
"""
View to extend the default login behavior from :class:`~django.contrib.auth.views.LoginView` with
multi-factor-authentication.
"""
#: A boolean that controls whether or not authenticated users accessing the login page will be redirected as if they
#: had just successfully logged in.
redirect_authenticated_user = True
#: The template to render (see :class:`~django.views.generic.base.TemplateResponseMixin`)
template_name = "authentication/login.html"
def form_valid(self, form: AuthenticationForm) -> HttpResponseRedirect:
"""
This function overwrites :meth:`~django.views.generic.edit.FormMixin.form_valid` which is called if the login
form is valid. In case the user has mfa-keys configured, the login is delegated to
:class:`~integreat_cms.cms.views.authentication.webauthn.webauthn_login_view.WebAuthnLoginView`. Else, the default method
:func:`~django.contrib.auth.login` is used to log the user in. After that, the user is redirected to
:attr:`~integreat_cms.core.settings.LOGIN_REDIRECT_URL`.
:param form: User login form
:return: Redirect user to mfa login view or to :attr:`~integreat_cms.core.settings.LOGIN_REDIRECT_URL`
"""
user = form.get_user()
if not user.passwordless_authentication_enabled:
if user.fido_keys.exists():
self.request.session["mfa_user_id"] = user.id
return redirect("public:login_webauthn")
if user.totp_key:
self.request.session["mfa_user_id"] = user.id
return redirect("public:login_totp")
auth_login(self.request, form.get_user())
return redirect(self.get_success_url())