跳转到主要内容

Pyramid OAuthLib集成

项目描述

Pyramid OAuthLib

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

Pyramid OAuthLib 是一个库,可以将优秀的 OAuthLib 库轻松集成到 Pyramid 应用程序中。它旨在简化OAuth应用程序的开发,提供平滑的迁移可能性,以便使用其他身份验证或授权方案的老旧代码库,并为Pyramid创建可插拔的OAuth组件提供配置模式。

注意: Pyramid OAuthLib 功能尚未完善!它缺少令牌撤销的钩子。虽然添加这个功能不难,但它不是最初版本发布的优先事项。

使用概述

配置

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)

令牌响应

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)

自定义授权类型

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')

许可证

Pyramid OAuthLib 在 2-Clause BSD License 下发布,有时也称为“简化版BSD许可证”或“FreeBSD许可证”。更多许可证信息可以在包含的 LICENSE.txt 文件中找到。

1.0.0 (2022-09-16)

重大更改

  • Pyramid OAuthLib 现在需要Python 3.7+。

  • Pyramid OAuthLib 现在需要OAuthLib 3+。

0.4.2 (2020-09-07)

错误修复

  • 添加OAuthLib缺少的参数。

0.4.1 (2019-06-28)

错误修复

  • 修复 duplicate_params 请求属性。

  • 修复版本说明,以指示OAuthLib 3尚未支持。

0.4.0 (2018-11-16)

重大更改

  • 停止支持Pyramid 1.3及以下版本。

功能

  • Pyramid 1.10支持。

0.3.0 (2018-03-10)

功能

  • Python 3支持。

  • 支持密码凭据流参数。

0.2.0 (2014-11-21)

功能

  • add_oauth_param 暴露为 Configurator 实例上的指令。OAuthLib 代码通常假定这些属性存在于请求实例中。公开此指令允许自定义扩展更易于移植到非 Pyramid 代码,通过避免使用 request.params,而是使用 OAuthLib 请求实例的透明属性访问模式。

0.1.1 (2014-08-04)

功能

  • 便于集成 OAuth 流的请求方法。

  • 用于与 OAuthLib 模块集成的请求参数。

  • 注册新的授权、响应和令牌类型,(可选) 使用点分命名解析。

  • 可查询的配置。

  • 完整的单元测试覆盖率

本版本中缺少

  • 支持撤销。

项目详情


下载文件

下载您平台的文件。如果您不确定要选择哪个,请了解有关 安装包 的更多信息。

源分布

pyramid_oauthlib-1.0.0.tar.gz (9.6 kB 查看哈希值)

上传时间

构建分布

pyramid_oauthlib-1.0.0-py3-none-any.whl (7.8 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面