Packages
guardian
1.0.0-beta.1
2.4.0
2.3.2
2.3.1
2.3.0
2.2.4
2.2.3
2.2.2
2.2.1
2.2.0
2.1.2
2.1.1
2.0.0
1.2.1
1.2.0
retired
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-beta.1
1.0.0-beta.0
0.14.6
0.14.5
0.14.4
0.14.3
retired
0.14.2
0.14.1
0.14.0
0.13.0
0.12.0
0.11.1
0.10.1
0.10.0
0.9.1
0.9.0
0.8.1
0.8.0
0.7.4
0.7.2
0.7.1
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.2
0.5.0
0.4.1
0.4.0
0.3.1
0.3.0
0.2.0
0.1.1
0.1.0
Elixir Authentication framework
Current section
Files
Jump to
Current section
Files
lib/guardian/token/verify.ex
defmodule Guardian.Token.Verify do
@moduledoc """
Interface for verifying tokens.
This is intended to be used primarily by token modules
but allows for a custom verification module to be created
if the one that ships with your TokenModule is not quite what you want.
"""
@doc """
Verify a single claim
You should also include a fallback for claims that you are not validating
```elixir
def verify_claim(_mod, _key, claims, _opts), do: {:ok, claims}
```
"""
@callback verify_claim(
mod :: module,
claim_key :: String.t,
claims :: Guardian.Token.claims,
options :: Guardian.options
) :: {:ok, Guardian.Token.claims} | {:error, atom}
defmacro __using__(_opts \\ []) do
quote do
def verify_claims(mod, claims, opts) do
Enum.reduce claims, {:ok, claims}, fn
{k, v}, {:ok, claims} -> verify_claim(mod, k, claims, opts)
_, {:error, reason} = err -> err
end
end
def verify_claim(_mod, _claim_key, claims, _opts), do: {:ok, claims}
defoverridable [verify_claim: 4]
end
end
@spec time_within_drift?(
mod :: module, time :: pos_integer
) :: true | false
@doc """
Checks that a time value is within the `allowed_drift` as
configured for the provided module
Allowed drift is measured in seconds and represents the maximum amount
of time a token may be expired for an still be considered valid.
This is to deal with clock skew.
"""
def time_within_drift?(mod, time) when is_integer(time) do
allowed_drift = apply(mod, :config, [:allowed_drift, 0]) / 1000
diff = abs(time - Guardian.timestamp())
diff <= allowed_drift
end
def time_within_drift?(_), do: true
@spec verify_literal_claims(
claims :: Guardian.Token.claims,
claims_to_check :: Guardian.Token.claims | nil,
opts :: Guardian.options
) :: {:ok, Guardian.Token.claims} | {:error, any}
@doc """
For claims, check the values against the values found in
`claims_to_check`. If there is a claim to check that does not match
verification fails.
"""
def verify_literal_claims(claims, nil, _opts), do: {:ok, claims}
def verify_literal_claims(claims, claims_to_check, _opts) do
results =
for {k, v} <- claims_to_check, into: [], do: verify_literal_claim(claims, k, v)
errors = Enum.filter(results, &(elem(&1, 0) == :error))
if Enum.any?(errors), do: hd(errors), else: {:ok, claims}
end
defp verify_literal_claim(claims, key, v) do
if Map.get(claims, key) == v, do: {:ok, claims}, else: {:error, key}
end
end