-
Notifications
You must be signed in to change notification settings - Fork 86
/
token.ex
87 lines (71 loc) · 1.92 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
defmodule Guardian.DB.Token do
@moduledoc """
A very simple model for storing tokens generated by `Guardian`.
"""
use Ecto.Schema
import Ecto.Changeset
alias Guardian.DB.Token
@primary_key {:jti, :string, autogenerate: false}
@required_fields ~w(jti aud)a
@allowed_fields ~w(jti typ aud iss sub exp jwt claims)a
schema "virtual: token" do
field(:typ, :string)
field(:aud, :string)
field(:iss, :string)
field(:sub, :string)
field(:exp, :integer)
field(:jwt, :string)
field(:claims, :map)
timestamps()
end
@doc """
Find one token by matching jti and aud.
"""
def find_by_claims(claims) do
adapter().one(claims, config())
end
@doc """
Create a new token based on the JWT and decoded claims.
"""
def create(claims, jwt) do
adapter().insert(changeset(claims, jwt), config())
end
@doc false
def changeset(claims, jwt) do
prepared_claims =
claims
|> Map.put("jwt", jwt)
|> Map.put("claims", claims)
%Token{}
|> cast(prepared_claims, @allowed_fields)
|> validate_required(@required_fields)
end
@doc """
Purge any tokens that are expired. This should be done periodically to keep
your DB table clean of clutter.
"""
def purge_expired_tokens do
timestamp = Guardian.timestamp()
adapter().purge_expired_tokens(timestamp, config())
end
@doc false
def destroy_by_sub(sub) do
adapter().delete_by_sub(sub, config())
end
@doc false
defp config do
Application.fetch_env!(:guardian, Guardian.DB)
end
@doc false
def destroy_token(nil, claims, jwt), do: {:ok, {claims, jwt}}
def destroy_token(model, claims, jwt) do
case adapter().delete(model, config()) do
{:error, _} -> {:error, :could_not_revoke_token}
nil -> {:error, :could_not_revoke_token}
_ -> {:ok, {claims, jwt}}
end
end
defp adapter do
Keyword.get(config(), :adapter, Guardian.DB.EctoAdapter)
end
end