

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

# OIDC 提供者的用戶端和對象驗證
<a name="oidc-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`宣告一律會與對象建立關聯。

OIDC ID 字符具有包含用戶端 IDs 的`aud`宣告，例如 `1example23456789`。

OIDC 存取字符具有包含字符受眾 URL 的`aud`宣告，例如 `https://myapplication.example.com`，以及包含用戶端 IDs的`client_id`宣告，例如 `1example23456789`。

設定您的政策存放區時，請輸入一或多個值來驗證**您的政策存放區所使用的對象驗證**權杖的對象。
+ **ID 字符** – Verified Permissions 透過檢查`aud`宣告中至少有一個用戶端 IDs成員符合對象驗證值來驗證用戶端 ID。
+ **存取權杖** – Verified Permissions 透過檢查`aud`宣告中的 URL 是否符合對象驗證值來驗證對象。如果沒有`aud`宣告，可以使用 `cid`或 `client_id`宣告來驗證對象。請洽詢您的身分提供者，了解正確的受眾聲明和格式。

## JWTs的用戶端授權
<a name="oidc-validation-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。