如何修改 AWS Cognito 用户池的访问和身份令牌的到期时间

How to modify expiry time of the access and identity tokens for AWS Cognito User Pools

我找不到任何说明是否以及如何修改 AWS Cognito 用户池的访问和身份令牌的到期时间的文档。

documentation指定默认在发射后1h过期。

有没有办法修改过期时间?

目前无法为您的用户池配置。每个人都设置为一小时。

编辑:查看 Mike 的评论,这是最近添加的。

Cognito 使用 OAuth 2.0 Specification。为了更新过期的令牌,您需要使用 Refresh Token 值来获取新的 Id Token.

  1. 为了在开始时进行身份验证,将从用户那里收集用户 ID 和密码并将其发送到 Cognito。
  2. 你得到两个代币。一种用于“访问”API,另一种用于在访问权限到期时“刷新”。
  3. 您无需再次要求用户输入用户名和密码;你只需要使用“刷新”令牌。
  4. 您不需要存储密码的明文(这会产生安全风险),因为“刷新”令牌会给您一个新的访问令牌。

真的很简单。 Cognito documentation to Refresh Tokens

中的更多信息

澄清:此回复是关于访问令牌(不是刷新令牌)

您可以从 Cognito 控制台配置令牌过期 General Settings / App Clients / {your app} / Show Details / Refresh token expiration (days)

https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html

By default, the refresh token expires 30 days after your app user signs in to your user pool. When you create an app for your user pool, you can set the app's refresh token expiration (in days) to any value between 1 and 3650.

目前web客户端似乎没有不到一天的选项(很奇怪)。

如果您使用 Mobile SDK 那么

https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html

The Mobile SDK for Android offers the option to change the minimum validity period of the ID and access tokens to a value between 0 and 30 minutes. See the setRefreshThreshold() method of CognitoIdentityProviderClientConfig in the AWS Mobile SDK for Android API Reference.

我想问题是如何获得对 Cognito 会话终止的精细控制。有一种方法可以做到这一点。但首先让我们回顾一下 Cognito 会话管理的工作原理:

  1. 授权令牌一小时后过期。
  2. 在发布刷新令牌时可能会请求新的身份验证令牌。
  3. 1 到 30 天后,Cognito 将不会发出刷新令牌 - 天数是在应用程序客户端设置中为每个应用程序配置的。

那么您可以如何更好地控制 Cognito 会话长度?答案是在您的 http 请求堆栈中插入一个过滤器来评估请求 - 如果用户出于任何原因必须注销,请向 Cognito 注销端点发出 302 重定向(并清除您的会话 cookie)。

这就是我们在 Kubernetes 中使用 Envoy(使用代理)所做的,也是 Spring。它还允许您连接逻辑,在用户的 1 小时访问令牌到期之前立即撤销对用户的访问。

https://docs.aws.amazon.com/cognito/latest/developerguide/logout-endpoint.html

自 2020 年 8 月 12 日起,AWS has announced该用户池现在支持自定义令牌到期时间。以下是要遵循的步骤:

  1. 打开您的 AWS Cognito 控制台。
  2. 转到 General Settings
  3. 向下滚动到 App clients 并单击编辑。
  4. 单击 Show Details 按钮可查看如下自定义选项:

Access token expiration must be between 5 minutes and 1 day. Cannot be greater than refresh token expiration.

有关 AWS Cognito 的更多详细信息,您可以关注此 link

如果您使用的是 CloudFormation 模板,请添加以下属性并以天为单位指定(尽管官方文档说它默认为小时)访问令牌应有效的时间长度。这是一个访问令牌有效期为 24 天的示例。

UserPoolClient:
    Type: "AWS::Cognito::UserPoolClient"
    Properties:
        ClientName: myuserpoolclient
        GenerateSecret: true
        UserPoolId: !Ref YourUserPool
        AccessTokenValidity: 24

文档: https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPoolClient.html#CognitoUserPools-CreateUserPoolClient-request-AccessTokenValidity