|Version 12 (modified by jkocherhans, 9 years ago) (diff)|
Multiple Authentication Backends
See http://code.djangoproject.com/ticket/1428 for the current patch that implements this.
The default authentication mechanism for Django will still check a username/password against django.contrib.auth.models.User
The path to the default authentication backend can be set in settings.py via the AUTHENTICATION_BACKEND variable. This backend is used to set the request.user attribute automatically. There is also a backend that is just a front for using multiple backends, but we'll get to that later.
Here's a code sample that authenticates a user. This would be used to process login forms. Like before, you'd check request.user.is_anonymous() if you want to test if user is logged in.
from django.contrib.auth import AuthUtil def login(self, request): authutil = AuthUtil() user = authutil.authenticate(request) if user is None: # do whatever for invalid logins else: # the user is valid, persist their id (username, email, token, etc.) in a session var or whatever. # do whatever else this view is supposed to do.
Note that the view is in charge of "logging in" a user, that is, the view must persist the id of the user somehow. A session variable is the easiest place, but a signed cookie would be desireable for those who don't want to use the session middleware. It would be nice if this were more convenient. It shouldn't be hard to do. Also, in practice, all of this would probably happen in a decorator. Adding that much boilerplate code to every view would be a little ridiculous.
For extra points, there should be ways of tying this all in with WSGI ;)
Credentials are extracted from the request by plugins. These plugins are just functions that take the request as their only argument and return a dict or string containing the credentials. You can have multiple ordered credential plugins by changing CREDENTIAL_PLUGINS in your settings file.
CREDENTIAL_PLUGINS = ( 'django.contrib.auth.credentials.username_password_form', 'django.contrib.auth.credentials.token', )
AuthUtil will use the first plugin and hand the credentials to AUTHENTICATION_BACKEND. If AUTHENTICATION_BACKEND returns None for the first set of credentials, the next plugin will be tried, and so on.
CREDENTIAL_PLUGINS defaults to ('django.contrib.auth.credentials.username_password_form',)
Using Multiple Backends
To use multiple authentication backends, set AUTHENTICATION_BACKEND to django.contrib.auth.backends. MultiAuthBackend in your settings file.You must also set AUTHENTICATION_BACKEND to a tuple of the backends you wish to use, in order.
AUTHENTICATION_BACKEND = 'django.contrib.auth.backends.MultiAuthBackend' MULTIAUTH_BACKENDS = ( 'django.contrib.auth.backends.LDAPBackend', 'django.contrib.auth.backends.ModelBackend', )
When you call authenticate or get_user on MultiAuthBackend, it will in turn call the same method on each backend in MULTIAUTH_BACKENDS in order.
Authentication backends are pretty simple. They just need to implement 2 methods, authenticate and get_user.
If the credentials match a user in this backend it returns a user object. If not, it returns None. Keep in mind that credentials could be a dict, a string, pretty much anything. You'll have to make sure that authenticate does the appropriate checking and returns None for credentials that it can't handle.
The user object will generally be an instance of django.contrib.auth.models.User, but really, it could be anything. You will need to at least fake the interface for django.contrib.auth.models.User if you want to use the admin system however. Your backend can create and save an instance of django.contrib.auth.models.User when a user logs in for the first time. You could also add them to a default set of groups at that time.
backend.get_user simply takes a user id and returns the user that matches that id. The user id is not neccessarily numeric, and in most cases it won't be. It could be a username, an email address, whatever. The important part is that it uniquely identifies a user.