Skip to content

Commit fc04d44

Browse files
author
ccharlesren
committed
添加动态注册证书认证功能,并解析,保存文件至根目录
Change-Id: Ie94ab2e3b240a7292b27ff9afca084cb45436217
1 parent f9158a0 commit fc04d44

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

hub/hub.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from hub.services.broadcast.broadcast import Broadcast
3737
from hub.services.shadow.shadow import Shadow
3838
from hub.services.ota.ota import Ota
39+
import os
3940

4041
class SingletonType(type):
4142
_instance_lock = threading.Lock()
@@ -901,17 +902,49 @@ def dynregDevice(self, timeout=10, dynregDomain=None):
901902
if 'Len' in resp and resp['Len'] > 0:
902903
reply_obj_data = reply_obj['Response']["Payload"]
903904
if reply_obj_data is not None:
904-
psk = self.__codec._AESUtil.decrypt(reply_obj_data.encode('UTF-8') , product_secret[:self.__codec._AESUtil.BLOCK_SIZE_16].encode('UTF-8'),
905+
payload = self.__codec._AESUtil.decrypt(reply_obj_data.encode('UTF-8') , product_secret[:self.__codec._AESUtil.BLOCK_SIZE_16].encode('UTF-8'),
905906
'0000000000000000'.encode('UTF-8'))
906-
psk = psk.decode('UTF-8', 'ignore').strip().strip(b'\x00'.decode())
907-
user_dict = json.loads(psk)
907+
payload = payload.decode('UTF-8', 'ignore').strip().strip(b'\x00'.decode())
908+
user_dict = json.loads(payload)
908909
self._logger.info('encrypt type: {}'.format(
909910
user_dict['encryptionType']))
910911

911-
self.__device_info.update_config_file(user_dict['psk'])
912-
self.__protocol_reinit()
913-
914-
return 0, user_dict['psk']
912+
encryptionType = user_dict['encryptionType']
913+
914+
if encryptionType is not None:
915+
if encryptionType == 2: #2表示密钥认证
916+
self.__device_info.update_config_file(user_dict['psk'])
917+
self.__protocol_reinit()
918+
return 0, user_dict['psk']
919+
920+
elif encryptionType == 1: #1表示证书认证
921+
#获取证书和秘钥
922+
cert = user_dict['clientCert']
923+
privateKey = user_dict['clientKey']
924+
#cert文件创建路径
925+
current_path = os.getcwd()
926+
cert_file_path = os.path.join(current_path,'cert.txt')
927+
cert_file = open(cert_file_path,'w')
928+
cert_file.write(cert)
929+
cert_file.close()
930+
#privateKey文件创建路径
931+
privateKey_file_path = os.path.join(current_path,'privateKey.txt')
932+
privateKey_file = open(privateKey_file_path,'w')
933+
privateKey_file.write(privateKey)
934+
privateKey_file.close()
935+
936+
#替换json文件对应路径
937+
self.__device_info.update_cert_config_file(cert_file_path)
938+
self.__device_info.update_privateKey_config_file(privateKey_file_path)
939+
self.__protocol_reinit()
940+
941+
return 0, 'success'
942+
else:
943+
self._logger.warring('encryptionType is other type')
944+
return -1, 'encryptionType is other type'
945+
else:
946+
self._logger.warring('encryptionType is null')
947+
return -1, 'encryptionType is null'
915948
else:
916949
self._logger.warring('payload is null')
917950
return -1, 'payload is null'

hub/sample/dynreg/example_dynreg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def example_dynreg():
5454
ret, msg = qcloud.dynregDevice()
5555
if ret == 0:
5656
# print("\033[1;36m dynamic register test success, psk: {}\033[0m".format(msg))
57-
print("\033[1;36m dynamic register test success, psk 内容在 msg 中 \033[0m")
57+
print("\033[1;36m dynamic register test success, 动态获取信息 内容在 msg 中 \033[0m")
5858
else:
5959
print("\033[1;31m dynamic register test fail, msg: {}\033[0m".format(msg))
6060
return False

hub/utils/providers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,28 @@ def update_config_file(self, psk):
214214
self.__device_secret = psk
215215
pass
216216

217+
def update_cert_config_file(self, cert):
218+
with open(self.__file_path, '+r', encoding='utf-8') as f:
219+
t = f.read()
220+
t = t.replace(self.__cert_file, cert)
221+
f.seek(0, 0)
222+
f.write(t)
223+
f.truncate()
224+
225+
self.__cert_file = cert
226+
pass
227+
228+
def update_privateKey_config_file(self, privateKey):
229+
with open(self.__file_path, '+r', encoding='utf-8') as f:
230+
t = f.read()
231+
t = t.replace(self.__private_key_file, privateKey)
232+
f.seek(0, 0)
233+
f.write(t)
234+
f.truncate()
235+
236+
self.__private_key_file = privateKey
237+
pass
238+
217239
@property
218240
def auth_mode(self):
219241
return self.__auth_mode

0 commit comments

Comments
 (0)