Skip to main content

pyramid_selectauth

Project description

pypi travis

An authentication policy for Pyramid that automagically selects the correct policy to use for a specific request, given a list of authentication policies

Overview

SelectableAuthenticationPolicy is a Pyramid authentication policy that selects another provided IAuthenticationPolicy object, to provide a different auth policy based on the specific request. Simply pass it a list of policies that should be tried in order, and register the ‘selected_policy’ request method that will select and cache the correct policy to use:

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

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”.

You can use the create_selectable_authentication_policy factory to just create the SelectableAuthenticationPolicy instance without setting it:

policies = [
    IPAuthenticationPolicy("127.0.*.*", principals=["local"])
    IPAuthenticationPolicy("192.168.*.*", principals=["trusted"])
]
policy = create_selectable_authentication_policy(config, policies)
policy.add_policy(IPAuthenticationPolicy("10.0.*.*", principals=["not-so-trusted"]))
config.set_authentication_policy(policy)

You can also just use config.include() to include the policy, and then add the subpolicies with the registered add_selectauth_policy directive on config:

config.include('pyramid_selectauth')
config.add_selectauth_policy(IPAuthenticationPolicy("127.0.*.*", principals=["local"]))
config.add_selectauth_policy(IPAuthenticationPolicy("192.168.*.*", principals=["trusted"]))

Policy selection method

The default selection method will call unauthenticated_userid on the provided policies in order, and select the first one that does not return None.

You can change the selection method by extending the SelectableAuthenticationPolicy and overriding the select_policy method with your logic to select the correct policy for the current request, and then specifying your class in the factories:

class MyPolicy(SelectableAuthenticationPolicy):
    def select_policy(self, request):
        return self._policies[0]  # Always uses the first policy (very useful!)


policies = [
    IPAuthenticationPolicy("127.0.*.*", principals=["local"])
    IPAuthenticationPolicy("192.168.*.*", principals=["trusted"])
]
policy = create_selectable_authentication_policy(config, policies, _class=MyPolicy)
config.set_authentication_policy(policy)

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

selectauth.policy_class = mypyramidapp.policies.MySelectAuthPolicy
selectauth.policies = mypyramidapp.policies.ipauthpolicyfactory mypyramidapp.policies.mypolicyfactory

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

config.include("pyramid_selectauth")

1.0.1 - 2018-09-24

  • Refactored policy methods using proxy_method decorator.

1.0.0 - 2018-09-23

  • Production-ready release.

0.0.4 - 2018-09-21

  • Initial release.

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_selectauth-1.0.1.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

pyramid_selectauth-1.0.1-py2.py3-none-any.whl (7.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyramid_selectauth-1.0.1.tar.gz.

File metadata

  • Download URL: pyramid_selectauth-1.0.1.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for pyramid_selectauth-1.0.1.tar.gz
Algorithm Hash digest
SHA256 69211a9c53637dffcb20cfd6836644c09b467e9b1b9eae63fcba22dc3d1dd4b4
MD5 707d86bc2ccd36bc60640dd1d822c70d
BLAKE2b-256 c00c74d186cc1c58f00d49c68a06d93e787f6c17a0bb97d073b4b32b1eb65acc

See more details on using hashes here.

File details

Details for the file pyramid_selectauth-1.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pyramid_selectauth-1.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for pyramid_selectauth-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5be9c56be90ceebf2de6825586fa54c0693aa2d7d52613f4731710146bf7d058
MD5 83ad525862653cab09b33bba62cd029c
BLAKE2b-256 70a686993a23a2f22e07ed11ccb3f782d0f3f0c11cec6f2517abb8e8c3809467

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