Skip to content

Commit 8ca27a4

Browse files
committed
Merge pull request #48 from CargoSense/remove-timex
Remove timex
2 parents b39fc89 + 23b81b9 commit 8ca27a4

File tree

9 files changed

+78
-26
lines changed

9 files changed

+78
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ If you wish to use instance roles to obtain AWS access keys you will need to add
2525
```elixir
2626
def deps do
2727
[
28-
ex_aws: "~> 0.4.0",
28+
ex_aws: "~> 0.4.2",
2929
poison: "~> 1.2.0",
3030
httpoison: "~> 0.7.0"
3131
]

lib/ex_aws/auth.ex

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
defmodule ExAws.Auth do
22
import ExAws.Auth.Utils
3-
alias Timex.DateFormat
43

54
@moduledoc false
65

76
def headers(http_method, url, service, config, headers, body) do
8-
now = %{Timex.Date.now | ms: 0}
7+
now = :os.timestamp |> :calendar.now_to_universal_time
98
headers = [
109
{"host", URI.parse(url).host},
1110
{"x-amz-date", amz_date(now)} |
@@ -32,8 +31,8 @@ defmodule ExAws.Auth do
3231
end
3332
def handle_temp_credentials(headers, _), do: headers
3433

35-
def auth_header(access_key, secret_key, http_method, url, region, service, headers, body, now) do
36-
date = DateFormat.format!(now, "%Y%m%d", :strftime)
34+
def auth_header(access_key, secret_key, http_method, url, region, service, headers, body, {date, _} = now) do
35+
date = date |> quasi_iso_format
3736
scope = "#{date}/#{region}/#{service}/aws4_request"
3837

3938
signing_key = build_signing_key(secret_key, date, region, service)
@@ -59,8 +58,8 @@ defmodule ExAws.Auth do
5958
end
6059

6160
def build_string_to_sign(canonical_request, now, scope) do
62-
timestamp = now |> ExAws.Auth.Utils.amz_date
63-
hashed_canonical_request = ExAws.Auth.Utils.hash_sha256(canonical_request)
61+
timestamp = now |> amz_date
62+
hashed_canonical_request = hash_sha256(canonical_request)
6463

6564
[
6665
"AWS4-HMAC-SHA256", "\n",

lib/ex_aws/auth/utils.ex

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
defmodule ExAws.Auth.Utils do
2-
def amz_date(now) do
3-
now
4-
|> Timex.DateFormat.format!("{ISOz}")
5-
|> String.replace("-", "")
6-
|> String.replace(":", "")
7-
end
8-
92
def hash_sha256(data) do
103
:sha256
114
|> :crypto.hash(data)
@@ -29,4 +22,21 @@ defmodule ExAws.Auth.Utils do
2922
|> Atom.to_string
3023
|> String.upcase
3124
end
25+
26+
def amz_date({date, time}) do
27+
date = date |> quasi_iso_format
28+
time = time |> quasi_iso_format
29+
30+
[date, "T", time, "Z"]
31+
|> IO.iodata_to_binary
32+
end
33+
34+
def quasi_iso_format({y, m, d}) do
35+
[y, m, d]
36+
|> Enum.map(&Integer.to_string/1)
37+
|> Enum.map(&zero_pad/1)
38+
end
39+
40+
defp zero_pad(<<_>> = val), do: "0" <> val
41+
defp zero_pad(val), do: val
3242
end

lib/ex_aws/config/auth_cache.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
defmodule ExAws.Config.AuthCache do
22
use GenServer
33

4+
@moduledoc false
5+
6+
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html
7+
48
def start_link(opts \\ []) do
59
GenServer.start_link(__MODULE__, :ok, opts)
610
end
@@ -37,9 +41,8 @@ defmodule ExAws.Config.AuthCache do
3741
end
3842

3943
def refresh_in(expiration) do
40-
expiration = Timex.DateFormat.parse!(expiration, "{ISOz}")
41-
|> Timex.Date.convert(:secs)
42-
time_to_expiration = expiration - Timex.Date.now(:secs)
44+
expiration = expiration |> ExAws.Utils.iso_z_to_secs
45+
time_to_expiration = expiration - ExAws.Utils.now_in_seconds
4346
refresh_in = time_to_expiration - 2 * 60 # check two min prior to expiration
4447
refresh_in * 1000
4548
end

lib/ex_aws/utils.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,29 @@ defmodule ExAws.Utils do
4747
def upcase(value) when is_binary(value) do
4848
String.upcase(value)
4949
end
50+
51+
@seconds_0_to_1970 :calendar.datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}})
52+
53+
def iso_z_to_secs(<<date::binary-10, "T", time::binary-8, "Z">>) do
54+
<<year::binary-4, "-", mon::binary-2, "-", day::binary-2>> = date
55+
<<hour::binary-2, ":", min::binary-2, ":", sec::binary-2>> = time
56+
year = year |> String.to_integer
57+
mon = mon |> String.to_integer
58+
day = day |> String.to_integer
59+
hour = hour |> String.to_integer
60+
min = min |> String.to_integer
61+
sec = sec |> String.to_integer
62+
63+
# Seriously? Gregorian seconds but not epoch seconds?
64+
greg_secs = :calendar.datetime_to_gregorian_seconds({{year, mon, day}, {hour, min, sec}})
65+
greg_secs - @seconds_0_to_1970
66+
end
67+
68+
def now_in_seconds do
69+
greg_secs = :os.timestamp
70+
|> :calendar.now_to_universal_time
71+
|> :calendar.datetime_to_gregorian_seconds
72+
73+
greg_secs - @seconds_0_to_1970
74+
end
5075
end

mix.exs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule ExAws.Mixfile do
33

44
def project do
55
[app: :ex_aws,
6-
version: "0.4.1",
6+
version: "0.4.2",
77
elixir: "~> 1.0",
88
description: "AWS client. Currently supports Dynamo, Kinesis, Lambda, S3",
99
name: "ExAws",
@@ -14,20 +14,17 @@ defmodule ExAws.Mixfile do
1414
end
1515

1616
def application do
17-
[applications: [:logger, :timex, :crypto],
17+
[applications: [:logger, :crypto],
1818
mod: {ExAws, []}]
1919
end
2020

2121
defp deps do
22-
[
23-
{:timex, "~> 0.13.4"},
24-
{:sweet_xml, "~> 0.2.1", only: [:dev, :test]} |
25-
deps(:test_dev)
26-
]
22+
deps(:test_dev)
2723
end
2824

2925
defp deps(:test_dev) do
3026
[
27+
{:sweet_xml, "~> 0.2.1", only: [:dev, :test]},
3128
{:earmark, "~> 0.1", only: :dev},
3229
{:ex_doc, "~> 0.7", only: :dev},
3330
{:sweet_xml, "~> 0.2.1", only: [:test]},

mix.lock

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
"mixunit": {:hex, :mixunit, "0.9.2"},
1010
"poison": {:hex, :poison, "1.2.1"},
1111
"ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5"},
12-
"sweet_xml": {:hex, :sweet_xml, "0.2.1"},
13-
"timex": {:hex, :timex, "0.13.4"}}
12+
"sweet_xml": {:hex, :sweet_xml, "0.2.1"}}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule ExAws.Auth.UtilsTest do
2+
use ExUnit.Case, async: true
3+
import ExAws.Auth.Utils
4+
5+
test "quasi_iso_date/1" do
6+
assert quasi_iso_format({2015, 1, 2}) == ["2015", "01", "02"]
7+
assert quasi_iso_format({2015, 11, 12}) == ["2015", "11", "12"]
8+
end
9+
10+
test "amz_date/1" do
11+
assert amz_date({{2015, 1, 2}, {1, 3, 5}}) == "20150102T010305Z"
12+
assert amz_date({{2015, 11, 22}, {11, 31, 51}}) == "20151122T113151Z"
13+
end
14+
15+
end

test/lib/ex_aws/utils_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ defmodule ExAws.UtilsTest do
2424
assert %{"FooBar" => ["foo", "bar"]} == [foo_bar: ["foo", "bar"]]
2525
|> camelize_keys(deep: true)
2626
end
27+
28+
test "iso_z_to_secs/1" do
29+
assert iso_z_to_secs("2015-07-05T22:16:18Z") == 1436134578
30+
end
2731
end

0 commit comments

Comments
 (0)