Skip to content

Commit 3f178c4

Browse files
authored
Check float values of time in time_within_drift?/2 (#700)
* Check float values of `time` in `time_within_drift?/2` * Bump version in mix.exs
1 parent e369d58 commit 3f178c4

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v2.2.4
4+
5+
### Enhancement
6+
7+
* Check float values of `time` in `time_within_drift?/2`.
8+
39
## v2.2.3
410

511
### Enhancement

lib/guardian/token/verify.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule Guardian.Token.Verify do
3939
end
4040
end
4141

42-
@spec time_within_drift?(mod :: module, time :: pos_integer) :: true | false
42+
@spec time_within_drift?(mod :: module, time :: pos_integer | float) :: true | false
4343
@doc """
4444
Checks that a time value is within the `allowed_drift` as
4545
configured for the provided module.
@@ -49,7 +49,7 @@ defmodule Guardian.Token.Verify do
4949
5050
This is to deal with clock skew.
5151
"""
52-
def time_within_drift?(mod, time) when is_integer(time) do
52+
def time_within_drift?(mod, time) when is_integer(time) or is_float(time) do
5353
allowed_drift = apply(mod, :config, [:allowed_drift, 0]) / 1000
5454
diff = abs(time - Guardian.timestamp())
5555
diff <= allowed_drift

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Guardian.Mixfile do
22
@moduledoc false
33
use Mix.Project
44

5-
@version "2.2.3"
5+
@version "2.2.4"
66
@url "https://github.com/ueberauth/guardian"
77
@maintainers [
88
"Daniel Neighman",

test/guardian/token/jwt_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,21 @@ defmodule Guardian.Token.JwtTest do
362362
assert {:error, :token_expired} = Jwt.verify_claims(ctx.impl, claims, [])
363363
end
364364

365+
test "it is invalid when exp is a float and too early", ctx do
366+
claims = Map.put(ctx.claims, "exp", Guardian.timestamp() * 1.0 - 1)
367+
assert {:error, :token_expired} = Jwt.verify_claims(ctx.impl, claims, [])
368+
end
369+
365370
test "it is invalid when nbf is too late", ctx do
366371
claims = Map.put(ctx.claims, "nbf", Guardian.timestamp() + 5)
367372
assert {:error, :token_not_yet_valid} = Jwt.verify_claims(ctx.impl, claims, [])
368373
end
369374

375+
test "it is invalid when nbf is a float and too late", ctx do
376+
claims = Map.put(ctx.claims, "nbf", Guardian.timestamp() * 1.0 + 5)
377+
assert {:error, :token_not_yet_valid} = Jwt.verify_claims(ctx.impl, claims, [])
378+
end
379+
370380
test "it is invalid when the issuer is not correct", ctx do
371381
claims = Map.put(ctx.claims, "iss", "someone-else")
372382
assert {:error, :invalid_issuer} = Jwt.verify_claims(ctx.impl, claims, [])

0 commit comments

Comments
 (0)