Cognito 用户池中的自定义属性是否适合存储动态用户信息?

Is a custom attribute in Cognito user pool the proper place to store dynamic user information?

Cognito 的 AWS 文档说:

Each custom attribute: Cannot be removed or changed once added to the user pool.

发件人:https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#user-pool-settings-custom-attributes

我相信他们指的是自定义属性的名称,而不是值本身。

因此,例如,可以从 Lambda 函数更改该值。假设我们正在为我们的电子商务网站中的每个用户存储保真度点数。

自定义属性是否适合存储此类信息?或者我应该创建一个新的 DynamoDB table 链接到用户池中的 UserId?

您当然可以将此信息存储在 Cognito 自定义属性中。如果你这样做,我会考虑两件事:

1) 确保用于对用户池进行身份验证的 Cognito 用户池客户端没有写入此属性的权限。否则,流氓用户可以编写代码来根据用户池对自己进行身份验证,并根据需要为自己提供尽可能多的保真度点。因此,您可能要考虑将自定义属性更新隐藏在服务后面。

2) 根据您需要更新此属性的频率以及您的整体 Cognito 使用模式,您在使用 Cognito updateAttributes API 时可能会遇到 RequestLimitExceeded 错误。几乎每次我尝试使用 Cognito 作为用户信息的主要数据存储时,我都会受到限制。 AWS 支持会提高您的限制,但错误会在没有警告的情况下发生,这在生产环境中不是很好。我总是默认使用 DynamoDB table。当然这只是我的经验所以 YMMV

在我看来,如果您需要属性值出现在身份令牌上,您应该使用自定义属性,否则最好将其存储在另一个地方。

示例:您有 'users' 是 'pages' 的管理员,然后您将这种关系存储在自定义属性中。 Cognito 将该信息添加到身份令牌中,然后当用户对 'pages' 进行 api 调用时,您只需要信任令牌中的信息,而不需要进行额外的数据库调用来检查用户是否相关到那个页面。

自定义属性非常严格,因为 documentation says:

You can add up to 50 custom attributes to your user pool. You can specify a minimum and/or maximum length for custom attributes. However, the maximum length for any custom attribute can be no more than 2048 characters.

Each custom attribute:

Can be defined as a string or a number.

Can't be required.

Can't be removed or changed once added to the user pool.

Can have a name with a character length that is within the limit that is accepted by Amazon Cognito. For more information, see Quotas in Amazon Cognito.

如果你不需要令牌上的属性,我更喜欢使用 dynameDB,限制少得多。