@@ -90,39 +90,6 @@ def _get_header_client_id(self):
9090 md5 .update (self ._device_identifier .encode ())
9191 return base64 .b64encode (md5 .digest ()).decode ()
9292
93- def _process_extra_hawk_values (self , values : dict | None ) -> str :
94- if values is None :
95- return ""
96- else :
97- sorted_keys = sorted (values .keys ())
98- result = []
99- for key in sorted_keys :
100- value = values .get (key )
101- result .append (f"{ key } ={ value } " )
102- return hashlib .md5 ("&" .join (result ).encode ()).hexdigest ()
103-
104- def _get_hawk_authentication (
105- self , rriot : RRiot , url : str , formdata : dict | None = None , params : dict | None = None
106- ) -> str :
107- timestamp = math .floor (time .time ())
108- nonce = secrets .token_urlsafe (6 )
109- formdata_str = self ._process_extra_hawk_values (formdata )
110- params_str = self ._process_extra_hawk_values (params )
111-
112- prestr = ":" .join (
113- [
114- rriot .u ,
115- rriot .s ,
116- nonce ,
117- str (timestamp ),
118- hashlib .md5 (url .encode ()).hexdigest (),
119- params_str ,
120- formdata_str ,
121- ]
122- )
123- mac = base64 .b64encode (hmac .new (rriot .h .encode (), prestr .encode (), hashlib .sha256 ).digest ()).decode ()
124- return f'Hawk id="{ rriot .u } ",s="{ rriot .s } ",ts="{ timestamp } ",nonce="{ nonce } ",mac="{ mac } "'
125-
12693 async def nc_prepare (self , user_data : UserData , timezone : str ) -> dict :
12794 """This gets a few critical parameters for adding a device to your account."""
12895 if (
@@ -144,7 +111,7 @@ async def nc_prepare(self, user_data: UserData, timezone: str) -> dict:
144111 "post" ,
145112 "/nc/prepare" ,
146113 headers = {
147- "Authorization" : self . _get_hawk_authentication (
114+ "Authorization" : _get_hawk_authentication (
148115 user_data .rriot , "/nc/prepare" , {"hid" : hid , "tzid" : timezone }
149116 ),
150117 },
@@ -177,7 +144,7 @@ async def add_device(self, user_data: UserData, s: str, t: str) -> dict:
177144 "GET" ,
178145 "/user/devices/newadd" ,
179146 headers = {
180- "Authorization" : self . _get_hawk_authentication (
147+ "Authorization" : _get_hawk_authentication (
181148 user_data .rriot , "/user/devices/newadd" , params = {"s" : s , "t" : t }
182149 ),
183150 },
@@ -337,7 +304,7 @@ async def get_home_data(self, user_data: UserData) -> HomeData:
337304 rriot .r .a ,
338305 self .session ,
339306 {
340- "Authorization" : self . _get_hawk_authentication (rriot , f"/user/homes/{ str (home_id )} " ),
307+ "Authorization" : _get_hawk_authentication (rriot , f"/user/homes/{ str (home_id )} " ),
341308 },
342309 )
343310 home_response = await home_request .request ("get" , "/user/homes/" + str (home_id ))
@@ -366,7 +333,7 @@ async def get_home_data_v2(self, user_data: UserData) -> HomeData:
366333 rriot .r .a ,
367334 self .session ,
368335 {
369- "Authorization" : self . _get_hawk_authentication (rriot , "/v2/user/homes/" + str (home_id )),
336+ "Authorization" : _get_hawk_authentication (rriot , "/v2/user/homes/" + str (home_id )),
370337 },
371338 )
372339 home_response = await home_request .request ("get" , "/v2/user/homes/" + str (home_id ))
@@ -393,7 +360,7 @@ async def get_home_data_v3(self, user_data: UserData) -> HomeData:
393360 rriot .r .a ,
394361 self .session ,
395362 {
396- "Authorization" : self . _get_hawk_authentication (rriot , "/v3/user/homes/" + str (home_id )),
363+ "Authorization" : _get_hawk_authentication (rriot , "/v3/user/homes/" + str (home_id )),
397364 },
398365 )
399366 home_response = await home_request .request ("get" , "/v3/user/homes/" + str (home_id ))
@@ -416,7 +383,7 @@ async def get_rooms(self, user_data: UserData, home_id: int | None = None) -> li
416383 rriot .r .a ,
417384 self .session ,
418385 {
419- "Authorization" : self . _get_hawk_authentication (rriot , "/v2/user/homes/" + str (home_id )),
386+ "Authorization" : _get_hawk_authentication (rriot , "/v2/user/homes/" + str (home_id )),
420387 },
421388 )
422389 room_response = await room_request .request ("get" , f"/user/homes/{ str (home_id )} /rooms" + str (home_id ))
@@ -441,7 +408,7 @@ async def get_scenes(self, user_data: UserData, device_id: str) -> list[HomeData
441408 rriot .r .a ,
442409 self .session ,
443410 {
444- "Authorization" : self . _get_hawk_authentication (rriot , f"/user/scene/device/{ str (device_id )} " ),
411+ "Authorization" : _get_hawk_authentication (rriot , f"/user/scene/device/{ str (device_id )} " ),
445412 },
446413 )
447414 scenes_response = await scenes_request .request ("get" , f"/user/scene/device/{ str (device_id )} " )
@@ -463,7 +430,7 @@ async def execute_scene(self, user_data: UserData, scene_id: int) -> None:
463430 rriot .r .a ,
464431 self .session ,
465432 {
466- "Authorization" : self . _get_hawk_authentication (rriot , f"/user/scene/{ str (scene_id )} /execute" ),
433+ "Authorization" : _get_hawk_authentication (rriot , f"/user/scene/{ str (scene_id )} /execute" ),
467434 },
468435 )
469436 execute_scene_response = await execute_scene_request .request ("POST" , f"/user/scene/{ str (scene_id )} /execute" )
@@ -546,3 +513,36 @@ async def request(self, method: str, url: str, params=None, data=None, headers=N
546513 finally :
547514 if close_session :
548515 await session .close ()
516+
517+
518+ def _process_extra_hawk_values (values : dict | None ) -> str :
519+ if values is None :
520+ return ""
521+ else :
522+ sorted_keys = sorted (values .keys ())
523+ result = []
524+ for key in sorted_keys :
525+ value = values .get (key )
526+ result .append (f"{ key } ={ value } " )
527+ return hashlib .md5 ("&" .join (result ).encode ()).hexdigest ()
528+
529+
530+ def _get_hawk_authentication (rriot : RRiot , url : str , formdata : dict | None = None , params : dict | None = None ) -> str :
531+ timestamp = math .floor (time .time ())
532+ nonce = secrets .token_urlsafe (6 )
533+ formdata_str = _process_extra_hawk_values (formdata )
534+ params_str = _process_extra_hawk_values (params )
535+
536+ prestr = ":" .join (
537+ [
538+ rriot .u ,
539+ rriot .s ,
540+ nonce ,
541+ str (timestamp ),
542+ hashlib .md5 (url .encode ()).hexdigest (),
543+ params_str ,
544+ formdata_str ,
545+ ]
546+ )
547+ mac = base64 .b64encode (hmac .new (rriot .h .encode (), prestr .encode (), hashlib .sha256 ).digest ()).decode ()
548+ return f'Hawk id="{ rriot .u } ",s="{ rriot .s } ",ts="{ timestamp } ",nonce="{ nonce } ",mac="{ mac } "'
0 commit comments