Skip to content

Commit

Permalink
More AWS refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed May 28, 2024
1 parent e19ee81 commit 4cf866e
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions lib/req/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,18 @@ defmodule Req.Utils do
#{body_digest}\
"""

string_to_sign =
iodata("""
AWS4-HMAC-SHA256
#{datetime_string}
#{date_string}/#{region}/#{service}/aws4_request
#{hex(sha256(canonical_request))}\
""")

signature =
aws_sigv4(
canonical_request,
string_to_sign,
date_string,
datetime_string,
region,
service,
secret_access_key
Expand Down Expand Up @@ -147,11 +154,18 @@ defmodule Req.Utils do
UNSIGNED-PAYLOAD\
"""

string_to_sign =
iodata("""
AWS4-HMAC-SHA256
#{datetime_string}
#{date_string}/#{region}/#{service}/aws4_request
#{hex(sha256(canonical_request))}\
""")

signature =
aws_sigv4(
canonical_request,
string_to_sign,
date_string,
datetime_string,
region,
service,
secret_access_key
Expand All @@ -161,21 +175,12 @@ defmodule Req.Utils do
end

def aws_sigv4(
request,
string_to_sign,
date_string,
datetime_string,
region,
service,
secret_access_key
) do
string_to_sign =
iodata("""
AWS4-HMAC-SHA256
#{datetime_string}
#{date_string}/#{region}/#{service}/aws4_request
#{hex(sha256(request))}\
""")

signature =
["AWS4", secret_access_key]
|> hmac(date_string)
Expand Down

0 comments on commit 4cf866e

Please sign in to comment.