Skip to content

Commit

Permalink
Merge pull request #101 from alejandrodevs/master
Browse files Browse the repository at this point in the history
Add the option to get the client secret dynamically.
  • Loading branch information
yordis authored Sep 20, 2023
2 parents a430ec7 + 34a6652 commit fcfc954
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
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

0 comments on commit fcfc954

Please sign in to comment.