-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
session.ex
162 lines (128 loc) · 5.05 KB
/
session.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
defmodule Pow.Plug.Session do
@moduledoc """
This plug will handle user authorization using session.
## Example
plug Plug.Session,
store: :cookie,
key: "_my_app_demo_key",
signing_salt: "secret"
plug Pow.Plug.Session,
repo: MyApp.Repo,
user: MyApp.User,
current_user_assigns_key: :current_user,
session_key: "auth",
session_store: {Pow.Store.CredentialsCache,
ttl: :timer.minutes(30),
namespace: "credentials"},
session_ttl_renewal: :timer.minutes(15),
cache_store_backend: Pow.Store.Backend.EtsCache,
users_context: Pow.Ecto.Users
## Configuration options
* `:session_key` - session key name, defaults to "auth". If `:otp_app` is
used it'll automatically prepend the key with the `:otp_app` value.
* `:session_store` - the credentials cache store. This value defaults to
`{CredentialsCache, backend: EtsCache}`. The `EtsCache` backend store
can be changed with the `:cache_store_backend` option.
* `:cache_store_backend` - the backend cache store. This value defaults to
`EtsCache`.
* `:session_ttl_renewal` - the ttl in milliseconds to trigger renewal of
sessions. Defaults to 15 minutes in miliseconds.
"""
use Pow.Plug.Base
alias Plug.Conn
alias Pow.{Config, Plug, Store.Backend.EtsCache, Store.CredentialsCache, UUID}
@session_key "auth"
@session_ttl_renewal :timer.minutes(15)
@doc """
Fetches session from credentials cache.
This will fetch a session from the credentials cache with the session id
fetched through `Plug.Conn.get_session/2` session. If the credentials are
stale (timestamp is older than the `:session_ttl_renewal` value), the session
will be regenerated with `create/3`.
"""
@spec fetch(Conn.t(), Config.t()) :: {Conn.t(), map() | nil}
def fetch(conn, config) do
conn = Conn.fetch_session(conn)
key = Conn.get_session(conn, session_key(config))
{store, store_config} = store(config)
store_config
|> store.get(key)
|> handled_fetched_value(conn, config)
end
@doc """
Create new session with a randomly generated unique session id.
This will store the unique session id with user credentials in the
credentials cache. The session id will be stored in the connection with
`Plug.Conn.put_session/3`. Any existing sessions will be deleted first with
`delete/2`.
The unique session id will be prepended by the `:otp_app` configuration
value, if present.
"""
@spec create(Conn.t(), map(), Config.t()) :: {Conn.t(), map()}
def create(conn, user, config) do
conn = Conn.fetch_session(conn)
key = session_id(config)
session_key = session_key(config)
{store, store_config} = store(config)
value = session_value(user)
conn = delete(conn, config)
store.put(store_config, key, value)
conn =
conn
|> delete(config)
|> Conn.put_session(session_key, key)
{conn, user}
end
@doc """
Delete an existing session in the credentials cache.
This will delete a session in the credentials cache with the session id
fetched through `Plug.Conn.get_session/2`. The session in the connection is
deleted too with `Plug.Conn.delete_session/2`.
"""
@spec delete(Conn.t(), Config.t()) :: Conn.t()
def delete(conn, config) do
conn = Conn.fetch_session(conn)
key = Conn.get_session(conn, session_key(config))
{store, store_config} = store(config)
session_key = session_key(config)
store.delete(store_config, key)
Conn.delete_session(conn, session_key)
end
defp handled_fetched_value(:not_found, conn, _config), do: {conn, nil}
defp handled_fetched_value({user, inserted_at}, conn, config) do
case session_stale?(inserted_at, config) do
true -> create(conn, user, config)
false -> {conn, user}
end
end
defp session_stale?(inserted_at, config) do
ttl = Config.get(config, :session_ttl_renewal, @session_ttl_renewal)
session_stale?(inserted_at, config, ttl)
end
defp session_stale?(_inserted_at, _config, nil), do: false
defp session_stale?(inserted_at, _config, ttl) do
inserted_at + ttl < timestamp()
end
defp session_id(config) do
uuid = UUID.generate()
Plug.prepend_with_namespace(config, uuid)
end
defp session_key(config) do
Config.get(config, :session_key, default_session_key(config))
end
defp default_session_key(config) do
Plug.prepend_with_namespace(config, @session_key)
end
defp session_value(user), do: {user, timestamp()}
defp store(config) do
case Config.get(config, :session_store, default_store(config)) do
{store, store_config} -> {store, store_config}
store -> {store, []}
end
end
defp default_store(config) do
backend = Config.get(config, :cache_store_backend, EtsCache)
{CredentialsCache, [backend: backend]}
end
defp timestamp, do: :os.system_time(:millisecond)
end