EpochtalkServer.Auth.Guardian (epochtalk_server v0.1.0)
Guardian provides a singular interface for authentication in Elixir applications
that is token
based.
Tokens should be:
- tamper proof
- include a payload (claims)
JWT tokens (the default) fit this description.
When using Guardian, you'll need an implementation module.
This module is what you will use to interact with tokens in your application. When youdefmodule MyApp.Guardian do use Guardian, otp_app: :my_app def subject_for_token(resource, _claims), do: {:ok, to_string(resource.id)} def resource_from_claims(claims) do find_me_a_resource(claims["sub"]) # {:ok, resource} or {:error, reason} end end
use
Guardian, the:otp_app
option is required. Any other option provided will be merged with the configuration in the config files. The Guardian module contains some generated functions and some callbacks.
generated-functions
Generated functions
default_token_type
default_token_type()
Overridable.
Provides the default token type for the token - "access"
Token types allow a developer to mark a token as having a particular purpose.
Different types of tokens can then be used specifically in your app.
Types may include (but are not limited to):
"access"
"refresh"
Access tokens should be short lived and are used to access resources on your API. Refresh tokens should be longer lived and whose only purpose is to exchange for a shorter lived access token. To specify the type of token, use the:token_type
option in theencode_and_sign
function. Token type is encoded into the token in the"typ"
field. Return - a string.
peek-token
peek(token)
Inspect a tokens payload. Note that this function does no verification.
Return - a map including the :claims
key.
config-config-key-default-nil
config()
, config(key, default \\ nil)
Without argument config
will return the full configuration Keyword list.
When given a key
and optionally a default, config
will fetch a resolved value
contained in the key.
See Guardian.Config.resolve_value/1
encode_and_sign-resource-claims-opts
encode_and_sign(resource, claims \\ %{}, opts \\ [])
Creates a signed token. Arguments:
resource
- The resource to represent in the token (i.e. the user)claims
- Any custom claims that you want to use in your tokenopts
- Options for the token module and callbacks For more information on options see the documentation for your token module.
The# Provide a token using the defaults including the default_token_type {:ok, token, full_claims} = MyApp.Guardian.encode_and_sign(user) # Provide a token including custom claims {:ok, token, full_claims} = MyApp.Guardian.encode_and_sign(user, %{some: "claim"}) # Provide a token including custom claims and a different token type/ttl {:ok, token, full_claims} = MyApp.Guardian.encode_and_sign(user, %{some: "claim"}, token_type: "refresh", ttl: {4, :weeks})
encode_and_sign
function calls a number of callbacks on your implementation module. SeeGuardian.encode_and_sign/4
decode_and_verify-token-claims_to_check-opts
decode_and_verify(token, claims_to_check \\ %{}, opts \\ [])
Decodes a token and verifies the claims are valid. Arguments:
token
- The token to decodeclaims_to_check
- A map of the literal claims that should be matched. If any of the claims do not literally match verification fails.opts
- The options to pass to the token module and callbacks Callbacks:decode_and_verify
calls a number of callbacks on your implementation module, SeeGuardian.decode_and_verify/4
# Decode and verify using the defaults {:ok, claims} = MyApp.Guardian.decode_and_verify(token) # Decode and verify with literal claims check. # If the claims in the token do not match those given verification will fail {:ok, claims} = MyApp.Guardian.decode_and_verify(token, %{match: "claim"}) # Decode and verify with literal claims check and options. # Options are passed to your token module and callbacks {:ok, claims} = MyApp.Guardian.decode_and_verify(token, %{match: "claim"}, some: "secret")
revoke-token-opts
revoke(token, opts \\ [])
Revoke a token. Note: this is entirely dependent on your token module and implementation callbacks.
{:ok, claims} = MyApp.Guardian.revoke(token, some: "option")
refresh-token-opts
refresh(token, opts \ [])
Refreshes the time on a token. This is used to re-issue a token with essentially the same claims but with a different expiry. Tokens are verified before performing the refresh to ensure only valid tokens may be refreshed. Arguments:
token
- The old token to refreshopts
- Options to pass to the Implementation Module and callbacks Options::ttl
- The new ttl. If not specified the default will be used.
See{:ok, {old_token, old_claims}, {new_token, new_claims}} = MyApp.Guardian.refresh(old_token, ttl: {1, :hour})
Guardian.refresh
exchange-old_token-from_type-to_type-options
exchange(old_token, from_type, to_type, options)
Exchanges one token for another of a different type.
Especially useful to trade in a refresh
token for an access
one.
Tokens are verified before performing the exchange to ensure that
only valid tokens may be exchanged.
Arguments:
old_token
- The existing token you wish to exchange.from_type
- The type the old token must be. Can be given a list of types.to_type
- The new type of token that you want back.options
- The options to pass to the token module and callbacks. Options: Options may be used by your token module or callbacks.ttl
- The ttl for the new token SeeGuardian.exchange
Note: Copied from Guardian Docs
Link to this section Summary
Functions
An optional callback invoked after the token has been generated and signed.
Callback implementation for Guardian.after_sign_in/5
.
Callback implementation for Guardian.before_sign_out/3
.
Callback implementation for Guardian.build_claims/3
.
Fetches the configuration for this module.
Returns a resolved value of the configuration found at a key.
Decodes and verifies a token using the configuration on the implementation module.
The default type of token for this module.
Encodes the claims.
Exchanges a token of one type for another.
Callback implementation for Guardian.on_exchange/3
.
An optional callback invoked when a token is refreshed.
An optional callback invoked when a token is revoked.
An optional callback invoked after the claims have been validated.
Provides the content of the token but without verification of either the claims or the signature.
Refresh a token.
Fetches the resource that is represented by claims.
For JWT this would normally be found in the sub
field.
Fetch the resource and claims directly from a token.
Revoke a token.
If Guardian.Plug.SlidingCookie is used, this callback will be invoked to return the new claims, or an error (which will mean the cookie will not be refreshed).
Fetches the subject for a token for the provided resource and claims The subject should be a short identifier that can be used to identify the resource.
Callback implementation for Guardian.verify_claims/2
.
Link to this section Functions
after_encode_and_sign(resource, claims, token, arg4)
An optional callback invoked after the token has been generated and signed.
after_sign_in(conn, r, t, c, o)
Callback implementation for Guardian.after_sign_in/5
.
before_sign_out(conn, location, opts)
Callback implementation for Guardian.before_sign_out/3
.
build_claims(c, _, _)
Callback implementation for Guardian.build_claims/3
.
config()
@spec config() :: Keyword.t()
Fetches the configuration for this module.
config(key, default \\ nil)
Returns a resolved value of the configuration found at a key.
decode_and_verify(token, claims_to_check \\ %{}, opts \\ [])
@spec decode_and_verify( Guardian.Token.token(), Guardian.Token.claims(), Guardian.options() ) :: {:ok, Guardian.Token.claims()} | {:error, any()}
Decodes and verifies a token using the configuration on the implementation module.
default_token_type()
@spec default_token_type() :: String.t()
The default type of token for this module.
encode_and_sign(resource, claims \\ %{}, opts \\ [])
@spec encode_and_sign(any(), Guardian.Token.claims(), Guardian.options()) :: {:ok, Guardian.Token.token(), Guardian.Token.claims()} | {:error, any()}
Encodes the claims.
See Guardian.encode_and_sign/4
for more information.
exchange(token, from_type, to_type, opts \\ [])
@spec exchange( token :: Guardian.Token.token(), from_type :: String.t() | [String.t(), ...], to_type :: String.t(), options :: Guardian.options() ) :: {:ok, {Guardian.Token.token(), Guardian.Token.claims()}, {Guardian.Token.token(), Guardian.Token.claims()}} | {:error, any()}
Exchanges a token of one type for another.
See Guardian.exchange
for more information.
on_exchange(old_stuff, new_stuff, options)
Callback implementation for Guardian.on_exchange/3
.
on_refresh(old_stuff, new_stuff, options)
An optional callback invoked when a token is refreshed.
on_revoke(claims, token, options)
An optional callback invoked when a token is revoked.
on_verify(claims, token, options)
An optional callback invoked after the claims have been validated.
peek(token)
Provides the content of the token but without verification of either the claims or the signature.
Claims will be present at the :claims
key.
See Guardian.peek/2
for more information.
refresh(old_token, opts \\ [])
@spec refresh(Guardian.Token.token(), Guardian.options()) :: {:ok, {Guardian.Token.token(), Guardian.Token.claims()}, {Guardian.Token.token(), Guardian.Token.claims()}} | {:error, any()}
Refresh a token.
See Guardian.refresh
for more information.
resource_from_claims(claims)
Fetches the resource that is represented by claims.
For JWT this would normally be found in the sub
field.
resource_from_token(token, claims_to_check \\ %{}, opts \\ [])
@spec resource_from_token( token :: Guardian.Token.token(), claims_to_check :: Guardian.Token.claims() | nil, opts :: Guardian.options() ) :: {:ok, Guardian.Token.resource(), Guardian.Token.claims()} | {:error, any()}
Fetch the resource and claims directly from a token.
See Guardian.resource_from_token
for more information.
revoke(token, opts \\ [])
@spec revoke(Guardian.Token.token(), Guardian.options()) :: {:ok, Guardian.Token.claims()} | {:error, any()}
Revoke a token.
See Guardian.revoke
for more information.
sliding_cookie(current_claims, current_resource, opts \\ [])
@spec sliding_cookie( current_claims :: Guardian.Token.claims(), current_resource :: Guardian.Token.resource(), options :: Guardian.options() ) :: {:ok, new_claims :: Guardian.Token.claims()} | {:error, any()}
If Guardian.Plug.SlidingCookie is used, this callback will be invoked to return the new claims, or an error (which will mean the cookie will not be refreshed).
subject_for_token(arg1, arg2)
Fetches the subject for a token for the provided resource and claims The subject should be a short identifier that can be used to identify the resource.
verify_claims(claims, options)
Callback implementation for Guardian.verify_claims/2
.