Status: | Official |
---|---|
Website: | http://static.repoze.org/whodocs/ |
Overview
This document describes how repoze.who is integrated into TurboGears and how you make get started with it. For more information, you may want to check repoze.who‘s website.
repoze.who is a powerful and extensible authentication package for arbitrary WSGI applications. By default TurboGears2 configures it to log using a form and retrieving the user informations through the user_name field of the User class. This is made possible by the authenticator plugin that TurboGears2 uses by default which is repoze.who.plugins.sa.SQLAlchemyAuthenticatorPlugin.
It’s a WSGI middleware which is able to authenticate the user through the method you want (e.g., LDAP or HTTP authentication), “remember” the user in future requests and log the user out.
You can customize the interaction with the user through four kinds of plugins, sorted by the order in which they are run on each request:
When repoze.who needs to store data about the authenticated user in the WSGI environment, it uses its repoze.who.identity key, which can be accessed using the code below:
from tg import request
# The authenticated user's data kept by repoze.who:
identity = request.environ.get('repoze.who.identity')
Such a value is a dictionary and is often called “the identity dict”. It will only be defined if the current user has been authenticated.
Tip
There is a short-cut to the code above in the WSGI request, which will be defined in {yourproject}.lib.base.BaseController if you enabled authentication and authorization when you created the project.
For example, to check whether the user has been authenticated you may use:
# ...
from tg import request
# ...
if request.identity:
flash('You are authenticated!')
``request.identity`` will equal to ``None`` if the user has not been
authenticated.
Likewise, this short-cut is also set in the template context as
``tg.identity``.
The username will be available in identity['repoze.who.userid'] (or request.identity['repoze.who.userid'], depending on the method you select).
By default, TurboGears 2.2.2 configures repoze.who to use repoze.who.plugins.friendlyform.FriendlyFormPlugin as the first identifier and challenger – using /login as the relative URL that will display the login form, /login_handler as the relative URL where the form will be sent and /logout_handler as the relative URL where the user will be logged out. The so-called rememberer of such identifier will be an instance of repoze.who.plugins.cookie.AuthTktCookiePlugin.
All these settings can be customized through the config.app_cfg.base_config.sa_auth options in your project. Identifiers, Authenticators and Challengers can be overridden providing a different list for each of them as:
base_config.sa_auth['identifiers'] = [('myidentifier', myidentifier)]
You don’t have to use repoze.who directly either, unless you decide not to use it the way TurboGears configures it.
If you’re looking for different authentication methods, you may want to visit the repoze.who website to check if the plugin you’re looking for is already available or how to create your own plugins.
To learn how to customize Authentication and Authorization in TurboGears you can give a look at Customizing Authentication.