From d7d931c42da0df9cd5d3b1578804d28806983e83 Mon Sep 17 00:00:00 2001 From: Hunter Jarrell Date: Wed, 16 Oct 2019 16:21:25 -0400 Subject: [PATCH] Add dependencies for rewrite. --- config/config.exs | 5 ++++ lib/ask_a_gator/guardian.ex | 30 +++++++++++++++++++ lib/ask_a_gator_web/plugs/set_current_user.ex | 25 ++++++++++++++++ mix.exs | 7 +++-- mix.lock | 3 ++ 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 lib/ask_a_gator/guardian.ex create mode 100644 lib/ask_a_gator_web/plugs/set_current_user.ex diff --git a/config/config.exs b/config/config.exs index 95b0415..15c6ef6 100644 --- a/config/config.exs +++ b/config/config.exs @@ -18,6 +18,11 @@ config :ask_a_gator, AskAGatorWeb.Endpoint, pubsub: [name: AskAGator.PubSub, adapter: Phoenix.PubSub.PG2], live_view: [signing_salt: "s3rSG2r10yr6Y4DFCAZAjt1JzJTjp83j"] +# Configures the Guardian JWT +config :ask_a_gator, AskAGator.Guardian, + issuer: "ask_a_gator", + secret_key: "anXVSCh81v7tKCs/ckuXVnkOKZXAzuIOZudPJaxisCZx5bPBVrvjT6SuzTeRLL/M" + # Configures Elixir's Logger config :logger, :console, format: "$time $metadata[$level] $message\n", diff --git a/lib/ask_a_gator/guardian.ex b/lib/ask_a_gator/guardian.ex new file mode 100644 index 0000000..e1898ca --- /dev/null +++ b/lib/ask_a_gator/guardian.ex @@ -0,0 +1,30 @@ +defmodule AskAGator.Guardian do + use Guardian, otp_app: :ask_a_gator + + alias AskAGator.Accounts.User + + def subject_for_token(%User{ id: id }, _claims) do + # You can use any value for the subject of your token but + # it should be useful in retrieving the resource later, see + # how it being used on `resource_from_claims/1` function. + # A unique `id` is a good subject, a non-unique email address + # is a poor subject. + sub = to_string(id) + {:ok, sub} + end + def subject_for_token(_, _) do + {:error, :reason_for_error} + end + + def resource_from_claims(claims) do + # Here we'll look up our resource from the claims, the subject can be + # found in the `"sub"` key. In `above subject_for_token/2` we returned + # the resource id so here we'll rely on that to look it up. + id = claims["sub"] + resource = AskAGator.Accounts.get_user!(id) + {:ok, resource} + end + # def resource_from_claims(_claims) do + # {:error, :reason_for_error} + # end +end diff --git a/lib/ask_a_gator_web/plugs/set_current_user.ex b/lib/ask_a_gator_web/plugs/set_current_user.ex new file mode 100644 index 0000000..daa0c56 --- /dev/null +++ b/lib/ask_a_gator_web/plugs/set_current_user.ex @@ -0,0 +1,25 @@ +defmodule AskAGatorWeb.Plugs.SetCurrentUser do + import Plug.Conn + + alias AskAGator.Repo + alias AskAGator.Accounts.User + + def init(_params) do + end + + def call(conn, _params) do + user_id = Plug.Conn.get_session(conn, :current_user) + + cond do + current_user = user_id && Repo.get(User, user_id) -> + conn + |> assign(:current_user, current_user) + |> assign(:user_signed_in?, true) + true -> + conn + |> assign(:current_user, nil) + |> assign(:user_signed_in?, false) + end + end + +end diff --git a/mix.exs b/mix.exs index b500298..06c7c83 100644 --- a/mix.exs +++ b/mix.exs @@ -33,10 +33,10 @@ defmodule AskAGator.MixProject do # Type `mix help deps` for examples and options. defp deps do [ - {:phoenix, "~> 1.4.5"}, + {:phoenix, "~> 1.4.10"}, {:phoenix_pubsub, "~> 1.1"}, {:phoenix_ecto, "~> 4.0"}, - {:ecto_sql, "~> 3.0"}, + {:ecto_sql, "~> 3.1"}, {:postgrex, ">= 0.0.0"}, {:phoenix_html, "~> 2.11"}, {:phoenix_live_reload, "~> 1.2", only: :dev}, @@ -45,7 +45,8 @@ defmodule AskAGator.MixProject do {:plug_cowboy, "~> 2.0"}, {:phoenix_live_view, "~> 0.3.0"}, {:floki, ">= 0.0.0", only: :test}, - {:bcrypt_elixir, "~> 2.0"} + {:bcrypt_elixir, "~> 2.0"}, + {:guardian, "~> 1.2"} ] end diff --git a/mix.lock b/mix.lock index 7080bd2..43e2345 100644 --- a/mix.lock +++ b/mix.lock @@ -1,4 +1,5 @@ %{ + "base64url": {:hex, :base64url, "0.0.1", "36a90125f5948e3afd7be97662a1504b934dd5dac78451ca6e9abf85a10286be", [:rebar], [], "hexpm"}, "bcrypt_elixir": {:hex, :bcrypt_elixir, "2.0.3", "64e0792d5b5064391927bf3b8e436994cafd18ca2d2b76dea5c76e0adcf66b7c", [:make, :mix], [{:comeonin, "~> 5.1", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"}, "comeonin": {:hex, :comeonin, "5.1.2", "fbbbbbfcf0f0e9900c0336d16c8d462edf838ba1759577e29cc5fbd7c28a4540", [:mix], [], "hexpm"}, "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"}, @@ -12,8 +13,10 @@ "file_system": {:hex, :file_system, "0.2.7", "e6f7f155970975789f26e77b8b8d8ab084c59844d8ecfaf58cbda31c494d14aa", [:mix], [], "hexpm"}, "floki": {:hex, :floki, "0.23.0", "956ab6dba828c96e732454809fb0bd8d43ce0979b75f34de6322e73d4c917829", [:mix], [{:html_entities, "~> 0.4.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm"}, "gettext": {:hex, :gettext, "0.17.0", "abe21542c831887a2b16f4c94556db9c421ab301aee417b7c4fbde7fbdbe01ec", [:mix], [], "hexpm"}, + "guardian": {:hex, :guardian, "1.2.1", "bdc8dd3dbf0fb7216cb6f91c11831faa1a64d39cdaed9a611e37f2413e584983", [:mix], [{:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.3", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm"}, "html_entities": {:hex, :html_entities, "0.4.0", "f2fee876858cf6aaa9db608820a3209e45a087c5177332799592142b50e89a6b", [:mix], [], "hexpm"}, "jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"}, + "jose": {:hex, :jose, "1.9.0", "4167c5f6d06ffaebffd15cdb8da61a108445ef5e85ab8f5a7ad926fdf3ada154", [:mix, :rebar3], [{:base64url, "~> 0.0.1", [hex: :base64url, repo: "hexpm", optional: false]}], "hexpm"}, "mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"}, "phoenix": {:hex, :phoenix, "1.4.10", "619e4a545505f562cd294df52294372d012823f4fd9d34a6657a8b242898c255", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.8.1 or ~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"}, "phoenix_ecto": {:hex, :phoenix_ecto, "4.0.0", "c43117a136e7399ea04ecaac73f8f23ee0ffe3e07acfcb8062fe5f4c9f0f6531", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},