
# 自定义模板鉴权示例
#### 示例1
证书认证设备，不限制UserName与ClientId参数取值，从设备证书通用名称（Common Name）中取值设备ID。
表1鉴权参数 
| 参数        | 说明  |
|:---|:---|
| Client ID | 任意值 |
| User Name | 任意值 |
| Password  | 空值  |
   
鉴权模板：
```
{
"template_name": "template1",
"description": "template1",
"template_body": {
"parameters": {
"iotda::certificate::common_name": {
"type": "String"
}
},
"resources": {
"device_id": {
"Ref": "iotda::certificate::common_name"
}
}
}
}
```
#### 示例2
当设备ID格式为：${ProductId}_${NodeId}，鉴权参数格式如下表所示时，自定义鉴权模板配置如下代码所示：
表2鉴权参数 
| 参数        | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
|:---|:---|
| Client ID | 固定格式： ``` ${ClientId}|securemode=2,signmethod=hmacsha256,timestamp=${timestamp}| ``` - *${ClientId}：* 固定格式 ${ProductId}.${NodeId}。 - *${NodeId}：* 设备标识码。  - *${ProductId}*：产品ID。    - *${timestamp}：*Unix时间戳，毫秒。                                                  |
| User Name | 由设备标识码和产品ID组成，固定格式： ``` ${NodeId}&${ProductId} ```                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| Password  | 以设备密码为密钥，将设备参数与参数值拼接后的字符串为加密串进行hmacsha256算法加密。 加密串格式： ``` clientId${clientId}deviceName${nodeId}productKey${productId}timestamp${timestamp} ``` - *${ClientId}：* 固定格式 ${ProductId}.${NodeId}。  - *${NodeId}：* 设备标识码。  - *${ProductId}*：产品ID。  - *${timestamp}：*时间戳。   |
   
鉴权模板：
```
{
"template_name": "template2",
"description": "template2",
"template_body": {
"parameters": {
"iotda::mqtt::client_id": {
"type": "String"
},
"iotda::mqtt::username": {
"type": "String"
},
"iotda::device::secret": {
"type": "String"
}
},
"resources": {
"device_id": {
"Fn::Join": [{
"Fn::SplitSelect": [
"${iotda::mqtt::username}",
"&",
1
]
}, "_", {
"Fn::SplitSelect": [
"${iotda::mqtt::username}",
"&",
0
]
}]
},
"timestamp": {
"type": "UNIX",
"value": {
"Fn::MathDiv": [{
"Fn::ParseLong": {
"Fn::SplitSelect": [{
"Fn::SubStringAfter": [{
"Fn::SplitSelect": ["${iotda::mqtt::client_id}", "|", 1]
}, "timestamp="]
}, ",", 0]
}
}, 1000]
}
},
"password": {
"Fn::HmacSHA256": [{
"Fn::Sub": [
"clientId${clientId}deviceName${deviceName}productKey${productKey}timestamp${timestamp}",
{
"clientId": {
"Fn::SplitSelect": [
"${iotda::mqtt::client_id}",
"|",
0
]
},
"deviceName": {
"Fn::SplitSelect": [
"${iotda::mqtt::username}",
"&",
0
]
},
"productKey": {
"Fn::SplitSelect": [
"${iotda::mqtt::username}",
"&",
1
]
},
"timestamp": {
"Fn::SplitSelect": [{
"Fn::SubStringAfter": [{
"Fn::SplitSelect": ["${iotda::mqtt::client_id}", "|", 1]
}, "timestamp="]
}, ",", 0]
}
}
]
},
"${iotda::device::secret}"
]
}
}
}
}
```
#### 示例3
当设备ID格式为：${productId}${nodeId}，鉴权参数格式如下表所示时，自定义鉴权模板配置如下代码所示：
表3鉴权参数 
| 参数        | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|
| Client ID | 固定格式： ``` ${productId}${nodeId} ``` - *${productId}*：产品ID。  - *${nodeId}：* 设备标识码。                                                                                                                                                                                                                                                                               |
| User Name | 固定格式： ``` ${productId}${nodeId};12010126;${connid};${expiry} ``` - *${productId}：* 产品ID。  - *${nodeId}：* 设备标识码。  - *${connid}：*一个随机字符串。  - *${expiry}：Unix时间戳，单位秒。*   |
| Password  | 固定格式： ``` ${token};hmacsha256 ``` - *${token}：*以BASE64解码后的设备密码为密钥，对User Name字段进行hmacsha256算法加密后的值。                                                                                                                                                                                                                                                                                                                                                               |
   
鉴权模板：
```
{
"template_name": "template3",
"description": "template3",
"template_body": {
"parameters": {
"iotda::mqtt::client_id": {
"type": "String"
},
"iotda::mqtt::username": {
"type": "String"
},
"iotda::device::secret": {
"type": "String"
}
},
"resources": {
"device_id": {
"Ref": "iotda::mqtt::client_id"
},
"timestamp": {
"type": "UNIX",
"value": {
"Fn::ParseLong": {
"Fn::SplitSelect": ["${iotda::mqtt::username}", ";", 3]
}
}
},
"password": {
"Fn::Sub": [
"${token};hmacsha256",
{
"token": {
"Fn::HmacSHA256": [
"${iotda::mqtt::username}",
{
"Fn::Base64Decode": "${iotda::device::secret}"
}
]
}
}
]
}
}
}
}
```
