-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
152 lines (121 loc) · 4.85 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import base64
import requests
import json
from requests import Response
import logging
from google.cloud.iot_v1.types.resources import Device
from google.protobuf.json_format import MessageToDict
import urllib3
urllib3.disable_warnings()
class HonoDeviceApi:
def __init__(self, api_url: str, project_id: str):
self.tenants_url = f"{api_url}/tenants"
self.devices_url = f"{api_url}/devices"
self.credentials_url = f"{api_url}/credentials"
self.configs_url = f"{api_url}/configs"
self.project_id = project_id
def __get_payload_pk(self, key: str, device: Device, algorithm: str):
key = key.replace("-----BEGIN PUBLIC KEY-----", "")
key = key.replace("-----END PUBLIC KEY-----", "")
key = key.replace("\n", "")
key = key.replace("\r", "")
if device.credentials is not None and len(device.credentials) > 0 and device.credentials[0].expiration_time and device.credentials[0].expiration_time.timestamp() != 0:
return json.dumps([{
"type": "rpk",
"auth-id": device.id,
"secrets": [
{
"algorithm": algorithm,
"key": key,
"not-after": device.credentials[0].expiration_time.strftime("%Y-%m-%dT%H:%M:%SZ")
}
]
}])
else:
return json.dumps([{
"type": "rpk",
"auth-id": device.id,
"secrets": [
{
"algorithm": algorithm,
"key": key
}
]
}])
def __get_payload_cert(self, key, device):
key = key.replace("-----BEGIN CERTIFICATE-----", "")
key = key.replace("-----END CERTIFICATE-----", "")
key = key.replace("\n", "")
key = key.replace("\r", "")
return json.dumps([{
"type": "rpk",
"auth-id": device.id,
"secrets": [
{
"cert": key
}
]
}])
def __get__header(self, token):
return {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {token}'
}
def create_tenant(self, tenant_id: str, token: str) -> Response:
""" Create a new tenant. """
url = f"{self.tenants_url}/{tenant_id}"
payload = json.dumps({
"ext": {
"messaging-type": "pubsub",
"projectId": self.project_id
}
})
return requests.request("POST", url, headers=self.__get__header(token), data=payload, verify=False)
def create_device(self, tenant_id: str, device: Device, token: str, gateway_id: str) -> Response:
""" Create a new device. """
url = f"{self.devices_url}/{tenant_id}/{device.id}"
payload = {}
if gateway_id is not None:
payload = json.dumps({
"via": [
gateway_id,
],
})
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {token}'
}
return requests.request("POST", url, headers=headers, data=payload, verify=False)
def create_device_config(self, tenant_id: str, device: Device, token: str) -> Response:
""" Create configs for a device. """
url = f"{self.configs_url}/{tenant_id}/{device.id}"
config_str = base64.b64encode(device.config.binary_data).decode()
payload = json.dumps({
"binaryData": config_str
})
return requests.request("POST", url, headers=self.__get__header(token), data=payload, verify=False)
def create_credential(self, tenant_id: str, device: Device, token: str) -> Response:
""" Create device credentials. """
url = f"{self.credentials_url}/{tenant_id}/{device.id}"
public_key = MessageToDict(device.credentials.pb[0])["publicKey"]
key = public_key["key"]
key_format = public_key["format"]
is_cert = False
if "X509" in key_format:
is_cert = True
else:
if "ES" in key_format:
algorithm = "EC"
elif "RS" in key_format:
algorithm = "RSA"
else:
logging.error(
f"Can not create credentials for device {device.id}, cause: unsupported credentials algorithm {key_format}")
return None
if is_cert:
payload = self.__get_payload_cert(key, device)
else:
payload = self.__get_payload_pk(key, device, algorithm)
return requests.request("PUT", url, headers=self.__get__header(token), data=payload, verify=False)