2020-09-07 09:42:59 +00:00
|
|
|
from importlib import import_module
|
|
|
|
|
2020-12-15 10:12:27 +00:00
|
|
|
from django.contrib.auth import BACKEND_SESSION_KEY
|
2020-09-07 09:42:59 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth import user_logged_in
|
|
|
|
from django.core.cache import cache
|
2018-11-27 02:21:47 +00:00
|
|
|
from django.dispatch import receiver
|
2020-09-14 03:22:45 +00:00
|
|
|
from django_cas_ng.signals import cas_user_authenticated
|
2019-02-27 00:45:00 +00:00
|
|
|
|
2020-04-28 13:00:22 +00:00
|
|
|
from jms_oidc_rp.signals import openid_user_login_failed, openid_user_login_success
|
2018-11-09 06:54:38 +00:00
|
|
|
|
2020-04-26 12:36:17 +00:00
|
|
|
from .signals import post_auth_success, post_auth_failed
|
2018-11-09 06:54:38 +00:00
|
|
|
|
|
|
|
|
2020-09-07 09:42:59 +00:00
|
|
|
@receiver(user_logged_in)
|
|
|
|
def on_user_auth_login_success(sender, user, request, **kwargs):
|
|
|
|
if settings.USER_LOGIN_SINGLE_MACHINE_ENABLED:
|
|
|
|
user_id = 'single_machine_login_' + str(user.id)
|
|
|
|
session_key = cache.get(user_id)
|
|
|
|
if session_key and session_key != request.session.session_key:
|
|
|
|
session = import_module(settings.SESSION_ENGINE).SessionStore(session_key)
|
|
|
|
session.delete()
|
|
|
|
cache.set(user_id, request.session.session_key, None)
|
|
|
|
|
|
|
|
|
2020-04-28 13:00:22 +00:00
|
|
|
@receiver(openid_user_login_success)
|
2020-04-26 12:36:17 +00:00
|
|
|
def on_oidc_user_login_success(sender, request, user, **kwargs):
|
2020-12-15 10:12:27 +00:00
|
|
|
request.session[BACKEND_SESSION_KEY] = 'OIDCAuthCodeBackend'
|
2020-04-26 12:36:17 +00:00
|
|
|
post_auth_success.send(sender, user=user, request=request)
|
2018-11-09 06:54:38 +00:00
|
|
|
|
|
|
|
|
2020-04-28 13:00:22 +00:00
|
|
|
@receiver(openid_user_login_failed)
|
2020-04-26 12:36:17 +00:00
|
|
|
def on_oidc_user_login_failed(sender, username, request, reason, **kwargs):
|
2020-12-15 10:12:27 +00:00
|
|
|
request.session[BACKEND_SESSION_KEY] = 'OIDCAuthCodeBackend'
|
2020-04-26 12:36:17 +00:00
|
|
|
post_auth_failed.send(sender, username=username, request=request, reason=reason)
|
2020-09-14 03:22:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(cas_user_authenticated)
|
|
|
|
def on_cas_user_login_success(sender, request, user, **kwargs):
|
2020-12-15 10:12:27 +00:00
|
|
|
request.session[BACKEND_SESSION_KEY] = 'CASBackend'
|
|
|
|
post_auth_success.send(sender, user=user, request=request)
|