RSS

django-cas-ng Example Project

An example project to demo how easy to integrate with django-cas-ng

django-cas-ng is Django CAS (Central Authentication Service) 1.0/2.0/3.0 client library to support SSO (Single Sign On) and SLO (Single Logout)!

To demo how easy to integrate with django-cas-ng, here is an example project to show how to use django-cas-ng to work with CAS login/logout.

The CAS login flow outline as below. The App Service here is refer to this demo project.

CAS example login flow

Example Project

Here is steps to create this example project:

Create a django project and install django-cas-ng

$ django-admin startproject mysite

$ cd mysite

$ pip install django-cas-ng
Collecting django-cas-ng
  Using cached https://files.pythonhosted.org/packages/a1/9d/014a146bdab0bd1cdba8f09c2a15ffeddd26604b3b894bcd702c5f6a68cd/django_cas_ng-4.0.1-py2.py3-none-any.whl
...
...
...
Installing collected packages: django-cas-ng
Successfully installed django-cas-ng-4.0.1

$ pip freeze | grep django-cas-ng >> requirements.txt

Config django-cas-ng

mysite/settings.py

Config INSTALLED_APPS: Add django_cas_ng to INSTALLED_APPS:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_cas_ng'
]

Config MIDDLEWARE: Add django_cas_ng.middleware.CASMiddleware to MIDDLEWARE:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django_cas_ng.middleware.CASMiddleware'
]

Config AUTHENTICATION_BACKENDS: Add django_cas_ng.backends.CASBackend to AUTHENTICATION_BACKENDS:

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'django_cas_ng.backends.CASBackend',
)

Config CAS server URL and CAS protocol version: To make this example project work, I created a demo CAS server host on Heroku. The server URL is https://django-cas-ng-demo-server.herokuapp.com/cas/

CAS_SERVER_URL = 'https://django-cas-ng-demo-server.herokuapp.com/cas/'
CAS_VERSION = '3'

mysite/urls.py

Add accounts/login and accounts/logout URL. To demo the login/logout, I also add a simple home page to show login/logout, the view is view.index.

from django.contrib import admin
from django.urls import path
import django_cas_ng.views
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('admin/', admin.site.urls),
    path('accounts/login', django_cas_ng.views.LoginView.as_view(), name='cas_ng_login'),
    path('accounts/logout', django_cas_ng.views.LogoutView.as_view(), name='cas_ng_logout'),
]

mysite/views.py

Create a new file named mysite/views.py and implement index method.

The demo view will check user is authenticated by using request.user.is_authenticated. If it is authenticated, it will get logged user name from request.user.username

from django.http import HttpResponse


def index(request):
    if request.user.is_authenticated:
        return HttpResponse('<p>Welcome to <a href="https://djangocas.dev">django-cas-ng</a>.</p><p>You logged in as <strong>%s</strong>.</p><p><a href="/accounts/logout">Logout</a></p>' % request.user)
    else:
        return HttpResponse('<p>Welcome to <a href="https://djangocas.dev">django-cas-ng</a>.</p><p><a href="/accounts/login">Login</a></p>')

Generate database tables

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, django_cas_ng, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying django_cas_ng.0001_initial... OK
  Applying sessions.0001_initial... OK

You can also get the entire project source code at django-cas-ng/example. The main change is commit 4dea11de05.

Run test server

$ python manage.py runserver
Performing system checks...

...
...
Django version 3.0.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

To play to this demo project, you can simply run following commands to clone the code and run your local server instance:

git clone https://github.com/django-cas-ng/example
cd example
pip install -r requirements.txt
python manage.py runserver

Live Demo

If you want to see a live demo, you can click here. The following username/password can be used to login in demo server.

username: admin
password: django-cas-ng

Demo CAS Server

To make the entire flow work, I also created a demo CAS server to work with this example project, open source at demo-cas-server.

Reference