Packages
guardian
2.2.4
2.4.0
2.3.2
2.3.1
2.3.0
2.2.4
2.2.3
2.2.2
2.2.1
2.2.0
2.1.2
2.1.1
2.0.0
1.2.1
1.2.0
retired
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-beta.1
1.0.0-beta.0
0.14.6
0.14.5
0.14.4
0.14.3
retired
0.14.2
0.14.1
0.14.0
0.13.0
0.12.0
0.11.1
0.10.1
0.10.0
0.9.1
0.9.0
0.8.1
0.8.0
0.7.4
0.7.2
0.7.1
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.2
0.5.0
0.4.1
0.4.0
0.3.1
0.3.0
0.2.0
0.1.1
0.1.0
Elixir Authentication framework
Current section
Files
Jump to
Current section
Files
lib/guardian/plug/keys.ex
defmodule Guardian.Plug.Keys do
@moduledoc """
Calculates keys for use with plug.
The keys relate to where in the session/connection
the data that Guardian deals in will be stored.
`token`, `claims`, `resource` are all keyed.
`token`, `claims`, `resource` are all stored on the conn.
`token` is stored in the session if a session is found.
"""
@doc false
@spec claims_key() :: atom
@spec claims_key(String.t() | atom) :: atom
def claims_key(key \\ :default), do: String.to_atom("#{base_key(key)}_claims")
@doc false
@spec resource_key() :: atom
@spec resource_key(String.t() | atom) :: atom
def resource_key(key \\ :default), do: String.to_atom("#{base_key(key)}_resource")
@doc false
@spec token_key() :: atom
@spec token_key(String.t() | atom) :: atom
def token_key(key \\ :default), do: String.to_atom("#{base_key(key)}_token")
@doc false
@spec base_key(String.t() | atom) :: atom
def base_key("guardian_" <> _ = the_key), do: String.to_atom(the_key)
def base_key(the_key), do: String.to_atom("guardian_#{the_key}")
def key_from_other(other_key) when is_binary(other_key) do
~r/^guardian_(?<key>.+)_(token|resource|claims)$/
|> Regex.named_captures(other_key)
|> extract_key()
end
def key_from_other(atom) do
atom
|> to_string()
|> key_from_other()
end
defp extract_key(%{"key" => key}), do: String.to_atom(key)
defp extract_key(_), do: nil
end