Packages
charon
1.3.3
4.3.0
4.3.0-beta1
4.2.0
4.2.0-beta3
4.2.0-beta2
4.2.0-beta1
4.1.0
4.0.1
4.0.0
4.0.0-beta3
4.0.0-beta2
4.0.0-beta1
3.4.1
3.4.0
3.3.0
3.2.0
3.1.1
3.1.0
3.0.3
3.0.2
3.0.1
3.0.0
3.0.0-beta1
2.8.0
2.8.0-beta1
2.7.3
2.7.2
2.7.1
2.7.0
2.6.1
2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.2.0-beta
2.1.3
2.1.2
2.1.2-beta
2.1.1
2.1.0
2.0.0
1.3.4
1.3.3
1.3.2-beta
1.3.1-beta
1.3.0-beta
1.2.0-beta
1.1.0-beta
1.0.1-beta
1.0.0-beta1
0.0.4-alpha
0.0.3-alpha
0.0.2-alpha
0.0.1-alpha
Authentication & sessions for API's.
Current section
Files
Jump to
Current section
Files
lib/charon/internal.ex
defmodule Charon.Internal do
@moduledoc false
# module consists of shared functions internal to the package
use __MODULE__.Constants
require Logger
alias Plug.Conn
@doc """
Put an auth error on the conn
"""
def auth_error(conn, error), do: Conn.put_private(conn, @auth_error, error)
@doc """
Get a `now` unix timestamp
"""
def now(), do: System.os_time(:second)
@doc """
Get a value from the conn/resolution's private map
"""
def get_private(_res = %{private: priv}, key), do: Map.get(priv, key)
@doc """
Merge a map into the conn/resolution's private map
"""
def put_private(conn_or_res = %{private: priv}, map),
do: %{conn_or_res | private: Map.merge(priv, map)}
@doc """
Put a key/value into the conn/resolution's private map
"""
def put_private(conn_or_res = %{private: priv}, key, value),
do: %{conn_or_res | private: Map.put(priv, key, value)}
@doc """
Generate a random URL-encoded string of `byte_size` bits.
"""
def random_url_encoded(byte_size) do
byte_size |> :crypto.strong_rand_bytes() |> Base.url_encode64(padding: false)
end
@doc """
Determine if the token's signature transport mechanism is `:cookie` or `:bearer`.
"""
def parse_sig_transport(token_signature_transport)
def parse_sig_transport("bearer"), do: :bearer
def parse_sig_transport("cookie"), do: :cookie
def parse_sig_transport(:bearer), do: :bearer
def parse_sig_transport(:cookie), do: :cookie
@doc """
Split the signature from a "header.payload.signature" token.
"""
def split_signature(token, ttl, cookie_opts) do
[header, payload, signature] = String.split(token, ".", parts: 3)
token = [header, ?., payload, ?.] |> IO.iodata_to_binary()
cookie_opts = Keyword.put(cookie_opts, :max_age, ttl)
{token, signature, cookie_opts}
end
end