2018-12-13 17:02:08 +00:00
|
|
|
"""passbook multi-factor authentication engine"""
|
|
|
|
from logging import getLogger
|
|
|
|
|
2018-12-14 08:49:34 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth import login
|
2018-12-13 17:02:08 +00:00
|
|
|
from django.http import Http404
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, reverse
|
2018-12-14 08:49:34 +00:00
|
|
|
from django.views.generic import View
|
2018-12-13 17:02:08 +00:00
|
|
|
|
|
|
|
from passbook.core.models import User
|
2018-12-14 08:49:34 +00:00
|
|
|
from passbook.lib.utils.reflection import class_to_path, path_to_class
|
2018-12-13 17:02:08 +00:00
|
|
|
|
|
|
|
LOGGER = getLogger(__name__)
|
|
|
|
|
|
|
|
class MultiFactorAuthenticator(View):
|
|
|
|
"""Wizard-like Multi-factor authenticator"""
|
|
|
|
|
|
|
|
SESSION_FACTOR = 'passbook_factor'
|
|
|
|
SESSION_PENDING_FACTORS = 'passbook_pending_factors'
|
|
|
|
SESSION_PENDING_USER = 'passbook_pending_user'
|
2018-12-14 08:49:34 +00:00
|
|
|
SESSION_USER_BACKEND = 'passbook_user_backend'
|
2018-12-13 17:02:08 +00:00
|
|
|
|
|
|
|
pending_user = None
|
|
|
|
pending_factors = []
|
|
|
|
|
2018-12-14 08:49:34 +00:00
|
|
|
factors = settings.AUTHENTICATION_FACTORS.copy()
|
2018-12-13 17:02:08 +00:00
|
|
|
|
|
|
|
_current_factor = None
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
# Extract pending user from session (only remember uid)
|
|
|
|
if MultiFactorAuthenticator.SESSION_PENDING_USER in request.session:
|
|
|
|
self.pending_user = get_object_or_404(
|
|
|
|
User, id=self.request.session[MultiFactorAuthenticator.SESSION_PENDING_USER])
|
|
|
|
else:
|
|
|
|
raise Http404
|
|
|
|
# Write pending factors to session
|
|
|
|
if MultiFactorAuthenticator.SESSION_PENDING_FACTORS in request.session:
|
|
|
|
self.pending_factors = request.session[MultiFactorAuthenticator.SESSION_PENDING_FACTORS]
|
|
|
|
else:
|
2018-12-14 08:49:34 +00:00
|
|
|
self.pending_factors = self.factors.copy()
|
|
|
|
# Read and instantiate factor from session
|
|
|
|
factor_class = None
|
|
|
|
if MultiFactorAuthenticator.SESSION_FACTOR not in request.session:
|
|
|
|
factor_class = self.pending_factors[0]
|
|
|
|
else:
|
|
|
|
factor_class = request.session[MultiFactorAuthenticator.SESSION_FACTOR]
|
|
|
|
factor = path_to_class(factor_class)
|
2018-12-13 17:02:08 +00:00
|
|
|
self._current_factor = factor(self)
|
|
|
|
self._current_factor.request = request
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
"""pass get request to current factor"""
|
2018-12-14 08:49:34 +00:00
|
|
|
LOGGER.debug("Passing GET to %s", class_to_path(self._current_factor.__class__))
|
2018-12-13 17:02:08 +00:00
|
|
|
return self._current_factor.get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
"""pass post request to current factor"""
|
2018-12-14 08:49:34 +00:00
|
|
|
LOGGER.debug("Passing POST to %s", class_to_path(self._current_factor.__class__))
|
2018-12-13 17:02:08 +00:00
|
|
|
return self._current_factor.post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def user_ok(self):
|
|
|
|
"""Redirect to next Factor"""
|
2018-12-14 08:49:34 +00:00
|
|
|
LOGGER.debug("Factor %s passed", class_to_path(self._current_factor.__class__))
|
|
|
|
# Remove passed factor from pending factors
|
|
|
|
if class_to_path(self._current_factor.__class__) in self.pending_factors:
|
|
|
|
self.pending_factors.remove(class_to_path(self._current_factor.__class__))
|
2018-12-13 17:02:08 +00:00
|
|
|
next_factor = None
|
|
|
|
if self.pending_factors:
|
|
|
|
next_factor = self.pending_factors.pop()
|
|
|
|
self.request.session[MultiFactorAuthenticator.SESSION_PENDING_FACTORS] = \
|
|
|
|
self.pending_factors
|
2018-12-14 08:49:34 +00:00
|
|
|
self.request.session[MultiFactorAuthenticator.SESSION_FACTOR] = next_factor
|
|
|
|
LOGGER.debug("Rendering Factor is %s", next_factor)
|
|
|
|
return redirect(reverse('passbook_core:mfa'))
|
2018-12-13 17:02:08 +00:00
|
|
|
# User passed all factors
|
|
|
|
LOGGER.debug("User passed all factors, logging in")
|
2018-12-14 08:49:34 +00:00
|
|
|
return self._user_passed()
|
2018-12-13 17:02:08 +00:00
|
|
|
|
|
|
|
def user_invalid(self):
|
|
|
|
"""Show error message, user could not be authenticated"""
|
|
|
|
LOGGER.debug("User invalid")
|
|
|
|
# TODO: Redirect to error view
|
2018-12-14 08:49:34 +00:00
|
|
|
|
|
|
|
def _user_passed(self):
|
|
|
|
"""User Successfully passed all factors"""
|
|
|
|
# user = authenticate(request=self.request, )
|
|
|
|
backend = self.request.session[MultiFactorAuthenticator.SESSION_USER_BACKEND]
|
|
|
|
login(self.request, self.pending_user, backend=backend)
|
|
|
|
LOGGER.debug("Logged in user %s", self.pending_user)
|
|
|
|
return redirect(reverse('passbook_core:overview'))
|