Skip to content

Commit fe64532

Browse files
authored
Merge pull request #12 from danschultzer/bug-fix-missing-access-token
Bug fix missing access token
2 parents 2904c6d + e121fa1 commit fe64532

File tree

8 files changed

+65
-16
lines changed

8 files changed

+65
-16
lines changed

lib/coherence_assent/strategies/oauth2.ex

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ defmodule CoherenceAssent.Strategy.OAuth2 do
2929
state
3030
|> check_state(client, params)
3131
|> get_access_token(config, params)
32-
|> get_user(config[:user_url])
33-
|> case do
34-
{:ok, user} -> {:ok, %{conn: conn, client: client, user: user}}
35-
{:error, error} -> {:error, %{conn: conn, error: error}}
36-
end
32+
|> get_user_with_client(config[:user_url], conn)
3733
end
3834

3935
@doc false
@@ -82,6 +78,17 @@ defmodule CoherenceAssent.Strategy.OAuth2 do
8278
end
8379
def get_user({:error, error}, _user_url), do: {:error, error}
8480

81+
defp get_user_with_client({:ok, client}, user_url, conn) do
82+
{:ok, client}
83+
|> get_user(user_url)
84+
|> case do
85+
{:ok, user} -> {:ok, %{conn: conn, client: client, user: user}}
86+
{:error, error} -> {:error, %{conn: conn, error: error}}
87+
end
88+
end
89+
defp get_user_with_client({:error, error}, _user_url, conn),
90+
do: {:error, %{conn: conn, error: error}}
91+
8592
defp process_user_response({:ok, %OAuth2.Response{body: user}}), do: {:ok, user}
8693
defp process_user_response({:error, %OAuth2.Response{status_code: 401}}),
8794
do: {:error, %CoherenceAssent.RequestError{message: "Unauthorized token"}}

test/coherence_assent/strategies/basecamp_test.exs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule CoherenceAssent.BasecampTest do
44
import OAuth2.TestHelpers
55
alias CoherenceAssent.Strategy.Basecamp
66

7+
@access_token "access_token"
8+
79
setup %{conn: conn} do
810
conn = session_conn(conn)
911

@@ -50,10 +52,12 @@ defmodule CoherenceAssent.BasecampTest do
5052
}]
5153

5254
Bypass.expect_once bypass, "POST", "/authorization/token", fn conn ->
53-
send_resp(conn, 200, Poison.encode!(%{access_token: "access_token"}))
55+
send_resp(conn, 200, Poison.encode!(%{access_token: @access_token}))
5456
end
5557

5658
Bypass.expect_once bypass, "GET", "/authorization.json", fn conn ->
59+
assert_access_token_in_header conn, @access_token
60+
5761
user = %{"expires_at" => "2012-03-22T16:56:48-05:00",
5862
"identity" => %{
5963
"id" => 9999999,

test/coherence_assent/strategies/facebook_test.exs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule CoherenceAssent.Strategy.FacebookTest do
44
import OAuth2.TestHelpers
55
alias CoherenceAssent.Strategy.Facebook
66

7+
@access_token "access_token"
8+
79
setup %{conn: conn} do
810
conn = session_conn(conn)
911

@@ -31,14 +33,16 @@ defmodule CoherenceAssent.Strategy.FacebookTest do
3133
assert {:ok, body, _conn} = Plug.Conn.read_body(conn)
3234
assert body =~ "scope=email"
3335

34-
send_resp(conn, 200, Poison.encode!(%{"access_token" => "access_token"}))
36+
send_resp(conn, 200, Poison.encode!(%{"access_token" => @access_token}))
3537
end
3638

3739
Bypass.expect_once bypass, "GET", "/me", fn conn ->
40+
assert_access_token_in_header conn, @access_token
41+
3842
conn = Plug.Conn.fetch_query_params(conn)
3943

4044
assert conn.params["fields"] == "name,email"
41-
assert conn.params["appsecret_proof"] == Base.encode16(:crypto.hmac(:sha256, "", "access_token"), case: :lower)
45+
assert conn.params["appsecret_proof"] == Base.encode16(:crypto.hmac(:sha256, "", @access_token), case: :lower)
4246

4347
user = %{name: "Dan Schultzer",
4448
email: "foo@example.com",

test/coherence_assent/strategies/github_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule CoherenceAssent.Strategy.GithubTest do
44
import OAuth2.TestHelpers
55
alias CoherenceAssent.Strategy.Github
66

7+
@access_token "access_token"
8+
79
setup %{conn: conn} do
810
conn = session_conn(conn)
911

@@ -28,10 +30,12 @@ defmodule CoherenceAssent.Strategy.GithubTest do
2830

2931
test "normalizes data", %{conn: conn, config: config, params: params, bypass: bypass} do
3032
Bypass.expect_once bypass, "POST", "/login/oauth/access_token", fn conn ->
31-
send_resp(conn, 200, Poison.encode!(%{access_token: "access_token"}))
33+
send_resp(conn, 200, Poison.encode!(%{access_token: @access_token}))
3234
end
3335

3436
Bypass.expect_once bypass, "GET", "/user", fn conn ->
37+
assert_access_token_in_header conn, @access_token
38+
3539
user = %{
3640
login: "octocat",
3741
id: 1,
@@ -68,6 +72,8 @@ defmodule CoherenceAssent.Strategy.GithubTest do
6872
end
6973

7074
Bypass.expect_once bypass, "GET", "/user/emails", fn conn ->
75+
assert_access_token_in_header conn, @access_token
76+
7177
emails = [
7278
%{
7379
email: "octocat@github.com",

test/coherence_assent/strategies/google_test.exs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule CoherenceAssent.GoogleTest do
44
import OAuth2.TestHelpers
55
alias CoherenceAssent.Strategy.Google
66

7+
@access_token "access_token"
8+
79
setup %{conn: conn} do
810
conn = session_conn(conn)
911

@@ -28,10 +30,12 @@ defmodule CoherenceAssent.GoogleTest do
2830

2931
test "normalizes data", %{conn: conn, config: config, params: params, bypass: bypass} do
3032
Bypass.expect_once bypass, "POST", "/o/oauth2/token", fn conn ->
31-
send_resp(conn, 200, Poison.encode!(%{access_token: "access_token"}))
33+
send_resp(conn, 200, Poison.encode!(%{access_token: @access_token}))
3234
end
3335

3436
Bypass.expect_once bypass, "GET", "/people/me/openIdConnect", fn conn ->
37+
assert_access_token_in_header conn, @access_token
38+
3539
user = %{"kind" => "plus#personOpenIdConnect",
3640
"gender" => "",
3741
"sub" => "1",

test/coherence_assent/strategies/oauth2_test.exs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule CoherenceAssent.Strategy.OAuth2Test do
44
import OAuth2.TestHelpers
55
alias CoherenceAssent.Strategy.OAuth2, as: OAuth2Strategy
66

7+
@access_token "access_token"
8+
79
setup %{conn: conn} do
810
conn = session_conn(conn)
911

@@ -31,10 +33,12 @@ defmodule CoherenceAssent.Strategy.OAuth2Test do
3133

3234
test "normalizes data", %{conn: conn, config: config, params: params, bypass: bypass} do
3335
Bypass.expect_once bypass, "POST", "/oauth/token", fn conn ->
34-
send_resp(conn, 200, Poison.encode!(%{access_token: "access_token"}))
36+
send_resp(conn, 200, Poison.encode!(%{access_token: @access_token}))
3537
end
3638

3739
Bypass.expect_once bypass, "GET", "/api/user", fn conn ->
40+
assert_access_token_in_header conn, @access_token
41+
3842
user = %{name: "Dan Schultzer", email: "foo@example.com", uid: "1"}
3943
Plug.Conn.resp(conn, 200, Poison.encode!(user))
4044
end
@@ -71,7 +75,7 @@ defmodule CoherenceAssent.Strategy.OAuth2Test do
7175
config = Keyword.put(config, :user_url, nil)
7276

7377
Bypass.expect_once bypass, "POST", "/oauth/token", fn conn ->
74-
send_resp(conn, 200, Poison.encode!(%{access_token: "access_token"}))
78+
send_resp(conn, 200, Poison.encode!(%{access_token: @access_token}))
7579
end
7680

7781
expected = %CoherenceAssent.ConfigurationError{message: "No user URL set"}
@@ -84,7 +88,7 @@ defmodule CoherenceAssent.Strategy.OAuth2Test do
8488
config = Keyword.put(config, :user_url, "http://localhost:8888/api/user")
8589

8690
Bypass.expect_once bypass, "POST", "/oauth/token", fn conn ->
87-
send_resp(conn, 200, Poison.encode!(%{access_token: "access_token"}))
91+
send_resp(conn, 200, Poison.encode!(%{access_token: @access_token}))
8892
end
8993

9094
expected = %OAuth2.Error{reason: :econnrefused}
@@ -95,10 +99,11 @@ defmodule CoherenceAssent.Strategy.OAuth2Test do
9599

96100
test "user url unauthorized access token", %{conn: conn, config: config, params: params, bypass: bypass} do
97101
Bypass.expect_once bypass, "POST", "/oauth/token", fn conn ->
98-
send_resp(conn, 200, Poison.encode!(%{access_token: "access_token"}))
102+
send_resp(conn, 200, Poison.encode!(%{access_token: @access_token}))
99103
end
100104

101105
Bypass.expect_once bypass, "GET", "/api/user", fn conn ->
106+
assert_access_token_in_header conn, @access_token
102107
Plug.Conn.resp(conn, 401, Poison.encode!(%{"error" => "Unauthorized"}))
103108
end
104109

test/coherence_assent/strategies/vk_test.exs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule CoherenceAssent.VKTest do
44
import OAuth2.TestHelpers
55
alias CoherenceAssent.Strategy.VK
66

7+
@access_token "access_token"
8+
79
setup %{conn: conn} do
810
conn = session_conn(conn)
911

@@ -33,15 +35,17 @@ defmodule CoherenceAssent.VKTest do
3335
assert {:ok, body, _conn} = Plug.Conn.read_body(conn)
3436
assert body =~ "scope=email"
3537

36-
send_resp(conn, 200, Poison.encode!(%{"access_token" => "access_token", "email" => "lindsay.stirling@example.com"}))
38+
send_resp(conn, 200, Poison.encode!(%{"access_token" => @access_token, "email" => "lindsay.stirling@example.com"}))
3739
end
3840

3941
Bypass.expect_once bypass, "GET", "/method/users.get", fn conn ->
42+
assert_access_token_in_header conn, @access_token
43+
4044
conn = Plug.Conn.fetch_query_params(conn)
4145

4246
assert conn.params["fields"] == "uid,first_name,last_name,photo_200,screen_name,verified"
4347
assert conn.params["v"] == "5.69"
44-
assert conn.params["access_token"] == "access_token"
48+
assert conn.params["access_token"] == @access_token
4549

4650
users = [%{"id" => 210700286,
4751
"first_name" => "Lindsay",

test/support/test_helpers.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
defmodule OAuth2.TestHelpers do
2+
@spec bypass_server(%Bypass{}) :: String.t
23
def bypass_server(%Bypass{port: port}) do
34
"http://localhost:#{port}"
45
end
6+
7+
@spec assert_access_token_in_header(Plug.Conn.t, String.t) :: true | no_return
8+
def assert_access_token_in_header(conn, token) do
9+
expected = {"authorization", "Bearer #{token}"}
10+
11+
case Enum.find(conn.req_headers, &(elem(&1, 0) == "authorization")) do
12+
^expected ->
13+
true
14+
{"authorization", "Bearer " <> found_token} ->
15+
ExUnit.Assertions.flunk("Expected bearer token #{token}, but received #{found_token}")
16+
_ ->
17+
ExUnit.Assertions.flunk("No bearer token found in headers")
18+
end
19+
end
520
end

0 commit comments

Comments
 (0)