@@ -42,13 +42,16 @@ import (
4242 "io/ioutil"
4343 "net/http"
4444 "net/url"
45+ "strings"
4546 "time"
4647
4748 "golang.org/x/oauth2"
4849)
4950
50- var (
51- identityBindingEndpoint = "https://sts.googleapis.com/v1/token"
51+ const (
52+ universeDomainPlaceholder = "UNIVERSE_DOMAIN"
53+ identityBindingEndpointTemplate = "https://sts.UNIVERSE_DOMAIN/v1/token"
54+ universeDomainDefault = "googleapis.com"
5255)
5356
5457type accessBoundary struct {
@@ -105,6 +108,18 @@ type DownscopingConfig struct {
105108 // access (or set of accesses) that the new token has to a given resource.
106109 // There can be a maximum of 10 AccessBoundaryRules.
107110 Rules []AccessBoundaryRule
111+ // UniverseDomain is the default service domain for a given Cloud universe.
112+ // The default value is "googleapis.com". Optional.
113+ UniverseDomain string
114+ }
115+
116+ // identityBindingEndpoint returns the identity binding endpoint with the
117+ // configured universe domain.
118+ func (dc * DownscopingConfig ) identityBindingEndpoint () string {
119+ if dc .UniverseDomain == "" {
120+ return strings .Replace (identityBindingEndpointTemplate , universeDomainPlaceholder , universeDomainDefault , 1 )
121+ }
122+ return strings .Replace (identityBindingEndpointTemplate , universeDomainPlaceholder , dc .UniverseDomain , 1 )
108123}
109124
110125// A downscopingTokenSource is used to retrieve a downscoped token with restricted
@@ -114,6 +129,9 @@ type downscopingTokenSource struct {
114129 ctx context.Context
115130 // config holds the information necessary to generate a downscoped Token.
116131 config DownscopingConfig
132+ // identityBindingEndpoint is the identity binding endpoint with the
133+ // configured universe domain.
134+ identityBindingEndpoint string
117135}
118136
119137// NewTokenSource returns a configured downscopingTokenSource.
@@ -135,7 +153,11 @@ func NewTokenSource(ctx context.Context, conf DownscopingConfig) (oauth2.TokenSo
135153 return nil , fmt .Errorf ("downscope: all rules must provide at least one permission: %+v" , val )
136154 }
137155 }
138- return downscopingTokenSource {ctx : ctx , config : conf }, nil
156+ return downscopingTokenSource {
157+ ctx : ctx ,
158+ config : conf ,
159+ identityBindingEndpoint : conf .identityBindingEndpoint (),
160+ }, nil
139161}
140162
141163// Token() uses a downscopingTokenSource to generate an oauth2 Token.
@@ -171,7 +193,7 @@ func (dts downscopingTokenSource) Token() (*oauth2.Token, error) {
171193 form .Add ("options" , string (b ))
172194
173195 myClient := oauth2 .NewClient (dts .ctx , nil )
174- resp , err := myClient .PostForm (identityBindingEndpoint , form )
196+ resp , err := myClient .PostForm (dts . identityBindingEndpoint , form )
175197 if err != nil {
176198 return nil , fmt .Errorf ("unable to generate POST Request %v" , err )
177199 }
0 commit comments