28
28
logger = logging .getLogger (__name__ )
29
29
30
30
31
+ # fmt: off
31
32
table_lv0 = [
32
33
0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
33
34
0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
51
52
0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
52
53
0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
53
54
]
55
+ # fmt: on
56
+
54
57
55
58
def compare (lhs : str , rhs : str ) -> int : # pylint:disable=too-many-return-statements
56
59
tables = [table_lv0 , table_lv4 ]
@@ -95,6 +98,7 @@ def _wrap_exception(ex, desired_type):
95
98
msg = ex .args [0 ]
96
99
return desired_type (msg )
97
100
101
+
98
102
# This method attempts to emulate the sorting done by the service
99
103
def _storage_header_sort (input_headers : List [Tuple [str , str ]]) -> List [Tuple [str , str ]]:
100
104
@@ -135,84 +139,97 @@ def __init__(self, account_name, account_key):
135
139
@staticmethod
136
140
def _get_headers (request , headers_to_sign ):
137
141
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 "
141
145
142
146
@staticmethod
143
147
def _get_verb (request ):
144
- return request .http_request .method + ' \n '
148
+ return request .http_request .method + " \n "
145
149
146
150
def _get_canonicalized_resource (self , request ):
147
151
uri_path = urlparse (request .http_request .url ).path
148
152
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
+ ):
153
161
uri_path = URL (uri_path )
154
- return '/' + self .account_name + str (uri_path )
162
+ return "/" + self .account_name + str (uri_path )
155
163
except TypeError :
156
164
pass
157
- return '/' + self .account_name + uri_path
165
+ return "/" + self .account_name + uri_path
158
166
159
167
@staticmethod
160
168
def _get_canonicalized_headers (request ):
161
- string_to_sign = ''
169
+ string_to_sign = ""
162
170
x_ms_headers = []
163
171
for name , value in request .http_request .headers .items ():
164
- if name .startswith (' x-ms-' ):
172
+ if name .startswith (" x-ms-" ):
165
173
x_ms_headers .append ((name .lower (), value ))
166
174
x_ms_headers = _storage_header_sort (x_ms_headers )
167
175
for name , value in x_ms_headers :
168
176
if value is not None :
169
- string_to_sign += '' .join ([name , ':' , value , ' \n ' ])
177
+ string_to_sign += "" .join ([name , ":" , value , " \n " ])
170
178
return string_to_sign
171
179
172
180
@staticmethod
173
181
def _get_canonicalized_resource_query (request ):
174
182
sorted_queries = list (request .http_request .query .items ())
175
183
sorted_queries .sort ()
176
184
177
- string_to_sign = ''
185
+ string_to_sign = ""
178
186
for name , value in sorted_queries :
179
187
if value is not None :
180
- string_to_sign += ' \n ' + name .lower () + ':' + unquote (value )
188
+ string_to_sign += " \n " + name .lower () + ":" + unquote (value )
181
189
182
190
return string_to_sign
183
191
184
192
def _add_authorization_header (self , request , string_to_sign ):
185
193
try :
186
194
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
189
197
except Exception as ex :
190
198
# Wrap any error that occurred as signing error
191
199
# Doing so will clarify/locate the source of problem
192
200
raise _wrap_exception (ex , AzureSigningError ) from ex
193
201
194
202
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 (
198
206
request ,
199
207
[
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
+ )
208
225
209
226
self ._add_authorization_header (request , string_to_sign )
210
227
# logger.debug("String_to_sign=%s", string_to_sign)
211
228
212
229
213
230
class StorageHttpChallenge (object ):
214
231
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."""
216
233
if not challenge :
217
234
raise ValueError ("Challenge cannot be empty" )
218
235
@@ -221,7 +238,7 @@ def __init__(self, challenge):
221
238
222
239
# name=value pairs either comma or space separated with values possibly being
223
240
# enclosed in quotes
224
- for item in re .split (' [, ]' , trimmed_challenge ):
241
+ for item in re .split (" [, ]" , trimmed_challenge ):
225
242
comps = item .split ("=" )
226
243
if len (comps ) == 2 :
227
244
key = comps [0 ].strip (' "' )
@@ -230,11 +247,11 @@ def __init__(self, challenge):
230
247
self ._parameters [key ] = value
231
248
232
249
# Extract and verify required parameters
233
- self .authorization_uri = self ._parameters .get (' authorization_uri' )
250
+ self .authorization_uri = self ._parameters .get (" authorization_uri" )
234
251
if not self .authorization_uri :
235
252
raise ValueError ("Authorization Uri not found" )
236
253
237
- self .resource_id = self ._parameters .get (' resource_id' )
254
+ self .resource_id = self ._parameters .get (" resource_id" )
238
255
if not self .resource_id :
239
256
raise ValueError ("Resource id not found" )
240
257
0 commit comments