Django

Code

Changeset 2918

Show
Ignore:
Timestamp:
05/16/06 11:23:49 (2 years ago)
Author:
jkocherhans
Message:

multi-auth: Updated docs to cover new authentication api.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/multi-auth/docs/authentication.txt

    r2892 r2918  
    268268-------------------- 
    269269 
    270 To log a user in, do the following within a view:: 
    271  
    272     from django.contrib.auth.models import SESSION_KEY 
    273     request.session[SESSION_KEY] = some_user.id 
    274  
    275 Because this uses sessions, you'll need to make sure you have 
    276 ``SessionMiddleware`` enabled. See the `session documentation`_ for more 
    277 information. 
    278  
    279 This assumes ``some_user`` is your ``User`` instance. Depending on your task, 
    280 you'll probably want to make sure to validate the user's username and password. 
     270Depending on your task, you'll probably want to make sure to validate the  
     271user's username and password before you log them in. The easiest way to do so  
     272is to use the built-in ``authenticate`` and ``login`` functions from within a  
     273view:: 
     274 
     275    from django.contrib.auth import authenticate, login 
     276    username = request.POST['username'] 
     277    password = request.POST['password'] 
     278    user = authenticate(username=username, password=password) 
     279    if user is not None: 
     280        login(request, user) 
     281 
     282``authenticate`` checks the username and password. If they are valid it  
     283returns a user object, otherwise it returns ``None``. ``login`` makes it so  
     284your users don't have send a username and password for every request. Because  
     285the ``login`` function uses sessions, you'll need to make sure you have  
     286``SessionMiddleware`` enabled. See the `session documentation`_ for  
     287more information. 
     288 
    281289 
    282290Limiting access to logged-in users 
     
    612620 
    613621.. _session framework: http://www.djangoproject.com/documentation/sessions/ 
     622 
     623Other Authentication Sources 
     624============================ 
     625 
     626Django supports other authentication sources as well. You can even use  
     627multiple sources at the same time. 
     628 
     629Using multiple backends 
     630----------------------- 
     631 
     632The list of backends to use is controlled by the ``AUTHENTICATION_BACKENDS``  
     633setting. This should be a tuple of python path names. It defaults to  
     634``('django.contrib.auth.backends.ModelBackend',)``. To add additional backends 
     635just add them to your settings.py file. Ordering matters, so if the same 
     636username and password is valid in multiple backends, the first one in the 
     637list will return a user object, and the remaining ones won't even get a chance. 
     638 
     639Writing an authentication backend 
     640--------------------------------- 
     641 
     642An authentication backend is a class that implements 2 methods:  
     643``get_user(id)`` and ``authenticate(**credentials)``. The ``get_user`` method  
     644takes an id, which could be a username, and database id, whatever, and returns  
     645a user object. The  ``authenticate`` method takes credentials as keyword  
     646arguments. Many times it will just look like this:: 
     647 
     648    class MyBackend: 
     649        def authenticate(username=None, password=None): 
     650            # check the username/password and return a user 
     651 
     652but it could also authenticate a token like so:: 
     653 
     654    class MyBackend: 
     655        def authenticate(token=None): 
     656            # check the token and return a user 
     657 
     658Regardless, ``authenticate`` should check the credentials it gets, and if they  
     659are valid, it should return a user object that matches those credentials. 
     660 
     661The Django admin system is tightly coupled to the Django User object described  
     662at the beginning of this document. For now, the best way to deal with this is to 
     663create a Django User object for each user that exists for your backend (i.e. 
     664in your ldap directory, your external sql database, etc.) You can either  
     665write a script to do this in advance, or your ``authenticate`` method can do  
     666it the first time a user logs in. `django.contrib.auth.backends.SettingsBackend`_ 
     667is an example of the latter approach. Note that you don't have to save a user's 
     668password in the Django User object. Your backend can still check the password 
     669against an external source, and return a Django User object. 
     670 
     671.. _django.contrib.auth.backends.SettingsBackend: http://code.djangoproject.com/browser/django/branches/magic-removal/django/contrib/auth/backends.py