-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_security_context.ex
48 lines (39 loc) · 1.29 KB
/
create_security_context.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
if Code.ensure_loaded?(Absinthe.Plug) do
defmodule PrimaAuth0Ex.Absinthe.CreateSecurityContext do
@moduledoc """
Plug that reads the permissions from the JWT passed in the `Authorization` header and stores them in the Absinthe context,
so that they can be accessed by the `PrimaAuth0Ex.Absinthe.RequirePermissions` middleware.
It does not validate the token! You should use the `PrimaAuth0Ex.Plug.VerifyAndValidateToken` plug to do that.
"""
alias PrimaAuth0Ex.Config
defmodule Auth0 do
@moduledoc false
@type t :: %__MODULE__{
dry_run: boolean(),
permissions: [String.t()] | nil
}
defstruct dry_run: false,
permissions: nil
end
@behaviour Plug
@impl true
def init(opts) do
Keyword.merge([dry_run: dry_run()], opts)
end
@impl true
def call(conn, dry_run: dry_run) do
permissions =
case Plug.Conn.get_req_header(conn, "authorization") do
["Bearer " <> token] -> PrimaAuth0Ex.Token.peek_permissions(token)
[] -> nil
end
Absinthe.Plug.assign_context(conn,
auth0: %Auth0{
permissions: permissions,
dry_run: dry_run
}
)
end
defp dry_run, do: Config.server(:dry_run, false)
end
end