Guardian v1.0.0-beta.0 Guardian.Token.Jwt View Source
Deals with things JWT This module should not be used directly. It is intended to be used by Guardian on behalf of your implementation as it’s token module.
Token types are encoded in the typ field.
Configuration
Configuration should be added to the implementation module
in either the configuration file or as options to use Guardian
Required
issuer- The issuer of the token. Your application name/idsecret_key- The secret key to use for the implementation module. This may be any resolvable value forGuardian.Config
Optional
token_verify_module- defaultGuardian.Token.Jwt.Verify. The module that verifies the claimsallowed_algos- The allowed algos to use for encoding and decoding. See JOSE for available. Default [“HS512”]ttl- The default time to live for all tokens. See the type in Guardian.ttltoken_ttla map oftoken_typetottl. Set specific ttls for specific types of tokensallowed_driftThe drift that is allowed when decoding/verifying a token in milli secondsverify_issuerVerify that the token was issued by the configured issuer. Default false
Options:
These options are available to encoding and decoding:
secretThe secret key to use for signingheadersThe Jose headers that should be usedallowed_algostoken_type- Override the default token typetoken_ttl- The time to live. SeeGuardian.Token.ttltype
Example
# encode a simple token
{:ok, token, claims} =
MyApp.Tokens.encode_and_sign(resource)
# encode a token with custom claims
{:ok, token, claims} =
MyApp.Tokens.encode_and_sign(resource, %{some: "claim"})
# encode a token with a custom type
{:ok, token, claims} =
MyApp.Tokens.encode_and_sign(resource, %{}, token_type: "refresh")
# encode a token with custom options
{:ok, token, claims} =
MyApp.Tokens.encode_and_sign(
resource,
%{},
secret: {MyModule, :get_my_secret},
token_ttl: {4, :weeks},
token_type: "refresh"
)
# decode a token
{:ok, claims} =
MyApp.Tokens.decode_and_verify(token)
# decode a token and check literal claims
{:ok, claims} =
MyApp.Tokens.decode_and_verify(token, %{"typ" => "refresh"})
# decode a token and check literal claims with options
{:ok, claims} =
MyApp.Tokens.decode_and_verify(token, %{"typ" => "refresh"} secret: {MyModule, :get_my_secret})
# exchange a token
{:ok, {old_token, old_claims}, {new_token, new_claims}} =
MyApp.Tokens.exchange(old_token, ["access", "refresh"], "access")
# exchange a token with options
{:ok, {old_token, old_claims}, {new_token, new_claims}} =
MyApp.Tokens.exchange(old_token, ["access", "refresh"], "access" secret: {MyModule, :get_my_secret}, token_ttl: {1, :hour})
# refresh a token using defaults
{:ok, {old_token, old_claims}, {new_token, new_claims}} = MyApp.Tokens.refresh(old_token)
# refresh a token using options
{:ok, {old_token, old_claims}, {new_token, new_claims}} =
MyApp.Tokens.refresh(old_token, token_ttl: {1, :week}, secret: {MyMod, :get_secret})
Token verify module
The token verify module by default is Guardian.Token.Jwt.Verify.
This module implements the Guardian.Token.Verify behaviour.
To customize your token validation you have 2 options.
- Implement the
verify_claimscallback on your implementation use Guardian.Token.Verifyin your own module and use that.
To create your own verify module use Guardian.Token.Verify and configure
your implementation to use it either through config files or when you setup your implementation.
defmodule MyApp.Tokens do
use Guardian, otp_app: :my_app,
token_verify_module: MyVerifyModule
# ... snip
end
Link to this section Summary
Functions
Builds the default claims for all JWT tokens
Create a token. Uses the claims, encodes and signs the token
Decodes the token and validates the signature
Exchange a token of one type to another
Inspect the JWT without any validation or signature checking
Refresh the token
Revoking a JWT by default does not do anything.
You’ll need to track the token in storage in some way
and revoke in your implementation callbacks.
See GuardianDb for an example
Generate unique token id
Verifies the claims
Link to this section Functions
Builds the default claims for all JWT tokens.
Note:
audis set to the configuredissuerunlessaudis set
Options:
Options may override the defaults found in the configuration.
token_type- Override the default token typetoken_ttl- The time to live. SeeGuardian.Token.ttltype
Create a token. Uses the claims, encodes and signs the token.
The signing secret will be found first from the options. If not specified the secret key from the configuration will be used.
Configuration:
secret_keyThe secret key to use for signing
Options:
secretThe secret key to use for signingheadersThe Jose headers that should be usedallowed_algos
The secret may be in the form of any resolved value from Guardian.Config
Decodes the token and validates the signature.
Options:
secret- Override the configured secret.Guardian.Config.config_valueis validallowed_algos- a list of allowable algos
Exchange a token of one type to another.
Type is encoded in the typ field.
Options:
secret- Override the configured secret.Guardian.Config.config_valueis validallowed_algos- a list of allowable algostoken_ttl- The time to live. SeeGuardian.Token.ttltype
Inspect the JWT without any validation or signature checking.
Return an map with keys: headers and claims
Refresh the token
Options:
secret- Override the configured secret.Guardian.Config.config_valueis validallowed_algos- a list of allowable algostoken_ttl- The time to live. SeeGuardian.Token.ttltype
Revoking a JWT by default does not do anything.
You’ll need to track the token in storage in some way
and revoke in your implementation callbacks.
See GuardianDb for an example.
Generate unique token id
Verifies the claims.
Configuration:
token_verify_moduleDefaultGuardian.Token.Jwt.Verifythe module to use to verify the claims