Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ is the following format:
id: 194400,
following: 173,
login: "nelsonic",
collaborators: 8
collaborators: 8,
scope: "repo,user:email"
}
```

Expand Down
11 changes: 10 additions & 1 deletion lib/elixir_auth_github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,22 @@ 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
|> set_scope(scope)
end

defp check_authenticated(error), do: {:error, error}

defp set_scope({:ok, user_details}, scope) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would actually make this a def instead of defp so that it could be tested independently
to make sure that it degrades gracefully. e.g. if scope is nil does it still work?

Looks like you're handling the case of _scope below but it's not immediately obvious. 💭

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not possible for just scope to be nil, as that would mean access_token is also nil and check_authenticated/1 would fail, with set_scope/2 forwarding the error.

user_details
|> Map.put(:scope, scope)
|> then(&{:ok, &1})
end

defp set_scope({:error, error}, _scope), do: {:error, error}

defp get_user_details(access_token) do
inject_poison().get!("https://api.github.com/user", [
#  https://developer.github.com/v3/#user-agent-required
Expand Down
14 changes: 9 additions & 5 deletions lib/httpoison_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ defmodule ElixirAuthGithub.HTTPoisonMock do
_headers,
_options
) do
%{body: "access_token=123"}
%{body: "access_token=123&scope=user"}
end

def post!(
Expand All @@ -112,11 +112,15 @@ defmodule ElixirAuthGithub.HTTPoisonMock do
_headers,
_options
) do
%{body: "access_token=42"}
%{body: "access_token=42&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)}
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
end
6 changes: 6 additions & 0 deletions test/elixir_auth_github_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ 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"
end

test "github_auth returns an error with a bad code" do
setup_test_environment_variables()
assert ElixirAuthGithub.github_auth("1234") ==
Expand Down