Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check float values of time in time_within_drift?/2 #700

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v2.2.4
yordis marked this conversation as resolved.
Show resolved Hide resolved

### 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
Original file line number Diff line number Diff line change
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
10 changes: 10 additions & 0 deletions test/guardian/token/jwt_test.exs
Original file line number Diff line number Diff line change
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