From a7f147c3b02bd9057c232baabc5651fb7d2bb37c Mon Sep 17 00:00:00 2001 From: Mike Cifelli <26522946+macifell@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:06:40 -0500 Subject: [PATCH 1/2] Authenticate newsletter endpoints --- config/dev.exs | 3 ++- config/releases.exs | 3 ++- config/test.exs | 3 ++- lib/recognizer/hal.ex | 20 +++++++++++++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/config/dev.exs b/config/dev.exs index 1c23a84..c00e8e2 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -1,7 +1,8 @@ import Config config :recognizer, - hal_url: "https://api-v2.genesis76.com" + hal_url: "https://api-v2.genesis76.com", + hal_token: "token" config :recognizer, Recognizer.Repo, username: "root", diff --git a/config/releases.exs b/config/releases.exs index b22c6dd..e92c5a9 100644 --- a/config/releases.exs +++ b/config/releases.exs @@ -7,7 +7,8 @@ recognizer_config = config :recognizer, redirect_url: recognizer_config["REDIRECT_URL"], - hal_url: recognizer_config["HAL_URL"] + hal_url: recognizer_config["HAL_URL"], + hal_token: recognizer_config["HAL_TOKEN"] config :recognizer, RecognizerWeb.Endpoint, url: [host: System.get_env("DOMAIN")], diff --git a/config/test.exs b/config/test.exs index 56e7abb..51484da 100644 --- a/config/test.exs +++ b/config/test.exs @@ -2,7 +2,8 @@ import Config config :recognizer, redis_host: System.get_env("REDIS_HOST", "localhost"), - hal_url: "https://api-v2.genesis76.com" + hal_url: "https://api-v2.genesis76.com", + hal_token: "token" config :argon2_elixir, t_cost: 1, diff --git a/lib/recognizer/hal.ex b/lib/recognizer/hal.ex index 0e9d397..19e5047 100644 --- a/lib/recognizer/hal.ex +++ b/lib/recognizer/hal.ex @@ -4,7 +4,11 @@ defmodule Recognizer.Hal do """ def update_newsletter(user) do - req = "/accounts/newsletter/interests" |> build_url() |> HTTPoison.get!() + req = + "/accounts/newsletter/interests" + |> build_url() + |> HTTPoison.get!(authentication_headers()) + interests = Jason.decode!(req.body)["interests"] groups = @@ -12,7 +16,11 @@ defmodule Recognizer.Hal do Map.put(acc, item["id"], String.to_existing_atom(user["newsletter"])) end) - req = "/accounts/newsletter?email_address=#{user["email"]}" |> build_url() |> HTTPoison.get!() + req = + "/accounts/newsletter?email_address=#{user["email"]}" + |> build_url() + |> HTTPoison.get!(authentication_headers()) + status = Jason.decode!(req.body)["status"] # only update if not already pending, or subscribed @@ -30,7 +38,9 @@ defmodule Recognizer.Hal do } |> Jason.encode!() - "/accounts/newsletter" |> build_url() |> HTTPoison.post!(body, %{"Content-type" => "application/json"}) + "/accounts/newsletter" + |> build_url() + |> HTTPoison.post!(body, [{"content-type", "application/json"}] ++ authentication_headers()) end end @@ -38,4 +48,8 @@ defmodule Recognizer.Hal do base_url = Application.get_env(:recognizer, :hal_url) Path.join([base_url, path]) end + + defp authentication_headers() do + [{"authentication", "Recognizer #{Application.get_env(:recognizer, :hal_token)}"}] + end end From 7c26f73fe77893fbccffe432eb2be25ba5eafe7f Mon Sep 17 00:00:00 2001 From: Mike Cifelli <26522946+macifell@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:33:57 -0500 Subject: [PATCH 2/2] Fix authorization header --- lib/recognizer/hal.ex | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/recognizer/hal.ex b/lib/recognizer/hal.ex index 19e5047..e4fc441 100644 --- a/lib/recognizer/hal.ex +++ b/lib/recognizer/hal.ex @@ -7,7 +7,7 @@ defmodule Recognizer.Hal do req = "/accounts/newsletter/interests" |> build_url() - |> HTTPoison.get!(authentication_headers()) + |> HTTPoison.get!(authorization_headers()) interests = Jason.decode!(req.body)["interests"] @@ -19,7 +19,7 @@ defmodule Recognizer.Hal do req = "/accounts/newsletter?email_address=#{user["email"]}" |> build_url() - |> HTTPoison.get!(authentication_headers()) + |> HTTPoison.get!(authorization_headers()) status = Jason.decode!(req.body)["status"] @@ -40,7 +40,7 @@ defmodule Recognizer.Hal do "/accounts/newsletter" |> build_url() - |> HTTPoison.post!(body, [{"content-type", "application/json"}] ++ authentication_headers()) + |> HTTPoison.post!(body, [{"content-type", "application/json"}] ++ authorization_headers()) end end @@ -49,7 +49,7 @@ defmodule Recognizer.Hal do Path.join([base_url, path]) end - defp authentication_headers() do - [{"authentication", "Recognizer #{Application.get_env(:recognizer, :hal_token)}"}] + defp authorization_headers() do + [{"authorization", "Recognizer #{Application.get_env(:recognizer, :hal_token)}"}] end end