wiki:PythonicAuthentication

Virtual Host Configuration, Multiple Projects and Pythonic Authentication

<VirtualHost *>
    ServerName projects.example.com

    <Directory />
        SetHandler mod_python
        PythonHandler trac.web.modpython_frontend
        PythonOption TracEnvParentDir .../tracs
        PythonOption TracUriRoot /
    </Directory>

    <LocationMatch "/[^/]+/login">
        AuthType Basic
        AuthName "Projects Trac Server"
        PythonAuthenHandler trac.projects
        Require valid-user
    </LocationMatch>
</VirtualHost>

The PythonAuthenHandler introduces an object that will handle the actual authentication process, in this case trac.projects is the name of a module to import (and thus must be located somewhere in the Python load path).

In this example, I created .../site-packages/trac/projects that contains this little script called __init__.py:

from mod_python import apache

def authenhandler(req):
    pw = req.get_basic_auth_pw()
    user = req.user
    if user == "admin" and pw == "trac":
        return apache.OK
    else:
        return apache.HTTP_UNAUTHORIZED

that of course is just a sample. The name authenhandler comes from the default behaviour of mod_python, but you may specify a different one using the syntax trac.projects::my_own_handler.