@@ -20,13 +20,17 @@ import (
2020 "fmt"
2121 "net/http"
2222 "net/url"
23+ "strings"
2324 "time"
2425
2526 "cloud.google.com/go/auth"
2627 "cloud.google.com/go/auth/internal"
2728)
2829
29- var identityBindingEndpoint = "https://sts.googleapis.com/v1/token"
30+ const (
31+ universeDomainPlaceholder = "UNIVERSE_DOMAIN"
32+ identityBindingEndpointTemplate = "https://sts.UNIVERSE_DOMAIN/v1/token"
33+ )
3034
3135// Options for configuring [NewCredentials].
3236type Options struct {
@@ -42,15 +46,27 @@ type Options struct {
4246 // Client configures the underlying client used to make network requests
4347 // when fetching tokens. Optional.
4448 Client * http.Client
49+ // UniverseDomain is the default service domain for a given Cloud universe.
50+ // The default value is "googleapis.com". Optional.
51+ UniverseDomain string
4552}
4653
47- func (c Options ) client () * http.Client {
48- if c .Client != nil {
49- return c .Client
54+ func (o * Options ) client () * http.Client {
55+ if o .Client != nil {
56+ return o .Client
5057 }
5158 return internal .CloneDefaultClient ()
5259}
5360
61+ // identityBindingEndpoint returns the identity binding endpoint with the
62+ // configured universe domain.
63+ func (o * Options ) identityBindingEndpoint () string {
64+ if o .UniverseDomain == "" {
65+ return strings .Replace (identityBindingEndpointTemplate , universeDomainPlaceholder , internal .DefaultUniverseDomain , 1 )
66+ }
67+ return strings .Replace (identityBindingEndpointTemplate , universeDomainPlaceholder , o .UniverseDomain , 1 )
68+ }
69+
5470// An AccessBoundaryRule Sets the permissions (and optionally conditions) that
5571// the new token has on given resource.
5672type AccessBoundaryRule struct {
@@ -108,17 +124,24 @@ func NewCredentials(opts *Options) (*auth.Credentials, error) {
108124 }
109125 }
110126 return auth .NewCredentials (& auth.CredentialsOptions {
111- TokenProvider : & downscopedTokenProvider {Options : opts , Client : opts .client ()},
127+ TokenProvider : & downscopedTokenProvider {
128+ Options : opts ,
129+ Client : opts .client (),
130+ identityBindingEndpoint : opts .identityBindingEndpoint (),
131+ },
112132 ProjectIDProvider : auth .CredentialsPropertyFunc (opts .Credentials .ProjectID ),
113133 QuotaProjectIDProvider : auth .CredentialsPropertyFunc (opts .Credentials .QuotaProjectID ),
114- UniverseDomainProvider : auth . CredentialsPropertyFunc (opts . Credentials .UniverseDomain ),
134+ UniverseDomainProvider : internal . StaticCredentialsProperty (opts .UniverseDomain ),
115135 }), nil
116136}
117137
118138// downscopedTokenProvider is used to retrieve a downscoped tokens.
119139type downscopedTokenProvider struct {
120140 Options * Options
121141 Client * http.Client
142+ // identityBindingEndpoint is the identity binding endpoint with the
143+ // configured universe domain.
144+ identityBindingEndpoint string
122145}
123146
124147type downscopedOptions struct {
@@ -159,7 +182,7 @@ func (dts *downscopedTokenProvider) Token(ctx context.Context) (*auth.Token, err
159182 form .Add ("subject_token" , tok .Value )
160183 form .Add ("options" , string (b ))
161184
162- resp , err := dts .Client .PostForm (identityBindingEndpoint , form )
185+ resp , err := dts .Client .PostForm (dts . identityBindingEndpoint , form )
163186 if err != nil {
164187 return nil , err
165188 }
0 commit comments