[mod_python] callable __auth__ (with detailed example code)

Robert Thomas Davis rdavisunr at yahoo.com
Thu Feb 23 13:42:44 EST 2006


Hey all (mostly graham though)

Here is exactly what I am trying to do...

from mod_python import apache
from mod_python import psp
from mod_python.Session import Session
import sys, time
from sql_defines import *
from connection_defines import USER, PASS

# db connection
db_conn = apache.import_module('db_conn', log=1)
db = db_conn.connection('Cursor')
device = apache.import_module('device', log=1)


def validate_user(req, user, passwd):

        if passwd == PASS:
                # user has successfully authenticated
                sess = Session(req)

                if sess.has_key('max_inactive'):
                        # this is an existing session

                        # check length of inactivity
                        elapsed = time.time() -
sess['last']

                        # reset timer for next request
                        sess['last'] = time.time()
                        sess.save()

                        # compare elapsed to maximum
allowed
                        if elapsed >
sess['max_inactive']:
                                sess.delete()

                                # force user to
reauthenticate
                                return 0
                        else:
                                #...still within time
limit

                                # allow user to
continue
                                return 1
                else:
                        # new session

                        # set maximum inactivity
allowed
                        sess['max_inactive'] = 500

                        # initialize timer
                        sess['last'] = time.time()

                        sess.save()

                        # allow user to continue
                        return 1
        else:
                # force user to reauthenticate
                return 0


def handle_page_build(req, obj, **kwargs):

        try:
                __create_tables()
                page = obj(req, **kwargs)
        except:
                error_page = error.error_page(req,
sys.exc_info())
                return error_page.build()
        else:
                return page.build()


def index(req):

        return main(req)


def main(req):

        return handle_page_build(req, home.home_page)


def devices(req):
        __auth_realm__ = "Devices!"
        def __auth__(req, user, passwd):
                return validate_user(req, user,
passwd)
        return handle_page_build(req,
device.devices_page)

Thanks!


More information about the Mod_python mailing list