

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 对客户端设备进行身份验证和授权
<a name="ipc-client-device-auth"></a>

**注意**  
此功能适用于 [Greengrass Nucleus 组件](greengrass-nucleus-component.md)的 v2.6.0 及更高版本。

使用客户端设备身份验证 IPC 服务开发自定义本地代理组件，使本地 IoT 设备（例如客户端设备）可以连接。

要使用这些 IPC 操作，请在自定义组件中包含[客户端设备身份验证组件](client-device-auth-component.md)版本 2.2.0 或更高版本。然后，您可以在自定义组件中使用 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>

下表列出了与客户端设备身份验证 IPC 服务交互时必须使用的最低版本。 AWS IoT Device SDK 


| SDK | 最低版本 | 
| --- | --- | 
|  [AWS IoT Device SDK 适用于 Java v2](https://github.com/aws/aws-iot-device-sdk-java-v2)  |  v1.9.3  | 
|  [AWS IoT Device SDK 适用于 Python v2](https://github.com/aws/aws-iot-device-sdk-python-v2)  |  v1.11.3  | 
|  [AWS IoT Device SDK 适用于 C\$1\$1 v2](https://github.com/aws/aws-iot-device-sdk-cpp-v2)  |  v1.18.3  | 
|  [AWS IoT Device SDK 适用于 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`


| 操作 | 说明 | 资源 | 
| --- | --- | --- | 
|  `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 证书列表。