@@ -117,7 +117,11 @@ defmodule AWS.Request do
117
117
118
118
case response_header_parameters do
119
119
[ _ | _ ] ->
120
- merge_body_with_response_headers ( response_body , response , response_header_parameters )
120
+ merge_body_with_response_headers (
121
+ response_body ,
122
+ response ,
123
+ response_header_parameters
124
+ )
121
125
122
126
_ ->
123
127
response_body
@@ -191,7 +195,8 @@ defmodule AWS.Request do
191
195
end
192
196
end
193
197
194
- defp encode! ( % Client { } = client , protocol , payload ) when protocol in @ valid_protocols and is_map ( payload ) do
198
+ defp encode! ( % Client { } = client , protocol , payload )
199
+ when protocol in @ valid_protocols and is_map ( payload ) do
195
200
encode_format =
196
201
case protocol do
197
202
"query" -> :query
@@ -244,17 +249,20 @@ defmodule AWS.Request do
244
249
245
250
canonical_request = Internal . canonical_request ( method , url , headers , body )
246
251
hashed_canonical_request = Util . sha256_hexdigest ( canonical_request )
247
- credential_scope = Internal . credential_scope ( short_date , client . region ,
248
- client . service )
249
- signing_key = Internal . signing_key ( client . secret_access_key , short_date ,
250
- client . region , client . service )
251
- string_to_sign = Internal . string_to_sign ( long_date , credential_scope ,
252
- hashed_canonical_request )
252
+ credential_scope = Internal . credential_scope ( short_date , client . region , client . service )
253
+
254
+ signing_key =
255
+ Internal . signing_key ( client . secret_access_key , short_date , client . region , client . service )
256
+
257
+ string_to_sign =
258
+ Internal . string_to_sign ( long_date , credential_scope , hashed_canonical_request )
259
+
253
260
signature = Util . hmac_sha256_hexdigest ( signing_key , string_to_sign )
254
261
signed_headers = Internal . signed_headers ( headers )
255
- authorization = Internal . authorization ( client . access_key_id ,
256
- credential_scope , signed_headers ,
257
- signature )
262
+
263
+ authorization =
264
+ Internal . authorization ( client . access_key_id , credential_scope , signed_headers , signature )
265
+
258
266
Internal . add_authorization_header ( headers , authorization )
259
267
end
260
268
@@ -274,23 +282,33 @@ defmodule AWS.Request do
274
282
headers = Internal . add_date_header ( headers , long_date )
275
283
canonical_request = Internal . canonical_request ( method , url , headers , body )
276
284
hashed_canonical_request = Util . sha256_hexdigest ( canonical_request )
277
- credential_scope = Internal . credential_scope ( short_date , client . region ,
278
- client . service )
279
- signing_key = Internal . signing_key ( client . secret_access_key , short_date ,
280
- client . region , client . service )
281
- string_to_sign = Internal . string_to_sign ( long_date , credential_scope ,
282
- hashed_canonical_request )
285
+ credential_scope = Internal . credential_scope ( short_date , client . region , client . service )
286
+
287
+ signing_key =
288
+ Internal . signing_key ( client . secret_access_key , short_date , client . region , client . service )
289
+
290
+ string_to_sign =
291
+ Internal . string_to_sign ( long_date , credential_scope , hashed_canonical_request )
292
+
283
293
signature = Util . hmac_sha256_hexdigest ( signing_key , string_to_sign )
284
294
signed_headers = Internal . signed_headers ( headers )
285
- credential = Enum . join ( [ client . access_key_id , short_date , client . region ,
286
- client . service , "aws4_request" ] , "/" )
287
- result = [ { "X-Amz-Algorithm" , "AWS4-HMAC-SHA256" } ,
288
- { "X-Amz-Credential" , credential } ,
289
- { "X-Amz-Date" , long_date } ,
290
- { "X-Amz-SignedHeaders" , signed_headers } ,
291
- { "X-Amz-Signature" , signature } ]
295
+
296
+ credential =
297
+ Enum . join (
298
+ [ client . access_key_id , short_date , client . region , client . service , "aws4_request" ] ,
299
+ "/"
300
+ )
301
+
302
+ result = [
303
+ { "X-Amz-Algorithm" , "AWS4-HMAC-SHA256" } ,
304
+ { "X-Amz-Credential" , credential } ,
305
+ { "X-Amz-Date" , long_date } ,
306
+ { "X-Amz-SignedHeaders" , signed_headers } ,
307
+ { "X-Amz-Signature" , signature }
308
+ ]
309
+
292
310
if expiry = :proplists . get_value ( "X-Amz-Expires" , headers , nil ) do
293
- [ { "X-Amz-Expires" , expiry } | result ]
311
+ [ { "X-Amz-Expires" , expiry } | result ]
294
312
else
295
313
result
296
314
end
@@ -323,6 +341,7 @@ defmodule AWS.Request do
323
341
def add_headers ( [ ] , headers ) do
324
342
headers
325
343
end
344
+
326
345
def add_headers ( [ { name , _ } = header | additions ] , headers ) do
327
346
case List . keyfind ( headers , name , 0 ) do
328
347
nil -> add_headers ( additions , [ header | headers ] )
@@ -343,23 +362,24 @@ defmodule AWS.Request.Internal do
343
362
of headers.
344
363
"""
345
364
def add_authorization_header ( headers , authorization ) do
346
- [ { "Authorization" , authorization } | headers ]
365
+ [ { "Authorization" , authorization } | headers ]
347
366
end
348
367
349
368
@ doc """
350
369
Add an `X-Amz-Date` header with a long date value in `YYMMDDTHHMMSSZ` format
351
370
to a list of headers.
352
371
"""
353
372
def add_date_header ( headers , date ) do
354
- [ { "X-Amz-Date" , date } | headers ]
373
+ [ { "X-Amz-Date" , date } | headers ]
355
374
end
356
375
357
376
@ doc """
358
377
Add an `X-Amz-Security-Token` if credentials configurations are configured for it
359
378
"""
360
379
def add_security_token ( headers , % AWS.Client { session_token: nil } ) , do: headers
380
+
361
381
def add_security_token ( headers , % AWS.Client { session_token: session_token } ) ,
362
- do: [ { "X-Amz-Security-Token" , session_token } | headers ]
382
+ do: [ { "X-Amz-Security-Token" , session_token } | headers ]
363
383
364
384
@ doc """
365
385
Add an X-Amz-Content-SHA256 header which is the hash of the payload.
@@ -374,11 +394,22 @@ defmodule AWS.Request.Internal do
374
394
Generate an AWS4-HMAC-SHA256 authorization signature.
375
395
"""
376
396
def authorization ( access_key_id , credential_scope , signed_headers , signature ) do
377
- Enum . join ( [ "AWS4-HMAC-SHA256 " ,
378
- "Credential=" , access_key_id , "/" , credential_scope , ", " ,
379
- "SignedHeaders=" , signed_headers , ", " ,
380
- "Signature=" , signature ] ,
381
- "" )
397
+ Enum . join (
398
+ [
399
+ "AWS4-HMAC-SHA256 " ,
400
+ "Credential=" ,
401
+ access_key_id ,
402
+ "/" ,
403
+ credential_scope ,
404
+ ", " ,
405
+ "SignedHeaders=" ,
406
+ signed_headers ,
407
+ ", " ,
408
+ "Signature=" ,
409
+ signature
410
+ ] ,
411
+ ""
412
+ )
382
413
end
383
414
384
415
@ doc """
@@ -405,7 +436,7 @@ defmodule AWS.Request.Internal do
405
436
"""
406
437
def canonical_request ( method , url , headers , body ) when is_atom ( method ) do
407
438
Atom . to_string ( method )
408
- |> String . upcase
439
+ |> String . upcase ( )
409
440
|> canonical_request ( url , headers , body )
410
441
end
411
442
@@ -414,8 +445,18 @@ defmodule AWS.Request.Internal do
414
445
canonical_headers = canonical_headers ( headers )
415
446
signed_headers = signed_headers ( headers )
416
447
payload_hash = AWS.Util . sha256_hexdigest ( body )
417
- Enum . join ( [ method , canonical_url , canonical_query_string ,
418
- canonical_headers , signed_headers , payload_hash ] , "\n " )
448
+
449
+ Enum . join (
450
+ [
451
+ method ,
452
+ canonical_url ,
453
+ canonical_query_string ,
454
+ canonical_headers ,
455
+ signed_headers ,
456
+ payload_hash
457
+ ] ,
458
+ "\n "
459
+ )
419
460
end
420
461
421
462
@ doc """
@@ -440,15 +481,15 @@ defmodule AWS.Request.Internal do
440
481
and header names are semicolon-joined in alphabetical order.
441
482
"""
442
483
def signed_headers ( headers ) do
443
- Enum . map ( headers , & signed_header / 1 ) |> Enum . sort |> Enum . join ( ";" )
484
+ Enum . map ( headers , & signed_header / 1 ) |> Enum . sort ( ) |> Enum . join ( ";" )
444
485
end
445
486
446
487
@ doc """
447
488
Generate a signing key from a secret access key, a short date in `YYMMDD`
448
489
format, a region identifier and a service identifier.
449
490
"""
450
491
def signing_key ( secret_access_key , short_date , region , service ) do
451
- "AWS4" <> secret_access_key
492
+ ( "AWS4" <> secret_access_key )
452
493
|> AWS.Util . hmac_sha256 ( short_date )
453
494
|> AWS.Util . hmac_sha256 ( region )
454
495
|> AWS.Util . hmac_sha256 ( service )
@@ -479,18 +520,19 @@ defmodule AWS.Request.Internal do
479
520
|> Enum . map ( & String . split ( & 1 , "=" ) )
480
521
|> Enum . sort ( )
481
522
|> Enum . map_join ( "&" , fn
482
- [ key , value ] -> key <> "=" <> value
483
- [ key ] -> key <> "="
484
- end )
523
+ [ key , value ] -> key <> "=" <> value
524
+ [ key ] -> key <> "="
525
+ end )
485
526
end
486
527
487
528
@ doc """
488
529
Generate the text to sign from a long date in `YYMMDDTHHMMSSZ` format, a
489
530
credential scope and a hashed canonical request.
490
531
"""
491
532
def string_to_sign ( long_date , credential_scope , hashed_canonical_request ) do
492
- Enum . join ( [ "AWS4-HMAC-SHA256" , long_date ,
493
- credential_scope , hashed_canonical_request ] , "\n " )
533
+ Enum . join (
534
+ [ "AWS4-HMAC-SHA256" , long_date , credential_scope , hashed_canonical_request ] ,
535
+ "\n "
536
+ )
494
537
end
495
-
496
538
end
0 commit comments