pyramid_auth’s documentation!

Introduction

This is a plugin for pyramid which provides a simple authentication system. The idea was to use exising authentication’s policies to provide multiple support. Currently this plugin support cookie, remote_user and ldap policies.

By default the cookie and ldap policies generate the form and the urls automatically:
  • /login: display the login form
  • /logout: logout the user
  • /forbidden: the user is redirected to this page when he is logged but doesn’t have the right permission to see a page.

If you want to generate your own urls you can set the following parameter

pyramid_auth.no_routes

If set in your config, no routes will be added automatically. It’s usefull when you use an API for authentication.

Note

If you want to change the rendering of the template to include your design you can:

  • Create a template in the folder templates/auth of your project named base.mak. Each templates (login, forbidden) inherit from it.
  • Create the login.mak and/or forbidden.mak templates in the folder templates/auth to overwrite the default ones.

Remote_user policy

This policy uses pyramid.authentication.RemoteUserAuthenticationPolicy. The user is authenticated by the http server which provides in the environ a key with the login.

Installation

In your .ini file add pyramid_auth to pyramid.includes like this:

pyramid.includes =
    pyramid_auth
    ...

Also you need to add pyramid_auth in setup.py in install_requires:

install_requires=[
    ...
    'pyramid_auth'
]

Configuration

You need to set some options in your .ini file. See this example for the required ones:

pyramid_auth.policy = remote_user

Options

environ_key
Default: REMOTE_USER. The key in the WSGI environ which provides the userid. Optional.
callback
Default: None. A callback passed the userid and the request, expected to return None if the userid doesn’t exist or a sequence of principal identifiers (possibly empty) representing groups if the user does exist. If callback is None, the userid will be assumed to exist with no group principals. Optional.
debug
Default: False. If debug is True, log messages to the Pyramid debug logger about the results of various authentication steps. Optional.

ldap policy

This policy uses pyramid_ldap. Basically the same logic than the cookie policy but we just validate the login/password with the ldap. As you will see in the configuration, it’s possible to get the ldap user’s groups. In this way, you will be able to set some permissions in your pyramid project according to the ldap configuration.

Installation

You need to have openldap header installed. For example on centos/fedora:

yum install openldap-devel

In your .ini file add pyramid_ldap and pyramid_auth to pyramid.includes like this:

pyramid.includes =
    pyramid_ldap
    pyramid_auth
    ...

Warning

the order is important, you need to include pyramid_ldap before pyramid_auth

Also you need to add pyramid_ldap and pyramid_auth in setup.py in install_requires:

install_requires=[
    ...
    'pyramid_ldap'
    'pyramid_auth'
]

Note

pyramid_ldap is not installed in pyramid_auth since we don’t want to force the installation of ldap if we don’t want to use it!

Configuration

You need to set some options in your .ini file. See this example for the required ones:

pyramid_auth.policy = ldap
pyramid_auth.ldap.cookie.secret = mysecret
pyramid_auth.ldap.setup.uri = http://ldap.lereskp.fr
pyramid_auth.ldap.setup.passwd = myldappasswd

pyramid_auth.ldap.login.base_dn = CN=Users,DC=lereskp,DC=fr
pyramid_auth.ldap.login.filter_tmpl = (sAMAccountName=$login)

If you want to put some permissions according to the ldap groups, you have to give the parameters to be able to query the ldap:

pyramid_auth.policy = ldap
pyramid_auth.ldap.cookie.secret = mysecret
pyramid_auth.ldap.setup.uri = http://ldap.lereskp.fr
pyramid_auth.ldap.setup.passwd = myldappasswd

pyramid_auth.ldap.login.base_dn = CN=Users,DC=lereskp,DC=fr
pyramid_auth.ldap.login.filter_tmpl = (sAMAccountName=$login)

pyramid_auth.ldap.groups.base_dn = CN=Users,DC=lereskp,DC=fr
pyramid_auth.ldap.groups.filter_tmpl = (&(objectCategory=group)(member=$userdn))

Options

Setup

pyramid_auth.ldap.setup.uri

ldap server uri. Required.

pyramid_auth.ldap.setup.bind

Default None. Bind that will be used to bind a connector. Optional.

pyramid_auth.ldap.setup.passwd

Default None. Password that will be used to bind a connector. Optional.

pyramid_auth.ldap.setup.size

Default 10. pool size. Optional.

pyramid_auth.ldap.setup.retry_max

Default 3. Number of attempts when a server is down. Optional.

pyramid_auth.ldap.setup.retry_delay

Default: .1. Delay in seconds before a retry. Optional.

pyramid_auth.ldap.setup.use_tls

Default False. Activate TLS when connecting. Optional.

pyramid_auth.ldap.setup.timeout

Default -1. Connector timeout. Optional.

pyramid_auth.ldap.setup.use_pool

Default True. Activates the pool. If False, will recreate a connector each time. Optional.
Login

pyramid_auth.ldap.login.base_dn

is the DN at which to begin the search.

pyramid_auth.ldap.login.filter_tmpl

is a string which can be used as an LDAP filter: it should contain the replacement value %(login)s.

pyramid_auth.ldap.login.scope

is any valid LDAP scope value (e.g. ldap.SCOPE_ONELEVEL).

pyramid_auth.ldap.login.cache_period

is the number of seconds to cache login search results; if it is 0, login search results will not be cached.
Groups

pyramid_auth.ldap.groups.base_dn

is the DN at which to begin the search.

pyramid_auth.ldap.groups.filter_tmpl

is a string which can be used as an LDAP filter: it should contain the replacement value %(userdn)s.

Important

In pyramid_ldap userdn represent the user distinguished name. In pyramid_auth it represents the user uid. So you should make your filter_tmpl according to the user uid.

pyramid_auth.ldap.groups.scope

is any valid LDAP scope value (e.g. ldap.SCOPE_SUBTREE). cache_period is the number of seconds to cache groups search results; if it is 0, groups search results will not be cached.
Extra

pyramid_auth.ldap.validate_function

Default: None. You can set a function to validate the ldap login/password it you want to be more specific. Optional.

pyramid_auth.ldap.callback

Default: None. A callback passed the userid and the request to extend the groups found by the ldap groups query. Optional.