2019-10-10 12:05:16 +00:00
|
|
|
"""recovery views"""
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth import login
|
|
|
|
from django.http import Http404, HttpRequest, HttpResponse
|
2020-10-18 12:34:22 +00:00
|
|
|
from django.shortcuts import redirect
|
2019-10-10 12:05:16 +00:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
from django.views import View
|
|
|
|
|
2020-10-18 12:34:22 +00:00
|
|
|
from passbook.core.models import Token, TokenIntents
|
2019-10-10 12:05:16 +00:00
|
|
|
|
|
|
|
|
2020-05-16 14:11:53 +00:00
|
|
|
class UseTokenView(View):
|
|
|
|
"""Use token to login"""
|
2019-10-10 12:05:16 +00:00
|
|
|
|
2020-10-18 12:34:22 +00:00
|
|
|
def get(self, request: HttpRequest, key: str) -> HttpResponse:
|
2020-05-16 14:11:53 +00:00
|
|
|
"""Check if token exists, log user in and delete token."""
|
2020-10-18 12:34:22 +00:00
|
|
|
tokens = Token.filter_not_expired(key=key, intent=TokenIntents.INTENT_RECOVERY)
|
|
|
|
if not tokens.exists():
|
2019-10-10 12:05:16 +00:00
|
|
|
raise Http404
|
2020-10-18 12:34:22 +00:00
|
|
|
token = tokens.first()
|
2020-05-16 14:11:53 +00:00
|
|
|
login(request, token.user, backend="django.contrib.auth.backends.ModelBackend")
|
|
|
|
token.delete()
|
2019-10-10 12:05:16 +00:00
|
|
|
messages.warning(request, _("Used recovery-link to authenticate."))
|
2019-12-31 11:51:16 +00:00
|
|
|
return redirect("passbook_core:overview")
|