Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged multi-auth branch to trunk. See the authentication docs for th…
…e ramifications of this change. Many, many thanks to Joseph Kocherhans for the hard work! git-svn-id: http://code.djangoproject.com/svn/django/trunk@3226 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
Showing
10 changed files
with
231 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/' | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.contrib.auth.models import User, check_password | ||
|
||
class ModelBackend: | ||
""" | ||
Authenticate against django.contrib.auth.models.User | ||
""" | ||
# TODO: Model, login attribute name and password attribute name should be | ||
# configurable. | ||
def authenticate(self, username=None, password=None): | ||
try: | ||
user = User.objects.get(username=username) | ||
if user.check_password(password): | ||
return user | ||
except User.DoesNotExist: | ||
return None | ||
|
||
def get_user(self, user_id): | ||
try: | ||
return User.objects.get(pk=user_id) | ||
except User.DoesNotExist: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters