@@ -113,13 +113,20 @@ import (
113113 "net/http"
114114 "regexp"
115115 "strconv"
116+ "strings"
116117 "time"
117118
118119 "golang.org/x/oauth2"
119120 "golang.org/x/oauth2/google/internal/impersonate"
120121 "golang.org/x/oauth2/google/internal/stsexchange"
121122)
122123
124+ const (
125+ universeDomainPlaceholder = "UNIVERSE_DOMAIN"
126+ defaultTokenURL = "https://sts.UNIVERSE_DOMAIN/v1/token"
127+ defaultUniverseDomain = "googleapis.com"
128+ )
129+
123130// now aliases time.Now for testing
124131var now = func () time.Time {
125132 return time .Now ().UTC ()
@@ -139,7 +146,9 @@ type Config struct {
139146 // Required.
140147 SubjectTokenType string
141148 // TokenURL is the STS token exchange endpoint. If not provided, will default to
142- // https://sts.googleapis.com/v1/token. Optional.
149+ // https://sts.UNIVERSE_DOMAIN/v1/token, with UNIVERSE_DOMAIN set to the
150+ // default service domain googleapis.com unless UniverseDomain is set.
151+ // Optional.
143152 TokenURL string
144153 // TokenInfoURL is the token_info endpoint used to retrieve the account related information (
145154 // user attributes like account identifier, eg. email, username, uid, etc). This is
@@ -177,6 +186,10 @@ type Config struct {
177186 // AwsSecurityCredentialsSupplier is an AWS Security Credential supplier for AWS credentials.
178187 // One of SubjectTokenSupplier, AWSSecurityCredentialSupplier or CredentialSource must be provided. Optional.
179188 AwsSecurityCredentialsSupplier AwsSecurityCredentialsSupplier
189+ // UniverseDomain is the default service domain for a given Cloud universe.
190+ // This value will be used in the default STS token URL. The default value
191+ // is "googleapis.com". It will not be used if TokenURL is set. Optional.
192+ UniverseDomain string
180193}
181194
182195var (
@@ -246,9 +259,8 @@ func (c *Config) tokenSource(ctx context.Context, scheme string) (oauth2.TokenSo
246259
247260// Subject token file types.
248261const (
249- fileTypeText = "text"
250- fileTypeJSON = "json"
251- defaultTokenUrl = "https://sts.googleapis.com/v1/token"
262+ fileTypeText = "text"
263+ fileTypeJSON = "json"
252264)
253265
254266// Format contains information needed to retireve a subject token for URL or File sourced credentials.
@@ -336,11 +348,20 @@ type SupplierOptions struct {
336348 SubjectTokenType string
337349}
338350
351+ // tokenURL returns the default STS token endpoint with the configured universe
352+ // domain.
353+ func (c * Config ) tokenURL () string {
354+ if c .UniverseDomain == "" {
355+ return strings .Replace (defaultTokenURL , universeDomainPlaceholder , defaultUniverseDomain , 1 )
356+ }
357+ return strings .Replace (defaultTokenURL , universeDomainPlaceholder , c .UniverseDomain , 1 )
358+ }
359+
339360// parse determines the type of CredentialSource needed.
340361func (c * Config ) parse (ctx context.Context ) (baseCredentialSource , error ) {
341362 //set Defaults
342363 if c .TokenURL == "" {
343- c .TokenURL = defaultTokenUrl
364+ c .TokenURL = c . tokenURL ()
344365 }
345366 supplierOptions := SupplierOptions {Audience : c .Audience , SubjectTokenType : c .SubjectTokenType }
346367
0 commit comments