Skip to content

Commit

Permalink
Allow to add extra params to callback_url (#45)
Browse files Browse the repository at this point in the history
* Allow to specify whitelisted param names for callback params.

* mini fix call callback_params when we need it

* Made requested changes after first pull
  • Loading branch information
radzserg authored and doomspork committed Jan 11, 2017
1 parent 0a2d887 commit ea11906
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@
erl_crash.dump
*.ez
/doc
/.idea
10 changes: 6 additions & 4 deletions lib/ueberauth.ex
Expand Up @@ -190,9 +190,10 @@ defmodule Ueberauth do
callback_path = callback_path(base_path, strategy)
callback_methods = callback_methods(opts)
callback_url = Keyword.get(opts, :callback_url)
callback_params = Keyword.get(opts, :callback_params)

request_opts = strategy_opts(strategy, request_path, callback_path, callback_methods, callback_url)
callback_opts = strategy_opts(strategy, request_path, callback_path, callback_methods, callback_url)
request_opts = strategy_opts(strategy, request_path, callback_path, callback_methods, callback_url, callback_params)
callback_opts = strategy_opts(strategy, request_path, callback_path, callback_methods, callback_url, callback_params)

acc
|> Map.put(request_path, {module, :run_request, request_opts})
Expand Down Expand Up @@ -225,14 +226,15 @@ defmodule Ueberauth do
end
end

defp strategy_opts({name, {module, opts}}, req_path, cb_path, cb_meths, cb_url) do
defp strategy_opts({name, {module, opts}}, req_path, cb_path, cb_meths, cb_url, cb_params) do
%{strategy_name: name,
strategy: module,
callback_path: cb_path,
request_path: req_path,
callback_methods: cb_meths,
options: opts,
callback_url: cb_url}
callback_url: cb_url,
callback_params: cb_params}
end

defp request_path(base_path, {name, {_, opts}}) do
Expand Down
17 changes: 16 additions & 1 deletion lib/ueberauth/strategies/helpers.ex
Expand Up @@ -59,7 +59,22 @@ defmodule Ueberauth.Strategy.Helpers do
@spec callback_url(Plug.Conn.t) :: String.t
def callback_url(conn, opts \\ []) do
from_private(conn, :callback_url) ||
full_url(conn, callback_path(conn), opts)
full_url(conn, callback_path(conn), callback_params(conn, opts))
end

@doc """
Build params for callback
This method will filter conn.params with whitelisted params from :callback_params settings
"""
@spec callback_params(Plug.Conn.t) :: list(String.t)
def callback_params(conn, opts \\ []) do
callback_params = from_private(conn, :callback_params) || []
callback_params = callback_params
|> Enum.map(fn(k) -> {String.to_atom(k), conn.params[k]} end)
|> Enum.filter(fn {_, v} -> v != nil end)
|> Enum.filter(fn {k, _} -> k != "provider" end)
Keyword.merge(opts, callback_params)
end

@doc """
Expand Down
14 changes: 13 additions & 1 deletion test/ueberauth_test.exs
Expand Up @@ -14,7 +14,9 @@ defmodule UeberauthTest do
end

test "simple callback phase" do
conn = conn(:get, "/auth/simple/callback") |> SpecRouter.call(@opts)
conn = :get
|> conn("/auth/simple/callback")
|> SpecRouter.call(@opts)
auth = conn.assigns.ueberauth_auth

assert auth.uid == "Elixir.Support.SimpleCallback-uid"
Expand Down Expand Up @@ -118,6 +120,8 @@ defmodule UeberauthTest do
test "callback_url port" do
conn = %{conn(:get, "/") | scheme: :https, port: 80}
conn = put_private(conn, :ueberauth_request_options, [callback_path: "/auth/provider/callback"])
conn = %{conn | params: %{}}

assert Ueberauth.Strategy.Helpers.callback_url(conn) ==
"https://www.example.com/auth/provider/callback"
end
Expand All @@ -129,6 +133,14 @@ defmodule UeberauthTest do
"https://www.example.com/auth/provider/callback"
end

test "callback_url has extra params" do
conn = conn(:get, "/")
conn = put_private(conn, :ueberauth_request_options, [callback_params: ["type"]])
conn = %{conn | params: %{"type" => "user", "param_2" => "param_2"}}
assert Ueberauth.Strategy.Helpers.callback_url(conn) ==
"http://www.example.com?type=user"
end

defp assert_standard_info(auth) do
info = auth.info

Expand Down

0 comments on commit ea11906

Please sign in to comment.