

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

# OIDC 提供商的客户和受众验证
<a name="oidc-validation"></a>

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

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

OIDC ID 令牌的`aud`声明包含客户端 IDs，例如。`1example23456789`

OIDC 访问令牌的`aud`声明包含令牌的受众网址（例如`https://myapplication.example.com`）和包含客户端 IDs（例如）的`client_id`声明。`1example23456789`

设置策略存储时，请输入一个或多个**受众验证**值，您的政策存储使用该值来验证令牌的受众。
+ **ID 令牌** — 已验证权限通过检查`aud`声明 IDs 中至少有一名客户成员与受众验证值匹配来验证客户端 ID。
+ **访问令牌** — 已验证权限通过检查`aud`声明中的网址是否与受众验证值相匹配来验证受众。如果不存在任何`aud`声明，则可以使用`cid`或`client_id`声明来验证受众。请咨询您的身份提供商，了解正确的受众主张和格式。

## 的客户端授权 JWTs
<a name="oidc-validation-other-idp"></a>

您可能需要在应用程序中处理 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](https://github.com/awslabs/aws-jwt-verify)库来验证由 OID IdPs C JWTs 兼容的签名。