Skip to main content

An authentication policy for Pyramid that proxies to a stack of other authentication policies

Project description

pypi ci coverage

An authentication policy for Pyramid that proxies to a stack of other authentication policies.

Overview

MultiAuthenticationPolicy is a Pyramid authentication policy that proxies to a stack of other IAuthenticationPolicy objects, to provide a combined auth solution from individual pieces. Simply pass it a list of policies that should be tried in order:

policies = [
    IPAuthenticationPolicy("127.0.*.*", principals=["local"])
    IPAuthenticationPolicy("192.168.*.*", principals=["trusted"])
]
authn_policy = MultiAuthenticationPolicy(policies)
config.set_authentication_policy(authn_policy)

This example uses the pyramid_ipauth module to assign effective principals based on originating IP address of the request. It combines two such policies so that requests originating from “127.0.*.*” will have principal “local” while requests originating from “192.168.*.*” will have principal “trusted”.

In general, the results from the stacked authentication policies are combined as follows:

  • authenticated_userid: return userid from first successful policy

  • unauthenticated_userid: return userid from first successful policy

  • effective_principals: return union of principals from all policies

  • remember: return headers from all policies

  • forget: return headers from all policies

Deployment Settings

It is also possible to specify the authentication policies as part of your paste deployment settings. Consider the following example:

[app:pyramidapp]
use = egg:mypyramidapp

multiauth.policies = ipauth1 ipauth2 pyramid_browserid

multiauth.policy.ipauth1.use = pyramid_ipauth.IPAuthentictionPolicy
multiauth.policy.ipauth1.ipaddrs = 127.0.*.*
multiauth.policy.ipauth1.principals = local

multiauth.policy.ipauth2.use = pyramid_ipauth.IPAuthentictionPolicy
multiauth.policy.ipauth2.ipaddrs = 192.168.*.*
multiauth.policy.ipauth2.principals = trusted

To configure authentication from these settings, simply include the multiauth module into your configurator:

config.include("pyramid_multiauth")

In this example you would get a MultiAuthenticationPolicy with three stacked auth policies. The first two, ipauth1 and ipauth2, are defined as the name of of a callable along with a set of keyword arguments. The third is defined as the name of a module, pyramid_browserid, which will be processed via the standard config.include() mechanism.

The end result would be a system that authenticates users via BrowserID, and assigns additional principal identifiers based on the originating IP address of the request.

If necessary, the group finder function and the authorization policy can also be specified from configuration:

[app:pyramidapp]
use = egg:mypyramidapp

multiauth.authorization_policy = mypyramidapp.acl.Custom
multiauth.groupfinder  = mypyramidapp.acl.groupfinder

...

MultiAuthPolicySelected Event

An event is triggered when one of the multiple policies configured is selected.

from pyramid_multiauth import MultiAuthPolicySelected


# Track policy used, for prefixing user_id and for logging.
def on_policy_selected(event):
    print("%s (%s) authenticated %s for request %s" % (event.policy_name,
                                                       event.policy,
                                                       event.userid,
                                                       event.request))

config.add_subscriber(on_policy_selected, MultiAuthPolicySelected)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyramid_multiauth-1.0.2.tar.gz (31.1 kB view details)

Uploaded Source

Built Distribution

pyramid_multiauth-1.0.2-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file pyramid_multiauth-1.0.2.tar.gz.

File metadata

  • Download URL: pyramid_multiauth-1.0.2.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for pyramid_multiauth-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e4c01c59dd865a637173c7d0e3de062895e4a1b3312e442add6182989a13ca83
MD5 f270abfe1dab0f64d29ae68782edb56e
BLAKE2b-256 c85130beabe10c2cfab7d43bd2c7c6a183eb917193841ba8d5979ea4e5cc5427

See more details on using hashes here.

File details

Details for the file pyramid_multiauth-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pyramid_multiauth-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0cd1f2d9c782458c3992a9183a55f9fcd257a2f1c6e7d62e44552f7cdaff9063
MD5 2479523f1e08029064237293b6a5d511
BLAKE2b-256 6c6bc25340c5efccdbb7352d88a513b881b986fbb02e3b94fce937e6786d725e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page