Skip to content

Commit 1ddb100

Browse files
committed
feat(elixir:gsmlg): Add guardian.
1 parent 8b97a27 commit 1ddb100

File tree

6 files changed

+39
-1
lines changed

6 files changed

+39
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule GSMLG.Guardian do
2+
use Guardian, otp_app: :gsmlg
3+
4+
def subject_for_token(%{id: id}, _claims) do
5+
# You can use any value for the subject of your token but
6+
# it should be useful in retrieving the resource later, see
7+
# how it being used on `resource_from_claims/1` function.
8+
# A unique `id` is a good subject, a non-unique email address
9+
# is a poor subject.
10+
sub = to_string(id)
11+
{:ok, sub}
12+
end
13+
def subject_for_token(_, _) do
14+
{:error, :reason_for_error}
15+
end
16+
17+
def resource_from_claims(%{"sub" => id}) do
18+
# Here we'll look up our resource from the claims, the subject can be
19+
# found in the `"sub"` key. In above `subject_for_token/2` we returned
20+
# the resource id so here we'll rely on that to look it up.
21+
resource = GSMLG.Repo.get_resource_by_id(id)
22+
{:ok, resource}
23+
end
24+
def resource_from_claims(_claims) do
25+
{:error, :reason_for_error}
26+
end
27+
end
28+

elixir/gsmlg_umbrella/apps/gsmlg/mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ defmodule GSMLG.MixProject do
4545
{:horde, github: "fmbraga/horde", tag: "585b1a57d195f3c3b0fec5992b5eedee8bc1b7e8"}, # temperally fix, wait for replace by production release
4646
{:swoosh, "~> 1.3"},
4747
{:absinthe, "~> 1.6"},
48+
{:guardian, "~> 2.0"},
4849
]
4950
end
5051

elixir/gsmlg_umbrella/apps/gsmlg_web/lib/gsmlg_web/endpoint.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ defmodule GSMLGWeb.Endpoint do
4747
pass: ["*/*"],
4848
json_decoder: Phoenix.json_library()
4949

50+
plug Guardian.Plug.VerifyHeader, key: :impersonate
51+
plug Guardian.Plug.EnsureAuthenticated, key: :impersonate
52+
5053
plug Plug.MethodOverride
5154
plug Plug.Head
5255
plug Plug.Session, @session_options

elixir/gsmlg_umbrella/apps/gsmlg_web/lib/gsmlg_web/router.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule GSMLGWeb.Router do
1414
plug :accepts, ["json"]
1515
end
1616

17-
scope "/", GSMLGWeb do
17+
scope "/admin", GSMLGWeb do
1818
pipe_through :browser
1919

2020
get "/", PageController, :index

elixir/gsmlg_umbrella/config/config.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ config :gsmlg,
2323
# at the `config/runtime.exs`.
2424
config :gsmlg, GSMLG.Mailer, adapter: Swoosh.Adapters.Local
2525

26+
config :gsmlg, GSMLG.Guardian,
27+
issuer: "gsmlg",
28+
secret_key: "Secret key. You can use `mix guardian.gen.secret` to get one"
29+
2630
# Swoosh API client is needed for adapters other than SMTP.
2731
config :swoosh, :api_client, false
2832

elixir/gsmlg_umbrella/mix.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
1919
"floki": {:hex, :floki, "0.31.0", "f05ee8a8e6a3ced4e62beeb2c79a63bc8e12ab98fbaaf6e6a3d9b76b1278e23f", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "b05afa372f5c345a5bf240ac25ea1f0f3d5fcfd7490ac0beeb4a203f9444891e"},
2020
"gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"},
21+
"guardian": {:hex, :guardian, "2.2.1", "5a4a949fd46eac79ef37f074ada7d1ef82e274bc99e335c286e042f5383f4f80", [:mix], [{:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "09b5c4d08f18524bd33ffe49617003cbca9f617237e23b5f42223cda61c5f052"},
2122
"horde": {:git, "https://github.com/fmbraga/horde.git", "585b1a57d195f3c3b0fec5992b5eedee8bc1b7e8", [tag: "585b1a57d195f3c3b0fec5992b5eedee8bc1b7e8"]},
2223
"html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"},
2324
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
25+
"jose": {:hex, :jose, "1.11.2", "f4c018ccf4fdce22c71e44d471f15f723cb3efab5d909ab2ba202b5bf35557b3", [:mix, :rebar3], [], "hexpm", "98143fbc48d55f3a18daba82d34fe48959d44538e9697c08f34200fa5f0947d2"},
2426
"libcluster": {:hex, :libcluster, "3.3.0", "f7d45ff56d88e9fb4c30aee662480cbab69ebc0e7f7da4ad8d01b1e4f7492da8", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "ecdcdc88334ec8eb18b10a13a1d5f22a3319a970b5b1e66cfe71c7719a4ab6cc"},
2527
"libring": {:hex, :libring, "1.5.0", "44313eb6862f5c9168594a061e9d5f556a9819da7c6444706a9e2da533396d70", [:mix], [], "hexpm", "04e843d4fdcff49a62d8e03778d17c6cb2a03fe2d14020d3825a1761b55bd6cc"},
2628
"merkle_map": {:hex, :merkle_map, "0.2.1", "01a88c87a6b9fb594c67c17ebaf047ee55ffa34e74297aa583ed87148006c4c8", [:mix], [], "hexpm", "fed4d143a5c8166eee4fa2b49564f3c4eace9cb252f0a82c1613bba905b2d04d"},

0 commit comments

Comments
 (0)