Guardian
A module that provides JWT based authentication for Elixir applications.
Guardian provides the framework for using JWT any elixir application, web based or otherwise, Where authentication is required.
The base unit of authentication currency is implemented using JWTs.
Configuration
config :guardian, Guardian,
issuer: "MyApp",
ttl: { 30, :days },
serializer: MyApp.GuardianSerializer,
secret_key: "lksjdlkjsdflkjsdf"
Guardian uses Joken, so you will also need to configure that.
Summary
decode_and_verify!(jwt) | If successfully verified, returns the claims encoded into the JWT. Raises otherwise |
decode_and_verify!(jwt, params) | If successfully verified, returns the claims encoded into the JWT. Raises otherwise |
decode_and_verify(jwt) | Verify the given JWT. This will decode_and_verify via decode_and_verify/2 |
decode_and_verify(jwt, params) | Verify the given JWT |
encode_and_sign(object) | Encode and sign a JWT from a resource. The resource will be run through the configured serializer to obtain a value suitable for storage inside a JWT |
encode_and_sign(object, audience) | Like encode_and_sign/1 but also accepts the audience (encoded to the aud key) for the JWT |
encode_and_sign(object, audience, claims) | Like encode_and_sign/2 but also encode anything found inside the claims map into the JWT |
issuer() | The configured issuer. If not configured, defaults to the node that issued |
revoke!(jwt) | Revokes the current token. This provides a hook to revoke, the logic for revocation of belongs in a Guardian.Hook.on_revoke This function is less efficient that revoke!/2. If you have claims, you should use that |
revoke!(jwt, claims) | Revokes the current token. This provides a hook to revoke, the logic for revocation of belongs in a Guardian.Hook.on_revoke |
serializer() | Fetch the configured serializer module |
Functions
Specs:
Verify the given JWT. This will decode_and_verify via decode_and_verify/2
Specs:
Verify the given JWT.
Specs:
- decode_and_verify!(String.t) :: Map
If successfully verified, returns the claims encoded into the JWT. Raises otherwise
Specs:
- decode_and_verify!(String.t, Map) :: Map
If successfully verified, returns the claims encoded into the JWT. Raises otherwise
Specs:
Encode and sign a JWT from a resource. The resource will be run through the configured serializer to obtain a value suitable for storage inside a JWT.
Specs:
Like encode_and_sign/1 but also accepts the audience (encoded to the aud key) for the JWT
The aud can be anything but suggested is “token”.
Specs:
- encode_and_sign(any, atom | String.t, Map) :: {:ok, String.t, Map} | {:error, atom} | {:error, String.t}
Like encode_and_sign/2 but also encode anything found inside the claims map into the JWT.
To encode permissions into the token, use the :perms
key and pass it a map with the relevant permissions (must be configured)
Example
Guardian.encode_and_sign(user, :token, perms: %{ default: [:read, :write] })
Specs:
- issuer :: String.t
The configured issuer. If not configured, defaults to the node that issued.
Revokes the current token. This provides a hook to revoke, the logic for revocation of belongs in a Guardian.Hook.on_revoke This function is less efficient that revoke!/2. If you have claims, you should use that.
Revokes the current token. This provides a hook to revoke, the logic for revocation of belongs in a Guardian.Hook.on_revoke
Specs:
- serializer :: Module.t
Fetch the configured serializer module