【问题标题】:How to implement OAuth to FastAPI with client ID & Secret如何使用客户端 ID 和 Secret 实现对 FastAPI 的 OAuth
【发布时间】:2020-11-24 14:56:19
【问题描述】:

我已经关注了有关 Oauth2 的文档,但它没有描述添加客户端 ID 和密码的过程

https://fastapi.tiangolo.com/advanced/security/oauth2-scopes/

这是做什么的

class UserInDB(User):
    hashed_password: str

来自原始示例

【问题讨论】:

    标签: python oauth-2.0 fastapi


    【解决方案1】:

    在文档中它使用OAuth2PasswordRequestForm 来验证用户,这个类基本上有 6 个不同的字段,

    grant_type: str = Form(None, regex="password"),
    username: str = Form(...),
    password: str = Form(...),
    scope: str = Form(""),
    client_id: Optional[str] = Form(None),
    client_secret: Optional[str] = Form(None),
    

    所以你可以添加client_idclient_secret,如果你有兴趣在这里Repository

    但我通常更喜欢authlib,它节省了很多时间,使它更容易。这是一个完整的示例,说明如何使用 authlib 创建 OAuth

    首先创建一个 OAuth 客户端

    from authlib.integrations.starlette_client import OAuth
    from starlette.config import Config
    
    config = Config('.env')  # read config from .env file
    oauth = OAuth(config)
    oauth.register(
        name='google',
        server_metadata_url='https://accounts.google.com/.well-known/openid-configuration',
        client_kwargs={
            'scope': 'openid email profile'
        }
    )
    

    这里不需要添加client_id 和client_secret,因为它们在.env 文件中。您不应该在真实产品的代码中对它们进行硬编码。Google 有一个 OpenID 发现端点,我们可以将此 URL 用于server_metadata_url。 Authlib 会自动获取此server_metadata_url 来为您配置 OAuth 客户端。

    现在我们将创建一个 FastAPI 应用程序来定义登录路由。

    from fastapi import FastAPI, Request
    from starlette.middleware.sessions import SessionMiddleware
    
    app = FastAPI()
    app.add_middleware(SessionMiddleware, secret_key="secret-string")
    

    我们需要这个 SessionMiddleware,因为 Authlib 将使用request.session 来存储临时代码和状态。以下代码是/login 端点,会将您重定向到 Google 帐户网站。

    @app.route('/login')
    async def login(request: Request):
        redirect_uri = request.url_for('auth')
        return await oauth.google.authorize_redirect(request, redirect_uri
    

    当您从 Google 网站授予访问权限时,Google 将重定向回您给定的redirect_uri,即request.url_for('auth')

    @app.route('/auth')
    async def auth(request: Request):
        token = await oauth.google.authorize_access_token(request)
        user = await oauth.google.parse_id_token(request, token)
        return user
    

    上面的代码会得到一个包含access_token和id_token的token。 id_token包含用户信息,我们只需要解析它就可以得到登录用户的信息。

    来源:Authlib-FastAPI-Google-Login

    另外,如果您仍想使用 Pure FastAPI,请查看此链接 FastAPI OAuth2PasswordRequestForm

    【讨论】:

      猜你喜欢
      • 2017-04-30
      • 2013-12-31
      • 1970-01-01
      • 2017-10-18
      • 2020-08-04
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多