

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# のクライアントとオーディエンスの検証 Amazon Cognito
<a name="cognito-validation"></a>

ID ソースをポリシーストアに追加すると、Verified Permissions には、ID トークンとアクセストークンが意図したとおりに使用されていることを確認する設定オプションがあります。この検証は、 `IsAuthorizedWithToken`および `BatchIsAuthorizedWithToken` API リクエストの処理で行われます。この動作は、ID トークンとアクセストークン、および Amazon Cognito と OIDC ID ソースによって異なります。Amazon Cognito ユーザープールプロバイダーを使用すると、Verified Permissions は ID トークンとアクセストークンの両方でクライアント ID を検証できます。OIDC プロバイダーを使用すると、Verified Permissions は ID トークンのクライアント ID とアクセストークンの対象者を検証できます。

*クライアント ID* は、アプリケーションが使用する ID プロバイダーインスタンスに関連付けられた識別子です`1example23456789`。例: 。*対象者*は、 など、アクセストークンの目的の*証明書利用者*または送信先に関連付けられた URL パスです`https://mytoken.example.com`。アクセストークンを使用する場合、`aud`クレームは常に対象者に関連付けられます。

Amazon Cognito ID トークンには、[アプリケーションクライアント](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-client-apps.html) ID を含む `aud`クレームがあります。アクセストークンには、アプリケーションクライアント ID も含まれる`client_id`クレームがあります。

ID ソースに**クライアントアプリケーション検証**に 1 つ以上の値を入力すると、Verified Permissions はこのアプリケーションクライアント IDs のリストを ID トークン`aud`クレームまたはアクセストークン`client_id`クレームと比較します。Verified Permissions は、 Amazon Cognito ID ソースの証明書利用者 URL を検証しません。

## JWTs のクライアント側の認可
<a name="identity-sources-other-idp"></a>

アプリケーションで JSON ウェブトークンを処理し、ポリシーストア ID ソースを使用せずにそのクレームを Verified Permissions に渡すことができます。JSON Web Token (JWT) からエンティティ属性を抽出し、Verified Permissions に解析できます。

この例では、JWT.1 を使用してアプリケーションから Verified Permissions を呼び出す方法を示します。

```
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) ライブラリを使用して OIDC 互換 IdPs によって署名された JWT を検証しています。