Joken.Parameters behaviour
This module defines the behaviour for encoding and decoding tokens as well as adding and validating claims.
The supported claims are [:exp, :nbf, :iat, :aud, :iss, :sub, :jti]
The following example would use Poison for encoding and decoding
and add and validate the exp
claim. All other claims would not be added
or validated.
Ex:
defmodule My.Parameters.Module do
@behaviour Joken.Parameters
def encode(map) do
Poison.encode!(map)
end
def decode(binary) do
Poison.decode!(binary, keys: :atoms!)
end
def claim(:exp, payload) do
12345678
end
def claim(_, _) do
nil
end
def validate_claim(:exp, payload) do
Joken.Parameters.validate_time_claim(payload, :exp, "Token expired", fn(expires_at, now) -> expires_at > now end)
end
def validate_claim(_, _) do
:ok
end
end
Summary↑
get_current_time() | Helper function to get the current time |
validate_claim(payload, key_to_check, value, full_name) | Helper function for validating non-time claims |
validate_time_claim(payload, key, error_msg, validate_time_fun) | Helper function for validating time claims (exp, nbf, iat) |
Functions
Helper function to get the current time
Helper function for validating non-time claims
Helper function for validating time claims (exp, nbf, iat)
Callbacks
Specs:
- claim(Atom.t, Joken.payload) :: nil | any
Adds the specified claim to the payload.
If nil is returned, then the claim will not be added to the payload. Otherwise, the value returned will be added to the payload
Specs:
- decode(String.t) :: Joken.payload
decode can take a string and return a map or a keyword list.
Specs:
- encode(Joken.payload) :: String.t
encode can take either a map or a keyword list or both and return a string.
Specs:
- validate_claim(Atom.t, Joken.payload) :: :ok | {:error, String.t}
Validates the claim on the payload.
Returns :ok
if the claim is validated correctly or
`{:error, message} if it does not