Skip to content

Commit

Permalink
remove all referece to x-forward-proto and just use Endpoint.url() in…
Browse files Browse the repository at this point in the history
…stead! #94
  • Loading branch information
nelsonic committed May 18, 2023
1 parent 5097b70 commit 943e441
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ passing in the `url` of your `App`:

```elixir
def index(conn, _params) do
base_url = MyApp.Endpoint.url()
base_url = MyAppWeb.Endpoint.url()
oauth_google_url = ElixirAuthGoogle.generate_oauth_url(base_url)
render(conn, "index.html",[oauth_google_url: oauth_google_url])
end
Expand All @@ -259,7 +259,7 @@ This uses
[Phoenix.Endpoint.url/0](https://hexdocs.pm/phoenix/Phoenix.Endpoint.html#c:url/0)
which is available in any `Phoenix` App.

Just remember to replace `MyApp` with the name of your `App`. 😉
Just remember to replace `MyAppWeb` with the name of your `App`. 😉

<br />

Expand Down
21 changes: 4 additions & 17 deletions lib/elixir_auth_google.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ defmodule ElixirAuthGoogle do
"#{Atom.to_string(s)}://#{h}:#{p}"
end

# When deployed to Fly.io the scheme is "http" and port is "80" ...
# This results in an incorrect url see:
# github.com/dwyl/elixir-auth-google/issues/94
def get_baseurl_from_conn(%{host: h, req_headers: r}) do
proto = case List.keyfind(r, "x-forwarded-proto", 0) do
{"x-forwarded-proto", proto} ->
proto

nil ->
"http"
end

"#{proto}://#{h}"
end

def get_baseurl_from_conn(%{host: h, scheme: s}) do
"#{Atom.to_string(s)}://#{h}"
end
Expand All @@ -61,9 +46,11 @@ defmodule ElixirAuthGoogle do

@doc """
`generate_redirect_uri/1` generates the Google redirect uri based on `conn`
or the `url`. If the `App.Endpoint.url()` e.g: auth.dwyl.com or gcal.fly.dev
or the `url`. If the `App.Endpoint.url()`
e.g: auth.dwyl.com or https://gcal.fly.dev
is passed into `generate_redirect_uri/1`,
return that `url` with the callback appended to it. #94
return that `url` with the callback appended to it.
See: github.com/dwyl/elixir-auth-google/issues/94
"""
@spec generate_redirect_uri(url) :: String.t()
def generate_redirect_uri(url) when is_binary(url) do
Expand Down
31 changes: 1 addition & 30 deletions test/elixir_auth_google_test.exs
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
defmodule ElixirAuthGoogleTest do
use ExUnit.Case, async: true
use Plug.Test
doctest ElixirAuthGoogle

import Mock

test "get_baseurl_from_conn(conn) x-forwarded-proto header for https #94" do

conn =
conn(:get, "/")
|> put_req_header("x-forwarded-proto", "https")
|> Map.put(:host, "gcal.fly.dev")

# dbg(conn)
# %{
# host: "gcal.fly.dev",
# port: 80,
# scheme: :http,
# req_headers: [
# { "x-forwarded-proto", "https"}
# ]
# }
assert ElixirAuthGoogle.get_baseurl_from_conn(conn) == "https://gcal.fly.dev"
end

test "get_baseurl_from_conn(conn) any header gives http #94" do

conn =
conn(:get, "/")
|> put_req_header("not-proto", "any")
|> Map.put(:host, "gcal.fly.dev")

assert ElixirAuthGoogle.get_baseurl_from_conn(conn) == "http://gcal.fly.dev"
end

test "get_baseurl_from_conn(conn) detects the URL based on conn.host HTTP" do
conn = %{
host: "localhost",
Expand Down Expand Up @@ -205,6 +175,7 @@ defmodule ElixirAuthGoogleTest do
assert ElixirAuthGoogle.generate_oauth_url(url) =~ auth_url
end


test "generate_oauth_url(url) with scheme e.g. https://gcal.fly.dev #94" do
no_scheme = "gcal.fly.dev"
url = "https://#{no_scheme}"
Expand Down

0 comments on commit 943e441

Please sign in to comment.