amazon-web-services - AWS Cognito 刷新 token : how to use them exactly?

标签 amazon-web-services oauth-2.0 amazon-cognito aws-cognito

我有一个应用程序,它使用授权代码流从 AWS Cognito 用户池 TOKEN 端点获取 3 个 token 。它接收一个 ID_TOKEN、一个 ACCESS_TOKEN 和一个 REFRESH_TOKEN。

应用使用ID_TOKEN获取CognitoAWSCredentials在身份池上:

var credentials = new CognitoAWSCredentials(IdentityPoolId, Region);
credentials.AddLogin("cognito-idp.<region>.amazonaws.com/<UserPoolId>", ID_TOKEN);

一段时间后,凭证停止工作,并且在 AWS 客户端上调用任何方法都会抛出 NotAuthorizedException ,正如预期的那样, token 会在 1 小时后过期。

我的理解是,当 token 过期时,可以再次使用 TOKEN 端点并传递 REFRESH_TOKEN 以取回新 token 。这很有用,如果可以避免,我宁愿不注销应用程序用户并强制他们再次通过 OAUTH 身份验证流程。

所以我尝试刷新 token ,HTTP POST 到 /oauth2/token ,与我第一次获取 token 时相同的请求 header ,但请求正文略有不同?grant_type=refresh_token&client_id=xxx&refresh_token=xxx
我得到一个 ID_TOKEN 和 ACCESS_TOKEN 但没有 REFRESH_TOKEN。

1- 这是预期的吗?这是否意味着在 2 小时后使用相同的 REFRESH_TOKEN 进行第二次刷新?或者是 TOKEN 端点给我的一个微妙的线索是刷新出了问题(更好的线索是端点返回错误......)

2- 仔细检查后,新 ID_TOKEN 与旧 ID_TOKEN 相同。这是预期的,为什么 TOKEN 端点没有返回新的 ID_TOKEN? (当我使用新的 ID_TOKEN 创建新的 CognitoAWSCredentials 时,我得到了无用的凭证。当我第一次访问任何 AWS 客户端时它们会抛出异常。实际上它们已经过期,因为新的 ID_TOKEN 是旧的。)

3- 我应该使用 ACCESS_TOKEN 而不是 ID_TOKEN,我将如何使用 ACCESS_TOKEN 获得 CognitoAWSCredentials?

4- CognitoAWSCredentials实际上是 RefreshingAWSCredentials ,那些真的会自动刷新,如何?

最佳答案

刷新 token 用于刷新 id 和访问 token ,它们的有效期只有一个小时。您可以在下游服务中使用 id token 或访问 token ,但例如 API Gateway 要求您传入 id token 。

你应该使用我们的官方 mobile SDKs当您使用 Cognito 时,不要担心刷新 token ,因为它们会为您完成。

关于amazon-web-services - AWS Cognito 刷新 token : how to use them exactly?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49928408/

相关文章:

java - Tomcat 启动时间太长 - Java SecureRandom

amazon-web-services - AWS Cognito/Amplify 返回空刷新 token

Symfony2+FOSOAuthServerBundle : Browser-less initial authentication

node.js - Cognito 开发人员使用 Node 验证身份

rest - 使用 Cognito 进行 REST API 身份验证

amazon-web-services - AWS 云信息 : How do I check if a bucket exists from within the Cloudformation template?

amazon-web-services - 在 CloudFormation 中使用条件和参数

amazon-web-services - SNS 是否允许基于 String.array 中多个值的存在进行过滤

oauth-2.0 - 如何要求登录 Elixir Phoenix 应用程序?

spring - 多个访问 token 和一个刷新 token