From 14d368296c64e7946694d31052d589a0104fe599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Guti=C3=A9rrez?= Date: Tue, 19 Sep 2023 13:47:41 -0600 Subject: [PATCH 1/5] Add the option to get the client secret dynamically --- lib/ueberauth/strategy/google/oauth.ex | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/ueberauth/strategy/google/oauth.ex b/lib/ueberauth/strategy/google/oauth.ex index c818cca..e85d9c8 100644 --- a/lib/ueberauth/strategy/google/oauth.ex +++ b/lib/ueberauth/strategy/google/oauth.ex @@ -27,7 +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() + + opts = + @defaults + |> Keyword.merge(config) + |> Keyword.merge(opts) + |> resolve_values() + |> generate_secret() + json_library = Ueberauth.json_library() OAuth2.Client.new(opts) @@ -89,4 +96,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 From a4c9875f71ff59e00138fb20c7b27de6ee59a246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Guti=C3=A9rrez?= Date: Wed, 20 Sep 2023 11:55:29 -0600 Subject: [PATCH 2/5] Add unit tests for dynamic client secret generation --- test/strategy/google/oauth_test.exs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test/strategy/google/oauth_test.exs diff --git a/test/strategy/google/oauth_test.exs b/test/strategy/google/oauth_test.exs new file mode 100644 index 0000000..83832b4 --- /dev/null +++ b/test/strategy/google/oauth_test.exs @@ -0,0 +1,20 @@ +defmodule Ueberauth.Strategy.Google.OAuthTest do + use ExUnit.Case + + 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 From be040edd9af975fae79d4ef6aa4f53e0845f3af6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Guti=C3=A9rrez?= Date: Wed, 20 Sep 2023 11:56:37 -0600 Subject: [PATCH 3/5] Add sync true in tests for OAuth module --- test/strategy/google/oauth_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/strategy/google/oauth_test.exs b/test/strategy/google/oauth_test.exs index 83832b4..1039d76 100644 --- a/test/strategy/google/oauth_test.exs +++ b/test/strategy/google/oauth_test.exs @@ -1,5 +1,5 @@ defmodule Ueberauth.Strategy.Google.OAuthTest do - use ExUnit.Case + use ExUnit.Case, async: true alias Ueberauth.Strategy.Google.OAuth From 78e5db0e1910987af940ab5ab16e51c94cc437c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Guti=C3=A9rrez?= Date: Wed, 20 Sep 2023 12:28:18 -0600 Subject: [PATCH 4/5] Update CHANGELOG and bumping package version --- CHANGELOG.md | 6 +++++- mix.exs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) 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/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 [ From 34a66525f65aae7af35f3876b7bcb85a2e58719a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Guti=C3=A9rrez?= Date: Wed, 20 Sep 2023 12:35:34 -0600 Subject: [PATCH 5/5] Improve a little bit the codebase --- lib/ueberauth/strategy/google/oauth.ex | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/ueberauth/strategy/google/oauth.ex b/lib/ueberauth/strategy/google/oauth.ex index e85d9c8..1f299bd 100644 --- a/lib/ueberauth/strategy/google/oauth.ex +++ b/lib/ueberauth/strategy/google/oauth.ex @@ -27,17 +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() - |> generate_secret() - 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