本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Amazon Verified Permissions 进行授权
Amazon Verified Permissions 是针对您构建的应用程序的授权服务。当您将 Amazon Cognito 用户群体添加为身份源时,应用程序可以将用户群体访问权限或身份(ID)令牌传递给 Verified Permissions,以便做出允许或拒绝决定。Verified Permissions 根据您使用 Cedar 策略语言
您的应用程序可以在 IsAuthorizedWithTokenAPI 请求中向已验证权限提供用户的身份或访问令牌。IsAuthorizedWithToken
API 还接受用户针对他们想要访问的 Resource
所请求的 Action
,以及有助于做出精细访问决策的其他自定义 Context
。
当应用程序在 IsAuthorizedWithToken
API 请求中提供令牌时,Verified Permissions 将执行以下验证。
-
您的用户群体是针对所请求的策略存储而配置的 Verified Permissions 身份来源。
-
访问令牌或身份令牌中的
client_id
或aud
声明分别与您提供给 Verified Permissions 的用户群体应用程序客户端 ID 相匹配。要验证此声明,您必须在 Verified Permissions 身份来源中配置客户端 ID 验证。 -
您的令牌未过期。
-
您的令牌中的
token_use
索赔值与您传递给的参数相匹配IsAuthorizedWithToken
。token_use
声明必须是access
您将其传递给accessToken
参数以及id
是否将其传递给identityToken
参数。 -
令牌中的签名来自用户群体中已发布的 JSON Web 密钥(JWK)。您可以在
https://cognito-idp.
中查看您的 JWK。Region
.amazonaws.com/your user pool ID
/.well-known/jwks.json
已撤销的令牌和已删除的用户
Verified Permissions 仅验证它从您的身份来源和用户令牌到期时间所了解的信息。Verified Permissions 不检查令牌是否撤销或用户是否存在。如果您从用户群体中撤销了用户的令牌或删除了用户的配置文件,则 Verified Permissions 在令牌到期之前仍会认为该令牌有效。
策略评估
将您的用户群体配置为策略存储的身份来源。将您的应用程序配置为在对 Verified Permissions 的请求中提交用户的令牌。对于每个请求,Verified Permissions 将令牌中的声明与策略进行比较。Verified Permissions 策略类似于 Amazon中的 IAM policy。该策略会声明主体、资源 和操作。如果您对于 Allow
的请求与允许的操作匹配,但与显式拒绝操作不匹配,则 Verified Permissions 将对您的请求进行响应;否则,它将以 Deny
进行响应。有关更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略。
自定义令牌
要更改、添加和删除您想要向 Verified Permissions 提供的用户声明,请使用令牌生成前 Lambda 触发器自定义身份令牌中的内容。使用令牌生成前触发器,您可以在令牌中添加和修改声明。例如,您可以在数据库中查询其他用户属性,并将这些属性编码为您的 ID 令牌。
注意
由于 Verified Permissions 处理声明的方式,请勿在令牌生成前函数中添加名为 cognito
、dev
或 custom
的声明。如果您提供的这些保留的声明前缀不是采用以冒号分隔的格式(例如 cognito:username
),而是采用完整的声明名称,则您的授权请求会失败。
Verified Permissions 不处理 cognito:groups
声明。
有关 Verified Permissions 如何将 Amazon Cognito 令牌中的声明映射到授权策略的更多信息,请参阅将 Amazon Cognito 令牌映射到 Verified Permissions 架构。
授权示例
以下是使用自定义 ID 令牌和从 Amazon API Gateway 检索资源的 Amazon Cognito 和 Verified Permissions 的用户群体流程示例。
授权流程示例
-
用户在应用程序中请求资源,而应用程序必须从 API 中检索该资源。
-
用户登录到您的 Amazon Cognito 用户群体。
-
用户群体将用户的详细信息发送到用户群体令牌生成前 Lambda 触发器。使用令牌生成前触发器是可选的。
-
Lambda 函数会在 Amazon DynamoDB 中查找用户,并将声明添加到用户的 ID 或访问令牌中。
-
Amazon Cognito 将访问权限和 ID 令牌返回给应用程序。
-
应用程序在对 Verified Permissions 的请求中提供用户的 ID 令牌和元数据。
-
Verified Permissions 处理用户的声明,并将其与您组织的策略存储中的策略进行比较。当它发现一个不覆盖
Deny
的Allow
条件时,Verified Permissions 会以其Allow
决定进行响应。 -
应用程序处理
Allow
响应,并将用户的访问令牌提供给您的 API Gateway Amazon Cognito 授权者。在本例中,访问令牌具有双重作用:也即,它对 Verified Permissions 的精细访问控制决策和 API Gateway 的基于作用域的访问控制决策都具有重要贡献。 -
您的 API 接受用户的令牌,并将请求的资源返回给应用程序。
-
应用程序向用户显示所请求的资源。
-
当用户请求其他受保护的资源时,应用程序将返回授权步骤,并将新的请求提供给 Verified Permissions。
Amazon Cognito 用户的示例策略
以下示例使用 Cedar 策略语言example_image.png
。John 是应用程序中的用户,他从应用程序客户端接收 ID 令牌,并在 GET 请求中将其传递到需要授权的 URL https://example.com/images/example_image.png
。John 的 ID 令牌拥有用户群体应用程序客户端 ID 1234567890example
的 aud
声明。令牌生成前 Lambda 函数还插入了一个新声明 costCenter
,对于 John 来说,值为 Finance1234
。
permit ( principal, actions in [Action::"readFile", "writeFile"], resource == ExampleCorp::File::"a8f3f048-ded7-4a95-89ca-e283644e1898" // example_image.png ) when { principal.aud == "1234567890example" && principal.costCenter like "Finance*" };
以下请求正文会导致 Allow
响应。
{ "accesstoken": "
[John's access token]
", "action": { "actionId": "readFile", "actionType": "Action" }, "resource": { "entityId": "example_image.png", "entityType": "Photo" } }
当您要在 Verified Permissions 策略中指定主体时,请使用以下格式:
principal == namespace::User::"[user pool ID]"|"[user sub]"
以下是 ID 为 us-east-1_Example
(带有子项)或用户 ID 为 973db890-092c-49e4-a9d0-912a4c0a20c7
的用户群体中用户的主体语句示例。
principal ==
ExampleCorp
::User::"us-east-1_Example
|973db890-092c-49e4-a9d0-912a4c0a20c7
",
基于属性的访问控制
为您的应用程序提供经过验证的权限授权,以及用于 Amazon 凭证的 Amazon Cognito 身份池的访问控制属性功能,都是基于属性的访问控制 (ABAC) 的形式。以下是 Verified Permissions 和 Amazon Cognito ABAC 的功能比较。在 ABAC 中,系统检查实体的属性,并根据您定义的条件做出授权决策。
服务 | 过程 | 结果 |
---|---|---|
Amazon Verified Permissions | Returns an 允许 or 拒绝 decision from analysis of a user
pool JWT. |
Access to application resources succeeds or fails based on Cedar policy evaluation. |
Amazon Cognito identity pools (attributes for access control) | Assigns 会话标签 to your user based on their attributes. IAM policy conditions
can check tags 允许 or 拒绝 user access to Amazon Web Services. |
A tagged session with temporary Amazon credentials for an IAM role. |