用Django和mod_wsgi設定SVN基本的授權認證

jieforest發表於2012-08-02
Consider this use case:

You have Django web application up and running on Apache and ‘mod_wsgi’. You plan to host Subversion repository on the same server and want it to be accessible to your web application users.

By default, Subversion will use ‘htpasswd’ file to authenticate users. So you will need to maintain two sets of users – one in ‘Django’ database and the other in Subversion ‘htpasswd’ file. You also need to keep them in sync so that the users have the same password and username for Subversion as well for logging in the web application.

Does that look like a lot of work to do? Is there a better way to do this? Yes, there is!

You can configure Apache to use Django’s authentication mechanism to authenticate users.

The ‘mod_wsgi’ comes to your rescue and makes things a lot easier for you.

Firstly, you need to define a ‘wsgi’ authentication handler script. which will tell Apache to use Django’s authentication for users accessing a Subversion repository.

CODE:

#! /usr/local/bin/python

import sys
import os
from django.contrib.auth.models import User
sys.path[0:0] = [
'/usr/lib/python/site-packages',
'/srv/src/app',
]
os.environ['DJANGO_SETTINGS_MODULE']='apphandler.settings'
from django.conf import settings

def check_password(environ, username, password):
        user = None
        try :
                   user = User.objects.get(**kwargs)
        except :
                return None
        return user.check_password(password)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-739609/,如需轉載,請註明出處,否則將追究法律責任。

相關文章