Guardian v2.1.1 Guardian.Plug View Source
Provides functions for the implementation module for dealing with Guardian in a Plug environment
defmodule MyApp.Tokens do
use Guardian, otp_app: :my_app
# ... snip
end
Your implementation module will be given a Plug
module for
interacting with plug.
If you're using Guardian in your application most of the setters will be uninteresting. They're mostly for library authors and Guardian itself.
The usual functions you'd use in your application are:
sign_in(conn, resource, claims \\ %{}, opts \\ [])
Sign in a resource for your application.
This will generate a token for your resource according to
your TokenModule and subject_for_token
callback.
sign_in
will also cache the resource
, claims
, and token
on the
connection.
conn = MyApp.Guardian.Plug.sign_in(conn, resource, my_custom_claims)
If there is a session present the token will be stored in the session to provide traditional session based authentication.
Link to this section Summary
Functions
Puts a response cookie which replaces the previous remember_me
cookie
and is set to immediately expire on the client.
Provides the default key for the location of a token in the session and connection
Link to this section Functions
authenticated?(conn, opts \\ [])
View Source
authenticated?(Plug.Conn.t(), Guardian.options()) :: true | false
authenticated?(Plug.Conn.t(), Guardian.options()) :: true | false
clear_remember_me(conn, mod, opts \\ [])
View Source
clear_remember_me(Plug.Conn.t(), module(), Guardian.options()) :: Plug.Conn.t()
clear_remember_me(Plug.Conn.t(), module(), Guardian.options()) :: Plug.Conn.t()
Puts a response cookie which replaces the previous remember_me
cookie
and is set to immediately expire on the client.
Note that while this can be used as a cheap way to sign out, a malicious client could still access your server using the old JWT from the old cookie. In other words, this does not in any way invalidate the token you issued, it just makes a compliant client forget it.
current_claims(conn, opts \\ [])
View Source
current_claims(Plug.Conn.t(), Guardian.options()) ::
Guardian.Token.claims() | nil
current_claims(Plug.Conn.t(), Guardian.options()) :: Guardian.Token.claims() | nil
current_resource(conn, opts \\ [])
View Source
current_resource(Plug.Conn.t(), Guardian.options()) :: any() | nil
current_resource(Plug.Conn.t(), Guardian.options()) :: any() | nil
current_token(conn, opts \\ [])
View Source
current_token(Plug.Conn.t(), Guardian.options()) :: Guardian.Token.token() | nil
current_token(Plug.Conn.t(), Guardian.options()) :: Guardian.Token.token() | nil
default_key()
View Source
default_key() :: String.t()
default_key() :: String.t()
Provides the default key for the location of a token in the session and connection
find_token_from_cookies(conn, opts \\ [])
View Source
find_token_from_cookies(conn :: Plug.Conn.t(), Keyword.t()) ::
{:ok, String.t()} | :no_token_found
find_token_from_cookies(conn :: Plug.Conn.t(), Keyword.t()) :: {:ok, String.t()} | :no_token_found
maybe_halt(conn, opts \\ [])
View Source
maybe_halt(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()
maybe_halt(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()
put_current_claims(conn, claims, opts \\ [])
View Source
put_current_claims(
Plug.Conn.t(),
Guardian.Token.claims() | nil,
Guardian.options()
) :: Plug.Conn.t()
put_current_claims( Plug.Conn.t(), Guardian.Token.claims() | nil, Guardian.options() ) :: Plug.Conn.t()
put_current_resource(conn, resource, opts \\ [])
View Source
put_current_resource(
Plug.Conn.t(),
resource :: any() | nil,
Guardian.options()
) :: Plug.Conn.t()
put_current_resource( Plug.Conn.t(), resource :: any() | nil, Guardian.options() ) :: Plug.Conn.t()
put_current_token(conn, token, opts \\ [])
View Source
put_current_token(
Plug.Conn.t(),
Guardian.Token.token() | nil,
Guardian.options()
) :: Plug.Conn.t()
put_current_token( Plug.Conn.t(), Guardian.Token.token() | nil, Guardian.options() ) :: Plug.Conn.t()
put_session_token(conn, token, opts \\ [])
View Source
put_session_token(Plug.Conn.t(), Guardian.Token.token(), Guardian.options()) ::
Plug.Conn.t()
put_session_token(Plug.Conn.t(), Guardian.Token.token(), Guardian.options()) :: Plug.Conn.t()
remember_me(conn, mod, resource, claims \\ %{}, opts \\ [])
View Source
remember_me(
Plug.Conn.t(),
module(),
any(),
Guardian.Token.claims(),
Guardian.options()
) :: Plug.Conn.t()
remember_me( Plug.Conn.t(), module(), any(), Guardian.Token.claims(), Guardian.options() ) :: Plug.Conn.t()
remember_me_from_token(conn, mod, token, claims_to_check \\ %{}, opts \\ [])
View Source
remember_me_from_token(
Plug.Conn.t(),
module(),
Guardian.Token.token(),
Guardian.Token.claims(),
Guardian.options()
) :: Plug.Conn.t()
remember_me_from_token( Plug.Conn.t(), module(), Guardian.Token.token(), Guardian.Token.claims(), Guardian.options() ) :: Plug.Conn.t()
session_active?(conn) View Source
sign_in(conn, impl, resource, claims \\ %{}, opts \\ [])
View Source
sign_in(
Plug.Conn.t(),
module(),
any(),
Guardian.Token.claims(),
Guardian.options()
) :: Plug.Conn.t()
sign_in( Plug.Conn.t(), module(), any(), Guardian.Token.claims(), Guardian.options() ) :: Plug.Conn.t()
sign_out(conn, impl, opts \\ [])
View Source
sign_out(Plug.Conn.t(), module(), Guardian.options()) :: Plug.Conn.t()
sign_out(Plug.Conn.t(), module(), Guardian.options()) :: Plug.Conn.t()