repoze.who (Identification & Authentication for WSGI)

3.1.0 · maintenance · verified Thu Apr 16

repoze.who is an identification and authentication framework for WSGI applications. It provides middleware to manage user identification, authentication, and authorization. The current stable version is 3.1.0, and it is actively maintained by the Pylons Project, though releases are infrequent.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up `repoze.who` with a basic authenticator and `BasicAuthPlugin` for identification and challenging. It shows how to use `make_who_with_config` to create the middleware and `add_new_r_w_to_environ` to populate the WSGI environment, then access the identity from `request.environ`. To run this example, you will also need to install `webob` and a WSGI server like `waitress`.

import os
from webob.dec import wsgify
from webob import Response
from repoze.who.config import make_who_with_config, add_new_r_w_to_environ
from repoze.who.plugins.basicauth import BasicAuthPlugin # Example plugin

# Dummy WSGI application
@wsgify
def my_app(request):
    identity = request.environ.get('repoze.who.identity')
    if identity:
        username = identity.get('repoze.who.userid')
        return Response(f'Hello, {username}! You are authenticated.')
    else:
        return Response('Please authenticate.', status=401)

# Configure repoze.who
# For real applications, use proper credential storage
users_db = {'admin': 'secret', 'user': 'password'}

def my_authenticator(environ, identity):
    if 'login' in identity and 'password' in identity:
        login = identity['login']
        password = identity['password']
        if users_db.get(login) == password:
            identity['repoze.who.userid'] = login # Store userid
            return login
    return None

# Example challenge (basic auth)
basic_auth_plugin = BasicAuthPlugin('My Realm')

# Who config dictionary (simplified)
who_config = {
    'identifiers': [('basic_auth_identifier', basic_auth_plugin)],
    'authenticators': [('my_auth', my_authenticator)],
    'challengers': [('basic_auth_challenger', basic_auth_plugin)],
    'log_stream': None,
    'log_level': 10 # DEBUG
}

# Create WhoMiddleware
who_middleware_app = make_who_with_config(my_app, who_config)

# Wrap the app to ensure 'repoze.who' object is in environ
@wsgify
def wrapped_app(request):
    add_new_r_w_to_environ(request.environ, who_config)
    return who_middleware_app(request)

# To run this with a WSGI server (e.g., waitress):
# pip install webob waitress
# from waitress import serve
# serve(wrapped_app, host='0.0.0.0', port=8000)
# (Uncomment above lines to run)

view raw JSON →