mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
multi-auth: Implemented new auth api.
git-svn-id: http://code.djangoproject.com/svn/django/branches/multi-auth@2884 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0990cce754
commit
0cd180cf76
@ -1,2 +1,71 @@
|
|||||||
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
|
|
||||||
|
SESSION_KEY = '_auth_user_id'
|
||||||
|
BACKEND_SESSION_KEY = '_auth_user_backend'
|
||||||
LOGIN_URL = '/accounts/login/'
|
LOGIN_URL = '/accounts/login/'
|
||||||
REDIRECT_FIELD_NAME = 'next'
|
REDIRECT_FIELD_NAME = 'next'
|
||||||
|
|
||||||
|
def load_backend(path):
|
||||||
|
i = path.rfind('.')
|
||||||
|
module, attr = path[:i], path[i+1:]
|
||||||
|
try:
|
||||||
|
mod = __import__(module, '', '', [attr])
|
||||||
|
except ImportError, e:
|
||||||
|
raise ImproperlyConfigured, 'Error importing authentication backend %s: "%s"' % (module, e)
|
||||||
|
try:
|
||||||
|
cls = getattr(mod, attr)
|
||||||
|
except AttributeError:
|
||||||
|
raise ImproperlyConfigured, 'Module "%s" does not define a "%s" authentication backend' % (module, attr)
|
||||||
|
return cls()
|
||||||
|
|
||||||
|
def get_backends():
|
||||||
|
from django.conf import settings
|
||||||
|
backends = []
|
||||||
|
for backend_path in settings.AUTHENTICATION_BACKENDS:
|
||||||
|
backends.append(load_backend(backend_path))
|
||||||
|
return backends
|
||||||
|
|
||||||
|
def authenticate(**credentials):
|
||||||
|
"""
|
||||||
|
If the given credentials, return a user object.
|
||||||
|
"""
|
||||||
|
for backend in get_backends():
|
||||||
|
try:
|
||||||
|
user = backend.authenticate(**credentials)
|
||||||
|
except TypeError:
|
||||||
|
# this backend doesn't accept these credentials as arguments, try the next one.
|
||||||
|
continue
|
||||||
|
if user is None:
|
||||||
|
continue
|
||||||
|
# annotate the user object with the path of the backend
|
||||||
|
user.backend = str(backend.__class__)
|
||||||
|
return user
|
||||||
|
|
||||||
|
def login(request, user):
|
||||||
|
"""
|
||||||
|
Persist a user id and a backend in the request. This way a user doesn't
|
||||||
|
have to reauthenticate on every request.
|
||||||
|
"""
|
||||||
|
if user is None:
|
||||||
|
user = request.user
|
||||||
|
# TODO: It would be nice to support different login methods, like signed cookies.
|
||||||
|
request.session[SESSION_KEY] = user.id
|
||||||
|
request.session[BACKEND_SESSION_KEY] = user.backend
|
||||||
|
|
||||||
|
def logout(request):
|
||||||
|
"""
|
||||||
|
Remove the authenticated user's id from request.
|
||||||
|
"""
|
||||||
|
del request.session[SESSION_KEY]
|
||||||
|
del request.session[BACKEND_SESSION_KEY]
|
||||||
|
|
||||||
|
def get_user(request):
|
||||||
|
from django.contrib.auth.models import AnonymousUser
|
||||||
|
try:
|
||||||
|
user_id = request.session[SESSION_KEY]
|
||||||
|
backend_path = request.session[BACKEND_SESSION_KEY]
|
||||||
|
backend = load_backend(backend_path)
|
||||||
|
user = backend.get_user(user_id) or AnonymousUser()
|
||||||
|
except KeyError:
|
||||||
|
user = AnonymousUser()
|
||||||
|
return user
|
||||||
|
Loading…
x
Reference in New Issue
Block a user