Joken.Config behaviour

This module defines the behaviour for the following:

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

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)

Callbacks

algorithm/0

Specs:

Returns the algorithm used

claim/2

Specs:

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

decode/1

Specs:

decode can take a string and return a map or a keyword list.

encode/1

Specs:

encode can take either a map or a keyword list or both and return a string.

secret_key/0

Specs:

Returns the secret key used for encoding and decoding

validate_claim/2

Specs:

Validates the claim on the payload.

Returns :ok if the claim is validated correctly or `{:error, message} if it does not