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

Add the option to get the client secret dynamically. #101

Merged
merged 5 commits into from
Sep 20, 2023
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 16 additions & 2 deletions lib/ueberauth/strategy/google/oauth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
[
Expand Down
20 changes: 20 additions & 0 deletions test/strategy/google/oauth_test.exs
Original file line number Diff line number Diff line change
@@ -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
Loading