2021-12-09 07:47:21 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.contrib.auth import get_user_model
|
2022-02-25 11:23:59 +00:00
|
|
|
from django.conf import settings
|
2021-12-09 07:47:21 +00:00
|
|
|
from django.db import transaction
|
|
|
|
|
|
|
|
from common.utils import get_logger
|
|
|
|
from authentication.errors import reason_choices, reason_user_invalid
|
|
|
|
from .signals import (
|
|
|
|
saml2_user_authenticated, saml2_user_authentication_failed,
|
|
|
|
saml2_create_or_update_user
|
|
|
|
)
|
2022-03-14 03:11:25 +00:00
|
|
|
from ..base import JMSModelBackend
|
2021-12-09 07:47:21 +00:00
|
|
|
|
|
|
|
__all__ = ['SAML2Backend']
|
|
|
|
|
2022-04-13 12:24:56 +00:00
|
|
|
logger = get_logger(__name__)
|
2021-12-09 07:47:21 +00:00
|
|
|
|
|
|
|
|
2022-03-14 03:11:25 +00:00
|
|
|
class SAML2Backend(JMSModelBackend):
|
2022-02-25 11:23:59 +00:00
|
|
|
@staticmethod
|
|
|
|
def is_enabled():
|
|
|
|
return settings.AUTH_SAML2
|
2021-12-09 07:47:21 +00:00
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def get_or_create_from_saml_data(self, request, **saml_user_data):
|
|
|
|
log_prompt = "Get or Create user [SAML2Backend]: {}"
|
|
|
|
logger.debug(log_prompt.format('start'))
|
|
|
|
|
|
|
|
user, created = get_user_model().objects.get_or_create(
|
|
|
|
username=saml_user_data['username'], defaults=saml_user_data
|
|
|
|
)
|
|
|
|
logger.debug(log_prompt.format("user: {}|created: {}".format(user, created)))
|
|
|
|
|
|
|
|
logger.debug(log_prompt.format("Send signal => saml2 create or update user"))
|
|
|
|
saml2_create_or_update_user.send(
|
|
|
|
sender=self, request=request, user=user, created=created, attrs=saml_user_data
|
|
|
|
)
|
|
|
|
return user, created
|
|
|
|
|
|
|
|
def authenticate(self, request, saml_user_data=None, **kwargs):
|
2022-08-04 06:40:33 +00:00
|
|
|
log_prompt = "Process authenticate [SAML2Backend]: {}"
|
2021-12-09 07:47:21 +00:00
|
|
|
logger.debug(log_prompt.format('Start'))
|
|
|
|
if saml_user_data is None:
|
2021-12-13 09:47:56 +00:00
|
|
|
logger.error(log_prompt.format('saml_user_data is missing'))
|
2021-12-09 07:47:21 +00:00
|
|
|
return None
|
|
|
|
|
2021-12-13 09:47:56 +00:00
|
|
|
logger.debug(log_prompt.format('saml data, {}'.format(saml_user_data)))
|
2021-12-09 07:47:21 +00:00
|
|
|
username = saml_user_data.get('username')
|
|
|
|
if not username:
|
2022-08-04 06:40:33 +00:00
|
|
|
logger.warning(log_prompt.format('username is missing'))
|
2021-12-09 07:47:21 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
user, created = self.get_or_create_from_saml_data(request, **saml_user_data)
|
|
|
|
|
|
|
|
if self.user_can_authenticate(user):
|
|
|
|
logger.debug(log_prompt.format('SAML2 user login success'))
|
|
|
|
saml2_user_authenticated.send(
|
|
|
|
sender=self, request=request, user=user, created=created
|
|
|
|
)
|
|
|
|
return user
|
|
|
|
else:
|
|
|
|
logger.debug(log_prompt.format('SAML2 user login failed'))
|
|
|
|
saml2_user_authentication_failed.send(
|
|
|
|
sender=self, request=request, username=username,
|
|
|
|
reason=reason_choices.get(reason_user_invalid)
|
|
|
|
)
|
|
|
|
return None
|