Skip to content

Commit

Permalink
Check float values of time in time_within_drift?/2 (#700)
Browse files Browse the repository at this point in the history
* Check float values of `time` in `time_within_drift?/2`

* Bump version in mix.exs
  • Loading branch information
danglduy committed Jun 16, 2022
1 parent e369d58 commit 3f178c4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## v2.2.4

### Enhancement

* Check float values of `time` in `time_within_drift?/2`.

## v2.2.3

### Enhancement
Expand Down
4 changes: 2 additions & 2 deletions lib/guardian/token/verify.ex
Expand Up @@ -39,7 +39,7 @@ defmodule Guardian.Token.Verify do
end
end

@spec time_within_drift?(mod :: module, time :: pos_integer) :: true | false
@spec time_within_drift?(mod :: module, time :: pos_integer | float) :: true | false
@doc """
Checks that a time value is within the `allowed_drift` as
configured for the provided module.
Expand All @@ -49,7 +49,7 @@ defmodule Guardian.Token.Verify do
This is to deal with clock skew.
"""
def time_within_drift?(mod, time) when is_integer(time) do
def time_within_drift?(mod, time) when is_integer(time) or is_float(time) do
allowed_drift = apply(mod, :config, [:allowed_drift, 0]) / 1000
diff = abs(time - Guardian.timestamp())
diff <= allowed_drift
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -2,7 +2,7 @@ defmodule Guardian.Mixfile do
@moduledoc false
use Mix.Project

@version "2.2.3"
@version "2.2.4"
@url "https://github.com/ueberauth/guardian"
@maintainers [
"Daniel Neighman",
Expand Down
10 changes: 10 additions & 0 deletions test/guardian/token/jwt_test.exs
Expand Up @@ -362,11 +362,21 @@ defmodule Guardian.Token.JwtTest do
assert {:error, :token_expired} = Jwt.verify_claims(ctx.impl, claims, [])
end

test "it is invalid when exp is a float and too early", ctx do
claims = Map.put(ctx.claims, "exp", Guardian.timestamp() * 1.0 - 1)
assert {:error, :token_expired} = Jwt.verify_claims(ctx.impl, claims, [])
end

test "it is invalid when nbf is too late", ctx do
claims = Map.put(ctx.claims, "nbf", Guardian.timestamp() + 5)
assert {:error, :token_not_yet_valid} = Jwt.verify_claims(ctx.impl, claims, [])
end

test "it is invalid when nbf is a float and too late", ctx do
claims = Map.put(ctx.claims, "nbf", Guardian.timestamp() * 1.0 + 5)
assert {:error, :token_not_yet_valid} = Jwt.verify_claims(ctx.impl, claims, [])
end

test "it is invalid when the issuer is not correct", ctx do
claims = Map.put(ctx.claims, "iss", "someone-else")
assert {:error, :invalid_issuer} = Jwt.verify_claims(ctx.impl, claims, [])
Expand Down

0 comments on commit 3f178c4

Please sign in to comment.