@@ -856,7 +856,11 @@ def login_with_return(
856856 LOGGER .error ("apisession:login_with_return:credentials are missing" )
857857 return {"authenticated" : False , "error" : "credentials are missing" }
858858
859- if resp .status_code == 200 and not resp .data .get ("two_factor_required" , False ):
859+ if (
860+ resp .status_code == 200
861+ and isinstance (resp .data , dict )
862+ and not resp .data .get ("two_factor_required" , False )
863+ ):
860864 LOGGER .info ("apisession:login_with_return:access authorized" )
861865 return {"authenticated" : True , "error" : "" }
862866 else :
@@ -884,7 +888,8 @@ def logout(self) -> None:
884888 self ._set_authenticated (False )
885889 else :
886890 try :
887- CONSOLE .error (resp .data ["detail" ])
891+ if isinstance (resp .data , dict ) and "detail" in resp .data :
892+ CONSOLE .error (resp .data ["detail" ])
888893 except (KeyError , TypeError , AttributeError ):
889894 if isinstance (resp .raw_data , bytes ):
890895 CONSOLE .error (resp .raw_data .decode ("utf-8" , errors = "replace" ))
@@ -1077,7 +1082,7 @@ def _getself(self) -> bool:
10771082 uri = "/api/v1/self"
10781083 LOGGER .info ('apisession:_getself: sending GET request to "%s"' , uri )
10791084 resp = self .mist_get (uri )
1080- if resp .status_code == 200 and resp .data :
1085+ if resp .status_code == 200 and resp .data and isinstance ( resp . data , dict ) :
10811086 # Deal with 2FA if needed
10821087 if (
10831088 resp .data .get ("two_factor_required" ) is True
@@ -1094,20 +1099,27 @@ def _getself(self) -> bool:
10941099 LOGGER .info (
10951100 "apisession:_getself:authentication Ok. Processing account privileges"
10961101 )
1097- for key , val in resp .data .items ():
1098- if key == "privileges" :
1099- self .privileges = Privileges (resp .data ["privileges" ])
1100- if key == "tags" :
1101- for tag in resp .data ["tags" ]:
1102- self .tags .append (tag )
1103- else :
1104- setattr (self , key , val )
1105- if self ._show_cli_notif :
1106- print ()
1107- print (" Authenticated " .center (80 , "-" ))
1108- print (f"\r \n Welcome { self .first_name } { self .last_name } !\r \n " )
1109- LOGGER .info ("apisession:_getself:account info processed successfully" )
1110- return True
1102+ if isinstance (resp .data , dict ):
1103+ for key , val in resp .data .items ():
1104+ if key == "privileges" :
1105+ self .privileges = Privileges (resp .data ["privileges" ])
1106+ if key == "tags" :
1107+ for tag in resp .data ["tags" ]:
1108+ self .tags .append (tag )
1109+ else :
1110+ setattr (self , key , val )
1111+ if self ._show_cli_notif :
1112+ print ()
1113+ print (" Authenticated " .center (80 , "-" ))
1114+ print (f"\r \n Welcome { self .first_name } { self .last_name } !\r \n " )
1115+ LOGGER .info (
1116+ "apisession:_getself:account info processed successfully"
1117+ )
1118+ return True
1119+ else :
1120+ raise ValueError (
1121+ "Unexpected format for privileges in the response data"
1122+ )
11111123 elif resp .proxy_error :
11121124 LOGGER .critical ("apisession:_getself:proxy not valid..." )
11131125 CONSOLE .critical ("Proxy not valid...\r \n " )
@@ -1171,7 +1183,11 @@ def get_privilege_by_org_id(self, org_id: str):
11711183 msp_id = None
11721184 try :
11731185 resp = self .mist_get (uri )
1174- if resp .data and resp .data .get ("msp_id" ):
1186+ if (
1187+ resp .data
1188+ and isinstance (resp .data , dict )
1189+ and resp .data .get ("msp_id" )
1190+ ):
11751191 LOGGER .info (
11761192 "apisession:get_privilege_by_org_id:org %s belong to msp_id %s" ,
11771193 {org_id },
@@ -1205,7 +1221,7 @@ def get_privilege_by_org_id(self, org_id: str):
12051221 "unable of find msp %s privileges in user data" ,
12061222 msp_id ,
12071223 )
1208- else :
1224+ elif isinstance ( resp . data , dict ) :
12091225 return {
12101226 "scope" : "org" ,
12111227 "org_id" : org_id ,
0 commit comments