2019-02-26 14:40:58 +00:00
|
|
|
"""passbook core authentication views"""
|
2018-11-11 12:41:48 +00:00
|
|
|
from django.contrib import messages
|
2019-02-25 19:46:23 +00:00
|
|
|
from django.contrib.auth import login, logout
|
2020-05-10 14:20:17 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2018-11-11 12:41:48 +00:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2019-02-25 19:46:23 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, reverse
|
2018-11-11 12:41:48 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-11-23 08:44:30 +00:00
|
|
|
from django.views import View
|
2019-10-01 08:24:10 +00:00
|
|
|
from structlog import get_logger
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2020-05-10 14:20:17 +00:00
|
|
|
from passbook.core.models import Nonce
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-10-04 08:08:53 +00:00
|
|
|
LOGGER = get_logger()
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-02-26 13:07:47 +00:00
|
|
|
|
2018-11-23 08:44:30 +00:00
|
|
|
class LogoutView(LoginRequiredMixin, View):
|
|
|
|
"""Log current user out"""
|
|
|
|
|
2020-05-10 14:20:17 +00:00
|
|
|
def dispatch(self, request: HttpRequest) -> HttpResponse:
|
2018-11-23 08:44:30 +00:00
|
|
|
"""Log current user out"""
|
|
|
|
logout(request)
|
|
|
|
messages.success(request, _("You've successfully been logged out."))
|
2019-12-31 11:51:16 +00:00
|
|
|
return redirect(reverse("passbook_core:auth-login"))
|
2018-12-10 12:51:38 +00:00
|
|
|
|
|
|
|
|
2019-02-25 19:46:23 +00:00
|
|
|
class PasswordResetView(View):
|
|
|
|
"""Temporarily authenticate User and allow them to reset their password"""
|
|
|
|
|
2020-05-10 14:20:17 +00:00
|
|
|
def get(self, request: HttpRequest, nonce_uuid: str) -> HttpResponse:
|
2019-02-25 19:46:23 +00:00
|
|
|
"""Authenticate user with nonce and redirect to password change view"""
|
|
|
|
# 3. (Optional) Trap user in password change view
|
2020-05-10 14:20:17 +00:00
|
|
|
nonce = get_object_or_404(Nonce, uuid=nonce_uuid)
|
2019-02-25 19:46:23 +00:00
|
|
|
# Workaround: hardcoded reference to ModelBackend, needs testing
|
2019-12-31 11:51:16 +00:00
|
|
|
nonce.user.backend = "django.contrib.auth.backends.ModelBackend"
|
2019-02-25 19:46:23 +00:00
|
|
|
login(request, nonce.user)
|
|
|
|
nonce.delete()
|
2019-12-31 11:51:16 +00:00
|
|
|
messages.success(
|
2020-02-24 13:40:12 +00:00
|
|
|
request, _(("Temporarily authenticated, please change your password")),
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
|
|
|
return redirect("passbook_core:user-change-password")
|