Skip to main content

A Django authentication backend for Ucam-WebAuth a.k.a. Raven

Project description

Introduction

django-ucamwebauth is a library which provides use of Cambridge University's Raven authentication for Django. It provides a Django authentication backend which can be added to AUTHENTICATION_BACKENDS in the Django settings module.

Use

Install django-ucamwebauth using pip:

pip install django-ucamwebauth

Then you can enable it within your Django project's settings.py:

AUTHENTICATION_BACKENDS = (
    'ucamwebauth.backends.RavenAuthBackend',
    'django.contrib.auth.backends.ModelBackend'
)

This allows both normal Django login and Raven login.

You should then enable the URLs for ucamwebauth:

#Django 1.11
urlpatterns = [
    ...
    url(r'', include('ucamwebauth.urls')),
    ...
]

#Django >=2.0
urlpatterns = [
    ...
    path(r'', include('ucamwebauth.urls')),
    ...
]

Minimum Config Settings

You then need to configure the app's settings. Raven has a live and test environments, the URL and certificate details are given below.

There are four minimum config settings:

UCAMWEBAUTH_LOGIN_URL: a string representing the URL for the Raven login redirect.
UCAMWEBAUTH_LOGOUT_URL: a string representing the logout URL for Raven.
UCAMWEBAUTH_RETURN_URL: the URL of your app which the Raven service should return the user to after authentication.
    (Default is generated automatically from the urlconf)
UCAMWEBAUTH_LOGOUT_REDIRECT: a string representing the URL to where the user is redirected when she logs out of the app
    (Default to '/').
UCAMWEBAUTH_NOT_CURRENT: a boolean value representing if raven users that are currently not members of the university
    should be allowed to log in (Default to False). More info: http://www.ucs.cam.ac.uk/accounts/ravenleaving
UCAMWEBAUTH_CERTS: a dictionary including key names and their associated certificates which can be downloaded from the
    Raven project pages.
UCAMWEBAUTH_TIMEOUT: An integer with the time (in seconds) that has to pass to consider an authentication timed out
    (Default to 30).
UCAMWEBAUTH_REDIRECT_AFTER_LOGIN: The url where you want to redirect the user after login (Default to '/').
UCAMWEBAUTH_CREATE_USER: This defaults to True, allowing the autocreation of users who have been successfully 
authenticated by Raven, but do not exist in the local database. The user is created with set_unusable_password().

An example, referencing the Raven test environment is given below:

UCAMWEBAUTH_LOGIN_URL = 'https://test.legacy.raven.cam.ac.uk/auth/authenticate.html'
UCA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olP
dhjssTh1fs3+EfixZY7wb92+f+vqOV5Hu6n55Y7LXArKoqLshVfyR1uOjB5/UlFi
FFS0LG7L1O0wMKbzDLJ94G3UNwBXOD+KFL5zcfCAzZNFqCentiHO08LfX/tr7+q6
DaMuIh8RQpOfowDsIhaiB2YtK1A+B6dJxnHAq+a6sX7ZBwtZoWyBtUalrNMyGGN4
eaEuRUIL9ilCksI7Nb4X91dyryO/L/QI4XQtbaz7j8K8hSJJ4MGb6Boc1Ov69ZEk
GY322DIS84WfeSem6Ujy4TyZSkmUPOiiT9bFN2MUomKJAgMBAAEwDQYJKoZIhvcN
AQELBQADggEBAGg+4IDXLtypkoxteb8KQWvt11d4xMljNmJ6k20gBpPEd5nzocQ7
MeSv9l66NrlsHCJ+BwJjJDgIS4dwkofu+hmkLNE6/d3uHugKazv3ySw+g5oIcLNB
vjgIhbD4krVSh4LvZxQR+sffQnrkzSSAB6/6QMTtAcor61A+0Xo4OhvPHMWOcS9I
rT57/9Slr9eEtSUcpKs+cyY9G6t9GxdC627diApsw+mG2X1raFIROXuXMzQQysEI
6SzxRFof2AutEgcyYRERLbHIqH5K1EgjLWs3s4PujOhTvhUoJZ7OlTjNmzNrFXKj
9nU4Uo6qClXDVKK7JecERsAfIedKgFGbQqY=
-----END CERTIFICATE-----
"""}

Errors

There are five possible exceptions that can be raised using this module: MalformedResponseError, InvalidResponseError, PublicKeyNotFoundError, and OtherStatusCode that return HTTP 500, or UserNotAuthorised that returns 403. You can catch these exceptions using process_exception middleware (https://docs.djangoproject.com/en/2.2/topics/http/middleware/#process_exception) to customize what the user will receive as a response. The module has a default behaviour for these exceptions with HTTP error codes and using their corresponding templates. To use the default behaviour just add:

MIDDLEWARE = [
    ...
    'ucamwebauth.middleware.DefaultErrorBehaviour',
    ...
]

INSTALLED_APPS = [
    ...
    'ucamwebauth',
    ...
]

You can also rewrite the ucamwebauth_<httpcode>.html templates. You only need to add the following lines to your own if you want to show the user the error message:

{% for message in messages %}
    {{ message }}<br/>
{% endfor %}

Authentication request parameters

This parameters are sent with the authentication request and allows the developer to tune the request to fit their app:

UCAMWEBAUTH_DESC: A text description of the resource requesting authentication which may be displayed to the end-user
    to further identify the resource to which his/her identity is being disclosed. Can be omitted.
UCAMWEBAUTH_IACT: The value 'yes' requires that a re-authentication exchange takes place with the user. This could be
    used prior to a sensitive transaction in an attempt to ensure that a previously authenticated user is still present
    at the browser. The value 'no' requires that the authentication request will only succeed if the user's identity
    can be returned without interacting with the user. This could be used as an optimisation to take advantage of any
    existing authentication but without actively soliciting one. If omitted or empty, then a previously established
    identity may be returned if the WLS supports doing so, and if not then the user will be prompted as necessary.
UCAMWEBAUTH_MSG: Text describing why authentication is being requested on this occasion which may be displayed to the
    end-user. Can be omitted.
UCAMWEBAUTH_PARAMS: Data that will be returned unaltered to the WAA in any 'authentication response message' issued as
    a result of this request. This could be used to carry the identity of the resource originally requested or other
    WAA state, or to associate authentication requests with their eventual replies. When returned, this data will be
    protected by the digital signature applied to the authentication response message but nothing else is done to
    ensure the integrity or confidentiality of this data - the WAA MUST take responsibility for this if necessary.
UCAMWEBAUTH_FAIL: If this parameter is 'yes' and the outcome of the request is anything other than success (i.e. the
    status code would be anything other than 200) then the WLS MUST return an informative error to the user and MUST
    not redirect back to the WAA. Setting this makes it easier to implement WAAs at the expense of a loss of
    flexibility in error handling.

The details of these can be found in the Raven WLS protocol documentation, here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-ucamwebauth-2.0.0.tar.gz (20.1 kB view hashes)

Uploaded Source

Built Distribution

django_ucamwebauth-2.0.0-py3-none-any.whl (23.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page