

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

# OIDC プロバイダーのクライアントとオーディエンスの検証
<a name="oidc-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`クレームは常に対象者に関連付けられます。

OIDC ID トークンには、 などのクライアント IDs を含む`aud`クレームがあります`1example23456789`。

OIDC Access トークンには、 などのトークンのオーディエンス URL を含む`aud`クレームと`https://myapplication.example.com`、 などのクライアント IDs を含む`client_id`クレームがあります`1example23456789`。

ポリシーストアを設定するときは、ポリシーストアがトークンの**対象者**を検証するために使用する対象者検証の値を 1 つ以上入力します。
+ **ID トークン** – Verified Permissions は、`aud`クレーム内のクライアント ID の少なくとも 1 つのメンバーが対象者検証値と一致することを確認して、クライアント IDs を検証します。
+ **アクセストークン** – Verified Permissions は、`aud`クレームの URL が対象者検証値と一致することを確認して対象者を検証します。`aud` クレームが存在しない場合、対象者は `cid`または `client_id`クレームを使用して検証できます。ID プロバイダーに正しい対象者のクレームと形式を確認してください。

## JWTs のクライアント側の認可
<a name="oidc-validation-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 を検証しています。