-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoken.ex
49 lines (42 loc) · 1.14 KB
/
token.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
defmodule AuthPlug.Token do
@moduledoc """
Token module to create and validate jwt.
see https://hexdocs.pm/joken/configuration.html#module-approach
"""
use Joken.Config
@secret System.get_env("SECRET_KEY_BASE")
@signer Joken.Signer.create("HS256", @secret)
@impl true
def token_config do
# ~ 1 year in seconds
default_claims(default_exp: 31_537_000)
end
@doc """
`generate_jwt!/1` invokes `Joken.generate_and_sign/3`
claims are the data to be signed.
"""
def generate_jwt!(claims) do
{:ok, token, _claims} =
token_config()
|> Joken.generate_and_sign(claims, @signer)
token
end
@doc """
`verify_jwt/1` verifies the given JWT and returns {:ok, claims}
where the claims are the original data that were signed.
"""
def verify_jwt(token) do
token_config()
|> Joken.verify_and_validate(token, @signer)
end
@doc """
`verify_jwt!/1` verifies the given JWT and returns claims
where the claims are the original data that were signed.
"""
def verify_jwt!(token) do
{:ok, claims} =
token_config()
|> Joken.verify_and_validate(token, @signer)
claims
end
end