Implementation of cookie signing as done in mod_auth_tkt.
mod_auth_tkt is an Apache module that looks for these signed cookies and sets REMOTE_USER, REMOTE_USER_TOKENS (a comma-separated list of groups) and REMOTE_USER_DATA (arbitrary string data).
This module is an alternative to the paste.auth.cookie module; it’s primary benefit is compatibility with mod_auth_tkt, which in turn makes it possible to use the same authentication process with non-Python code run under Apache.
Middleware that checks for signed cookies that match what mod_auth_tkt looks for (if you have mod_auth_tkt installed, you don’t need this middleware, since Apache will set the environmental variables for you).
Arguments:
If used with mod_auth_tkt, then these settings (except logout_path) should match the analogous Apache configuration settings.
This also adds two functions to the request:
environ['paste.auth_tkt.set_user'](userid, tokens='', user_data='')
This sets a cookie that logs the user in. tokens is a string (comma-separated groups) or a list of strings. user_data is a string for your own use.
environ['paste.auth_tkt.logout_user']()
Logs out the user.
Creates the AuthTKTMiddleware.
secret is requird, but can be set globally or locally.
This class represents an authentication token. You must pass in the shared secret, the userid, and the IP address. Optionally you can include tokens (a list of strings, representing role names), ‘user_data’, which is arbitrary data available for your own use in later scripts. Lastly, you can override the cookie name and timestamp.
Once you provide all the arguments, use .cookie_value() to generate the appropriate authentication ticket. .cookie() generates a Cookie object, the str() of which is the complete cookie header to be sent.
CGI usage:
token = auth_tkt.AuthTick('sharedsecret', 'username',
os.environ['REMOTE_ADDR'], tokens=['admin'])
print 'Status: 200 OK'
print 'Content-type: text/html'
print token.cookie()
print
... redirect HTML ...
Webware usage:
token = auth_tkt.AuthTick('sharedsecret', 'username',
self.request().environ()['REMOTE_ADDR'], tokens=['admin'])
self.response().setCookie('auth_tkt', token.cookie_value())
Be careful not to do an HTTP redirect after login; use meta refresh or Javascript – some browsers have bugs where cookies aren’t saved when set on a redirect.