From 74839bb757958a9bb99081a14769c3766c49991a Mon Sep 17 00:00:00 2001 From: prplmg Date: Tue, 9 May 2023 15:04:10 -0600 Subject: [PATCH 1/8] added scope element to auth return --- lib/elixir_auth_github.ex | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/elixir_auth_github.ex b/lib/elixir_auth_github.ex index e018259..f3b358f 100644 --- a/lib/elixir_auth_github.ex +++ b/lib/elixir_auth_github.ex @@ -49,8 +49,8 @@ defmodule ElixirAuthGithub do """ def login_url(%{scopes: scopes, state: state}) do login_url() <> - "&scope=#{Enum.join(scopes, "%20")}" <> - "&state=#{state}" + "&scope=#{Enum.join(scopes, "%20")}" <> + "&state=#{state}" end def login_url(%{scopes: scopes}) do @@ -82,9 +82,11 @@ defmodule ElixirAuthGithub do |> check_authenticated end - defp check_authenticated(%{"access_token" => access_token}) do + defp check_authenticated(%{"access_token" => access_token, "scope" => scope}) do access_token |> get_user_details + |> Map.put(:scope, scope) + |> then(&{:ok, &1}) end defp check_authenticated(error), do: {:error, error} @@ -126,8 +128,7 @@ defmodule ElixirAuthGithub do # transform map with keys as strings into keys as atoms! # https://stackoverflow.com/questions/31990134 - atom_key_map = for {key, val} <- user, into: %{}, do: {String.to_atom(key), val} - {:ok, atom_key_map} + _atom_key_map = for {key, val} <- user, into: %{}, do: {String.to_atom(key), val} end defp set_user_details(error, _token), do: {:error, error} From a69c399f4722ed683ef49a7b8c8f0ec1f6ceea26 Mon Sep 17 00:00:00 2001 From: prplmg Date: Tue, 9 May 2023 15:06:22 -0600 Subject: [PATCH 2/8] formatting fixes --- lib/elixir_auth_github.ex | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/elixir_auth_github.ex b/lib/elixir_auth_github.ex index f3b358f..e018259 100644 --- a/lib/elixir_auth_github.ex +++ b/lib/elixir_auth_github.ex @@ -49,8 +49,8 @@ defmodule ElixirAuthGithub do """ def login_url(%{scopes: scopes, state: state}) do login_url() <> - "&scope=#{Enum.join(scopes, "%20")}" <> - "&state=#{state}" + "&scope=#{Enum.join(scopes, "%20")}" <> + "&state=#{state}" end def login_url(%{scopes: scopes}) do @@ -82,11 +82,9 @@ defmodule ElixirAuthGithub do |> check_authenticated end - defp check_authenticated(%{"access_token" => access_token, "scope" => scope}) do + defp check_authenticated(%{"access_token" => access_token}) do access_token |> get_user_details - |> Map.put(:scope, scope) - |> then(&{:ok, &1}) end defp check_authenticated(error), do: {:error, error} @@ -128,7 +126,8 @@ defmodule ElixirAuthGithub do # transform map with keys as strings into keys as atoms! # https://stackoverflow.com/questions/31990134 - _atom_key_map = for {key, val} <- user, into: %{}, do: {String.to_atom(key), val} + atom_key_map = for {key, val} <- user, into: %{}, do: {String.to_atom(key), val} + {:ok, atom_key_map} end defp set_user_details(error, _token), do: {:error, error} From 91dc85d9cd4e7ccfbbe06b8792e8325cf4c7e3e5 Mon Sep 17 00:00:00 2001 From: prplmg Date: Tue, 9 May 2023 15:13:19 -0600 Subject: [PATCH 3/8] formatting --- lib/elixir_auth_github.ex | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/elixir_auth_github.ex b/lib/elixir_auth_github.ex index e018259..9c2cbc9 100644 --- a/lib/elixir_auth_github.ex +++ b/lib/elixir_auth_github.ex @@ -82,9 +82,11 @@ defmodule ElixirAuthGithub do |> check_authenticated end - defp check_authenticated(%{"access_token" => access_token}) do + defp check_authenticated(%{"access_token" => access_token, "scope" => scope}) do access_token |> get_user_details + |> Map.put(:scope, scope) + |> then(&{:ok, &1}) end defp check_authenticated(error), do: {:error, error} @@ -126,8 +128,7 @@ defmodule ElixirAuthGithub do # transform map with keys as strings into keys as atoms! # https://stackoverflow.com/questions/31990134 - atom_key_map = for {key, val} <- user, into: %{}, do: {String.to_atom(key), val} - {:ok, atom_key_map} + _atom_key_map = for {key, val} <- user, into: %{}, do: {String.to_atom(key), val} end defp set_user_details(error, _token), do: {:error, error} From 7699bc22367d8d3cbc035070328b1290013ca759 Mon Sep 17 00:00:00 2001 From: prplmg Date: Thu, 11 May 2023 04:11:54 -0600 Subject: [PATCH 4/8] abstracted scope assignment to its own function added scope to mock requests --- lib/elixir_auth_github.ex | 12 ++++++++++-- lib/httpoison_mock.ex | 13 +++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/elixir_auth_github.ex b/lib/elixir_auth_github.ex index 9c2cbc9..12616cb 100644 --- a/lib/elixir_auth_github.ex +++ b/lib/elixir_auth_github.ex @@ -85,11 +85,18 @@ defmodule ElixirAuthGithub do defp check_authenticated(%{"access_token" => access_token, "scope" => scope}) do access_token |> get_user_details + |> set_scope(scope) + end + + defp check_authenticated(error), do: {:error, error} + + defp set_scope({:ok, user_details}, scope) do + user_details |> Map.put(:scope, scope) |> then(&{:ok, &1}) end - defp check_authenticated(error), do: {:error, error} + defp set_scope({:error, error}, _scope), do: {:error, error} defp get_user_details(access_token) do inject_poison().get!("https://api.github.com/user", [ @@ -128,7 +135,8 @@ defmodule ElixirAuthGithub do # transform map with keys as strings into keys as atoms! # https://stackoverflow.com/questions/31990134 - _atom_key_map = for {key, val} <- user, into: %{}, do: {String.to_atom(key), val} + atom_key_map = for {key, val} <- user, into: %{}, do: {String.to_atom(key), val} + {:ok, atom_key_map} end defp set_user_details(error, _token), do: {:error, error} diff --git a/lib/httpoison_mock.ex b/lib/httpoison_mock.ex index 95056f7..8d17c3b 100644 --- a/lib/httpoison_mock.ex +++ b/lib/httpoison_mock.ex @@ -103,7 +103,7 @@ defmodule ElixirAuthGithub.HTTPoisonMock do _headers, _options ) do - %{body: "access_token=123"} + %{body: "access_token=123&scope=user"} end def post!( @@ -112,7 +112,16 @@ defmodule ElixirAuthGithub.HTTPoisonMock do _headers, _options ) do - %{body: "access_token=42"} + %{body: "access_token=42&scope=user"} + end + + def post!( + "https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=12345", + _body, + _headers, + _options + ) do + %{body: "access_token=12345&scope=user"} end # for some reason GitHub's Post returns a URI encoded string From 86b347dbc5c12a869c017e3ecace5aa832ffd257 Mon Sep 17 00:00:00 2001 From: prplmg Date: Thu, 11 May 2023 04:26:43 -0600 Subject: [PATCH 5/8] removed unused `post!` fallback mock --- lib/httpoison_mock.ex | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/httpoison_mock.ex b/lib/httpoison_mock.ex index 8d17c3b..ea0cd6b 100644 --- a/lib/httpoison_mock.ex +++ b/lib/httpoison_mock.ex @@ -123,9 +123,4 @@ defmodule ElixirAuthGithub.HTTPoisonMock do ) do %{body: "access_token=12345&scope=user"} end - - # for some reason GitHub's Post returns a URI encoded string - def post!(_url, _body, _headers, _options) do - %{body: URI.encode_query(@valid_body)} - end end From a232ecbd3d5360eee5aa3c14159cb96e0ebbff6d Mon Sep 17 00:00:00 2001 From: prplmg Date: Thu, 11 May 2023 05:34:49 -0600 Subject: [PATCH 6/8] documented new key in readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 54e310f..bedaa4d 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,8 @@ is the following format: id: 194400, following: 173, login: "nelsonic", - collaborators: 8 + collaborators: 8, + scope: "repo,user:email" } ``` From a82e730af75c1acb43c9bd743f7baf4c1caba42a Mon Sep 17 00:00:00 2001 From: prplmg Date: Thu, 11 May 2023 05:43:42 -0600 Subject: [PATCH 7/8] added explicit test case --- test/elixir_auth_github_test.exs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/elixir_auth_github_test.exs b/test/elixir_auth_github_test.exs index 031279d..c3c568a 100644 --- a/test/elixir_auth_github_test.exs +++ b/test/elixir_auth_github_test.exs @@ -53,6 +53,13 @@ defmodule ElixirAuthGithubTest do assert res.id == "19" end + test "github_auth returns scope string" do + setup_test_environment_variables() + {:ok, res} = ElixirAuthGithub.github_auth("12345") + assert res.scope == "user" + assert res.id == "19" + end + test "github_auth returns an error with a bad code" do setup_test_environment_variables() assert ElixirAuthGithub.github_auth("1234") == From f809bbe6285a47abce2f2469cf7c468b81f2119a Mon Sep 17 00:00:00 2001 From: prplmg Date: Thu, 11 May 2023 05:47:50 -0600 Subject: [PATCH 8/8] removed unnecessary line --- test/elixir_auth_github_test.exs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/elixir_auth_github_test.exs b/test/elixir_auth_github_test.exs index c3c568a..434e127 100644 --- a/test/elixir_auth_github_test.exs +++ b/test/elixir_auth_github_test.exs @@ -57,7 +57,6 @@ defmodule ElixirAuthGithubTest do setup_test_environment_variables() {:ok, res} = ElixirAuthGithub.github_auth("12345") assert res.scope == "user" - assert res.id == "19" end test "github_auth returns an error with a bad code" do