3232import gc
3333import json
3434import time
35- import circuitpython_base64 as base64
36- import circuitpython_hmac as hmac
37- import circuitpython_parse as parse
3835import adafruit_requests as requests
3936import adafruit_logging as logging
4037from adafruit_logging import Logger
41- import adafruit_hashlib as hashlib
4238from . import constants
39+ from .quote import quote
40+ from .keys import compute_derived_symmetric_key
4341
4442# Azure HTTP error status codes
4543AZURE_HTTP_ERROR_CODES = [400 , 401 , 404 , 403 , 412 , 429 , 500 ]
@@ -89,17 +87,6 @@ def __init__(self, socket, id_scope: str, device_id: str, key: str, logger: Logg
8987
9088 requests .set_socket (socket )
9189
92- @staticmethod
93- def compute_derived_symmetric_key (secret : str , msg : str ) -> bytes :
94- """Computes a derived symmetric key from a secret and a message
95- :param str secret: The secret to use for the key
96- :param str msg: The message to use for the key
97- :returns: The derived symmetric key
98- :rtype: bytes
99- """
100- secret = base64 .b64decode (secret )
101- return base64 .b64encode (hmac .new (secret , msg = msg .encode ("utf8" ), digestmod = hashlib .sha256 ).digest ())
102-
10390 def _loop_assign (self , operation_id , headers ) -> str :
10491 uri = "https://%s/%s/registrations/%s/operations/%s?api-version=%s" % (
10592 constants .DPS_END_POINT ,
@@ -109,9 +96,8 @@ def _loop_assign(self, operation_id, headers) -> str:
10996 constants .DPS_API_VERSION ,
11097 )
11198 self ._logger .info ("- iotc :: _loop_assign :: " + uri )
112- target = parse .urlparse (uri )
11399
114- response = self ._run_get_request_with_retry (target . geturl () , headers )
100+ response = self ._run_get_request_with_retry (uri , headers )
115101
116102 try :
117103 data = response .json ()
@@ -205,8 +191,8 @@ def register_device(self, expiry: int) -> str:
205191 """
206192 # pylint: disable=C0103
207193 sr = self ._id_scope + "%2Fregistrations%2F" + self ._device_id
208- sig_no_encode = DeviceRegistration . compute_derived_symmetric_key (self ._key , sr + "\n " + str (expiry ))
209- sig_encoded = parse . quote (sig_no_encode , "~()*!.'" )
194+ sig_no_encode = compute_derived_symmetric_key (self ._key , sr + "\n " + str (expiry ))
195+ sig_encoded = quote (sig_no_encode , "~()*!.'" )
210196 auth_string = "SharedAccessSignature sr=" + sr + "&sig=" + sig_encoded + "&se=" + str (expiry ) + "&skn=registration"
211197
212198 headers = {
@@ -226,13 +212,12 @@ def register_device(self, expiry: int) -> str:
226212 self ._device_id ,
227213 constants .DPS_API_VERSION ,
228214 )
229- target = parse .urlparse (uri )
230215
231216 self ._logger .info ("Connecting..." )
232- self ._logger .info ("URL: " + target . geturl () )
217+ self ._logger .info ("URL: " + uri )
233218 self ._logger .info ("body: " + json .dumps (body ))
234219
235- response = self ._run_put_request_with_retry (target . geturl () , body , headers )
220+ response = self ._run_put_request_with_retry (uri , body , headers )
236221
237222 data = None
238223 try :
0 commit comments