authex v0.1.1 Authex View Source

To come…

Link to this section Summary

Functions

Returns the current scopes from a Plug.Conn

Returns the current user from a Plug.Conn

Turns a usable data structure into a compact token using a serializer module

Turns a token into a usable data structure using a serializer module

Signs an Authex.Token struct, creating a compact token

Creates a new Authex.Token struct from the given claims and options

Verifies a compact token

Link to this section Functions

Link to this function current_scopes(conn) View Source
current_scopes(Plug.Conn.t()) :: {:ok, list()} | :error

Returns the current scopes from a Plug.Conn.

Parameters

  • conn: A Plug.Conn struct.
Link to this function current_user(conn) View Source
current_user(Plug.Conn.t()) :: {:ok, term()} | :error

Returns the current user from a Plug.Conn.

Parameters

  • conn: A Plug.Conn struct.
Link to this function for_token(resource) View Source
for_token(term()) :: Authex.Token.t() | :error

Turns a usable data structure into a compact token using a serializer module.

Parameters

  • resource: Any usable data structure.

Examples

iex> %{id: 1} |> Authex.for_token() |> is_binary()
true
Link to this function from_token(token) View Source
from_token(Authex.Token.t()) :: term() | {:error, atom()}

Turns a token into a usable data structure using a serializer module.

Parameters

  • token: An Authex.Token struct or compact token binary.

Examples

iex> [sub: 1] |> Authex.token() |> Authex.sign() |> Authex.from_token()
%{id: 1, scopes: []}
Link to this function sign(token, options \\ []) View Source
sign(Authex.Token.t(), list()) :: binary()

Signs an Authex.Token struct, creating a compact token.

Parameters

  • token: An Authex.Token struct.
  • options: A keyword list of options.

Options

  • :secret - the secret key to sign the token with.
  • :alg - the algorithm to sign the token with.

Examples

iex> Authex.token() |> Authex.sign() |> is_binary()
true
Link to this function token(claims \\ [], options \\ []) View Source
token(list(), list()) :: Authex.Token.t()

Creates a new Authex.Token struct from the given claims and options

Parameters

  • claims: A keyword list of JWT claims.
  • options: A keyword list of options.

Options

  • :time - the base time (timestamp format) in which to use.
  • :ttl - the TTL for the token.

Examples

iex> token = Authex.token([sub: 1], [ttl: 60])
iex> with %Authex.Token{sub: sub} <- token, do: sub
1
Link to this function verify(compact_token, options \\ []) View Source
verify(binary(), list()) :: {:ok, Authex.Token.t()} | {:error, atom()}

Verifies a compact token.

Parameters

  • compact_token: A compact token binary.
  • options: A keyword list of options.

Options

  • :secret - the secret key to verify the token with.
  • :alg - the algorithm to verify the token with.

Examples

iex> {:ok, token} = [sub: 1] |> Authex.token() |> Authex.sign() |> Authex.verify()
iex> with %Authex.Token{sub: sub} <- token, do: sub
1