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

Allow custom token fetching defined in config #4

Closed
Closed
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,34 @@ hard-coded string (e.g. `"gcs-bucket"`) or a system env tuple (e.g.
`{:system, "WAFFLE_BUCKET"}`). You can also override this in your definition
module (e.g. `def bucket(), do: "my-bucket"`).

### Custom Token Generation ###

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:

```elixir
defmodule MyCredentials do
@behaviour Waffle.Storage.Google.TokenFetcher
@impl Waffle.Storage.Google.TokenFetcher
def get_token(scopes) when is_list(scopes), do: get_token(Enum.join(scopes, " "))
@impl Waffle.Storage.Google.TokenFetcher
def get_token(scope) when is_binary(scope) do
{:ok, token} = Goth.Token.for_scope({"[email protected]", scope})
token.token
end
end
```

And configure it to use this new module instead of the default token generation:

```elixir
config :waffle,
storage: Waffle.Storage.Google,
bucket: "gcs-bucket-name",
token_fetcher: MyCredentials
```

## URL Signing

If your bucket/object permissions do not allow for public access, you will need
Expand Down
6 changes: 4 additions & 2 deletions lib/waffle/storage/google/cloud_storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ defmodule Waffle.Storage.Google.CloudStorage do
"""
@spec conn(String.t) :: Tesla.Env.client
def conn(scope \\ @full_control_scope) do
{:ok, token} = Goth.Token.for_scope(scope)
Connection.new(token.token)
token_store = Application.get_env(:waffle, :token_fetcher, Waffle.Storage.Google.Token.DefaultFetcher)

token_store.get_token(scope)
|> Connection.new()
end

@doc """
Expand Down
9 changes: 9 additions & 0 deletions lib/waffle/storage/google/token/default_fetcher.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Waffle.Storage.Google.Token.DefaultFetcher do
@behaviour Waffle.Storage.Google.Token.Fetcher

@impl Waffle.Storage.Google.Token.Fetcher
def get_token(scope) do
{:ok, token} = Goth.Token.for_scope(scope)
token.token
end
end
3 changes: 3 additions & 0 deletions lib/waffle/storage/google/token/fetcher.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Waffle.Storage.Google.Token.Fetcher do
@callback get_token(binary) :: binary
end