-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add official support to Goth >= 1.3 (#2)
The PR is a suggestion in how we could offically support Goth >= 1.3. Basically, the Goth >= 1.3, the user's app need to start a process that will fetch and own a token. To be fair, this library doesn't need to do much to transit to Goth 1.3 thanks to the Token Fetcher behaviour. So this PR make changes to make the library use Goth >= 1.3 as an example, update the Readme, and now requires the library to suppliy a token fetcher module.
- Loading branch information
1 parent
0e9e904
commit ecfe374
Showing
9 changed files
with
76 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ Add it to your mix dependencies: | |
```elixir | ||
defp deps do | ||
[ | ||
{:goth, "~> 1.3"}, | ||
{:waffle_gcs, "~> 0.2"} | ||
] | ||
end | ||
|
@@ -56,7 +57,8 @@ All configuration values are stored under the `:waffle` app key. E.g. | |
config :waffle, | ||
storage: Waffle.Storage.Google.CloudStorage, | ||
bucket: "gcs-bucket", | ||
storage_dir: "uploads/waffle" | ||
storage_dir: "uploads/waffle", | ||
token_fetcher: MyApp.WaffleTokenFetcher | ||
``` | ||
|
||
**Note**: a valid bucket name is a required config. This can either be a | ||
|
@@ -66,32 +68,40 @@ module (e.g. `def bucket(), do: "my-bucket"`). | |
|
||
Authentication is done through Goth which requires credentials (https://github.com/peburrows/goth#installation). | ||
|
||
### Custom Token Generation ### | ||
### Goth >= 1.3 ### | ||
|
||
By default, the credentials provided to Goth will be used to generate tokens. | ||
If you have multiple sets of credentials in Goth or otherwise need more control | ||
over token generation, you can define your own module: | ||
For newer versions of Goth, you **must** provide the token fetcher module, for example: | ||
|
||
```elixir | ||
defmodule MyCredentials do | ||
defmodule MyApp.WaffleTokenFetcher do | ||
@behaviour Waffle.Storage.Google.Token.Fetcher | ||
@impl Waffle.Storage.Google.Token.Fetcher | ||
def get_token(scopes) when is_list(scopes), do: get_token(Enum.join(scopes, " ")) | ||
@impl Waffle.Storage.Google.Token.Fetcher | ||
def get_token(scope) when is_binary(scope) do | ||
{:ok, token} = Goth.Token.for_scope({"[email protected]", scope}) | ||
token.token | ||
|
||
@impl true | ||
def get_token(_scope) when is_binary(_scope) do | ||
Goth.fetch!(MyApp.Goth).token | ||
end | ||
end | ||
``` | ||
|
||
And configure it to use this new module instead of the default token generation: | ||
And configure it to use your module: | ||
|
||
```elixir | ||
config :waffle, | ||
storage: Waffle.Storage.Google.CloudStorage, | ||
bucket: "gcs-bucket-name", | ||
token_fetcher: MyApp.WaffleTokenFetcher | ||
``` | ||
|
||
### Goth < 1.3 ### | ||
|
||
You can use the Goth 1.1 token fetcher that reads the default credentials from | ||
Goth. | ||
|
||
```elixir | ||
config :waffle, | ||
storage: Waffle.Storage.Google.CloudStorage, | ||
bucket: "gcs-bucket-name", | ||
token_fetcher: MyCredentials | ||
token_fetcher: Waffle.Storage.Googke.Token.Fetcher.GothTokenFetcher | ||
``` | ||
|
||
## URL Signing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
if Version.match?(to_string(Application.spec(:goth, :vsn)), "< 1.3.0") do | ||
defmodule Waffle.Storage.Google.Token.GothTokenFetcher do | ||
@moduledoc """ | ||
A token fetcher that uses Goth 1.1 to fetch a token for a given scope. | ||
This module would raise an runtime exception if Goth 1.3 or newer is available. | ||
""" | ||
@behaviour Waffle.Storage.Google.Token.Fetcher | ||
|
||
@impl true | ||
def get_token(scope) when is_binary(scope) do | ||
{:ok, token} = Goth.Token.for_scope(scope) | ||
token.token | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Waffle.GothTokenFetcher do | ||
@moduledoc """ | ||
An example of fetching token for Goth >= 1.3.0 | ||
""" | ||
@behaviour Waffle.Storage.Google.Token.Fetcher | ||
|
||
@impl true | ||
def get_token(_scope) do | ||
Goth.fetch!(Waffle.Goth).token | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters