Signals

Signals allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place.

django-cas-ng defines two signals:

  • cas_user_authenticated

  • cas_user_logout

django_cas_ng.signals.cas_user_authenticated

Sent on successful authentication, the CASBackend will fire the cas_user_authenticated signal.

Arguments sent with this signal

sender

[CASBackend] The authentication backend instance that authenticated the user.

user

[str] The user instance that was just authenticated.

created

[bool] Boolean as to whether the user was just created.

attributes

[Dict] Attributes returned during by the CAS during authentication.

ticket

[str] The ticket used to authenticate the user with the CAS.

service

[str] The service used to authenticate the user with the CAS.

request

[HttpRequest] The request that was used to login.

django_cas_ng.signals.cas_user_logout

Sent on user logout. Will be fired over manual logout or logout via CAS SingleLogOut query.

Arguments sent with this signal

sender

[str] manual if manual logout, slo on SingleLogOut

user

[str] The user instance that is logged out.

session

[Session] The current session we are loging out.

ticket

[str] The ticket used to authenticate the user with the CAS. (if found, else value if set to None)

Receiver Example

Here is a simple example to use @receiver decorator to receive signals.

You can also check the signal usage in example app.

#
# File: signals.py
#

import json

from django.dispatch import receiver
from django_cas_ng.signals import cas_user_authenticated, cas_user_logout


@receiver(cas_user_authenticated)
def cas_user_authenticated_callback(sender, **kwargs):
    args = {}
    args.update(kwargs)
    print('''cas_user_authenticated_callback:
    user: %s
    created: %s
    attributes: %s
    ''' % (
        args.get('user'),
        args.get('created'),
        json.dumps(args.get('attributes'), sort_keys=True, indent=2)))


@receiver(cas_user_logout)
def cas_user_logout_callback(sender, **kwargs):
    args = {}
    args.update(kwargs)
    print('''cas_user_logout_callback:
    user: %s
    session: %s
    ticket: %s
    ''' % (
        args.get('user'),
        args.get('session'),
        args.get('ticket')))

Test cases

Also checkout the test cases source code to see it usage.

tests.test_signals.test_signal_not_fired_if_auth_fails(monkeypatch, django_user_model)[source]

Test that the cas_user_authenticated signal is not fired when CAS authentication fails.

tests.test_signals.test_signal_when_user_already_exists(monkeypatch, django_user_model)[source]

Test that when CAS authentication creates a user, the signal is called with created = False

tests.test_signals.test_signal_when_user_is_created(monkeypatch, django_user_model)[source]

Test that when CAS authentication creates a user, the signal is called with created = True

tests.test_signals.test_signal_when_user_logout_manual(monkeypatch, django_user_model)[source]
tests.test_signals.test_signal_when_user_logout_slo(monkeypatch, django_user_model, settings)[source]