1.2.2 Creating a basic auth environment

The web.auth module is designed so that the data can be stored in ways using different drivers. Currently only a database storage driver exists allowing auth information to be stored in any relational database supported by the web.database module. The web.database module includes SnakeSQL, a pure Python database which works like a library, so you can use the web.auth module even if you do not have access to another relational database engine.

In this example we are using a database to store the auth and session information so we setup a database cursor named cursor as described in the documentation for the web.database module.

import web.database
connection = web.database.connect(adapter='snakesql', database='test', autoCreate=1)
cursor = connection.cursor()

Next we need to create the necessary tables which will store information about the users and their access rights. To do this we need an admin object:

admin = web.auth.admin(driver='database', cursor=cursor)

If we haven't already created the auth tables we can do so like this:

if not admin.completeAuthEnvironment():
    admin.removeAuthEnvironment(ignoreErrors=True)
    admin.createAuthEnvironment()
connection.commit()

If any of the tables are missing, this code removes all existing tables thereby destroying all the data they contain (ignoring errors produced because of missing tables) and re-creates all the tables. The connection.commit() saves the changes to the database.



Subsections