2015-05-22 19:14:33 +00:00
CAS Server
2016-07-03 16:40:11 +00:00
##########
2015-12-12 16:26:19 +00:00
.. image :: https://travis-ci.org/nitmir/django-cas-server.svg?branch=master
:target: https://travis-ci.org/nitmir/django-cas-server
2015-05-22 19:14:33 +00:00
2015-12-18 12:08:16 +00:00
.. image :: https://img.shields.io/pypi/v/django-cas-server.svg
:target: https://pypi.python.org/pypi/django-cas-server
.. image :: https://img.shields.io/pypi/l/django-cas-server.svg
2015-12-18 18:22:56 +00:00
:target: https://www.gnu.org/licenses/gpl-3.0.html
2015-12-18 12:08:16 +00:00
2016-06-26 08:41:15 +00:00
.. image :: https://api.codacy.com/project/badge/Grade/255c21623d6946ef8802fa7995b61366
:target: https://www.codacy.com/app/valentin-samir/django-cas-server
.. image :: https://api.codacy.com/project/badge/Coverage/255c21623d6946ef8802fa7995b61366
:target: https://www.codacy.com/app/valentin-samir/django-cas-server
2015-12-12 17:02:51 +00:00
CAS Server is a Django application implementing the `CAS Protocol 3.0 Specification
2016-06-26 08:28:13 +00:00
<https://apereo.github.io/cas/4.2.x/protocol/CAS-Protocol-Specification.html> `_.
2015-05-30 17:19:22 +00:00
2015-05-22 19:14:33 +00:00
By defaut, the authentication process use django internal users but you can easily
use any sources (see auth classes in the auth.py file)
2016-07-01 00:38:36 +00:00
The defaut login/logout template use `django-bootstrap3 <https://github.com/dyve/django-bootstrap3> `__
2015-12-12 17:02:51 +00:00
but you can use your own templates using settings variables.
2016-03-18 12:02:36 +00:00
Note that for Django 1.7 compatibility, you need a version of
2016-07-01 00:38:36 +00:00
`django-bootstrap3 <https://github.com/dyve/django-bootstrap3> `__ < 7.0.0
2016-03-18 12:02:36 +00:00
like the 6.2.2 version.
2016-07-03 16:40:11 +00:00
.. contents :: Table of Contents
2015-12-12 17:02:51 +00:00
Features
2016-07-03 16:40:11 +00:00
========
2015-12-12 17:02:51 +00:00
* Support CAS version 1.0, 2.0, 3.0
* Support Single Sign Out
* Configuration of services via the django Admin application
* Fine control on which user's attributes are passed to which service
* Possibility to rename/rewrite attributes per service
* Possibility to require some attribute values per service
2016-07-03 16:44:00 +00:00
* Federated mode between multiple CAS
2015-12-12 17:02:51 +00:00
* Supports Django 1.7, 1.8 and 1.9
* Supports Python 2.7, 3.x
2015-05-22 19:14:33 +00:00
Quick start
2016-07-03 16:40:11 +00:00
===========
2016-07-01 00:38:36 +00:00
1. If you want to make a virtualenv for `` django-cas-server `` , you will need the following
2016-06-26 10:54:18 +00:00
dependencies on a bare debian like system::
virtualenv build-essential python-dev libxml2-dev libxslt1-dev zlib1g-dev
If you want to use python3 instead of python2, replace `` python-dev `` with `` python3-dev `` .
If you intend to run the tox tests you will also need `` python3.4-dev `` depending of the current
version of python3 on your system.
2015-05-22 19:14:33 +00:00
2016-07-01 00:38:36 +00:00
2. Add "cas_server" to your INSTALLED_APPS setting like this::
2015-05-22 19:14:33 +00:00
INSTALLED_APPS = (
2015-12-12 17:02:51 +00:00
'django.contrib.admin',
2015-05-22 19:14:33 +00:00
...
2015-12-12 12:51:59 +00:00
'bootstrap3',
2015-05-22 19:14:33 +00:00
'cas_server',
)
2015-05-30 17:19:22 +00:00
For internatinalization support, add "django.middleware.locale.LocaleMiddleware"
to your MIDDLEWARE_CLASSES setting like this::
MIDDLEWARE_CLASSES = (
...
'django.middleware.locale.LocaleMiddleware',
...
)
2016-07-01 00:38:36 +00:00
3. Include the cas_server URLconf in your project urls.py like this::
2015-05-22 19:14:33 +00:00
2015-12-12 17:02:51 +00:00
urlpatterns = [
url(r'^admin/', admin.site.urls),
...
url(r'^cas/', include('cas_server.urls', namespace="cas_server")),
]
2015-05-22 19:14:33 +00:00
2016-07-01 00:38:36 +00:00
4. Run `python manage.py migrate` to create the cas_server models.
2015-05-22 19:14:33 +00:00
2015-12-13 14:25:44 +00:00
2016-07-01 00:38:36 +00:00
5. You should add some management commands to a crontab: `` clearsessions `` ,
2015-12-13 14:25:44 +00:00
`` cas_clean_tickets `` and `` cas_clean_sessions `` .
2016-07-01 00:38:36 +00:00
* `` clearsessions `` : please see `Clearing the session store <https://docs.djangoproject.com/en/stable/topics/http/sessions/#clearing-the-session-store> `_ .
* `` cas_clean_tickets `` : old tickets and timed-out tickets do not get purge from
the database automatically. They are just marked as invalid. `` cas_clean_tickets ``
is a clean-up management command for this purpose. It send SingleLogOut request
to services with timed out tickets and delete them.
* `` cas_clean_sessions `` : Logout and purge users (sending SLO requests) that are
inactive since more than `` SESSION_COOKIE_AGE `` . The default value for is `` 1209600 ``
seconds (2 weeks). You probably should reduce it to something like `` 86400 `` seconds (1 day).
2015-12-13 14:25:44 +00:00
2016-07-01 00:38:36 +00:00
You could for example do as bellow :
2015-12-13 14:25:44 +00:00
.. code-block ::
0 0 * * * cas-user /path/to/project/manage.py clearsessions
*/5 * * * * cas-user /path/to/project/manage.py cas_clean_tickets
5 0 * * * cas-user /path/to/project/manage.py cas_clean_sessions
2016-07-01 00:38:36 +00:00
6. Start the development server and visit http://127.0.0.1:8000/admin/
2015-05-22 19:14:33 +00:00
to add a first service allowed to authenticate user agains the CAS
(you'll need the Admin app enabled).
2016-07-01 00:38:36 +00:00
7. Visit http://127.0.0.1:8000/cas/ to login with your django users.
2015-12-13 14:25:44 +00:00
2015-12-12 16:26:19 +00:00
Settings
2016-07-03 16:40:11 +00:00
========
2015-12-12 16:26:19 +00:00
All settings are optional. Add them to `` settings.py `` to customize `` django-cas-server `` :
2016-07-03 16:40:11 +00:00
Template settings
-----------------
2015-12-12 16:26:19 +00:00
2016-06-20 11:31:59 +00:00
* `` CAS_LOGO_URL `` : Url to the logo showed in the up left corner on the default
templates. Set it to `` False `` to disable it.
2015-12-12 16:30:46 +00:00
* `` CAS_LOGIN_TEMPLATE `` : Path to the template showed on `` /login `` then the user
is not autenticated. The default is `` "cas_server/login.html" `` .
2015-12-13 14:25:44 +00:00
* `` CAS_WARN_TEMPLATE `` : Path to the template showed on `` /login?service=... `` then
2015-12-12 16:30:46 +00:00
the user is authenticated and has asked to be warned before beeing connected
to a service. The default is `` "cas_server/warn.html" `` .
* `` CAS_LOGGED_TEMPLATE `` : Path to the template showed on `` /login `` then to user is
authenticated. The default is `` "cas_server/logged.html" `` .
* `` CAS_LOGOUT_TEMPLATE `` : Path to the template showed on `` /logout `` then to user
is being disconnected. The default is `` "cas_server/logout.html" ``
* `` CAS_REDIRECT_TO_LOGIN_AFTER_LOGOUT `` : Should we redirect users to `/login` after they
logged out instead of displaying `` CAS_LOGOUT_TEMPLATE `` . The default is `` False `` .
2015-12-12 16:26:19 +00:00
2016-07-03 16:40:11 +00:00
Authentication settings
-----------------------
2015-12-12 16:26:19 +00:00
2016-06-26 10:54:18 +00:00
* `` CAS_AUTH_CLASS `` : A dotted path to a class or a class implementing
2016-07-01 00:38:36 +00:00
`` cas_server.auth.AuthUser `` . The default is `` "cas_server.auth.DjangoAuthUser" ``
2015-12-12 16:26:19 +00:00
2015-12-13 14:25:44 +00:00
* `` SESSION_COOKIE_AGE `` : This is a django settings. Here, it control the delay in seconds after
which inactive users are logged out. The default is `` 1209600 `` (2 weeks). You probably should
reduce it to something like `` 86400 `` seconds (1 day).
2016-06-26 10:54:18 +00:00
* `` CAS_PROXY_CA_CERTIFICATE_PATH `` : Path to certificate authorities file. Usually on linux
2015-12-12 16:30:46 +00:00
the local CAs are in `` /etc/ssl/certs/ca-certificates.crt `` . The default is `` True `` which
tell requests to use its internal certificat authorities. Settings it to `` False `` should
disable all x509 certificates validation and MUST not be done in production.
x509 certificate validation is perform upon PGT issuance.
2015-12-12 16:26:19 +00:00
2015-12-12 16:30:46 +00:00
* `` CAS_SLO_MAX_PARALLEL_REQUESTS `` : Maximum number of parallel single log out requests send.
If more requests need to be send, there are queued. The default is `` 10 `` .
2015-12-13 12:50:01 +00:00
* `` CAS_SLO_TIMEOUT `` : Timeout for a single SLO request in seconds. The default is `` 5 `` .
2015-12-12 16:26:19 +00:00
2016-06-20 12:15:00 +00:00
2016-07-03 16:43:14 +00:00
Federation settings
-------------------
2016-06-20 12:15:00 +00:00
* `` CAS_FEDERATE `` : A boolean for activating the federated mode (see the federate section below).
The default is `` False `` .
2016-06-21 14:19:35 +00:00
* `` CAS_FEDERATE_PROVIDERS `` : A dictionnary for the allowed identity providers (see the federate
2016-06-20 12:15:00 +00:00
section below). The default is `` {} `` .
2016-06-21 14:19:35 +00:00
* `` CAS_FEDERATE_PROVIDERS_LIST `` : A list in with the keys of `` CAS_FEDERATE_PROVIDERS `` are ordened
for beeing displayed on the login page. The default is the list of all the keys of
`` CAS_FEDERATE_PROVIDERS `` sorted in natural order (0 < 2 < 10 < 20 < a = A < … < z = Z and
lexicographical)
2016-06-21 13:41:43 +00:00
* `` CAS_FEDERATE_REMEMBER_TIMEOUT `` : Time after witch the cookie use for "remember my identity
provider" expire. The default is `` 604800 `` , one week. The cookie is called
`` _remember_provider `` .
2016-06-20 12:15:00 +00:00
2016-07-03 16:40:11 +00:00
Tickets validity settings
-------------------------
2015-12-12 16:26:19 +00:00
2015-12-12 16:30:46 +00:00
* `` CAS_TICKET_VALIDITY `` : Number of seconds the service tickets and proxy tickets are valid.
This is the maximal time between ticket issuance by the CAS and ticket validation by an
application. The default is `` 60 `` .
* `` CAS_PGT_VALIDITY `` : Number of seconds the proxy granting tickets are valid.
The default is `` 3600 `` (1 hour).
2016-06-26 10:54:18 +00:00
* `` CAS_TICKET_TIMEOUT `` : Number of seconds a ticket is kept in the database before sending
2015-12-12 16:30:46 +00:00
Single Log Out request and being cleared. The default is `` 86400 `` (24 hours).
2015-12-12 16:26:19 +00:00
2016-07-03 16:40:11 +00:00
Tickets miscellaneous settings
------------------------------
2015-12-12 16:26:19 +00:00
2015-12-12 16:30:46 +00:00
* `` CAS_TICKET_LEN `` : Default ticket length. All CAS implementation MUST support ST and PT
up to 32 chars, PGT and PGTIOU up to 64 chars and it is RECOMMENDED that all tickets up
to 256 chars are supports. Here the default is `` 64 `` .
* `` CAS_LT_LEN `` : Length of the login tickets. Login tickets are only processed by `` django-cas-server ``
thus there is no length restriction on it. The default is `` CAS_TICKET_LEN `` .
* `` CAS_ST_LEN `` : Length of the service tickets. The default is `` CAS_TICKET_LEN `` .
You may need to lower is to `` 32 `` if you use some old clients.
* `` CAS_PT_LEN `` : Length of the proxy tickets. The default is `` CAS_TICKET_LEN `` .
This length should be the same as `` CAS_ST_LEN `` . You may need to lower is to `` 32 ``
if you use some old clients.
* `` CAS_PGT_LEN `` : Length of the proxy granting tickets. The default is `` CAS_TICKET_LEN `` .
* `` CAS_PGTIOU_LEN `` : Length of the proxy granting tickets IOU. The default is `` CAS_TICKET_LEN `` .
* `` CAS_LOGIN_TICKET_PREFIX `` : Prefix of login tickets. The default is `` "LT" `` .
* `` CAS_SERVICE_TICKET_PREFIX `` : Prefix of service tickets. The default is `` "ST" `` .
The CAS specification mandate that service tickets MUST begin with the characters ST
so you should not change this.
2016-06-26 10:54:18 +00:00
* `` CAS_PROXY_TICKET_PREFIX `` : Prefix of proxy ticket. The default is `` "PT" `` .
2015-12-12 16:30:46 +00:00
* `` CAS_PROXY_GRANTING_TICKET_PREFIX `` : Prefix of proxy granting ticket. The default is `` "PGT" `` .
* `` CAS_PROXY_GRANTING_TICKET_IOU_PREFIX `` : Prefix of proxy granting ticket IOU. The default is `` "PGTIOU" `` .
2015-12-12 16:26:19 +00:00
2016-07-03 16:40:11 +00:00
Mysql backend settings
----------------------
Only usefull if you are using the mysql authentication backend:
2015-12-12 16:26:19 +00:00
2015-12-12 16:30:46 +00:00
* `` CAS_SQL_HOST `` : Host for the SQL server. The default is `` "localhost" `` .
* `` CAS_SQL_USERNAME `` : Username for connecting to the SQL server.
* `` CAS_SQL_PASSWORD `` : Password for connecting to the SQL server.
* `` CAS_SQL_DBNAME `` : Database name.
* `` CAS_SQL_DBCHARSET `` : Database charset. The default is `` "utf8" ``
* `` CAS_SQL_USER_QUERY `` : The query performed upon user authentication.
The username must be in field `` username `` , the password in `` password `` ,
2015-12-12 17:02:51 +00:00
additional fields are used as the user attributes.
2015-12-12 16:30:46 +00:00
The default is `` "SELECT user AS usersame, pass AS password, users.* FROM users WHERE user = %s" ``
2016-06-26 18:29:47 +00:00
* `` CAS_SQL_PASSWORD_CHECK `` : The method used to check the user password. Must be one of the following:
2016-06-26 18:34:26 +00:00
2016-07-01 00:38:36 +00:00
* `` "crypt" `` (see <https://en.wikipedia.org/wiki/Crypt_(C)>), the password in the database
should begin this $
* `` "ldap" `` (see https://tools.ietf.org/id/draft-stroeder-hashed-userpassword-values-01.html)
the password in the database must begin with one of {MD5}, {SMD5}, {SHA}, {SSHA}, {SHA256},
{SSHA256}, {SHA384}, {SSHA384}, {SHA512}, {SSHA512}, {CRYPT}.
* `` "hex_HASH_NAME" `` with `` HASH_NAME `` in md5, sha1, sha224, sha256, sha384, sha512.
The hashed password in the database is compare to the hexadecimal digest of the clear
password hashed with the corresponding algorithm.
* `` "plain" `` , the password in the database must be in clear.
2016-06-26 18:29:47 +00:00
The default is `` "crypt" `` .
2015-12-12 16:26:19 +00:00
2016-06-26 10:54:18 +00:00
2016-07-03 16:40:11 +00:00
Test backend settings
---------------------
Only usefull if you are using the test authentication backend:
2016-06-26 10:54:18 +00:00
* `` CAS_TEST_USER `` : Username of the test user. The default is `` "test" `` .
* `` CAS_TEST_PASSWORD `` : Password of the test user. The default is `` "test" `` .
* `` CAS_TEST_ATTRIBUTES `` : Attributes of the test user. The default is
2016-06-27 21:55:17 +00:00
`` {'nom': 'Nymous', 'prenom': 'Ano', 'email': 'anonymous@example.net',
'alias': ['demo1', 'demo2']}`` .
2016-06-26 10:54:18 +00:00
2015-12-12 16:26:19 +00:00
Authentication backend
2016-07-03 16:40:11 +00:00
======================
2015-12-12 16:26:19 +00:00
`` django-cas-server `` comes with some authentication backends:
2015-12-12 16:30:46 +00:00
* dummy backend `` cas_server.auth.DummyAuthUser `` : all authentication attempt fails.
2016-06-26 10:54:18 +00:00
* test backend `` cas_server.auth.TestAuthUser `` : username, password and returned attributes
for the user are defined by the `` CAS_TEST_* `` settings.
2015-12-19 16:14:02 +00:00
* django backend `` cas_server.auth.DjangoAuthUser `` : Users are authenticated agains django users system.
2015-12-12 17:02:51 +00:00
This is the default backend. The returned attributes are the fields available on the user model.
2015-12-12 16:30:46 +00:00
* mysql backend `` cas_server.auth.MysqlAuthUser `` : see the 'Mysql backend settings' section.
2015-12-12 17:02:51 +00:00
The returned attributes are those return by sql query `` CAS_SQL_USER_QUERY `` .
2016-06-20 12:15:00 +00:00
* federated backend `` cas_server.auth.CASFederateAuth `` : It is automatically used then `` CAS_FEDERATE `` is `` True `` .
You should not set it manually without setting `` CAS_FEDERATE `` to `` True `` .
2015-12-13 12:50:29 +00:00
Logs
2016-07-03 16:40:11 +00:00
====
2015-12-13 12:50:29 +00:00
`` django-cas-server `` logs most of its actions. To enable login, you must set the `` LOGGING ``
2016-06-26 10:54:18 +00:00
(https://docs.djangoproject.com/en/stable/topics/logging) variable in `` settings.py `` .
2015-12-13 12:50:29 +00:00
Users successful actions (login, logout) are logged with the level `` INFO `` , failures are logged
with the level `` WARNING `` and user attributes transmitted to a service are logged with the level `` DEBUG `` .
For exemple to log to syslog you can use :
.. code-block :: python
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'cas_syslog': {
'format': 'cas: %(levelname)s %(message)s'
},
},
'handlers': {
'cas_syslog': {
'level': 'INFO',
'class': 'logging.handlers.SysLogHandler',
'address': '/dev/log',
'formatter': 'cas_syslog',
},
},
'loggers': {
'cas_server': {
'handlers': ['cas_syslog'],
'level': 'INFO',
'propagate': True,
},
},
}
Or to log to a file:
.. code-block :: python
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'cas_file': {
'format': '%(asctime)s %(levelname)s %(message)s'
},
},
'handlers': {
'cas_file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': '/tmp/cas_server.log',
'formatter': 'cas_file',
},
},
'loggers': {
'cas_server': {
'handlers': ['cas_file'],
'level': 'INFO',
'propagate': True,
},
},
}
2016-06-20 12:15:00 +00:00
Federation mode
2016-07-03 16:43:14 +00:00
===============
2016-06-20 12:15:00 +00:00
`` django-cas-server `` comes with a federation mode. Then `` CAS_FEDERATE `` is `` True `` ,
user are invited to choose an identity provider on the login page, then, they are redirected
to the provider CAS to authenticate. This provider transmit to `` django-cas-server `` the user
2016-06-20 12:20:49 +00:00
username and attributes. The user is now logged in on `` django-cas-server `` and can use
2016-06-20 12:15:00 +00:00
services using `` django-cas-server `` as CAS.
The list of allowed identity providers is defined using the `` CAS_FEDERATE_PROVIDERS `` parameter.
For instance:
.. code-block :: python
CAS_FEDERATE_PROVIDERS = {
2016-06-21 12:42:35 +00:00
"example.com": ("https://cas.example.com", 3, "Example dot com"),
"exemple.fr": ("https://cas.exemple.fr", 3, "Exemple point fr"),
2016-06-20 12:15:00 +00:00
}
`` CAS_FEDERATE_PROVIDERS `` is a dictionnary using provider names as key and a tuple
2016-06-21 12:42:35 +00:00
(cas address, cas version protocol, provider verbose name) as value.
2016-06-20 12:15:00 +00:00
In federation mode, `` django-cas-server `` build user's username as follow:
`` provider_returned_username@provider_name `` .
You can choose the provider returned username for `` django-cas-server `` and the provider name
2016-06-20 12:20:49 +00:00
in order to make sense.
2016-06-21 12:42:35 +00:00
The "provider verbose name" is showed on the select menu of the login page.
2016-06-20 12:20:49 +00:00
Then using federate mode, you should add one command to a daily crontab: `` cas_clean_federate `` .
This command clean the local cache of federated user from old unused users.
2016-06-20 12:21:57 +00:00
You could for example do as bellow :
.. code-block ::
10 0 * * * cas-user /path/to/project/manage.py cas_clean_federate