Skip to content

Commit

Permalink
add client_id to state in all auth_controller tests #55
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 29, 2020
1 parent 0a181cc commit d379fb6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 9 additions & 2 deletions lib/auth_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule AuthWeb.AuthController do
name: person.givenName,
template: "welcome"
})
|> IO.inspect(label: "email")
# |> IO.inspect(label: "email")

# IO.inspect(state, label: "state handler/3:53")

Expand Down Expand Up @@ -80,6 +80,13 @@ defmodule AuthWeb.AuthController do
|> halt()
end


@doc """
`get_client_secret_from_state/1` gets the client_id from state,
attempts to decode_decrypt it and then look it up in apikeys
if it finds the corresponding client_secret it returns the client_secret.
All other failure conditions return a 0 (zero) which results in a 401.
"""
def get_client_secret_from_state(state) do
query = URI.decode_query(state)
# IO.inspect(query, label: "query")
Expand All @@ -96,7 +103,7 @@ defmodule AuthWeb.AuthController do
apikeys = Auth.Apikey.list_apikeys_for_person(person_id)
# IO.inspect(apikeys)
Enum.filter(apikeys, fn(k) ->
k.client_id == client_id # and state =~ k.url
k.client_id == client_id and state =~ k.url
end) |> List.first() |> Map.get(:client_secret)
# check for URL match!
end
Expand Down
13 changes: 10 additions & 3 deletions test/auth_web/controllers/auth_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@ defmodule AuthWeb.AuthControllerTest do

test "github_handler/2 github auth callback", %{conn: conn} do
conn = get(conn, "/auth/github/callback",
%{code: "123", state: "http://localhost/" <>
%{code: "123", state: "http://localhost:4000/" <>
"&client_id=" <> AuthPlug.Token.client_id() })
# assert html_response(conn, 200) =~ "test@gmail.com"
assert html_response(conn, 302) =~ "http://localhost"
end

test "google_handler/2 for google auth callback", %{conn: conn} do
conn = get(conn, "/auth/google/callback",
%{code: "234", state: "http://localhost/" <>
%{code: "234", state: "http://localhost:4000/" <>
"&client_id=" <> AuthPlug.Token.client_id() })

# assert html_response(conn, 200) =~ "nelson@gmail.com"
assert html_response(conn, 302) =~ "http://localhost"
end

test "google_handler/2 show welcome page", %{conn: conn} do
# IO.inspect(AuthPlug.Helpers.get_baseurl_from_conn(conn), label: "baseurl")
# Google Auth Mock makes the state https://www.example.com
# so we need to create a new API_KEY with that url:
{:ok, key} = %{"name" => "example key", "url" => "https://www.example.com"}
|> AuthWeb.ApikeyController.make_apikey(1)
|> Auth.Apikey.create_apikey()

conn = get(conn, "/auth/google/callback",
%{code: "234",
state: AuthPlug.Helpers.get_baseurl_from_conn(conn) <>
"&client_id=" <> AuthPlug.Token.client_id() })
"&client_id=" <> key.client_id })

# assert html_response(conn, 200) =~ "nelson@gmail.com"
assert html_response(conn, 302) =~ "redirected"
Expand Down

0 comments on commit d379fb6

Please sign in to comment.