Skip to content

Commit

Permalink
Use System.os_time, closes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 2, 2024
1 parent b867348 commit cc8cb32
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/plug/crypto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ defmodule Plug.Crypto do
* `:key_digest` - option passed to `Plug.Crypto.KeyGenerator`
when generating the encryption and signing keys. Defaults to `:sha256`
* `:signed_at` - set the timestamp of the token in seconds.
Defaults to `System.system_time(:millisecond)`
Defaults to `System.os_time(:millisecond)`
* `:max_age` - the default maximum age of the token. Defaults to
`86400` seconds (1 day) and it may be overridden on `verify/4`.
Expand Down Expand Up @@ -196,7 +196,7 @@ defmodule Plug.Crypto do
* `:key_digest` - option passed to `Plug.Crypto.KeyGenerator`
when generating the encryption and signing keys. Defaults to `:sha256`
* `:signed_at` - set the timestamp of the token in seconds.
Defaults to `System.system_time(:millisecond)`
Defaults to `System.os_time(:millisecond)`
* `:max_age` - the default maximum age of the token. Defaults to
`86400` seconds (1 day) and it may be overridden on `decrypt/4`.
Expand Down Expand Up @@ -347,5 +347,5 @@ defmodule Plug.Crypto do
defp expired?(_signed, max_age_secs) when max_age_secs <= 0, do: true
defp expired?(signed, max_age_secs), do: signed + trunc(max_age_secs * 1000) < now_ms()

defp now_ms, do: System.system_time(:millisecond)
defp now_ms, do: System.os_time(:millisecond)
end
2 changes: 1 addition & 1 deletion test/plug/crypto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ defmodule Plug.CryptoTest do

test "supports signed_at in seconds" do
seconds_in_day = 24 * 60 * 60
day_ago_seconds = System.system_time(:second) - seconds_in_day
day_ago_seconds = System.os_time(:second) - seconds_in_day
token = encrypt(@key, "secret", 1, signed_at: day_ago_seconds)
assert decrypt(@key, "secret", token, max_age: seconds_in_day + 1) == {:ok, 1}

Expand Down

0 comments on commit cc8cb32

Please sign in to comment.