Packages
guardian
0.6.3
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/verify_session.ex
defmodule Guardian.Plug.VerifySession do
@moduledoc """
Use this plug to verify a token contained in a session.
## Example
plug Guardian.Plug.VerifySession
You can also specify a location to look for the token
## Example
plug Guardian.Plug.VerifySession, key: :secret
Verifying the session will update the claims on the request, available with Guardian.Plug.claims/1
In the case of an error, the claims will be set to { :error, reason }
"""
import Guardian.Keys
@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.claims(conn, key) do
{ :ok, _ } -> conn
{ :error, :no_session } ->
jwt = Plug.Conn.get_session(conn, base_key(key))
if jwt do
case Guardian.decode_and_verify(jwt, %{ }) do
{ :ok, claims } ->
conn
|> Guardian.Plug.set_claims({ :ok, claims }, key)
|> Guardian.Plug.set_current_token(jwt, key)
{ :error, reason } ->
conn
|> Plug.Conn.delete_session(base_key(key))
|> Guardian.Plug.set_claims({ :error, reason }, key)
end
else
conn
end
end
end
end