A way to integrate Python's Django framework with a certification system

Source: Internet
Author: User
It is possible to integrate Django with the username and password or authentication methods of other existing authentication systems.

For example, your company may already have LDAP installed, and the appropriate user name and password are stored for each employee. If users have separate accounts on both LDAP and Django-based applications, it can be a headache for both the network administrator and the user.

To solve this problem, the Django authentication system enables you to interact with other authentication resources in a plug-in manner. You can override the default database-based mode of Diango, and you can also use the default system to interact with other systems.
Specify the authentication background

In the background, Django maintains a background list to check for authentication. When a person calls Django.contrib.auth.authenticate () (as described in Chapter 14), Django attempts to traverse the authentication background for its authentication. If the first authentication method fails, Django tries to authenticate the second, and so on, until the attempt is complete.

The authentication background list is specified in the Authentication_backends settings. It should be an array of names that point to the python path of the Python class that knows how to authenticate. These classes can be anywhere in your Python path.

By default, Authentication_backends is set to the following:

(' Django.contrib.auth.backends.ModelBackend ',)

That's the Basic authentication mode that detects the Django user database.

The order of the authentication_backends is important if the username and password are valid in multiple backgrounds, then Django will stop further processing after the first correct match.
Write the authentication background

An authentication background is actually a class that implements the following two methods: Get_user (ID) and authenticate (**credentials).

Method Get_user requires a parameter ID, which can be a user name, a database ID, or any other value, and the method returns a User object.

Method authenticate uses a certificate as a key parameter. In most cases, the method looks like this:

Class Mybackend (object):  def authenticate (self, Username=none, password=none):    # Check the Username/password and return a User.

But sometimes it can also authenticate a phrase, such as:

Class Mybackend (object):  def authenticate (self, token=none):    # Check The token and return a User.

In each method, authenticate should detect the certificate it obtains, and when the certificate is valid, returns a User object that matches that certificate, and returns None if the certificate is invalid. If they are not valid, return none.

The Django management system is tightly connected to the User object of its own background database. The best way to do this is to create a corresponding Django user object for each user in your back-end database, such as an LDAP directory, an external SQL database, and so on. You can write a script in advance to do this, or you can implement it in the Authenticate method when a user logs on for the first time.

The following is a sample daemon that authenticates the username and password variables defined in the setting.py file and creates a corresponding Django user object the first time the user authenticates.

From django.conf import settingsfrom django.contrib.auth.models import User, Check_passwordclass settingsbackend (  Object): "" "Authenticate against the settings Admin_login and Admin_password. Use the login name, and a hash of the password. For example:admin_login = ' ADMIN ' Admin_password = ' sha1$4e987$afbcf42e21bd417fb71db8c66b321e9fc33051de ' "" "Def Auth Enticate (self, Username=none, password=none): Login_valid = (settings. Admin_login = = username) pwd_valid = check_password (password, settings. Admin_password) if login_valid and Pwd_valid:try:user = User.objects.get (username=username) except U Ser. Doesnotexist: # Create a new user. Note that we can set the password # to anything, because it won ' t be checked;        The password # from settings.py would. user = User (username=username, password= ' get from settings.py ') User.is_staff = True User.is_superuser = Tru e User.save () return user return None def get_useR (Self, user_id): Try:return User.objects.get (pk=user_id) except User.DoesNotExist:return None 

For more authentication module backstage, refer to Django documentation.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.