Joken.Config behaviour
This module defines the behaviour for the following:
- encoding and decoding tokens
- adding and validating claims
- secret key used for encoding and decoding
- the algorithm used
The supported claims are defined in the type, Joken.claim
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.Config.Module do
@behaviour Joken.Config
def secret_key() do
Application.get_env(:app, :secret_key)
end
def algorithm() do
:H256
end
def encode(map) do
Poison.encode!(map)
end
def decode(binary) do
Poison.decode!(binary, keys: :atoms!)
end
def claim(:exp, payload) do
Joken.Config.get_current_time() + 300
end
def claim(_, _) do
nil
end
def validate_claim(:exp, payload) do
Joken.Config.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:
- algorithm :: Joken.algorithm
Returns the algorithm used
Specs:
- claim(Joken.claim, 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:
- secret_key :: String.t
Returns the secret key used for encoding and decoding
Specs:
- validate_claim(Joken.claim, 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