

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 的用戶端和對象驗證 Amazon Cognito
<a name="cognito-validation"></a>

當您將身分來源新增至政策存放區時，Verified Permissions 具有組態選項，可驗證 ID 和存取權杖是否如預期般使用。此驗證會在處理 `IsAuthorizedWithToken`和 `BatchIsAuthorizedWithToken` API 請求時發生。ID 和存取字符，以及 Amazon Cognito 和 OIDC 身分來源的行為有所不同。使用 Amazon Cognito 使用者集區提供者，Verified Permissions 可以驗證 ID 和存取權杖中的用戶端 ID。使用 OIDC 供應商，Verified Permissions 可以驗證 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`宣告。存取字符的`client_id`宣告也包含應用程式用戶端 ID。

當您在身分來源中輸入一或多個**用戶端應用程式驗證**值時，Verified Permissions 會將此應用程式用戶端 IDs 清單與 ID 字符`aud`宣告或存取字符`client_id`宣告進行比較。Verified Permissions 不會驗證 Amazon Cognito 身分來源的依賴方對象 URL。

## JWTs的用戶端授權
<a name="identity-sources-other-idp"></a>

您可能想要在應用程式中處理 JSON Web 字符，並在不使用政策存放區身分來源的情況下將其宣告傳遞給 Verified Permissions。您可以從 JSON Web Token (JWT) 擷取實體屬性，並將其剖析為驗證許可。

此範例示範如何使用 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];
}
```

1 此程式碼範例使用 [aws-jwt-verify](https://github.com/awslabs/aws-jwt-verify) 程式庫來驗證由 OIDC 相容 IdPs 簽署的 JWTs。