Writing Auth Backends¶
newauth provides a couple base classes for defining backends. All backends
should extend the BaseAuthBackend. Backend classes are created in much the same
way as you would for the django.contrib.auth app. You need to create an
authenticate()
method that
takes the arguments (credentials) that your backend requires and a
get_user()
method that takes an id and returns an instance of the user.
Here is a very simple backend that authenticates a user using only their user id. Not a very secure authentication but it illustrates how to write an authentication backend:
from nweauth.backend import BaseAuthBackend
from newauth.models import User
class UserIdBackend(BaseAuthBackend):
def authenticate(self, user_id):
return self.get_user(user_id)
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist, e:
return None
Using Model Authentication¶
Most of the time, however, you will be creating models extending the
UserBase
class so you can
extend the provided ModelAuthBackend
which provides a default
implementation of get_user()
which retrieves an
instance of the model associated with the backend in the NEWAUTH_BACKENDS
in your settings.py.