Skip to content

Commit

Permalink
only compute timestamp string if not supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
zerth committed Oct 10, 2017
1 parent e2aa4d0 commit a5b362f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/awsv4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ headers(Credentials, #{service := Service,
path => "/",
query_params => #{},
signed_headers => #{},
aws_date => isonow(),
aws_date => undefined,
host => join($., [Service, Region, "amazonaws.com"])},
headers_(Credentials, maps:merge(Defaults, Parameters), RequestPayload).

-spec headers(Credentials :: credentials(),
Service :: string(), % e.g., "s3"
Region :: string(), % e.g., "us-west-2"
Host :: string(), % e.g., "bucketname.s3.amazonaws.com"
AwsDate :: aws_datetime(), % e.g., "20170101T000000Z"
AwsDate :: undefined | aws_datetime(), % e.g., "20170101T000000Z"
TargetAPI :: string() | undefined, % e.g., "DynamoDB_20120810.CreateTable"
Method :: string(), % e.g., "GET", "POST"
Path :: string(),
Expand All @@ -60,7 +60,11 @@ headers(#credentials{secret_access_key = SecretAccessKey,
Method, Path, QueryParams, ExtraSignedHeaders,
RequestPayload) ->

Date = lists:sublist(AwsDate, 8), % yyyymmdd
ActualAwsDate = case AwsDate of
undefined -> isonow();
_ -> AwsDate
end,
Date = lists:sublist(ActualAwsDate, 8), % yyyymmdd
Scope = [Date, Region, Service, "aws4_request"],

Hash = sha256,
Expand All @@ -73,7 +77,7 @@ headers(#credentials{secret_access_key = SecretAccessKey,

Headers = lists:keysort(1, [{string:to_lower(K), V}
|| {K, V} <- [{"host", Host},
{"x-amz-date", AwsDate},
{"x-amz-date", ActualAwsDate},
{"x-amz-security-token", SecurityToken},
{"x-amz-target", TargetAPI}]
++ if
Expand All @@ -99,7 +103,7 @@ headers(#credentials{secret_access_key = SecretAccessKey,
CredentialScope = join($/, Scope),

StringToSign = join($\n, [Algorithm,
AwsDate,
ActualAwsDate,
CredentialScope,
hexlify(crypto:hash(Hash, CanonicalRequest))]),

Expand Down

0 comments on commit a5b362f

Please sign in to comment.