Joken
Encodes and decodes JSON Web Tokens.
Usage:
Looks for a joken config block with secret_key
, algorithm
, and json_module
. Json module being a module that implements the Joken.Codec
Behaviour
defmodule My.Json.Module do
alias Poison, as: JSON
@behaviour Joken.Json
def encode(map) do
JSON.encode!(map)
end
def decode(binary) do
JSON.decode!(binary, keys: :atoms!)
end
end
config :joken
secret_key: "test",
json_module: My.Json.Module,
algorithm: :HS256, #Optional. defaults to :HS256
then to encode and decode
{:ok, token} = Joken.encode(%{username: "johndoe"})
{:ok, decoded_payload} = Joken.decode(jwt)
Summary↑
decode(jwt, claims \\ %{}) | Decodes the given JSON Web Token and gets the payload. Optionally checks against the given claims for validity |
encode(payload, claims \\ %{}) | Encodes the given payload and optional claims into a JSON Web Token |
Types ↑
algorithm :: :HS256 | :HS384 | :HS512
status :: :ok | :error
Functions
Specs:
Decodes the given JSON Web Token and gets the payload. Optionally checks against the given claims for validity
Joken.decode(token, %{ aud: "self" })
Specs:
Encodes the given payload and optional claims into a JSON Web Token
Joken.encode(%{ name: "John Doe" }, %{ iss: "self"})