

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

# Amazon Verified Permissions 範例政策
<a name="policies-examples"></a>

此處包含的一些政策範例是基本的 Cedar 政策範例，有些則是 Verified Permissions-specific。基本政策連結到 Cedar 政策語言參考指南，並包含在其中。如需 Cedar 政策語法的詳細資訊，請參閱[《Cedar 政策語言參考指南》中的 Cedar 中的基本政策建構](https://docs.cedarpolicy.com/policies/syntax-policy.html)。

**政策範例**
+ [允許存取個別實體](https://docs.cedarpolicy.com/policies/policy-examples.html#allow-acces-indivuals)
+ [允許存取實體群組](https://docs.cedarpolicy.com/policies/policy-examples.html#allow-acces-groups)
+ [允許存取任何實體](https://docs.cedarpolicy.com/policies/policy-examples.html#allow-any)
+ [允許存取實體的屬性 (ABAC)](https://docs.cedarpolicy.com/policies/policy-examples.html#allow-abac)
+ [拒絕存取](https://docs.cedarpolicy.com/policies/policy-examples.html#deny-access)
+ [使用括號表示法來參考字符屬性](#policies-examples-brackets)
+ [使用點表示法來參考屬性](#policies-examples-dot)
+ [反映 Amazon Cognito ID 字符屬性](#policies-examples-cognito-id)
+ [反映 OIDC ID 字符屬性](#policies-examples-oidc-id)
+ [反映 Amazon Cognito 存取權杖屬性](#policies-examples-cognito-access)
+ [反映 OIDC 存取權杖屬性](#policies-examples-oidc-access)

## 使用括號表示法來參考字符屬性
<a name="policies-examples-brackets"></a>

下列範例示範如何建立使用括號表示法來參考字符屬性的政策。

如需在 Verified Permissions 中的政策中使用權杖屬性的詳細資訊，請參閱[將 Amazon Cognito 權杖映射至結構描述](cognito-map-token-to-schema.md)，以及將 [OIDC 權杖映射至結構描述](oidc-map-token-to-schema.md)。

```
permit (
    principal in MyCorp::UserGroup::"us-west-2_EXAMPLE|MyUserGroup",
    action,
    resource
) when {
    principal["cognito:username"] == "alice" &&
    principal["custom:employmentStoreCode"] == "petstore-dallas" &&
    principal has email && principal.email == "alice@example.com" &&
    context["ip-address"] like "192.0.2.*"
};
```

## 使用點表示法來參考屬性
<a name="policies-examples-dot"></a>

下列範例示範如何建立使用點表示法來參考屬性的政策。

如需在 Verified Permissions 中的政策中使用權杖屬性的詳細資訊，請參閱[將 Amazon Cognito 權杖映射至結構描述](cognito-map-token-to-schema.md)，以及將 [OIDC 權杖映射至結構描述](oidc-map-token-to-schema.md)。

```
permit(principal, action, resource)
when {
    principal.cognito.username == "alice" &&
    principal.custom.employmentStoreCode == "petstore-dallas" &&
    principal.tenant == "x11app-tenant-1" &&
    principal has email && principal.email == "alice@example.com"
};
```

## 反映 Amazon Cognito ID 字符屬性
<a name="policies-examples-cognito-id"></a>

下列範例示範如何從中建立政策參考 ID 字符屬性 Amazon Cognito。

如需在 Verified Permissions 中的政策中使用權杖屬性的詳細資訊，請參閱[將 Amazon Cognito 權杖映射至結構描述](cognito-map-token-to-schema.md)，以及將 [OIDC 權杖映射至結構描述](oidc-map-token-to-schema.md)。

```
permit (
    principal in MyCorp::UserGroup::"us-west-2_EXAMPLE|MyUserGroup",
    action,
    resource
) when {
    principal["cognito:username"] == "alice" &&
    principal["custom:employmentStoreCode"] == "petstore-dallas" &&
    principal.tenant == "x11app-tenant-1" &&
    principal has email && principal.email == "alice@example.com"
};
```

## 反映 OIDC ID 字符屬性
<a name="policies-examples-oidc-id"></a>

以下範例示範如何從 OIDC 供應商建立政策參考 ID 字符屬性。

如需在 Verified Permissions 中的政策中使用權杖屬性的詳細資訊，請參閱[將 Amazon Cognito 權杖映射至結構描述](cognito-map-token-to-schema.md)，以及將 [OIDC 權杖映射至結構描述](oidc-map-token-to-schema.md)。

```
permit (
    principal in MyCorp::UserGroup::"MyOIDCProvider|MyUserGroup",
    action,
    resource
) when {
    principal.email_verified == true && principal.email == "alice@example.com" &&
    principal.phone_number_verified == true && principal.phone_number like "+1206*"
};
```

## 反映 Amazon Cognito 存取權杖屬性
<a name="policies-examples-cognito-access"></a>

以下範例示範如何建立政策，參考來自 的存取權杖屬性 Amazon Cognito。

如需在 Verified Permissions 中的政策中使用權杖屬性的詳細資訊，請參閱[將 Amazon Cognito 權杖映射到結構描述](cognito-map-token-to-schema.md)，以及將 [OIDC 權杖映射到結構描述](oidc-map-token-to-schema.md)。

```
permit(principal, action in [MyApplication::Action::"Read", MyApplication::Action::"GetStoreInventory"], resource)
when { 
    context.token.client_id == "52n97d5afhfiu1c4di1k5m8f60" &&
    context.token.scope.contains("MyAPI/mydata.write")
};
```

## 反映 OIDC 存取權杖屬性
<a name="policies-examples-oidc-access"></a>

以下範例示範如何建立政策，參考來自 OIDC 供應商的存取權杖屬性。

如需在 Verified Permissions 中的政策中使用權杖屬性的詳細資訊，請參閱[將 Amazon Cognito 權杖映射至結構描述](cognito-map-token-to-schema.md)，以及將 [OIDC 權杖映射至結構描述](oidc-map-token-to-schema.md)。

```
permit(
    principal, 
    action in [MyApplication::Action::"Read", MyApplication::Action::"GetStoreInventory"],
    resource
)
when { 
    context.token.client_id == "52n97d5afhfiu1c4di1k5m8f60" &&
    context.token.scope.contains("MyAPI-read")
};
```