gpt4 book ai didi

javascript - 如何使用从 Cognito 返回的代码来获取 AWS 凭证?

转载 作者:可可西里 更新时间:2023-11-01 02:22:39 24 4
gpt4 key购买 nike

现在,我正在努力理解 AWS Cognito,所以也许有人可以帮助我。我设置了一个域来为我的用户池提供 Cognito 的托管 UI,就像描述的那样 here .所以当我去 https://<my-domain>.auth.us-east-1.amazoncognito.com/login?response_type=code&client_id=<MY_POOL_CLIENT_ID>&redirect_uri=https://localhost:8080我得到一个登录页面,我的用户可以在其中使用 Google 登录我的应用程序。那部分工作得很好。

我对如何处理用户登录后从该页面返回的代码感到困惑。因此,一旦我被重定向到 Google 并授权该应用程序查看我的信息,我就会被重定向回我的一个查询参数中带有代码的 URL。现在我正在重定向到本地主机,因此重定向 URL 如下所示:

https://localhost:8080/?code=XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX

这段代码到底是什么?另外,我如何使用它为我的用户访问 AWS 资源?

最佳答案

首先,拧紧身份验证一千次。没有人值得花半天时间看这个狗屎。

使用 Cognito 授权的 API 网关的身份验证

成分

  1. client_idclient_secret:在 Cognito > General Settings > App clients 你可以找到 App client id,然后点击 Show Details 找到 App client secret

  2. 对于 header Authorization: Basic YWJjZGVmZzpvMWZjb28zc...,您需要使用以下代码对这两个进行编码:Base64Encode(client_id:client_secret),例如在 Python 中:

    import base64  
    base64.b64encode('qcbstohg3o:alksjdlkjdasdksd')`

    旁注:Postman 还可以选择在 Authorization > Basic Auth

    中生成它
  3. redirect_uri:传入body,是你在App integration > App client settings配置的回调url。
    这必须与您配置的相匹配,否则您将得到一个完全无用的消息 { "error": "invalid_grant"}

从代码中获取 token 的请求示例:

curl --location --request POST 'https://mycognitodomain.auth.us-east-1.amazoncognito.com/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Authorization: Basic <base64 encoded client_id:client_secret>' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'client_id=<client_id>' \
--data-urlencode 'code=<use the code you received post login>' \
--data-urlencode 'redirect_uri=https://myapp.com'

这将返回您的 token :

{
"access_token":"eyJz9sdfsdfsdfsd",
"refresh_token":"dn43ud8uj32nk2je",
"id_token":"dmcxd329ujdmkemkd349r",
"token_type":"Bearer",
"expires_in":3600
}

然后获取 id_token 并插入您的 API 调用:

curl --location --request GET 'https://myapigateway.execute-api.us-east-1.amazonaws.com/' \
--header 'Authorization: <id_token>'

好的,这被标记为 JavaScript,但因为我们在 Python 中也受苦

友情提示:这是一个例子,请不要硬编码你的 secret 。

import requests

# In: General Settings > App clients > Show details
client_id = "ksjahdskaLAJS ..."
client_secret = "dssaKJHSAKJHDSsjdhksjHSKJDskdjhsa..."

# URL in your application that receives the code post-authentication
# (Cognito lets you use localhost for testing purposes)
callback_uri = "http://localhost:8001/accounts/amazon-cognito/login/callback/"

# Find this in: App Integration > Domain
cognito_app_url = "https://my-application-name.auth.us-west-2.amazoncognito.com"

# this is the response code you received - you can get a code to test by going to
# going to App Integration > App client settings > Lunch Hosted UI
# and doing the login steps, even if it redirects you to an invalid URL after login
# you can see the code in the querystring, for example:
# http://localhost:8001/accounts/amazon-cognito/login/callback/?code=b2ca649e-b34a-44a7-be1a-121882e27fe6
code="b2ca649e-b34a-44a7-be1a-121882e27fe6"

token_url = f"{cognito_app_url}/oauth2/token"
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)

params = {
"grant_type": "authorization_code",
"client_id": client_id,
"code": code,
"redirect_uri": callback_uri
}

response = requests.post(token_url, auth=auth, data=params)

print(response.json()) # don't judge me, this is an example

关于javascript - 如何使用从 Cognito 返回的代码来获取 AWS 凭证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45785898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com