Skip to main content

Pyramid OAuthLib integration

Project description

https://travis-ci.org/tilgovi/pyramid-oauthlib.svg?branch=master http://img.shields.io/coveralls/tilgovi/pyramid-oauthlib.svg

Pyramid OAuthLib is a library to integrate the excellent OAuthLib library easily into Pyramid applications. It is designed to ease development of OAuth applications, provide smooth migration possibilites to legacy codebases using other authentication or authorization schemes, and configuration patterns for creating pluggable OAuth components for Pyramid.

NOTICE: Pyramid OAuthLib is not feature complete! It is missing the hooks for token revocation. While this shouldn’t be hard to add, it wasn’t a priority to get the initial version released.

Usage Overview

Configuration:

def includeme(config):
    """Integration with OAuthLib is as smooth as possible."""
    from oauthlib.oauth2 import BearerToken, AuthorizationCodeGrant

    # Validator callback functions are passed Pyramid request objects so
    # you can access your request properties, database sessions, etc.
    # The request object is populated with accessors for the properties
    # referred to in the OAuthLib docs and used by its built in types.
    validator = MyRequestValidator()

    # Register response types to create grants.
    config.add_response_type('oauthlib.oauth2.AuthorizationCodeGrant',
                             name='code',
                             request_validator=validator)

    # Register grant types to validate token requests.
    config.add_grant_type('oauthlib.oauth2.AuthorizationCodeGrant',
                          name='authorization_code',
                          request_validator=validator)

    # Register the token types to use at token endpoints.
    # The second parameter to all registrations may be left out to set it
    # as default to use when no corresponding request parameter specifies
    # the grant, response or token type. Be aware that the built in types
    # will fail if a matching request parameter is missing, though.
    config.add_token_type('oauthlib.oauth2.BearerToken'
                          request_validator=validator)

Token response:

def access_token(request):
    """Core functionality is available directly from the request.

    Responses from OAuthLib are wrapped in a response object of type
    :class:`pyramid.response.Response` so they can be returned directly
    from views.
    """
    userid = request.authenticated_userid
    if userid is not None:
        credentials = dict(userId=userid)
    else:
        credentials = None

    return request.create_token_response(credentials=credentials)

Custom grant type:

from oauthlib.oauth2 import ClientCredentialsGrant, InvalidClientError
from pyramid.authentication import BadCSRFToken
from pyramid.session import check_csrf_token

class SessionGrant(ClientCredentialsGrant):

    """A combined authentication and authorization session assertion grant.

    When the Authorization Server and the Token Service are the same server
    this grant type uses a single assertion, the CSRF token, for client
    authentication and an authorization grant.[1] This works particularly
    well with :class:`pyramid.authentication.SessionAuthenticationPolicy`.

    [1] http://tools.ietf.org/html/draft-ietf-oauth-assertions-01#section-3
    """

    def validate_token_request(self, request):
        try:
            check_csrf_token(request, token='assertion')
        except BadCSRFToken:
            raise InvalidClientError(request=request)

        # An object with the confidential client_id and client_secret.
        request.client = LOCAL_CLIENT

        if request.client is None:
            raise InvalidClientError(request=request)

        request.client_id = request.client_id or request.client.client_id


def includeme(config):
    config.add_grant_type(SessionGrant, 'assertion')

License

Pyramid OAuthLib is released under the 2-Clause BSD License, sometimes referred to as the “Simplified BSD License” or the “FreeBSD License”. More license information can be found in the included LICENSE.txt file.

Next release

0.0

  • Initial version

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_oauthlib-0.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

pyramid_oauthlib-0.1-py2.py3-none-any.whl (8.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyramid_oauthlib-0.1.tar.gz.

File metadata

File hashes

Hashes for pyramid_oauthlib-0.1.tar.gz
Algorithm Hash digest
SHA256 d0842d7cf339660bf78d2e6d72de91f2e7bbf41205312e2be05722e32d3eec80
MD5 58d4e7a5245b2f8b7ac4e4f83dd179d9
BLAKE2b-256 f63b62fd7078066ca2312f5def358beef6f7fed80e4c30174ed755274c2a074c

See more details on using hashes here.

File details

Details for the file pyramid_oauthlib-0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pyramid_oauthlib-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9c48d79ce1e603f6762ab3ffa54465fe55a9279e6fb06be3d0fc698e2fca3da6
MD5 d55adbd0759faf016187ccf5a3f9486e
BLAKE2b-256 fb64bc5e834fdbbe9f89a919dc69b495b6f94e0a2464195f7a09eac511b21229

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