The repoze.what Quickstart plugin¶
| Author: | Gustavo Narea. |
|---|---|
| Latest release: | 1.0.10 |
Overview
This plugin allows you to take advantage of a rather simple, and usual, authentication and authorization setup, in which the users’ data, the groups and the permissions used in the application are all stored in a SQLAlchemy or Elixir-managed database.
Put simply, it configures repoze.who and repoze.what in one
go so that you can have an authentication and authorization system working
quickly – hence the name.
How to install¶
The minimum requirements are SQLAlchemy, repoze.who.plugins.sa,
repoze.who.plugins.friendlyform, repoze.what and
repoze.what.plugins.sql, and you can install it all with
easy_install:
easy_install repoze.what-quickstart
Support and development¶
The prefered place to ask questions is the Repoze mailing list. Some users are on the #repoze IRC channel.
This project is hosted on GitHub.
How to use it¶
To get started quickly, you may copy the SQLAlchemy-powered model defined in model_sa_example.py (or model_elixir_example.py for Elixir) and then create at least a few rows to try it out:
u = User()
u.user_name = u'manager'
u.password = u'managepass'
DBSession.save(u)
g = Group()
g.group_name = u'managers'
g.users.append(u)
DBSession.save(g)
p = Permission()
p.permission_name = u'manage'
p.groups.append(g)
DBSession.save(p)
DBSession.flush()
Now that you have some rows in your database, you can set up authentication and authorization as explained in the next section.
How to set it up¶
Although this is a repoze.what plugin and repoze.what is meant
to deal with authorization only, this module configures authentication and
identification for you through repoze.who as well.
Such a setup is performed by the setup_sql_auth() function:
-
repoze.what.plugins.quickstart.setup_sql_auth(app, user_class, group_class, permission_class, dbsession, form_plugin=None, form_identifies=True, cookie_secret='secret', cookie_name='authtkt', login_url='/login', login_handler='/login_handler', post_login_url=None, logout_handler='/logout_handler', post_logout_url=None, login_counter_name=None, translations={}, cookie_timeout=None, cookie_reissue_time=None, charset='iso-8859-1', use_default_authenticator=True, **who_args)¶ Configure
repoze.whoandrepoze.whatwith SQL-only authentication and authorization, respectively.Parameters: - app – Your WSGI application.
- user_class – The SQLAlchemy/Elixir class for the users.
- group_class – The SQLAlchemy/Elixir class for the groups.
- permission_class – The SQLAlchemy/Elixir class for the permissions.
- dbsession – The SQLAlchemy/Elixir session.
- form_plugin – The main
repoze.whochallenger plugin; this is usually a login form. - form_identifies (bool) – Whether the
form_pluginmay and should act as anrepoze.whoidentifier. - cookie_secret (str) – The “secret” for the AuthTktCookiePlugin (set a custom one!).
- cookie_name (str) – The name for the AuthTktCookiePlugin.
- login_url (str) – The URL where the login form is displayed.
- login_handler (str) – The URL where the login form is submitted.
- post_login_url (str) – The URL/path where users should be redirected to after login.
- logout_handler – The URL where the logout is handled.
- post_logout_url (str) – The URL/path where users should be redirected to after logout.
- login_counter_name (str) – The name of the variable in the query string
that represents the login counter; defaults to
__logins. - translations (dict) – The model translations.
- cookie_timeout (
int) – The time (in seconds) during which the session cookie would be valid. - cookie_reissue_time (
int) – How often should the session cookie be reissued (in seconds); must be less thantimeout. - use_default_authenticator (
bool) – Whether the default SQL authenticator should be used.
Returns: The WSGI application with authentication and authorization middleware.
It configures
repoze.whowith the following plugins:Identifiers:
repoze.who.plugins.friendlyform.FriendlyFormPluginas the first identifier and challenger – usingloginas the URL/path where the login form will be displayed,login_handleras the URL/path where the form will be sent andlogout_handleras the URL/path where the user will be logged out. The so-called rememberer of such an identifier will be the identifier below.If
post_login_urlis defined, the user will be redirected to that page after login. Likewise, ifpost_logout_urlis defined, the user will be redirected to that page after logout.You can override the
repoze.who.plugins.friendlyform.FriendlyFormPlugin’s login counter variable name (which defaults to__logins) by defininglogin_counter_name.Tip
This plugin may be overridden with the
form_pluginargument. See also theform_identifiesargument.repoze.who.plugins.auth_tkt.AuthTktCookiePlugin. You can customize the cookie name and secret using thecookie_nameandcookie_secretarguments, respectively.
Then it will append the identifiers you pass through the
identifierskeyword argument, if any.Authenticators:
repoze.who.plugins.sa.SQLAlchemyAuthenticatorPlugin(unlessuse_default_authenticatorisFalse), using theuser_classanddbsessionarguments as its user class and DB session, respectively.
Then it will be appended to the authenticators you pass through the
authenticatorskeyword argument, if any. The default authenticator would have the lowest precedence.Challengers:
- The same Form-based plugin used in the identifiers.
Then it will append the challengers you pass through the
challengerskeyword argument, if any.Metadata providers:
repoze.who.plugins.sa.SQLAlchemyUserMDPlugin, using theuser_classanddbsessionarguments as its user class and DB session, respectively.
Then it will append the metadata providers you pass through the
mdproviderskeyword argument, if any.
The
charsetis passed to any component which needs to decode/encode data to/from the user. At present, onlyFriendlyFormPlugindoes.Additional keyword arguments will be passed to
repoze.who.middleware.PluggableAuthenticationMiddleware.Warning
It’s very important to set a custom
cookie_secret! It’s the key to encrypt and decrypt the cookies, so you shouldn’t leave the default one.Note
If you don’t want to use the groups/permissions-based authorization pattern, then set
group_classandpermission_classtoNone.New in version 1.0.5: Introduced the
cookie_timeoutandcookie_reissue_timearguments.New in version 1.0.6: Introduced the
charsetargument.New in version 1.0.8: Introduced the
use_default_authenticatorargument.New in version 1.0.9: Added support for the
dummy_validate_passwordtranslation inrepoze.who.plugins.sa.SQLAlchemyAuthenticatorPluginv1.0.1.
See “changing attribute names” to learn how to use the translations
argument in setup_sql_auth().
Customizing the model definition¶
Your auth-related model doesn’t have to be like the default one, where the
class for your users, groups and permissions are, respectively, User,
Group and Permission, and your users’ user name is available in
User.user_name. What if you prefer Member and Team instead of
User and Group, respectively? Or what if you prefer Group.members
instead of Group.users? Read on!
Changing class names¶
Changing the name of an auth-related class (User, Group or Permission)
is a rather simple task. Just rename it in your model, and then make sure to
update the parameters you pass to setup_sql_auth() accordingly.
Changing attribute names¶
You can also change the name of the attributes assumed by
repoze.what in your auth-related classes, such as renaming
User.groups to User.memberships.
Changing such values is what repoze.what calls “translating”.
You may set the translations for the attributes of the models
repoze.what deals with in a dictionary passed to setup_sql_auth()
as its translations parameters. For
example, if you want to replace Group.users with Group.members, you may
use the following translation dictionary:
translations['users'] = 'members'
Below are the translations that you would be able to set in the translations
dictionary used above:
user_name: The translation for the attribute inUser.user_name.users: The translation for the attribute inGroup.users.group_name: The translation for the attribute inGroup.group_name.groups: The translation for the attribute inUser.groupsandPermission.groups.permission_name: The translation for the attribute inPermission.permission_name.permissions: The translation for the attribute inUser.permissionsandGroup.permissions.validate_password: The translation for the method inUser.validate_password.
Contents¶
repoze.whatQuickstart via configuration filesrepoze.what.plugins.quickstartreleases- Version 1.0.9 (2011-11-29)
- Version 1.0.8 (2010-05-20)
- Version 1.0.7 (2010-05-05)
- Version 1.0.6 (2010-01-31)
- Version 1.0.5 (2010-01-27)
- Version 1.0.4 (2009-12-07)
- Version 1.0.2 and 1.0.3 (2009-10-09)
- Version 1.0.1 (2009-08-14)
- Version 1.0 (2009-03-02)
- Version 1.0rc4 (2009-02-18)
- Version 1.0rc3 (2009-02-17)
- Version 1.0rc2 (2009-02-11)
- Version 1.0rc1 (2009-01-30)