跳转到主要内容

Pyramid的Google登录

项目描述

Pyramid的Google登录认证策略 (OAuth2服务器端流程)

此扩展不会配置任何认证策略。您需要负责在您的Pyramid应用程序中设置正确的安全配置。当通过Google认证时,此扩展调用方法 pyramid.security.remember 并假定认证策略将记住用户身份。

安装

使用setuptools安装,例如(在虚拟环境内)

$ pip install pyramid_google_login

设置: 应用程序

一旦安装了 pyramid_google_login,您必须使用 config.include 机制将其包含到您的Pyramid项目配置中。在您的Pyramid项目的 __init__.py

config = Configurator(.....)
config.include('pyramid_google_login')

另外,您也可以在您的 .ini 文件中使用 pyramid.includes 配置值

[app:myapp]
pyramid.includes = pyramid_google_login

设置:配置

必填设置

security.google_login.client_id = xxxxxxx.apps.googleusercontent.com
security.google_login.client_secret = xxxxxxxxxxxxxxxxxxxxxxxxx

可选设置

# List of Google scopes (``email`` is automatically included)
security.google_login.scopes = email

# Set the access type to ``offline`` to get a refresh_token (default: online)
security.google_login.access_type = online

# Field used to extract the userid (generally ``email`` or ``id``)
security.google_login.user_id_field = email

# Restrict authentication to a Google Apps domain
security.google_login.hosted_domain = example.net

# Redirect destination for logged in user.
security.google_login.landing_url = /
security.google_login.landing_route = my_frontend_route
security.google_login.landing_route = mymodule:static/

# Add a banner on the sign in page
security.google_login.signin_banner = Welcome on Project Euler

# Add an advice on the sign in page
security.google_login.signin_advice = Ask Dilbert for access

设置:Google项目

  • https://console.developers.google.com 上创建一个项目

  • 创建 OAuth 客户端 ID

    • 选择 Web 应用程序 应用程序类型

    • 在 Javascript Origins 中添加您主机的所有变体

      • 安全和非安全 URL 被区分

      • 可选地使用 https://:6543 而不是 http://127.0.0.1:6543(它会被拒绝)来包含您的开发主机

注意

  • pyramid_google_login 本身不需要任何权限。

  • 客户端 ID 参数被大量缓存。在开发中,重新创建客户端 ID 通常是最好的选择。

一般用法

当用户必须通过 Google 进行身份验证时,必须将其发送到 auth_signin 路由 URL。辅助方法 pyramid_google_login.redirect_to_signin 将用户重定向到登录页面。此辅助方法便于指定下一个 URL 和可选消息。

@forbidden_view_config()
def unauthenticated(context, request):
    return redirect_to_signin(request, url=request.path_qs)

一旦用户经过身份验证,就会广播 UserLoggedIn pyramid 事件。应用程序可以执行后续验证、创建或更新用户配置文件。

之后,调用 pyramid.security.remember 辅助方法。

然后,用户将被重定向到以下 URL:

  • 查询参数(登录页面):url

  • 设置:security.google_login.landing_url

  • 回退:/

当用户必须注销时,必须将其引导到 auth_logout 路由 URL。一旦注销,他将被重定向回登录页面。

离线使用

如果您想代表用户调用 Google API,您必须存储在 UserLoggedIn 事件中提供的 OAuth2 令牌。在 expires_in 期间,access_token 可用。然后必须使用 refresh_token 来刷新 access_token。此 refresh_token 有效,直到用户撤销应用程序权限。

默认情况下,请求的唯一范围是 email 以标识用户。要调用其他 Google API,必须添加相关的范围,如下所示

[app:myapp]

security.google_login.scopes =
    email
    https://www.googleapis.com/auth/admin.directory.user.readonly

事件

UserLoggedIn

用户已通过 Google 登录。

属性

  • userid

  • oauth2_token

    • access_token

    • expires_in

    • refresh_token

  • user_info

    • Google user_info 属性…

UserLoggedOut

用户已注销。

属性

  • userid

开发

此项目支持 Dad (https://github.com/pior/dad)。

运行测试

$ pip install -r requirements.txt
$ nosetests

运行 pylama(linters)

$ pylama

创建新版本

$ dad release 1.0

项目详情


下载文件

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

源分发

pyramid_google_login-1.2.1.tar.gz (18.0 kB 查看哈希)

上传于 来源

支持者

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