Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amended oAuth strategy to resolve m/f/a tuples #60

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/ueberauth/strategy/google.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ defmodule Ueberauth.Strategy.Google do
|> with_param(:prompt, conn)
|> with_param(:state, conn)

opts = [redirect_uri: callback_url(conn)]

opts = oauth_client_options_from_conn(conn)
redirect!(conn, Ueberauth.Strategy.Google.OAuth.authorize_url!(params, opts))
end

@doc """
Handles the callback from Google.
"""
def handle_callback!(%Plug.Conn{params: %{"code" => code}} = conn) do
params = [code: code]
opts = [redirect_uri: callback_url(conn)]
opts = oauth_client_options_from_conn(conn)

case Ueberauth.Strategy.Google.OAuth.get_access_token(params, opts) do
{:ok, token} ->
fetch_user(conn, token)
Expand Down Expand Up @@ -116,7 +116,6 @@ defmodule Ueberauth.Strategy.Google do
}
end


defp fetch_user(conn, token) do
conn = put_private(conn, :google_token, token)

Expand Down Expand Up @@ -144,6 +143,17 @@ defmodule Ueberauth.Strategy.Google do
if option(conn, key), do: Keyword.put(opts, key, option(conn, key)), else: opts
end

defp oauth_client_options_from_conn(conn) do
base_options = [redirect_uri: callback_url(conn)]
request_options = conn.private[:ueberauth_request_options].options

case {request_options[:client_id], request_options[:client_secret]} do
{nil, _} -> base_options
{_, nil} -> base_options
{id, secret} -> [client_id: id, client_secret: secret] ++ base_options
end
end

defp option(conn, key) do
Keyword.get(options(conn), key, Keyword.get(default_options(), key))
end
Expand Down
18 changes: 11 additions & 7 deletions lib/ueberauth/strategy/google/oauth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ defmodule Ueberauth.Strategy.Google.OAuth do
These options are only useful for usage outside the normal callback phase of Ueberauth.
"""
def client(opts \\ []) do
config = Application.get_env(:ueberauth, Ueberauth.Strategy.Google.OAuth)

opts =
@defaults
|> Keyword.merge(opts)
|> Keyword.merge(config)

config = Application.get_env(:ueberauth, __MODULE__, [])
opts = @defaults |> Keyword.merge(opts) |> Keyword.merge(config) |> resolve_values()
OAuth2.Client.new(opts)
end

Expand Down Expand Up @@ -75,4 +70,13 @@ defmodule Ueberauth.Strategy.Google.OAuth do
|> put_header("Accept", "application/json")
|> OAuth2.Strategy.AuthCode.get_token(params, headers)
end

defp resolve_values(list) do
for {key, value} <- list do
{key, resolve_value(value)}
end
end

defp resolve_value({m, f, a}) when is_atom(m) and is_atom(f), do: apply(m, f, a)
defp resolve_value(v), do: v
end