2018-12-14 08:49:34 +00:00
|
|
|
"""passbook multi-factor authentication engine"""
|
2019-03-10 20:47:08 +00:00
|
|
|
from inspect import Signature
|
2018-12-14 08:49:34 +00:00
|
|
|
|
2019-02-25 15:41:33 +00:00
|
|
|
from django.contrib import messages
|
2019-03-10 20:47:08 +00:00
|
|
|
from django.contrib.auth import _clean_credentials
|
|
|
|
from django.contrib.auth.signals import user_login_failed
|
2018-12-18 14:34:15 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.forms.utils import ErrorList
|
2019-02-26 13:07:47 +00:00
|
|
|
from django.shortcuts import redirect, reverse
|
2018-12-18 14:34:15 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2018-12-14 08:49:34 +00:00
|
|
|
from django.views.generic import FormView
|
2019-10-01 08:24:10 +00:00
|
|
|
from structlog import get_logger
|
2018-12-14 08:49:34 +00:00
|
|
|
|
2019-02-24 21:39:09 +00:00
|
|
|
from passbook.core.forms.authentication import PasswordFactorForm
|
2019-02-25 19:46:23 +00:00
|
|
|
from passbook.core.models import Nonce
|
2019-02-26 13:07:47 +00:00
|
|
|
from passbook.core.tasks import send_email
|
2019-10-07 14:33:48 +00:00
|
|
|
from passbook.factors.base import AuthenticationFactor
|
|
|
|
from passbook.factors.view import AuthenticationView
|
2018-12-14 08:49:34 +00:00
|
|
|
from passbook.lib.config import CONFIG
|
2019-03-10 20:47:08 +00:00
|
|
|
from passbook.lib.utils.reflection import path_to_class
|
2018-12-14 08:49:34 +00:00
|
|
|
|
2019-10-04 08:08:53 +00:00
|
|
|
LOGGER = get_logger()
|
2018-12-14 08:49:34 +00:00
|
|
|
|
|
|
|
|
2019-03-10 20:47:08 +00:00
|
|
|
def authenticate(request, backends, **credentials):
|
|
|
|
"""If the given credentials are valid, return a User object.
|
2019-10-07 14:33:48 +00:00
|
|
|
|
2019-03-10 20:47:08 +00:00
|
|
|
Customized version of django's authenticate, which accepts a list of backends"""
|
|
|
|
for backend_path in backends:
|
|
|
|
backend = path_to_class(backend_path)()
|
|
|
|
try:
|
|
|
|
signature = Signature.from_callable(backend.authenticate)
|
|
|
|
signature.bind(request, **credentials)
|
|
|
|
except TypeError:
|
2019-10-04 08:21:33 +00:00
|
|
|
LOGGER.debug("Backend doesn't accept our arguments", backend=backend)
|
2019-03-10 20:47:08 +00:00
|
|
|
# This backend doesn't accept these credentials as arguments. Try the next one.
|
|
|
|
continue
|
2019-10-04 08:21:33 +00:00
|
|
|
LOGGER.debug('Attempting authentication...', backend=backend)
|
2019-03-10 20:47:08 +00:00
|
|
|
try:
|
|
|
|
user = backend.authenticate(request, **credentials)
|
|
|
|
except PermissionDenied:
|
2019-10-04 08:21:33 +00:00
|
|
|
LOGGER.debug('Backend threw PermissionDenied', backend=backend)
|
2019-03-10 20:47:08 +00:00
|
|
|
# This backend says to stop in our tracks - this user should not be allowed in at all.
|
|
|
|
break
|
|
|
|
if user is None:
|
|
|
|
continue
|
|
|
|
# Annotate the user object with the path of the backend.
|
|
|
|
user.backend = backend_path
|
|
|
|
return user
|
|
|
|
|
|
|
|
# The credentials supplied are invalid to all backends, fire signal
|
|
|
|
user_login_failed.send(sender=__name__, credentials=_clean_credentials(
|
|
|
|
credentials), request=request)
|
|
|
|
|
2019-02-24 21:39:09 +00:00
|
|
|
class PasswordFactor(FormView, AuthenticationFactor):
|
2018-12-14 08:49:34 +00:00
|
|
|
"""Authentication factor which authenticates against django's AuthBackend"""
|
|
|
|
|
2019-02-24 21:39:09 +00:00
|
|
|
form_class = PasswordFactorForm
|
2018-12-14 12:50:58 +00:00
|
|
|
template_name = 'login/factors/backend.html'
|
2018-12-14 08:49:34 +00:00
|
|
|
|
2019-02-25 15:41:33 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs['show_password_forget_notice'] = CONFIG.y('passbook.password_reset.enabled')
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
if 'password-forgotten' in request.GET:
|
2019-02-25 19:46:23 +00:00
|
|
|
nonce = Nonce.objects.create(user=self.pending_user)
|
|
|
|
LOGGER.debug("DEBUG %s", str(nonce.uuid))
|
2019-02-26 13:07:47 +00:00
|
|
|
# Send mail to user
|
|
|
|
send_email.delay(self.pending_user.email, _('Forgotten password'),
|
|
|
|
'email/account_password_reset.html', {
|
|
|
|
'url': self.request.build_absolute_uri(
|
2019-03-08 11:47:50 +00:00
|
|
|
reverse('passbook_core:auth-password-reset',
|
2019-02-26 13:07:47 +00:00
|
|
|
kwargs={
|
|
|
|
'nonce': nonce.uuid
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
2019-02-25 15:41:33 +00:00
|
|
|
self.authenticator.cleanup()
|
|
|
|
messages.success(request, _('Check your E-Mails for a password reset link.'))
|
|
|
|
return redirect('passbook_core:auth-login')
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
2018-12-14 08:49:34 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
"""Authenticate against django's authentication backend"""
|
|
|
|
uid_fields = CONFIG.y('passbook.uid_fields')
|
|
|
|
kwargs = {
|
|
|
|
'password': form.cleaned_data.get('password'),
|
|
|
|
}
|
|
|
|
for uid_field in uid_fields:
|
|
|
|
kwargs[uid_field] = getattr(self.authenticator.pending_user, uid_field)
|
2018-12-18 14:34:15 +00:00
|
|
|
try:
|
2019-03-10 20:47:08 +00:00
|
|
|
user = authenticate(self.request, self.authenticator.current_factor.backends, **kwargs)
|
2018-12-18 14:34:15 +00:00
|
|
|
if user:
|
|
|
|
# User instance returned from authenticate() has .backend property set
|
|
|
|
self.authenticator.pending_user = user
|
2019-02-16 08:52:37 +00:00
|
|
|
self.request.session[AuthenticationView.SESSION_USER_BACKEND] = user.backend
|
2018-12-18 14:34:15 +00:00
|
|
|
return self.authenticator.user_ok()
|
|
|
|
# No user was found -> invalid credentials
|
|
|
|
LOGGER.debug("Invalid credentials")
|
|
|
|
# Manually inject error into form
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
errors = form._errors.setdefault("password", ErrorList())
|
|
|
|
errors.append(_("Invalid password"))
|
|
|
|
return self.form_invalid(form)
|
|
|
|
except PermissionDenied:
|
|
|
|
# User was found, but permission was denied (i.e. user is not active)
|
2019-10-04 08:21:33 +00:00
|
|
|
LOGGER.debug("Denied access", **kwargs)
|
2018-12-18 14:34:15 +00:00
|
|
|
return self.authenticator.user_invalid()
|