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

Support the scopes option in workload identity #178

Merged
merged 1 commit into from
Dec 20, 2024
Merged
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
13 changes: 9 additions & 4 deletions lib/goth/token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,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 @@ -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
Loading