diff --git a/CHANGELOG.md b/CHANGELOG.md index fcc6d94..c83f7d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,13 @@ ## (Unreleased) +## v0.11.0 + +* Allow using a function to generate the client secret [101](https://github.com/ueberauth/ueberauth_google/pull/101) + ## v0.10.3 -- Handle `%OAuth2.Response{status_code: 503}` with no `error_description` in `get_access_token` [99](https://github.com/ueberauth/ueberauth_google/pull/99) +* Handle `%OAuth2.Response{status_code: 503}` with no `error_description` in `get_access_token` [99](https://github.com/ueberauth/ueberauth_google/pull/99) ## v0.10.2 diff --git a/lib/ueberauth/strategy/google/oauth.ex b/lib/ueberauth/strategy/google/oauth.ex index c818cca..1f299bd 100644 --- a/lib/ueberauth/strategy/google/oauth.ex +++ b/lib/ueberauth/strategy/google/oauth.ex @@ -27,10 +27,14 @@ defmodule Ueberauth.Strategy.Google.OAuth do """ def client(opts \\ []) do config = Application.get_env(:ueberauth, __MODULE__, []) - opts = @defaults |> Keyword.merge(config) |> Keyword.merge(opts) |> resolve_values() json_library = Ueberauth.json_library() - OAuth2.Client.new(opts) + @defaults + |> Keyword.merge(config) + |> Keyword.merge(opts) + |> resolve_values() + |> generate_secret() + |> OAuth2.Client.new() |> OAuth2.Client.put_serializer("application/json", json_library) end @@ -89,4 +93,14 @@ defmodule Ueberauth.Strategy.Google.OAuth do 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 + + defp generate_secret(opts) do + if is_tuple(opts[:client_secret]) do + {module, fun} = opts[:client_secret] + secret = apply(module, fun, [opts]) + Keyword.put(opts, :client_secret, secret) + else + opts + end + end end diff --git a/mix.exs b/mix.exs index 5012ee4..369f18b 100644 --- a/mix.exs +++ b/mix.exs @@ -2,7 +2,7 @@ defmodule UeberauthGoogle.Mixfile do use Mix.Project @source_url "https://github.com/ueberauth/ueberauth_google" - @version "0.10.3" + @version "0.11.0" def project do [ diff --git a/test/strategy/google/oauth_test.exs b/test/strategy/google/oauth_test.exs new file mode 100644 index 0000000..1039d76 --- /dev/null +++ b/test/strategy/google/oauth_test.exs @@ -0,0 +1,20 @@ +defmodule Ueberauth.Strategy.Google.OAuthTest do + use ExUnit.Case, async: true + + alias Ueberauth.Strategy.Google.OAuth + + defmodule MyApp.Google do + def client_secret(_opts), do: "custom_client_secret" + end + + describe "client/1" do + test "uses client secret in the config when it is not a tuple" do + assert %OAuth2.Client{client_secret: "client_secret"} = OAuth.client() + end + + test "generates client secret when it is using a tuple config" do + options = [client_secret: {MyApp.Google, :client_secret}] + assert %OAuth2.Client{client_secret: "custom_client_secret"} = OAuth.client(options) + end + end +end