Packages
guardian
0.5.0
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/load_resource.ex
defmodule Guardian.Plug.LoadResource do
@moduledoc """
Fetches the resource specified in a set of claims.
The Guardian.serializer/0 is used once the subject is extracted from the token.
The resource becomes available at Guardian.Plug.current_resource(conn) if successful.
If there is no valid JWT in the request so far (Guardian.Plug.VerifySession / Guardian.Plug.VerifyAuthorization) did not find a valid token
then nothing will occur, and the Guardian.Plug.current_resource/1 will be nil
"""
@doc false
def init(opts \\ %{}), do: Enum.into(opts, %{})
@doc false
def call(conn, opts) do
key = Dict.get(opts, :key, :default)
case Guardian.Plug.current_resource(conn, key) do
{ :ok, _ } -> conn
{ :error, _ } -> conn
_ ->
case Guardian.Plug.claims(conn, key) do
{ :ok, claims } ->
case Guardian.serializer.from_token(Dict.get(claims, "sub")) do
{ :ok, resource } -> Guardian.Plug.set_current_resource(conn, resource, key)
{ :error, _ } -> Guardian.Plug.set_current_resource(conn, nil, key)
end
{ :error, _ } -> Guardian.Plug.set_current_resource(conn, nil, key)
_ -> Guardian.Plug.set_current_resource(conn, nil, key)
end
end
end
end