开发者

Alternative Django Authenication

开发者 https://www.devze.com 2022-12-26 06:25 出处:网络
Need to integrate Django with an existing authentication system.That system has it\'s own database, API, login/logout,edit profile web pages and cookie.

Need to integrate Django with an existing authentication system. That system has it's own database, API, login/logout,edit profile web pages and cookie.

(I may have to add a few additional profile fields stored/updated locally)

What's the proper approach to subst开发者_如何学Pythonitute the out-of-the-box authentication in Django?


The proper approach to substitute authentication from django's out-of-the-box to your own is to substitute your classes in the AUTHENTICATION_BACKENDS tuple in settings.py as described in http://docs.djangoproject.com/en/dev/topics/auth/#specifying-authentication-backends. This is incredibly useful for just the issue you're describing.

A good example of an authentication backend done this way is django-cas. This uses CAS to authenticate in a django application. You can use this as your template and just write hooks into your own authentication system identically.

HTH


I've created a custom authentication backend when I've had to do something similar to what you have to do. See: http://docs.djangoproject.com/en/dev/topics/auth/#writing-an-authentication-backend

In the authenticate function you call your api to authenticate the user, and then map them to a django.contrib.auth.model.User object on some primary key, like username for example. If the primary key is something other than username I usually create a mapping object, or put it into the profile object for the project.


This depends on how you want to handle the problem. If you don't need to keep the existing system running, your best bet is to import their data into the django project.

If the authentication system must stay in tact, you might have to write a wrapper for django.auth. I've done this in the past using SQLAlchemy http://www.sqlalchemy.org to integrate to the external database.

It might be helpful to take a look at the Django 1.2 multi-db support http://djangoadvent.com/1.2/multiple-database-support

In either case I'd try to get the user information into django.auth rather than to write your own authentication system.

0

精彩评论

暂无评论...
验证码 换一张
取 消