

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

# 新增內容
<a name="context"></a>

*內容*是與政策決策相關的資訊，但不屬於委託人、動作或資源的身分。存取權杖宣告是內容。您可能只想允許來自一組來源 IP 地址的動作，或只有在您的使用者已使用 MFA 登入時。您的應用程式可以存取此內容式工作階段資料，且必須將其填入授權請求。Verified Permissions 授權請求中的內容資料必須在 `contextMap`元素中採用 JSON 格式。

說明此內容的範例來自[範例政策存放區](policy-stores-create.md)。若要遵循，請在您的測試環境中建立 **DigitalPetStore** 範例政策存放區。

下列內容物件會根據範例 **DigitalPetStore** 政策存放區，宣告應用程式的其中一個 Cedar 資料類型。

```
"context": {
  "contextMap": {
    "AccountCodes": {
      "set": [
        {
          "long": 111122223333
        },
        {
          "long": 444455556666
        },
        {
          "long": 123456789012
        }
      ]
    },
    "approvedBy": {
    "entityIdentifier": {
      "entityId": "Bob",
      "entityType": "DigitalPetStore::User"
    }
    },
    "MfaAuthorized": {
      "boolean": true
    },
    "NetworkInfo": {
      "record": {
        "IPAddress": {
          "string": "192.0.2.178"
        },
        "Country": {
          "string": "United States of America"
        },
        "SSL": {
          "boolean": true
        }
    }
    },
    "RequestedOrderCount": {
      "long": 4
    },
    "UserAgent": {
      "string": "My UserAgent 1.12"
    }
  }
}
```授權內容中的資料類型

**Boolean**  
二進位`true`或`false`值。在此範例中， `true`的布林值`MfaAuthenticated`表示客戶在請求檢視其順序之前已執行多重要素驗證。

**設定**  
內容元素的集合。集合成員可以是所有相同的類型，如本範例所示，也可以是不同類型的類型，包括巢狀集合。在此範例中，客戶與 3 個不同的帳戶相關聯。

**String**  
字母、數字或符號的序列，以`"`字元括住。在此範例中，`UserAgent`字串代表客戶用來請求檢視訂單的瀏覽器。

**Long**  
整數。在此範例中， `RequestedOrderCount`表示此請求是客戶要求檢視四個過去訂單所產生的批次的一部分。

**記錄**  
屬性的集合。您必須在請求內容中宣告這些屬性。具有結構描述的政策存放區必須在結構描述中包含此實體和實體的屬性。在此範例中，`NetworkInfo`記錄包含使用者原始 IP、用戶端決定的 IP 地理位置，以及傳輸中加密的相關資訊。

**EntityIdentifier**  
在請求的 `entities`元素中宣告的實體和屬性參考。在此範例中，使用者順序已由員工 核准`Bob`。

若要在 **DigitalPetStore** 應用程式中測試此範例內容，您必須更新請求 `entities`、您的政策存放區結構描述，以及描述**客戶角色 - 取得訂單**的靜態政策。

## 修改 DigitalPetStore 以接受授權內容
<a name="authorization-context-example"></a>

**DigitalPetStore** 一開始不是非常複雜的政策存放區。它不包含任何預先設定的政策或內容屬性，以支援我們呈現的內容。若要使用此內容資訊評估範例授權請求，請對政策存放區和授權請求進行下列修改。如需使用存取權杖資訊做為內容的內容範例，請參閱[映射 Amazon Cognito 存取權杖](cognito-map-token-to-schema.md#cognito-map-access-token)和[映射 OIDC 存取權杖](oidc-map-token-to-schema.md#oidc-map-access-token)。

------
#### [ Schema ]

將下列更新套用至您的政策存放區結構描述，以支援新的內容屬性。在 `GetOrder`中`actions`更新，如下所示。

```
"GetOrder": {
  "memberOf": [],
  "appliesTo": {
    "resourceTypes": [
      "Order"
    ],
    "context": {
      "type": "Record",
      "attributes": {
        "AccountCodes": {
          "type": "Set",
          "required": true,
          "element": {
            "type": "Long"
          }
        },
        "approvedBy": {
          "name": "User",
          "required": true,
          "type": "Entity"
        },
        "MfaAuthorized": {
          "type": "Boolean",
          "required": true
        },
        "NetworkInfo": {
          "type": "NetworkInfo",
          "required": true
        },
        "RequestedOrderCount": {
          "type": "Long",
          "required": true
        },
        "UserAgent": {
          "required": true,
          "type": "String"
        }
      }
    },
    "principalTypes": [
      "User"
    ]
  }
}
```

若要參考請求內容`NetworkInfo`中名為 的`record`資料類型，請在 之前的結構描述中新增以下內容，以在結構描述中建立 [commonType](https://docs.cedarpolicy.com/schema/schema.html#schema-commonTypes) 建構`actions`。`commonType` 建構是一組共用屬性，您可以套用至不同的實體。

```
"commonTypes": {
  "NetworkInfo": {
    "attributes": {
      "IPAddress": {
        "type": "String",
        "required": true
      },
      "SSL": {
        "required": true,
        "type": "Boolean"
      },
      "Country": {
        "required": true,
        "type": "String"
      }
    },
    "type": "Record"
  }
},
```

------
#### [ Policy ]

下列政策會設定每個提供的內容元素都必須滿足的條件。它以現有的靜態政策為基礎，並描述**客戶角色 - 取得訂單**。此政策最初只需要提出請求的委託人是資源的擁有者。

```
permit (
    principal in DigitalPetStore::Role::"Customer",
    action in [DigitalPetStore::Action::"GetOrder"],
    resource
) when {
    principal == resource.owner &&
    context.AccountCodes.contains(111122223333) &&
    context.approvedBy in DigitalPetStore::Role::"Employee" &&
    context.MfaAuthorized == true &&
    context.NetworkInfo.Country like "*United States*" &&
    context.NetworkInfo.IPAddress like "192.0.2.*" &&
    context.NetworkInfo.SSL == true &&
    context.RequestedOrderCount <= 4 &&
    context.UserAgent like "*My UserAgent*"
};
```

我們現在要求擷取訂單的請求，必須符合我們新增至請求的其他內容條件。

1. 使用者必須已使用 MFA 登入。

1. 使用者的 Web 瀏覽器`User-Agent`必須包含字串 `My UserAgent`。

1. 使用者必須已請求檢視 4 個或更少的訂單。

1. 其中一個使用者的帳戶代碼必須是 `111122223333`。

1. 使用者的 IP 地址必須源自美國，且必須位於加密的工作階段，且其 IP 地址必須以 開頭`192.0.2.`。

1. 員工必須已核准其訂單。在授權請求的 `entities`元素中，我們將宣告具有 角色`Bob`的使用者`Employee`。

------
#### [ Request body ]

使用適當的結構描述和政策設定政策存放區之後，您可以將此授權請求呈現給 Verified Permissions API 操作 [IsAuthorized](https://docs.aws.amazon.com/verifiedpermissions/latest/apireference/API_IsAuthorized.html)。請注意，客`entities`群包含 的定義`Bob`，即角色為 的使用者`Employee`。

```
{
  "principal": {
    "entityType": "DigitalPetStore::User",
    "entityId": "Alice"
  },
  "action": {
    "actionType": "DigitalPetStore::Action",
    "actionId": "GetOrder"
  },
  "resource": {
    "entityType": "DigitalPetStore::Order",
    "entityId": "1234"
  },
  "context": {
    "contextMap": {
      "AccountCodes": {
        "set": [
          {"long": 111122223333},
          {"long": 444455556666},
          {"long": 123456789012}
        ]
      },
      "approvedBy": {
        "entityIdentifier": {
          "entityId": "Bob",
          "entityType": "DigitalPetStore::User"
        }
      },
      "MfaAuthorized": {
        "boolean": true
      },
      "NetworkInfo": {
        "record": {
          "Country": {"string": "United States of America"},
          "IPAddress": {"string": "192.0.2.178"},
          "SSL": {"boolean": true}
        }
      },
      "RequestedOrderCount":{
        "long": 4
      },
      "UserAgent": {
        "string": "My UserAgent 1.12"
      }
    }
  },
  "entities": {
    "entityList": [
      {
        "identifier": {
          "entityType": "DigitalPetStore::User",
          "entityId": "Alice"
        },
        "attributes": {
          "memberId": {
            "string": "801b87f2-1a5c-40b3-b580-eacad506d4e6"
          }
        },
        "parents": [
          {
            "entityType": "DigitalPetStore::Role",
            "entityId": "Customer"
          }
        ]
      },
      {
        "identifier": {
          "entityType": "DigitalPetStore::User",
          "entityId": "Bob"
        },
        "attributes": {
          "memberId": {
            "string": "49d9b81e-735d-429c-989d-93bec0bcfd8b"
          }
        },
        "parents": [
          {
            "entityType": "DigitalPetStore::Role",
            "entityId": "Employee"
          }
        ]
      },
      {
        "identifier": {
          "entityType": "DigitalPetStore::Order",
          "entityId": "1234"
        },
        "attributes": {
          "owner": {
            "entityIdentifier": {
              "entityType": "DigitalPetStore::User",
              "entityId": "Alice"
            }
          }
        },
        "parents": []
      }
     ]
   },
   "policyStoreId": "PSEXAMPLEabcdefg111111"
}
```

------