Skip to content

Commit

Permalink
slight refactor into reuseable functions #42
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 29, 2020
1 parent 178a6e2 commit d87f2d7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
30 changes: 17 additions & 13 deletions lib/auth_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,29 @@ defmodule AuthWeb.AuthController do
IO.inspect(client_id, label: "client_id")
case not is_nil(client_id) do
true -> # Lookup client_id in apikeys table
person_id = AuthWeb.ApikeyController.decode_decrypt(client_id)
# IO.inspect(person_id, label: "person_id")
if person_id == 0 do # decode_decrypt fails with state 0
# IO.inspect(person_id, label: "person_id:88")
0
else
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
end) |> List.first() |> Map.get(:client_secret)
# check for URL match!
end
get_client_secret(client_id, state)

false -> # state without client_id is not valid
0
end
end

def get_client_secret(client_id, state) do
person_id = AuthWeb.ApikeyController.decode_decrypt(client_id)
# IO.inspect(person_id, label: "person_id")
if person_id == 0 do # decode_decrypt fails with state 0
# IO.inspect(person_id, label: "person_id:88")
0
else
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
end) |> List.first() |> Map.get(:client_secret)

end
end



def add_jwt_url_param(person, state, client_secret) do
Expand Down
18 changes: 14 additions & 4 deletions lib/auth_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule AuthWeb.PageController do

def index(conn, _params) do
state = get_referer(conn)

oauth_github_url = ElixirAuthGithub.login_url(%{scopes: ["user:email"], state: state})
oauth_google_url = ElixirAuthGoogle.generate_oauth_url(conn, state)

Expand Down Expand Up @@ -34,14 +35,23 @@ defmodule AuthWeb.PageController do
true ->
query = URI.decode_query(conn.query_string)
ref = Map.get(query, "referer")
client_id = Map.get(query, "client_id")
client_id = get_client_id_from_query(conn)
ref |> append_client_id(client_id)

false -> # no referer, redirect back to this app.
# IO.inspect("false: no referer")
AuthPlug.Helpers.get_baseurl_from_conn(conn) <> "/profile"
false -> # no referer, redirect back to Auth app.
AuthPlug.Helpers.get_baseurl_from_conn(conn)
<> "/profile" <> AuthPlug.Token.client_id()
end
end
|> URI.encode |> IO.inspect(label: "referer")
end

def get_client_id_from_query(conn) do
case conn.query_string =~ "client_id" do
true ->
Map.get(URI.decode_query(conn.query_string), "client_id")
false -> # no client_id, redirect back to this app.
0
end
end
end

0 comments on commit d87f2d7

Please sign in to comment.