2828logger = logging .getLogger (__name__ )
2929
3030
31+ # fmt: off
3132table_lv0 = [
3233 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
3334 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
5152 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
5253 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
5354]
55+ # fmt: on
56+
5457
5558def compare (lhs : str , rhs : str ) -> int : # pylint:disable=too-many-return-statements
5659 tables = [table_lv0 , table_lv4 ]
@@ -95,6 +98,7 @@ def _wrap_exception(ex, desired_type):
9598 msg = ex .args [0 ]
9699 return desired_type (msg )
97100
101+
98102# This method attempts to emulate the sorting done by the service
99103def _storage_header_sort (input_headers : List [Tuple [str , str ]]) -> List [Tuple [str , str ]]:
100104
@@ -135,84 +139,97 @@ def __init__(self, account_name, account_key):
135139 @staticmethod
136140 def _get_headers (request , headers_to_sign ):
137141 headers = dict ((name .lower (), value ) for name , value in request .http_request .headers .items () if value )
138- if ' content-length' in headers and headers [' content-length' ] == '0' :
139- del headers [' content-length' ]
140- return ' \n ' .join (headers .get (x , '' ) for x in headers_to_sign ) + ' \n '
142+ if " content-length" in headers and headers [" content-length" ] == "0" :
143+ del headers [" content-length" ]
144+ return " \n " .join (headers .get (x , "" ) for x in headers_to_sign ) + " \n "
141145
142146 @staticmethod
143147 def _get_verb (request ):
144- return request .http_request .method + ' \n '
148+ return request .http_request .method + " \n "
145149
146150 def _get_canonicalized_resource (self , request ):
147151 uri_path = urlparse (request .http_request .url ).path
148152 try :
149- if isinstance (request .context .transport , AioHttpTransport ) or \
150- isinstance (getattr (request .context .transport , "_transport" , None ), AioHttpTransport ) or \
151- isinstance (getattr (getattr (request .context .transport , "_transport" , None ), "_transport" , None ),
152- AioHttpTransport ):
153+ if (
154+ isinstance (request .context .transport , AioHttpTransport )
155+ or isinstance (getattr (request .context .transport , "_transport" , None ), AioHttpTransport )
156+ or isinstance (
157+ getattr (getattr (request .context .transport , "_transport" , None ), "_transport" , None ),
158+ AioHttpTransport ,
159+ )
160+ ):
153161 uri_path = URL (uri_path )
154- return '/' + self .account_name + str (uri_path )
162+ return "/" + self .account_name + str (uri_path )
155163 except TypeError :
156164 pass
157- return '/' + self .account_name + uri_path
165+ return "/" + self .account_name + uri_path
158166
159167 @staticmethod
160168 def _get_canonicalized_headers (request ):
161- string_to_sign = ''
169+ string_to_sign = ""
162170 x_ms_headers = []
163171 for name , value in request .http_request .headers .items ():
164- if name .startswith (' x-ms-' ):
172+ if name .startswith (" x-ms-" ):
165173 x_ms_headers .append ((name .lower (), value ))
166174 x_ms_headers = _storage_header_sort (x_ms_headers )
167175 for name , value in x_ms_headers :
168176 if value is not None :
169- string_to_sign += '' .join ([name , ':' , value , ' \n ' ])
177+ string_to_sign += "" .join ([name , ":" , value , " \n " ])
170178 return string_to_sign
171179
172180 @staticmethod
173181 def _get_canonicalized_resource_query (request ):
174182 sorted_queries = list (request .http_request .query .items ())
175183 sorted_queries .sort ()
176184
177- string_to_sign = ''
185+ string_to_sign = ""
178186 for name , value in sorted_queries :
179187 if value is not None :
180- string_to_sign += ' \n ' + name .lower () + ':' + unquote (value )
188+ string_to_sign += " \n " + name .lower () + ":" + unquote (value )
181189
182190 return string_to_sign
183191
184192 def _add_authorization_header (self , request , string_to_sign ):
185193 try :
186194 signature = sign_string (self .account_key , string_to_sign )
187- auth_string = ' SharedKey ' + self .account_name + ':' + signature
188- request .http_request .headers [' Authorization' ] = auth_string
195+ auth_string = " SharedKey " + self .account_name + ":" + signature
196+ request .http_request .headers [" Authorization" ] = auth_string
189197 except Exception as ex :
190198 # Wrap any error that occurred as signing error
191199 # Doing so will clarify/locate the source of problem
192200 raise _wrap_exception (ex , AzureSigningError ) from ex
193201
194202 def on_request (self , request ):
195- string_to_sign = \
196- self ._get_verb (request ) + \
197- self ._get_headers (
203+ string_to_sign = (
204+ self ._get_verb (request )
205+ + self ._get_headers (
198206 request ,
199207 [
200- 'content-encoding' , 'content-language' , 'content-length' ,
201- 'content-md5' , 'content-type' , 'date' , 'if-modified-since' ,
202- 'if-match' , 'if-none-match' , 'if-unmodified-since' , 'byte_range'
203- ]
204- ) + \
205- self ._get_canonicalized_headers (request ) + \
206- self ._get_canonicalized_resource (request ) + \
207- self ._get_canonicalized_resource_query (request )
208+ "content-encoding" ,
209+ "content-language" ,
210+ "content-length" ,
211+ "content-md5" ,
212+ "content-type" ,
213+ "date" ,
214+ "if-modified-since" ,
215+ "if-match" ,
216+ "if-none-match" ,
217+ "if-unmodified-since" ,
218+ "byte_range" ,
219+ ],
220+ )
221+ + self ._get_canonicalized_headers (request )
222+ + self ._get_canonicalized_resource (request )
223+ + self ._get_canonicalized_resource_query (request )
224+ )
208225
209226 self ._add_authorization_header (request , string_to_sign )
210227 # logger.debug("String_to_sign=%s", string_to_sign)
211228
212229
213230class StorageHttpChallenge (object ):
214231 def __init__ (self , challenge ):
215- """ Parses an HTTP WWW-Authentication Bearer challenge from the Storage service. """
232+ """Parses an HTTP WWW-Authentication Bearer challenge from the Storage service."""
216233 if not challenge :
217234 raise ValueError ("Challenge cannot be empty" )
218235
@@ -221,7 +238,7 @@ def __init__(self, challenge):
221238
222239 # name=value pairs either comma or space separated with values possibly being
223240 # enclosed in quotes
224- for item in re .split (' [, ]' , trimmed_challenge ):
241+ for item in re .split (" [, ]" , trimmed_challenge ):
225242 comps = item .split ("=" )
226243 if len (comps ) == 2 :
227244 key = comps [0 ].strip (' "' )
@@ -230,11 +247,11 @@ def __init__(self, challenge):
230247 self ._parameters [key ] = value
231248
232249 # Extract and verify required parameters
233- self .authorization_uri = self ._parameters .get (' authorization_uri' )
250+ self .authorization_uri = self ._parameters .get (" authorization_uri" )
234251 if not self .authorization_uri :
235252 raise ValueError ("Authorization Uri not found" )
236253
237- self .resource_id = self ._parameters .get (' resource_id' )
254+ self .resource_id = self ._parameters .get (" resource_id" )
238255 if not self .resource_id :
239256 raise ValueError ("Resource id not found" )
240257
0 commit comments