README.rst 28.4 KB
Newer Older
Valentin Samir's avatar
Valentin Samir committed
1
CAS Server
Valentin Samir's avatar
Valentin Samir committed
2
##########
Valentin Samir's avatar
Valentin Samir committed
3

Valentin Samir's avatar
Valentin Samir committed
4
|travis| |version| |lisence| |codacy| |coverage| |doc|
5

6
CAS Server is a Django application implementing the `CAS Protocol 3.0 Specification
7
<https://apereo.github.io/cas/4.2.x/protocol/CAS-Protocol-Specification.html>`_.
8

Valentin Samir's avatar
Valentin Samir committed
9
By default, the authentication process use django internal users but you can easily
Valentin Samir's avatar
Valentin Samir committed
10 11
use any sources (see auth classes in the auth.py file)

Valentin Samir's avatar
Valentin Samir committed
12 13
.. contents:: Table of Contents

14
Features
Valentin Samir's avatar
Valentin Samir committed
15
========
16 17 18 19 20 21 22

* 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
23
* Federated mode between multiple CAS
24 25
* Supports Django 1.7, 1.8 and 1.9
* Supports Python 2.7, 3.x
Valentin Samir's avatar
Valentin Samir committed
26

Valentin Samir's avatar
Valentin Samir committed
27 28 29 30 31
Dependencies
============

``django-cas-server`` depends on the following python packages:

32
* Django >= 1.7.1 < 1.10
Valentin Samir's avatar
Valentin Samir committed
33 34 35
* requests >= 2.4
* requests_futures >= 0.9.5
* lxml >= 3.4
36 37 38 39 40 41 42 43
* six >= 1.8

Minimal version of packages dependancy are just indicative and meens that ``django-cas-server`` has
been tested with it. Previous versions of dependencies may or may not work.

Additionally, denpending of the authentication backend you plan to use, you may need the following
python packages:

44 45 46
* ldap3
* psycopg2
* mysql-python
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74


Here there is a table with the name of python packages and the corresponding packages providing
them on debian like systems and centos like systems.
You should try as much as possible to use system packages as there are automatically updated then
you update your system. You can then install Not Available (N/A)
packages on your system using pip inside a virtualenv as described in the `Installation`_ section.
For use with python3, just replace python(2) in the table by python3.

+------------------+-------------------------+---------------------+
| python package   | debian like systems     | centos like systems |
+==================+=========================+=====================+
| Django           | python-django           | python-django       |
+------------------+-------------------------+---------------------+
| requests         | python-requests         | python-requests     |
+------------------+-------------------------+---------------------+
| requests_futures | python-requests-futures | N/A                 |
+------------------+-------------------------+---------------------+
| lxml             | python-lxml             | python-lxml         |
+------------------+-------------------------+---------------------+
| six              | python-six              | python-six          |
+------------------+-------------------------+---------------------+
| ldap3            | python-ldap3            | python-ldap3        |
+------------------+-------------------------+---------------------+
| psycopg2         | python-psycopg2         | python-psycopg2     |
+------------------+-------------------------+---------------------+
| mysql-python     | python-mysqldb          | python2-mysql       |
+------------------+-------------------------+---------------------+
Valentin Samir's avatar
Valentin Samir committed
75 76 77 78 79 80 81 82 83 84 85 86

Installation
============

The recommended installation mode is to use a virtualenv with ``--system-site-packages``

1. Make sure that python virtualenv is installed

2. Install python packages available via the system package manager:

   On debian like systems::

87
    $ sudo apt-get install python-django python-requests python-six python-lxml python-requests-futures
Valentin Samir's avatar
Valentin Samir committed
88 89 90 91 92 93 94 95 96

   On debian jessie, you can use the version of python-django available in the
   `backports <https://backports.debian.org/Instructions/>`_.

   On centos like systems::

    $ sudo yum install python-django python-requests python-six python-lxml

3. Create a virtualenv::
Valentin Samir's avatar
Valentin Samir committed
97

Valentin Samir's avatar
Valentin Samir committed
98 99 100 101 102 103
    $ virtualenv --system-site-packages cas_venv
    Running virtualenv with interpreter /var/www/html/cas-server/bin/python2
    Using real prefix '/usr'
    New python executable in cas/bin/python2
    Also creating executable in cas/bin/python
    Installing setuptools, pip...done.
104 105 106

4. And `activate it <https://virtualenv.pypa.io/en/stable/userguide/#activate-script>`__::

Valentin Samir's avatar
Valentin Samir committed
107
    $ cd cas_venv/; . bin/activate
Valentin Samir's avatar
Valentin Samir committed
108

109
5. Create a django project::
Valentin Samir's avatar
Valentin Samir committed
110

Valentin Samir's avatar
Valentin Samir committed
111 112
   $ django-admin startproject cas_project
   $ cd cas_project
Valentin Samir's avatar
Valentin Samir committed
113

114
6. Install `django-cas-server`. To use the last published release, run::
Valentin Samir's avatar
Valentin Samir committed
115 116 117 118 119 120 121 122 123 124

    $ pip install django-cas-server

   Alternatively if you want to use the version of the git repository, you can clone it::

    $ git clone https://github.com/nitmir/django-cas-server
    $ cd django-cas-server
    $ pip install -r requirements.txt

   Then, either run ``make install`` to create a python package using the sources of the repository
125
   and install it with pip, or place the ``cas_server`` directory into your
Valentin Samir's avatar
Valentin Samir committed
126
   `PYTHONPATH <https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH>`_
127
   (for instance by symlinking ``cas_server`` to the root of your django project).
Valentin Samir's avatar
Valentin Samir committed
128

129
7. Open ``cas_project/settings.py`` in you favourite editor and follow the quick start section.
Valentin Samir's avatar
Valentin Samir committed
130 131 132 133 134 135


Quick start
===========

1. Add "cas_server" to your INSTALLED_APPS setting like this::
Valentin Samir's avatar
Valentin Samir committed
136 137

    INSTALLED_APPS = (
138
        'django.contrib.admin',
Valentin Samir's avatar
Valentin Samir committed
139 140 141 142
        ...
        'cas_server',
    )

Valentin Samir's avatar
Valentin Samir committed
143
   For internationalization support, add "django.middleware.locale.LocaleMiddleware"
144 145 146 147 148 149 150 151
   to your MIDDLEWARE_CLASSES setting like this::

    MIDDLEWARE_CLASSES = (
        ...
        'django.middleware.locale.LocaleMiddleware',
        ...
    )

Valentin Samir's avatar
Valentin Samir committed
152 153 154
2. Include the cas_server URLconf in your project urls.py like this::

    from django.conf.urls import url, include
Valentin Samir's avatar
Valentin Samir committed
155

156 157 158 159 160
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        ...
        url(r'^cas/', include('cas_server.urls', namespace="cas_server")),
    ]
Valentin Samir's avatar
Valentin Samir committed
161

Valentin Samir's avatar
Valentin Samir committed
162
3. Run ``python manage.py migrate`` to create the cas_server models.
Valentin Samir's avatar
Valentin Samir committed
163

164

Valentin Samir's avatar
Valentin Samir committed
165
4. You should add some management commands to a crontab: ``clearsessions``,
166 167
   ``cas_clean_tickets`` and ``cas_clean_sessions``.

168 169 170 171 172 173 174 175
   * ``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).
176

177
   You could for example do as bellow :
178 179 180 181 182 183 184

   .. 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

Valentin Samir's avatar
Valentin Samir committed
185 186
5. Run ``python manage.py createsuperuser`` to create an administrator user.

187
6. Start the development server and visit http://127.0.0.1:8000/admin/
Valentin Samir's avatar
Valentin Samir committed
188
   to add a first service allowed to authenticate user against the CAS
189
   (you'll need the Admin app enabled). See the `Service Patterns`_ section bellow.
Valentin Samir's avatar
Valentin Samir committed
190

191
7. Visit http://127.0.0.1:8000/cas/ to login with your django users.
192 193 194



Valentin Samir's avatar
Valentin Samir committed
195 196

Settings
Valentin Samir's avatar
Valentin Samir committed
197
========
Valentin Samir's avatar
Valentin Samir committed
198 199 200 201

All settings are optional. Add them to ``settings.py`` to customize ``django-cas-server``:


Valentin Samir's avatar
Valentin Samir committed
202 203
Template settings
-----------------
Valentin Samir's avatar
Valentin Samir committed
204

Valentin Samir's avatar
Valentin Samir committed
205
* ``CAS_LOGO_URL``: URL to the logo showed in the up left corner on the default
206
  templates. Set it to ``False`` to disable it.
207 208
* ``CAS_FAVICON_URL``: URL to the favicon (shortcut icon) used by the default templates.
  Default is a key icon. Set it to ``False`` to disable it.
209 210 211 212 213 214 215 216 217 218 219
* ``CAS_COMPONENT_URLS``: URLs to css and javascript external components. It is a dictionnary
  and it must have the five following keys: ``"bootstrap3_css"``, ``"bootstrap3_js"``,
  ``"html5shiv"``, ``"respond"``, ``"jquery"``. The default is::

        {
            "bootstrap3_css": "//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css",
            "bootstrap3_js": "//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js",
            "html5shiv": "//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js",
            "respond": "//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js",
            "jquery": "//code.jquery.com/jquery.min.js",
        }
220

Valentin Samir's avatar
Valentin Samir committed
221 222
* ``CAS_LOGIN_TEMPLATE``: Path to the template showed on ``/login`` then the user
  is not autenticated.  The default is ``"cas_server/login.html"``.
223
* ``CAS_WARN_TEMPLATE``: Path to the template showed on ``/login?service=...`` then
Valentin Samir's avatar
Valentin Samir committed
224
  the user is authenticated and has asked to be warned before being connected
Valentin Samir's avatar
Valentin Samir committed
225 226 227 228 229 230 231
  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``.
Valentin Samir's avatar
Valentin Samir committed
232 233


Valentin Samir's avatar
Valentin Samir committed
234 235
Authentication settings
-----------------------
Valentin Samir's avatar
Valentin Samir committed
236

237 238 239 240
* ``CAS_AUTH_CLASS``: A dotted path to a class or a class implementing
  ``cas_server.auth.AuthUser``. The default is ``"cas_server.auth.DjangoAuthUser"``
  Available classes bundled with ``django-cas-server`` are listed below in the
  `Authentication backend`_ section.
Valentin Samir's avatar
Valentin Samir committed
241

242 243 244
* ``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).
245

Valentin Samir's avatar
Valentin Samir committed
246
* ``CAS_PROXY_CA_CERTIFICATE_PATH``: Path to certificate authorities file. Usually on linux
Valentin Samir's avatar
Valentin Samir committed
247 248 249 250
  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.
Valentin Samir's avatar
Valentin Samir committed
251

Valentin Samir's avatar
Valentin Samir committed
252 253
* ``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``.
254
* ``CAS_SLO_TIMEOUT``: Timeout for a single SLO request in seconds. The default is ``5``.
Valentin Samir's avatar
Valentin Samir committed
255

256

257 258
Federation settings
-------------------
259

260 261
* ``CAS_FEDERATE``: A boolean for activating the federated mode (see the `Federation mode`_
  section below). The default is ``False``.
262 263 264
* ``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``.
265 266


267 268 269 270 271 272 273 274 275 276
New version warnings settings
-----------------------------

* ``CAS_NEW_VERSION_HTML_WARNING``: A boolean for diplaying a warning on html pages then a new
  version of the application is avaible. Once closed by a user, it is not displayed to this user
  until the next new version. The default is ``True``.
* ``CAS_NEW_VERSION_EMAIL_WARNING``: A bolean sot sending a email to ``settings.ADMINS`` when a new
  version is available. The default is ``True``.


Valentin Samir's avatar
Valentin Samir committed
277 278
Tickets validity settings
-------------------------
Valentin Samir's avatar
Valentin Samir committed
279

Valentin Samir's avatar
Valentin Samir committed
280 281 282 283 284
* ``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).
Valentin Samir's avatar
Valentin Samir committed
285
* ``CAS_TICKET_TIMEOUT``: Number of seconds a ticket is kept in the database before sending
Valentin Samir's avatar
Valentin Samir committed
286
  Single Log Out request and being cleared. The default is ``86400`` (24 hours).
Valentin Samir's avatar
Valentin Samir committed
287

Valentin Samir's avatar
Valentin Samir committed
288 289
Tickets miscellaneous settings
------------------------------
Valentin Samir's avatar
Valentin Samir committed
290

Valentin Samir's avatar
Valentin Samir committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
* ``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.
Valentin Samir's avatar
Valentin Samir committed
308
* ``CAS_PROXY_TICKET_PREFIX``: Prefix of proxy ticket. The default is ``"PT"``.
Valentin Samir's avatar
Valentin Samir committed
309 310
* ``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"``.
Valentin Samir's avatar
Valentin Samir committed
311 312


Valentin Samir's avatar
Valentin Samir committed
313 314
Mysql backend settings
----------------------
315
Deprecated, see the `Sql backend settings`_.
Valentin Samir's avatar
Valentin Samir committed
316
Only usefull if you are using the mysql authentication backend:
Valentin Samir's avatar
Valentin Samir committed
317

Valentin Samir's avatar
Valentin Samir committed
318 319 320 321 322 323 324
* ``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``,
325
  additional fields are used as the user attributes.
Valentin Samir's avatar
Valentin Samir committed
326
  The default is ``"SELECT user AS username, pass AS password, users.* FROM users WHERE user = %s"``
327
* ``CAS_SQL_PASSWORD_CHECK``: The method used to check the user password. Must be one of the following:
Valentin Samir's avatar
Valentin Samir committed
328

329 330 331 332 333 334 335 336 337
  * ``"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.
338 339

  The default is ``"crypt"``.
Valentin Samir's avatar
Valentin Samir committed
340

Valentin Samir's avatar
Valentin Samir committed
341

342 343 344
Sql backend settings
--------------------
Only usefull if you are using the sql authentication backend. You must add a ``"cas_server"``
345
database to `settings.DATABASES <https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-DATABASES>`__
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
as defined in the django documentation. It is then the database
use by the sql backend.

* ``CAS_SQL_USER_QUERY``: The query performed upon user authentication.
  The username must be in field ``username``, the password in ``password``,
  additional fields are used as the user attributes.
  The default is ``"SELECT user AS username, pass AS password, users.* FROM users WHERE user = %s"``
* ``CAS_SQL_PASSWORD_CHECK``: The method used to check the user password. Must be one of the following:

  * ``"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.

  The default is ``"crypt"``.
* ``CAS_SQL_PASSWORD_CHARSET``: Charset the SQL users passwords was hash with. This is needed to
  encode the user sended password before hashing it for comparison. The default is ``"utf-8"``.


Ldap backend settings
---------------------
Only usefull if you are using the ldap authentication backend:

* ``CAS_LDAP_SERVER``: Address of the LDAP server. The default is ``"localhost"``.
* ``CAS_LDAP_USER``: User bind address, for example ``"cn=admin,dc=crans,dc=org"`` for
  connecting to the LDAP server.
* ``CAS_LDAP_PASSWORD``: Password for connecting to the LDAP server.
* ``CAS_LDAP_BASE_DN``: LDAP search base DN, for example ``"ou=data,dc=crans,dc=org"``.
* ``CAS_LDAP_USER_QUERY``: Search filter for searching user by username. User inputed usernames are
  escaped using ``ldap3.utils.conv.escape_bytes``. The default is ``"(uid=%s)"``
* ``CAS_LDAP_USERNAME_ATTR``: Attribute used for users usernames. The default is ``"uid"``
* ``CAS_LDAP_PASSWORD_ATTR``: Attribute used for users passwords. The default is ``"userPassword"``
* ``CAS_LDAP_PASSWORD_CHECK``: The method used to check the user password. Must be one of the following:

  * ``"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.

  The default is ``"ldap"``.
* ``CAS_LDAP_PASSWORD_CHARSET``: Charset the LDAP users passwords was hash with. This is needed to
  encode the user sended password before hashing it for comparison. The default is ``"utf-8"``.


Valentin Samir's avatar
Valentin Samir committed
400 401 402
Test backend settings
---------------------
Only usefull if you are using the test authentication backend:
Valentin Samir's avatar
Valentin Samir committed
403 404 405 406

* ``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
407 408
  ``{'nom': 'Nymous', 'prenom': 'Ano', 'email': 'anonymous@example.net',
  'alias': ['demo1', 'demo2']}``.
Valentin Samir's avatar
Valentin Samir committed
409

Valentin Samir's avatar
Valentin Samir committed
410 411

Authentication backend
Valentin Samir's avatar
Valentin Samir committed
412
======================
Valentin Samir's avatar
Valentin Samir committed
413 414 415

``django-cas-server`` comes with some authentication backends:

Valentin Samir's avatar
Valentin Samir committed
416
* dummy backend ``cas_server.auth.DummyAuthUser``: all authentication attempt fails.
Valentin Samir's avatar
Valentin Samir committed
417 418
* test backend ``cas_server.auth.TestAuthUser``: username, password and returned attributes
  for the user are defined by the ``CAS_TEST_*`` settings.
Valentin Samir's avatar
Valentin Samir committed
419
* django backend ``cas_server.auth.DjangoAuthUser``: Users are authenticated against django users system.
420
  This is the default backend. The returned attributes are the fields available on the user model.
421 422 423 424
* mysql backend ``cas_server.auth.MysqlAuthUser``: Deprecated, use the sql backend instead.
  see the `Mysql backend settings`_ section. The returned attributes are those return by sql query
  ``CAS_SQL_USER_QUERY``.
* sql backend ``cas_server.auth.SqlAuthUser``: see the `Sql backend settings`_ section.
425
  The returned attributes are those return by sql query ``CAS_SQL_USER_QUERY``.
426 427
* ldap backend ``cas_server.auth.LdapAuthUser``: see the `Ldap backend settings`_ section.
  The returned attributes are those of the ldap node returned by the query filter ``CAS_LDAP_USER_QUERY``.
428 429
* 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``.
430

431

432
Logs
Valentin Samir's avatar
Valentin Samir committed
433
====
434 435

``django-cas-server`` logs most of its actions. To enable login, you must set the ``LOGGING``
Valentin Samir's avatar
Valentin Samir committed
436
(https://docs.djangoproject.com/en/stable/topics/logging) variable in ``settings.py``.
437 438 439 440

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``.

Valentin Samir's avatar
Valentin Samir committed
441
For example to log to syslog you can use :
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

.. 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,
            },
        },
    }
499

Valentin Samir's avatar
Valentin Samir committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
Service Patterns
================

In a CAS context, ``Service`` refers to the application the client is trying to access.
By extension we use ``service`` for the URL of such an application.

By default, ``django-cas-server`` do not allow any service to use the CAS to authenticate users.
In order to allow services, you need to connect to the django admin interface using a django
superuser, and add a first service pattern.

A service pattern comes with 9 fields:

* ``Position``: an integer used to change the order in which services are matched against
  service patterns.
* ``Name``: the name of the service pattern. It will be displayed to the users asking for a ticket
  for a service matching this service pattern on the login page.
* ``Pattern``: a regular expression used to match services.
* ``User field``: the user attribute to use as username for services matching this service pattern.
  Leave it empty to use the login name.
* ``Restrict username``: if checked, only login name defined below are allowed to get tickets
  for services matching this service pattern.
* ``Proxy``: if checked, allow the creation of Proxy Ticket for services matching this
  service pattern. Otherwise, only Service Ticket will be created.
* ``Proxy callback``: if checked, services matching this service pattern are allowed to retrieve Proxy
  Granting Ticket. A service with a Proxy Granting Ticket can get Proxy Ticket for other services.
  Hence you must only check this for trusted services that need it. (For instance, a webmail needs
  Proxy Ticket to authenticate himself as the user to the imap server).
* ``Single log out``: Check it to send Single Log Out requests to authenticated services matching
  this service pattern. SLO requests are send to all services the user is authenticated to then
  the user disconnect.
* ``Single log out callback``: The http(s) URL to POST the SLO requests. If empty, the service URL
  is used. This field is useful to allow non http services (imap, smtp, ftp) to handle SLO requests.

A service pattern has 4 associated models:

* ``Usernames``: a list of username associated with the ``Restrict username`` field
* ``Replace attribut names``: a list of user attributes to send to the service. Choose the name
  used for sending the attribute by setting ``Remplacement`` or leave it empty to leave it unchanged.
* ``Replace attribut values``: a list of sent user attributes for which value needs to be tweak.
  Replace the attribute value by the string obtained by replacing the leftmost non-overlapping
  occurrences of ``pattern`` in string by ``replace``. In ``replace`` backslash escapes are processed.
  Matched groups are captures by \1, \2, etc.
* ``Filter attribut values``: a list of user attributes for which value needs to match a regular
  expression. For instance, service A may need an email address, and you only want user with
  an email address to connect to it. To do so, put ``email`` in ``Attribute`` and ``.*`` in ``pattern``.

Then a user ask a ticket for a service, the service URL is compare against each service patterns
sorted by `position`. The first service pattern that matches the service URL is chosen.
Hence, you should give low `position` to very specific patterns like
``^https://www\.example\.com(/.*)?$`` and higher `position` to generic patterns like ``^https://.*``.
So the service URL `https://www.examle.com` will use the service pattern for
``^https://www\.example\.com(/.*)?$`` and not the one for ``^https://.*``.

553 554

Federation mode
555
===============
556 557 558 559

``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
560
username and attributes. The user is now logged in on ``django-cas-server`` and can use
561 562
services using ``django-cas-server`` as CAS.

563 564
The list of allowed identity providers is defined using the django admin application.
With the development server started, visit http://127.0.0.1:8000/admin/ to add identity providers.
565

566
An identity provider comes with 5 fields:
567

Valentin Samir's avatar
Valentin Samir committed
568
* ``Position``: an integer used to tweak the order in which identity providers are displayed on
569
  the login page. Identity providers are sorted using position first, then, on equal position,
Valentin Samir's avatar
Valentin Samir committed
570 571
  using ``verbose name`` and then, on equal ``verbose name``, using ``suffix``.
* ``Suffix``: the suffix that will be append to the username returned by the identity provider.
572
  It must be unique.
Valentin Samir's avatar
Valentin Samir committed
573 574 575 576
* ``Server url``: the URL to the identity provider CAS. For instance, if you are using
  ``https://cas.example.org/login`` to authenticate on the CAS, the `server url` is
  ``https://cas.example.org``
* ``CAS protocol version``: the version of the CAS protocol to use to contact the identity provider.
577
  The default is version 3.
Valentin Samir's avatar
Valentin Samir committed
578 579
* ``Verbose name``: the name used on the login page to display the identity provider.
* ``Display``: a boolean controlling the display of the identity provider on the login page.
580
  Beware that this do not disable the identity provider, it just hide it on the login page.
Valentin Samir's avatar
Valentin Samir committed
581
  User will always be able to log in using this provider by fetching ``/federate/provider_suffix``.
582 583 584


In federation mode, ``django-cas-server`` build user's username as follow:
585 586 587 588
``provider_returned_username@provider_suffix``.
Choose the provider returned username for ``django-cas-server`` and the provider suffix
in order to make sense, as this built username is likely to be displayed to end users in
applications.
589

590 591 592 593 594

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.


Valentin Samir's avatar
Valentin Samir committed
595 596 597 598 599
You could for example do as bellow :

.. code-block::

    10   0  * * * cas-user /path/to/project/manage.py cas_clean_federate
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616



.. |travis| image:: https://badges.genua.fr/travis/nitmir/django-cas-server/master.svg
    :target: https://travis-ci.org/nitmir/django-cas-server

.. |version| image:: https://badges.genua.fr/pypi/v/django-cas-server.svg
    :target: https://pypi.python.org/pypi/django-cas-server

.. |lisence| image:: https://badges.genua.fr/pypi/l/django-cas-server.svg
    :target: https://www.gnu.org/licenses/gpl-3.0.html

.. |codacy| image:: https://badges.genua.fr/codacy/grade/255c21623d6946ef8802fa7995b61366/master.svg
    :target: https://www.codacy.com/app/valentin-samir/django-cas-server

.. |coverage| image:: https://badges.genua.fr/codacy/coverage/255c21623d6946ef8802fa7995b61366/master.svg
    :target: https://www.codacy.com/app/valentin-samir/django-cas-server
Valentin Samir's avatar
Valentin Samir committed
617 618 619

.. |doc| image:: https://badges.genua.fr/local/readthedocs/?version=latest
    :target: http://django-cas-server.readthedocs.io