Skip to content

Commit

Permalink
Support the scopes option in workload identity
Browse files Browse the repository at this point in the history
  • Loading branch information
sekiyama58 committed Nov 14, 2024
1 parent 8c3431f commit 9c8e8e2
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/goth/token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,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 @@ -365,7 +370,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)
})
Expand Down Expand Up @@ -415,12 +420,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 9c8e8e2

Please sign in to comment.