Skip to content

Commit

Permalink
Support the scopes option in workload identity (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
sekiyama58 authored Dec 20, 2024
1 parent 2ac9f6e commit 4551d0b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/goth/token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ defmodule Goth.Token do
"service_account_impersonation_url" => service_account_impersonation_url
}

request(%{config | source: {:workload_identity, credentials}})
request(%{config | source: {:workload_identity, credentials, opts}})
end
end

Expand Down Expand Up @@ -372,6 +372,11 @@ defmodule Goth.Token do
end

defp request(%{source: {:workload_identity, credentials}} = config) do
request(%{config | source: {:workload_identity, credentials, []}})
end

defp request(%{source: {:workload_identity, credentials, options}} = config)
when is_map(credentials) and is_list(options) do
%{
"token_url" => token_url,
"audience" => audience,
Expand All @@ -386,7 +391,7 @@ defmodule Goth.Token do
"audience" => audience,
"grant_type" => "urn:ietf:params:oauth:grant-type:token-exchange",
"requested_token_type" => "urn:ietf:params:oauth:token-type:access_token",
"scope" => "https://www.googleapis.com/auth/cloud-platform",
"scope" => List.first(@default_scopes),
"subject_token_type" => subject_token_type,
"subject_token" => subject_token_from_credential_source(credential_source, config)
})
Expand Down Expand Up @@ -440,12 +445,12 @@ defmodule Goth.Token do

defp handle_workload_identity_response(
{:ok, %{status: 200, body: body}},
%{source: {:workload_identity, %{"service_account_impersonation_url" => url}}} = config
%{source: {:workload_identity, %{"service_account_impersonation_url" => url}, options}} = config
) do
%{"access_token" => token, "token_type" => type} = Jason.decode!(body)

headers = [{"content-type", "text/json"}, {"Authorization", "#{type} #{token}"}]
body = Jason.encode!(%{scope: "https://www.googleapis.com/auth/cloud-platform"})
body = Jason.encode!(%{scope: Keyword.get(options, :scopes, @default_scopes)})
response = request(config.http_client, method: :post, url: url, headers: headers, body: body)

handle_response(response)
Expand Down

0 comments on commit 4551d0b

Please sign in to comment.