96 lines
3.9 KiB
Python
96 lines
3.9 KiB
Python
# authentic2-auth-fc - authentic2 authentication for FranceConnect
|
|
# Copyright (C) 2019 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import json
|
|
import logging
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.contrib.auth.backends import ModelBackend
|
|
from django.core.exceptions import MultipleObjectsReturned, PermissionDenied
|
|
from django.db import IntegrityError
|
|
|
|
from authentic2 import hooks
|
|
from authentic2.a2_rbac.utils import get_default_ou
|
|
|
|
from . import models, utils
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class FcBackend(ModelBackend):
|
|
def authenticate(self, request=None, sub=None, **kwargs):
|
|
user_info = kwargs.get('user_info')
|
|
user = None
|
|
try:
|
|
try:
|
|
account = models.FcAccount.objects.select_related().get(sub=sub)
|
|
except MultipleObjectsReturned:
|
|
account = models.FcAccount.objects.select_related().get(sub=sub, order=0)
|
|
except models.FcAccount.DoesNotExist:
|
|
logger.debug(u'user with the sub %s does not exist.', sub)
|
|
else:
|
|
user = account.user
|
|
logger.debug(u'found user %s with sub %s', user, sub)
|
|
if not user.is_active:
|
|
logger.info(u'user %s login refused, it is inactive', user)
|
|
raise PermissionDenied
|
|
if user_info:
|
|
User = get_user_model()
|
|
user_qs = User.objects.filter(**kwargs.get('user_filter', {}))
|
|
if user_qs.count() > 1:
|
|
return
|
|
if user_qs.exists():
|
|
user = user_qs.get()
|
|
|
|
if not user:
|
|
user = User(ou=get_default_ou())
|
|
user.set_unusable_password()
|
|
user.save()
|
|
try:
|
|
models.FcAccount.objects.create(
|
|
user=user, sub=sub, order=0, token=json.dumps(kwargs['token'])
|
|
)
|
|
except IntegrityError:
|
|
# uniqueness check failed, as the user is new, it can only means that the sub is not unique
|
|
# let's try again
|
|
user.delete()
|
|
return self.authenticate(sub, **kwargs)
|
|
else:
|
|
logger.debug(
|
|
u'user creation enabled with fc_account (sub : %s - token : %s)',
|
|
sub,
|
|
json.dumps(kwargs['token']),
|
|
)
|
|
hooks.call_hooks('event', name='fc-create', user=user, sub=sub)
|
|
|
|
# always handle given_name and family_name
|
|
updated = []
|
|
if user_info.get('given_name') and user.first_name != user_info['given_name']:
|
|
user.first_name = user_info['given_name']
|
|
updated.append('given name: "%s"' % user_info['given_name'])
|
|
if user_info.get('family_name') and user.last_name != user_info['family_name']:
|
|
user.last_name = user_info['family_name']
|
|
updated.append('family name: "%s"' % user_info['family_name'])
|
|
if updated:
|
|
logger.debug('updated (%s)', ' - '.join(updated))
|
|
utils.apply_user_info_mappings(user, user_info)
|
|
return user
|
|
|
|
def get_saml2_authn_context(self):
|
|
import lasso
|
|
|
|
return lasso.SAML2_AUTHN_CONTEXT_PASSWORD_PROTECTED_TRANSPORT
|