亚马逊 Cognito 的客户和受众验证 - Amazon Verified Permissions
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

亚马逊 Cognito 的客户和受众验证

向策略存储中添加身份源时,Verified Permissions 具有用于验证 ID 和访问令牌是否按预期使用的配置选项。这种验证发生在 BatchIsAuthorizedWithToken API 请求IsAuthorizedWithToken的处理过程中。身份令牌和访问令牌以及 Amazon Cognito 和 OIDC 身份源的行为有所不同。通过 Amazon Cognito 用户池提供商,经过验证的权限可以验证身份和访问令牌中的客户端 ID。通过 OIDC 提供商,经过验证的权限可以验证 ID 令牌中的客户端 ID 和访问令牌中的受众。

例如,客户端 ID 是与您的应用程序使用的身份提供商实例关联的标识符1example23456789。例如,受众是与访问令牌的预期依赖方或目标相关联的 URL 路径https://mytoken.example.com。使用访问令牌时,aud声明始终与受众相关联。

亚马逊 Cognito ID 令牌的aud声明包含应用程序客户端 ID。访问令牌的client_id声明也包含应用程序客户端 ID。

当您在身份源中为客户端应用程序验证输入一个或多个值时,Verified Permissions 会将此应用程序客户端 IDs 列表与 ID 令牌aud声明或访问令牌client_id声明进行比较。已验证权限不会验证 Amazon Cognito 身份源的中继方受众网址。

的客户端授权 JWTs

您可能需要在应用程序中处理 JSON Web 令牌并将其声明传递给已验证权限,而无需使用策略存储标识源。您可以从 JSON Web 令牌 (JWT) 中提取实体属性并将其解析为已验证的权限。

此示例说明如何使用 JWT 从应用程序调用 “已验证权限”。¹

async function authorizeUsingJwtToken(jwtToken) { const payload = await verifier.verify(jwtToken); let principalEntity = { entityType: "PhotoFlash::User", // the application needs to fill in the relevant user type entityId: payload["sub"], // the application need to use the claim that represents the user-id }; let resourceEntity = { entityType: "PhotoFlash::Photo", //the application needs to fill in the relevant resource type entityId: "jane_photo_123.jpg", // the application needs to fill in the relevant resource id }; let action = { actionType: "PhotoFlash::Action", //the application needs to fill in the relevant action id actionId: "GetPhoto", //the application needs to fill in the relevant action type }; let entities = { entityList: [], }; entities.entityList.push(...getUserEntitiesFromToken(payload)); let policyStoreId = "PSEXAMPLEabcdefg111111"; // set your own policy store id const authResult = await client .isAuthorized({ policyStoreId: policyStoreId, principal: principalEntity, resource: resourceEntity, action: action, entities, }) .promise(); return authResult; } function getUserEntitiesFromToken(payload) { let attributes = {}; let claimsNotPassedInEntities = ['aud', 'sub', 'exp', 'jti', 'iss']; Object.entries(payload).forEach(([key, value]) => { if (claimsNotPassedInEntities.includes(key)) { return; } if (Array.isArray(value)) { var attibuteItem = []; value.forEach((item) => { attibuteItem.push({ string: item, }); }); attributes[key] = { set: attibuteItem, }; } else if (typeof value === 'string') { attributes[key] = { string: value, } } else if (typeof value === 'bigint' || typeof value ==='number') { attributes[key] = { long: value, } } else if (typeof value === 'boolean') { attributes[key] = { boolean: value, } } }); let entityItem = { attributes: attributes, identifier: { entityType: "PhotoFlash::User", entityId: payload["sub"], // the application needs to use the claim that represents the user-id } }; return [entityItem]; }

¹ 此代码示例使用该aws-jwt-verify库来验证由 OID IdPs C JWTs 兼容的签名。