From d87f2d79660c9ea89e74d804e2e99453a897525c Mon Sep 17 00:00:00 2001 From: nelsonic Date: Wed, 29 Apr 2020 16:43:13 +0100 Subject: [PATCH] slight refactor into reuseable functions #42 --- lib/auth_web/controllers/auth_controller.ex | 30 ++++++++++++--------- lib/auth_web/controllers/page_controller.ex | 18 ++++++++++--- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/lib/auth_web/controllers/auth_controller.ex b/lib/auth_web/controllers/auth_controller.ex index 0e1b4848..d76a6236 100644 --- a/lib/auth_web/controllers/auth_controller.ex +++ b/lib/auth_web/controllers/auth_controller.ex @@ -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 diff --git a/lib/auth_web/controllers/page_controller.ex b/lib/auth_web/controllers/page_controller.ex index 3b9af187..b675e7e3 100644 --- a/lib/auth_web/controllers/page_controller.ex +++ b/lib/auth_web/controllers/page_controller.ex @@ -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) @@ -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