

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

# 驗證和授權用戶端裝置
<a name="ipc-client-device-auth"></a>

**注意**  
此功能適用於 [Greengrass 核元件](greengrass-nucleus-component.md)的 v2.6.0 和更新版本。

使用用戶端裝置驗證 IPC 服務來開發自訂本機代理程式元件，例如用戶端裝置等本機 IoT 裝置可以連線。

若要使用這些 IPC 操作，請在自訂[元件中包含 2.2.0 版或更新版本的用戶端裝置身分驗證](client-device-auth-component.md)元件做為相依性。然後，您可以在自訂元件中使用 IPC 操作來執行下列動作：
+ 驗證連接至核心裝置的用戶端裝置身分。
+ 為用戶端裝置建立工作階段以連線至核心裝置。
+ 驗證用戶端裝置是否具有執行 動作的許可。
+ 在核心裝置的伺服器憑證輪換時接收通知。

**Topics**
+ [最低 SDK 版本](#ipc-client-device-auth-sdk-versions)
+ [Authorization](#ipc-client-device-auth-authorization)
+ [VerifyClientDeviceIdentity](#ipc-operation-verifyclientdeviceidentity)
+ [GetClientDeviceAuthToken](#ipc-operation-getclientdeviceauthtoken)
+ [AuthorizeClientDeviceAction](#ipc-operation-authorizeclientdeviceaction)
+ [SubscribeToCertificateUpdates](#ipc-operation-subscribetocertificateupdates)

## 最低 SDK 版本
<a name="ipc-client-device-auth-sdk-versions"></a>

下表列出 AWS IoT Device SDK 您必須用來與用戶端裝置驗證 IPC 服務互動的 最低版本。


| SDK | 最低版本 | 
| --- | --- | 
|  [AWS IoT Device SDK for Java v2](https://github.com/aws/aws-iot-device-sdk-java-v2)  |  1.9.3 版  | 
|  [AWS IoT Device SDK for Python v2](https://github.com/aws/aws-iot-device-sdk-python-v2)  |  1.11.3 版  | 
|  [AWS IoT Device SDK 適用於 C\$1\$1 v2](https://github.com/aws/aws-iot-device-sdk-cpp-v2)  |  1.18.3 版  | 
|  [AWS IoT Device SDK for JavaScript v2](https://github.com/aws/aws-iot-device-sdk-js-v2)  |  v1.12.0  | 

## Authorization
<a name="ipc-client-device-auth-authorization"></a>

若要在自訂元件中使用用戶端裝置驗證 IPC 服務，您必須定義授權政策，以允許您的元件執行這些操作。如需定義授權政策的資訊，請參閱 [授權元件執行 IPC 操作](interprocess-communication.md#ipc-authorization-policies)。

用戶端裝置身分驗證和授權的授權政策具有下列屬性。

**IPC 服務識別符：** `aws.greengrass.clientdevices.Auth`


| 作業 | Description | Resources | 
| --- | --- | --- | 
|  `aws.greengrass#VerifyClientDeviceIdentity`  |  允許元件驗證用戶端裝置的身分。  |  `*`  | 
|  `aws.greengrass#GetClientDeviceAuthToken`  |  允許元件驗證用戶端裝置的登入資料，並為該用戶端裝置建立工作階段。  |  `*`  | 
|  `aws.greengrass#AuthorizeClientDeviceAction`  |  允許元件驗證用戶端裝置是否具有執行 動作的許可。  |  `*`  | 
|  `aws.greengrass#SubscribeToCertificateUpdates`  |  允許元件在核心裝置的伺服器憑證輪換時接收通知。  |  `*`  | 
|  `*`  |  允許元件執行所有用戶端裝置驗證 IPC 服務操作。  |  `*`  | 

### 授權政策範例
<a name="ipc-client-device-auth-authorization-policy-examples"></a>

您可以參考下列授權政策範例，協助您設定元件的授權政策。

**Example 範例授權政策**  
下列範例授權政策允許元件執行所有用戶端裝置驗證 IPC 操作。  

```
{
  "accessControl": {
    "aws.greengrass.clientdevices.Auth": {
      "com.example.MyLocalBrokerComponent:clientdevices:1": {
        "policyDescription": "Allows access to authenticate and authorize client devices.",
        "operations": [
          "aws.greengrass#VerifyClientDeviceIdentity",
          "aws.greengrass#GetClientDeviceAuthToken",
          "aws.greengrass#AuthorizeClientDeviceAction",
          "aws.greengrass#SubscribeToCertificateUpdates"
        ],
        "resources": [
          "*"
        ]
      }
    }
  }
}
```

## VerifyClientDeviceIdentity
<a name="ipc-operation-verifyclientdeviceidentity"></a>

驗證用戶端裝置的身分。此操作會驗證用戶端裝置是否為有效 AWS IoT 物件。

### 請求
<a name="ipc-operation-verifyclientdeviceidentity-request"></a>

此操作的請求具有下列參數：

`credential`  
用戶端裝置的登入資料。此物件 `ClientDeviceCredential`包含下列資訊：    
`clientDeviceCertificate` (Python：`client_device_certificate`)  
用戶端裝置的 X.509 裝置憑證。

### 回應
<a name="ipc-operation-verifyclientdeviceidentity-response"></a>

此操作的回應包含下列資訊：

`isValidClientDevice` (Python：`is_valid_client_device`)  
用戶端裝置的身分是否有效。

## GetClientDeviceAuthToken
<a name="ipc-operation-getclientdeviceauthtoken"></a>

驗證用戶端裝置的登入資料，並為用戶端裝置建立工作階段。此操作會傳回工作階段字符，您可以在後續請求中使用它來[授權用戶端裝置動作](#ipc-operation-authorizeclientdeviceaction)。

若要成功連接用戶端裝置，[用戶端裝置身分驗證元件](client-device-auth-component.md#client-device-auth-component-configuration)必須授予用戶端裝置使用的用戶端 ID `mqtt:connect`許可。

### 請求
<a name="ipc-operation-getclientdeviceauthtoken-request"></a>

此操作的請求具有下列參數：

`credential`  
用戶端裝置的登入資料。此物件 `CredentialDocument`包含下列資訊：    
`mqttCredential` (Python：`mqtt_credential`)  
用戶端裝置的 MQTT 登入資料。指定用戶端裝置用來連線的用戶端 ID 和憑證。此物件 `MQTTCredential`包含下列資訊：    
`clientId` (Python：`client_id`)  
用於連線的用戶端 ID。  
`certificatePem` (Python：`certificate_pem`)  
用於連線的 X.509 裝置憑證。  
`username`  
此屬性目前未使用。  
`password`  
此屬性目前未使用。

### 回應
<a name="ipc-operation-getclientdeviceauthtoken-response"></a>

此操作的回應包含下列資訊：

`clientDeviceAuthToken` (Python：`client_device_auth_token`)  
用戶端裝置的工作階段字符。您可以在後續請求中使用此工作階段字符來授權此用戶端裝置的動作。

## AuthorizeClientDeviceAction
<a name="ipc-operation-authorizeclientdeviceaction"></a>

驗證用戶端裝置是否具有對資源執行動作的許可。*用戶端裝置授權政策*指定用戶端裝置在連線至核心裝置時可執行的許可。您可以在設定用戶端裝置[身分驗證元件時定義用戶端裝置](client-device-auth-component.md#client-device-auth-component-configuration)授權政策。

### 請求
<a name="ipc-operation-authorizeclientdeviceaction-request"></a>

此操作的請求具有下列參數：

`clientDeviceAuthToken` (Python：`client_device_auth_token`)  
用戶端裝置的工作階段字符。

`operation`  
要授權的操作。

`resource`  
用戶端裝置執行操作的資源。

### 回應
<a name="ipc-operation-authorizeclientdeviceaction-response"></a>

此操作的回應包含下列資訊：

`isAuthorized` (Python：`is_authorized`)  
用戶端裝置是否獲授權在資源上執行 操作。

## SubscribeToCertificateUpdates
<a name="ipc-operation-subscribetocertificateupdates"></a>

訂閱以在每次輪換時接收核心裝置的新伺服器憑證。當伺服器憑證輪換時，代理程式必須使用新的伺服器憑證重新載入。

根據預設[，用戶端裝置身分驗證元件](client-device-auth-component.md)會每 7 天輪換一次伺服器憑證。您可以將輪換間隔設定為 2 到 10 天。

<a name="ipc-subscribe-operation-note"></a>此操作是您訂閱事件訊息串流的訂閱操作。若要使用此操作，請使用處理事件訊息、錯誤和串流關閉的函數來定義串流回應處理常式。如需詳細資訊，請參閱[訂閱 IPC 事件串流](interprocess-communication.md#ipc-subscribe-operations)。

**事件訊息類型：** `CertificateUpdateEvent`

### 請求
<a name="ipc-operation-subscribetocertificateupdates-request"></a>

此操作的請求具有下列參數：

`certificateOptions` (Python：`certificate_options`)  
要訂閱的憑證更新類型。此物件 `CertificateOptions`包含下列資訊：    
`certificateType` (Python：`certificate_type`)  
要訂閱的憑證更新類型。選擇下列選項：  
+ `SERVER`

### 回應
<a name="ipc-operation-subscribetocertificateupdates-response"></a>

此操作的回應包含下列資訊：

`messages`  
訊息串流。此物件 `CertificateUpdateEvent`包含下列資訊：    
`certificateUpdate` (Python：`certificate_update`)  
新憑證的相關資訊。此物件 `CertificateUpdate`包含下列資訊：    
`certificate`  
憑證。  
`privateKey` (Python：`private_key`)  
憑證的私有金鑰。  
`publicKey` (Python：`public_key`)  
憑證的公有金鑰。  
`caCertificates` (Python：`ca_certificates`)  
憑證的 CA 憑證鏈中的憑證授權機構 (CA) 憑證清單。