Skip to content

Commit

Permalink
Require Goth v1.3+ (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumm authored Sep 7, 2023
1 parent 81ca726 commit e794829
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 111 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Relax `nimble_options` dependency.

- Optional dependency `goth` must be v1.3+.

### Removed

- The `:scope` option has been removed.

## [0.8.0] - 2022-10-26

This version moves Cloud PubSub from Tesla to Finch, so read the notes below and upgrade with care.
Expand Down
40 changes: 25 additions & 15 deletions lib/broadway_cloud_pub_sub/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ defmodule BroadwayCloudPubSub.Options do

@default_receive_timeout :infinity

@default_scope "https://www.googleapis.com/auth/pubsub"

definition = [
# Handled by Broadway.
broadway: [type: :any, doc: false],
Expand Down Expand Up @@ -80,12 +78,10 @@ defmodule BroadwayCloudPubSub.Options do
before the pull client returns an error.
"""
],
scope: [
type: {:custom, __MODULE__, :type_non_empty_string_or_tagged_tuple, [[{:name, :scope}]]},
default: @default_scope,
goth: [
type: :atom,
doc: """
A string representing the scope or scopes to use when fetching
an access token. Note that this option only applies to the
The `Goth` module to use for authentication. Note that this option only applies to the
default token generator.
"""
],
Expand All @@ -94,7 +90,7 @@ defmodule BroadwayCloudPubSub.Options do
doc: """
An MFArgs tuple that will be called before each request
to fetch an authentication token. It should return `{:ok, String.t()} | {:error, any()}`.
By default this will invoke `Goth.Token.for_scope/1` with `"#{@default_scope}"`.
By default this will invoke `Goth.fetch/1` with the `:goth` option.
See the "Custom token generator" section below for more information.
"""
],
Expand Down Expand Up @@ -152,22 +148,36 @@ defmodule BroadwayCloudPubSub.Options do
Builds an MFArgs tuple for a token generator.
"""
def make_token_generator(opts) do
scope = Keyword.fetch!(opts, :scope)
goth = Keyword.get(opts, :goth)

unless goth do
require Logger

Logger.error("""
the :goth option is required for the default authentication token generator
If you are upgrading from an earlier version of Goth, refer to the
upgrade guide for more information:
https://hexdocs.pm/goth/upgrade_guide.html
""")
end

ensure_goth_loaded()
{__MODULE__, :generate_goth_token, [scope]}
{__MODULE__, :generate_goth_token, [goth]}
end

defp ensure_goth_loaded() do
unless Code.ensure_loaded?(Goth.Token) do
unless Code.ensure_loaded?(Goth) do
require Logger

Logger.error("""
the default authentication token generator uses the Goth library but it's not available
Add goth to your dependencies:
defp deps() do
{:goth, "~> 1.0"}
defp deps do
{:goth, "~> 1.3"}
end
Or provide your own token generator:
Expand Down Expand Up @@ -250,8 +260,8 @@ defmodule BroadwayCloudPubSub.Options do
end

@doc false
def generate_goth_token(scope) do
with {:ok, %{token: token}} <- Goth.Token.for_scope(scope) do
def generate_goth_token(goth_name) do
with {:ok, %{token: token}} <- Goth.fetch(goth_name) do
{:ok, token}
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/broadway_cloud_pub_sub/producer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ defmodule BroadwayCloudPubSub.Producer do
For example, define a `MyApp.fetch_token/0` function:
defmodule MyApp do
@scope "https://www.googleapis.com/auth/pubsub"
def fetch_token() do
with {:ok, token} <- Goth.Token.for_scope(@scope)
@doc "Fetches a Google auth token"
def fetch_token do
with {:ok, token} <- Goth.fetch(MyApp.Goth)
{:ok, token.token}
end
end
Expand Down Expand Up @@ -83,6 +83,7 @@ defmodule BroadwayCloudPubSub.Producer do
name: MyBroadway,
producer: [
module: {BroadwayCloudPubSub.Producer,
goth: MyApp.Goth,
subscription: "projects/my-project/subscriptions/my_subscription"
}
],
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule BroadwayCloudPubSub.MixProject do
{:jason, "~> 1.0"},
{:nimble_options, "~> 0.3.7 or ~> 0.4 or ~> 1.0"},
{:telemetry, "~> 0.4.3 or ~> 1.0"},
{:goth, "~> 1.0", optional: true},
{:goth, "~> 1.3", optional: true},
{:ex_doc, "~> 0.23", only: :docs},
{:bypass, "~> 2.1", only: :test}
]
Expand Down
6 changes: 3 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"ex_doc": {:hex, :ex_doc, "0.29.0", "4a1cb903ce746aceef9c1f9ae8a6c12b742a5461e6959b9d3b24d813ffbea146", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "f096adb8bbca677d35d278223361c7792d496b3fc0d0224c9d4bc2f651af5db1"},
"finch": {:hex, :finch, "0.9.0", "8b772324aebafcaba763f1dffaa3e7f52f8c4e52485f50f48bbb2f42219a2e87", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a93bfcad9ca50fa3cb2d459f27667d9a87cfbb7fecf9b29b2e78a50bc2ab445d"},
"gen_stage": {:hex, :gen_stage, "1.1.1", "78d83b14ca742f4c252770bcdf674d83378ca41579c387c57e2f06d70f596317", [:mix], [], "hexpm", "eb90d2d72609050a66ce42b7d4a69323a60c892a09ead0680d5d8ef16b9a034e"},
"goth": {:hex, :goth, "1.2.0", "92d6d926065a72a7e0da8818cc3a133229b56edf378022c00d9886c4125ce769", [:mix], [{:httpoison, "~> 0.11 or ~> 1.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:joken, "~> 2.0", [hex: :joken, repo: "hexpm", optional: false]}], "hexpm", "4974932ab3b782c99a6fdeb0b968ddd61436ef14de5862bd6bb0227386c63b26"},
"goth": {:hex, :goth, "1.4.2", "a598dfbce6fe65db3f5f43b1ab2ce8fbe3b2fe20a7569ad62d71c11c0ddc3f41", [:mix], [{:finch, "~> 0.9", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:jose, "~> 1.11", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "d51bb6544dc551fe5754ab72e6cf194120b3c06d924282aaa3321a516ed3b98a"},
"hackney": {:hex, :hackney, "1.18.0", "c4443d960bb9fba6d01161d01cd81173089686717d9490e5d3606644c48d121f", [:rebar3], [{:certifi, "~>2.8.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "9afcda620704d720db8c6a3123e9848d09c87586dc1c10479c42627b905b5c5e"},
"hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"},
"httpoison": {:hex, :httpoison, "1.6.2", "ace7c8d3a361cebccbed19c283c349b3d26991eff73a1eaaa8abae2e3c8089b6", [:mix], [{:hackney, "~> 1.15 and >= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "aa2c74bd271af34239a3948779612f87df2422c2fdcfdbcec28d9c105f0773fe"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"joken": {:hex, :joken, "2.2.0", "2daa1b12be05184aff7b5ace1d43ca1f81345962285fff3f88db74927c954d3a", [:mix], [{:jose, "~> 1.9", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "b4f92e30388206f869dd25d1af628a1d99d7586e5cf0672f64d4df84c4d2f5e9"},
"jose": {:hex, :jose, "1.9.0", "4167c5f6d06ffaebffd15cdb8da61a108445ef5e85ab8f5a7ad926fdf3ada154", [:mix, :rebar3], [{:base64url, "~> 0.0.1", [hex: :base64url, repo: "hexpm", optional: false]}], "hexpm", "6429c4fee52b2dda7861ee19a4f09c8c1ffa213bee3a1ec187828fde95d447ed"},
"jose": {:hex, :jose, "1.11.6", "613fda82552128aa6fb804682e3a616f4bc15565a048dabd05b1ebd5827ed965", [:mix, :rebar3], [], "hexpm", "6275cb75504f9c1e60eeacb771adfeee4905a9e182103aa59b53fed651ff9738"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
Expand Down
Loading

0 comments on commit e794829

Please sign in to comment.