2019-10-08 12:30:17 +00:00
|
|
|
"""passbook multi-factor authentication engine"""
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.http import HttpRequest
|
2020-05-08 14:10:27 +00:00
|
|
|
from django.shortcuts import reverse
|
2019-10-08 12:30:17 +00:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
from structlog import get_logger
|
|
|
|
|
|
|
|
from passbook.core.models import Nonce
|
|
|
|
from passbook.factors.email.tasks import send_mails
|
|
|
|
from passbook.factors.email.utils import TemplateEmailMessage
|
2020-05-07 19:30:52 +00:00
|
|
|
from passbook.flows.factor_base import AuthenticationFactor
|
2020-05-08 14:10:27 +00:00
|
|
|
from passbook.flows.planner import PLAN_CONTEXT_PENDING_USER
|
2019-10-08 12:30:17 +00:00
|
|
|
from passbook.lib.config import CONFIG
|
|
|
|
|
|
|
|
LOGGER = get_logger()
|
|
|
|
|
|
|
|
|
|
|
|
class EmailFactorView(AuthenticationFactor):
|
|
|
|
"""Dummy factor for testing with multiple factors"""
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2019-12-31 11:51:16 +00:00
|
|
|
kwargs["show_password_forget_notice"] = CONFIG.y(
|
|
|
|
"passbook.password_reset.enabled"
|
|
|
|
)
|
2019-10-08 12:30:17 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2020-05-08 14:10:27 +00:00
|
|
|
pending_user = self.executor.plan.context[PLAN_CONTEXT_PENDING_USER]
|
|
|
|
nonce = Nonce.objects.create(user=pending_user)
|
2019-10-08 12:30:17 +00:00
|
|
|
# Send mail to user
|
|
|
|
message = TemplateEmailMessage(
|
2019-12-31 11:51:16 +00:00
|
|
|
subject=_("Forgotten password"),
|
|
|
|
template_name="email/account_password_reset.html",
|
2020-05-08 14:10:27 +00:00
|
|
|
to=[pending_user.email],
|
2019-10-08 12:30:17 +00:00
|
|
|
template_context={
|
2019-12-31 11:51:16 +00:00
|
|
|
"url": self.request.build_absolute_uri(
|
|
|
|
reverse(
|
|
|
|
"passbook_core:auth-password-reset",
|
|
|
|
kwargs={"nonce": nonce.uuid},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
2020-05-08 14:10:27 +00:00
|
|
|
send_mails(self.executor.current_factor, message)
|
2019-12-31 11:51:16 +00:00
|
|
|
messages.success(request, _("Check your E-Mails for a password reset link."))
|
2020-05-08 14:10:27 +00:00
|
|
|
return self.executor.cancel()
|
2019-10-08 12:30:17 +00:00
|
|
|
|
|
|
|
def post(self, request: HttpRequest):
|
|
|
|
"""Just redirect to next factor"""
|
2020-05-08 14:10:27 +00:00
|
|
|
return self.executor.factor_ok()
|