authentik/passbook/recovery/views.py

25 lines
900 B
Python
Raw Normal View History

"""recovery views"""
from django.contrib import messages
from django.contrib.auth import login
from django.http import Http404, HttpRequest, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.utils.translation import gettext as _
from django.views import View
2020-05-16 15:11:53 +01:00
from passbook.core.models import Token
2020-05-16 15:11:53 +01:00
class UseTokenView(View):
"""Use token to login"""
def get(self, request: HttpRequest, uuid: str) -> HttpResponse:
2020-05-16 15:11:53 +01:00
"""Check if token exists, log user in and delete token."""
token: Token = get_object_or_404(Token, pk=uuid)
if token.is_expired:
token.delete()
raise Http404
2020-05-16 15:11:53 +01:00
login(request, token.user, backend="django.contrib.auth.backends.ModelBackend")
token.delete()
messages.warning(request, _("Used recovery-link to authenticate."))
2019-12-31 11:51:16 +00:00
return redirect("passbook_core:overview")