Guardian.Token.Jwt (Guardian v2.4.0)
View SourceDeals with things JWT. This module should not be used directly.
It is intended to be used by Guardian on behalf of your implementation as its 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.ttl. The default byGuardian.Token.JWTis{4, :weeks}token_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 falsesecret_fetcherA module used to fetch the secret. Default:Guardian.Token.Jwt.SecretFetcher
Options:
These options are available to encoding and decoding:
secretThe secret key to use for signingheadersThe Jose headers that should be usedallowed_algos- A list of allowable algostoken_type- Override the default token type. The default is "access"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, ["some", "args"]},
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, ["some", "args"]})
# 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, ["some", "args"]}, 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, ttl: {1, :week}, secret: {MyMod, :get_secret, ["some", "args"})
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
endSecretFetcher
When you need dynamic secret verification, you should use a custom
Guardian.Token.Jwt.SecretFetcher module.
This will allow you to use the header values to determine dynamically the key that should be used.
defmodule MyCustomSecretFetcher do
use Guardian.Token.Jwt.SecretFetcher
def fetch_signing_secret(impl_module, opts) do
# fetch the secret for signing
end
def fetch_verifying_secret(impl_module, token_headers, opts) do
# fetch the secret for verifying the token
end
endIf the signing secret contains a "kid" (https://tools.ietf.org/html/rfc7515#section-4.1.4) it will be passed along to the signature to provide a hint about which secret was used.
This can be useful for specifying which public key to use during verification if you're using a public/private key rotation strategy.
An example implementation of this can be found here: https://gist.github.com/mpinkston/469009001b694d3ca162894d74c9bfe3
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.
Generate unique token id.
Verifies the claims.
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 typettl- 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 algosttl- 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 algosttl- 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.
Options:
token_verify_module- the module to use to verify the claims. DefaultGuardian.Token.Jwt.Verify