跳转到主要内容

对GitHub进行用户身份验证的ASGI中间件

项目描述

asgi-auth-github

PyPI CircleCI License

对GitHub进行用户身份验证的ASGI中间件。

(最初是datasette-auth-github的一部分,现在作为一个独立的项目分离出来。)

安装说明

将此添加到您的ASGI应用程序中

from asgi_auth_github import GitHubAuth
from your_asgi_app import asgi_app


app = GitHubAuth(
    asgi_app,
    client_id="github_client_id",
    client_secret="github_client_secret",
    require_auth=True,
    # Other options:
    # cookie_ttl=24 * 60 * 60,
    # disable_auto_login=True,
    # allow_users=["simonw"],
    # allow_orgs=["my-org"],
    # allow_teams=["my-org/engineering"],
)

有关其他参数的文档,请参阅datasette-auth-github 0.12文档

以这种方式包装后,如果用户尚未登录,您的应用程序将重定向用户到GitHub进行身份验证。身份验证通过签名cookie记录。

中间件向作用域添加了一个新的"auth"键,其中包含已登录用户的详细信息,然后将这些信息传递给您的应用程序。`scope["auth"]`键的内容如下所示

{
    "id": "1234 (their GitHub user ID)",
    "name": "Their Display Name",
    "username": "their-github-username",
    "email": "their-github@email-address.com",
    "ts": 1562602415
}

“ts”值是一个表示用户上次登录时间的整数`time.time()`时间戳。

如果用户未登录(并且您不使用必需的身份验证),则将`"auth"`作用域键设置为`None`。

使用Starlette的示例

以下是一个使用Starlette ASGI框架的示例。在运行此代码之前,您需要将您的`client_id`和`client_secret`添加到此代码中。

将以下内容保存为starlette_demo.py

from asgi_auth_github import GitHubAuth
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
import uvicorn

app = Starlette(debug=True)


async def homepage(request):
    return JSONResponse({"auth": request.scope["auth"]})


app = Starlette(debug=True, routes=[Route("/", homepage),])


authenticated_app = GitHubAuth(
    app,
    client_id="...",
    client_secret="...",
    require_auth=True,
)

if __name__ == "__main__":
    uvicorn.run(authenticated_app, host="0.0.0.0", port=8001)

按照这种方式安装依赖项

pip install uvicorn starlette asgi-auth-github

然后使用以下命令运行它

python starlette_demo.py

项目详情


下载文件

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

源分发

此版本没有可用的源分发文件。请参阅有关生成分发存档的教程。

构建分发

asgi_auth_github-0.1.1-py3-none-any.whl (12.5 kB 查看哈希值)

上传时间: Python 3

由以下支持

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