From 1a9613921528a9396e750c47965b89d3e1a6f8bc Mon Sep 17 00:00:00 2001 From: nelsonic Date: Fri, 24 Apr 2020 14:27:32 +0100 Subject: [PATCH] display profile page if referer is same as base_url #42 --- lib/auth_web/controllers/auth_controller.ex | 17 ++++++++++------- lib/auth_web/controllers/page_controller.ex | 4 ++-- mix.exs | 2 +- mix.lock | 2 +- .../controllers/auth_controller_test.exs | 4 ++-- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/auth_web/controllers/auth_controller.ex b/lib/auth_web/controllers/auth_controller.ex index 40c388e9..e8f71d1a 100644 --- a/lib/auth_web/controllers/auth_controller.ex +++ b/lib/auth_web/controllers/auth_controller.ex @@ -49,16 +49,19 @@ defmodule AuthWeb.AuthController do |> IO.inspect(label: "email") # check if valid state (HTTP referer) is defined: - case not is_nil(state) do - # redirect - true -> - conn - |> redirect(external: add_jwt_url_param(person, state)) - - false -> + base_url = AuthPlug.Helpers.get_baseurl_from_conn(conn) + IO.inspect(state, label: "state") + IO.inspect(base_url, label: "base_url") + case state =~ base_url do + true -> # display welcome page conn |> put_view(AuthWeb.PageView) |> render(:welcome, person: person) + + false -> # redirect + conn + |> redirect(external: add_jwt_url_param(person, state)) + end end diff --git a/lib/auth_web/controllers/page_controller.ex b/lib/auth_web/controllers/page_controller.ex index 0f0137ad..70e5ad88 100644 --- a/lib/auth_web/controllers/page_controller.ex +++ b/lib/auth_web/controllers/page_controller.ex @@ -36,9 +36,9 @@ defmodule AuthWeb.PageController do Map.get(query, "referer") |> IO.inspect(label: "url referer") - false -> # no referer, redirect back to this app. TODO: + false -> # no referer, redirect back to this app. IO.inspect("false: no referer") - ElixirAuthGoogle.get_baseurl_from_conn(conn) + AuthPlug.Helpers.get_baseurl_from_conn(conn) end end |> URI.encode |> IO.inspect(label: "referer") diff --git a/mix.exs b/mix.exs index c2121ba9..b1b90b16 100644 --- a/mix.exs +++ b/mix.exs @@ -61,7 +61,7 @@ defmodule Auth.Mixfile do # https://github.com/dwyl/elixir-auth-google {:elixir_auth_google, "~> 1.2.0"}, # https://github.com/dwyl/auth_plug - {:auth_plug, "~> 0.7.0"}, + {:auth_plug, "~> 0.8.0"}, # Field Validation and Encryption: github.com/dwyl/fields {:fields, "~> 2.4.0"}, diff --git a/mix.lock b/mix.lock index b2c72824..e44a2813 100644 --- a/mix.lock +++ b/mix.lock @@ -1,6 +1,6 @@ %{ "argon2_elixir": {:hex, :argon2_elixir, "2.3.0", "e251bdafd69308e8c1263e111600e6d68bd44f23d2cccbe43fcb1a417a76bc8e", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "28ccb63bff213aecec1f7f3dde9648418b031f822499973281d8f494b9d5a3b3"}, - "auth_plug": {:hex, :auth_plug, "0.7.0", "ce8001402b2c2dbabe743c86c030d28d276f6ce138beac112323a342d1d44e15", [:mix], [{:joken, "~> 2.2.0", [hex: :joken, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "1c96440dd73bb323cca32c838ead5aea44e16ed6e221ed5b248c7f02c498aef9"}, + "auth_plug": {:hex, :auth_plug, "0.8.0", "1200a5dfc979b4f82efd803dd0042b8a54171e7947964fdd87ea52f14580106a", [:mix], [{:joken, "~> 2.2.0", [hex: :joken, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "652df6e403554718f59405ca4a3d21f5130399429ae8b0357eb50b5c2a8c7a5e"}, "certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"}, "comeonin": {:hex, :comeonin, "5.3.1", "7fe612b739c78c9c1a75186ef2d322ce4d25032d119823269d0aa1e2f1e20025", [:mix], [], "hexpm", "d6222483060c17f0977fad1b7401ef0c5863c985a64352755f366aee3799c245"}, "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"}, diff --git a/test/auth_web/controllers/auth_controller_test.exs b/test/auth_web/controllers/auth_controller_test.exs index 51be2697..2dfd390c 100644 --- a/test/auth_web/controllers/auth_controller_test.exs +++ b/test/auth_web/controllers/auth_controller_test.exs @@ -16,9 +16,9 @@ defmodule AuthWeb.AuthControllerTest do assert html_response(conn, 302) =~ "http://localhost" end - test "google_handler/2 nil state", %{conn: conn} do + test "google_handler/2 show welcome page", %{conn: conn} do conn = get(conn, "/auth/google/callback", - %{code: "234", state: nil}) + %{code: "234", state: AuthPlug.Helpers.get_baseurl_from_conn(conn)}) assert html_response(conn, 200) =~ "nelson@gmail.com" # assert html_response(conn, 302) =~ "redirected"