@@ -42,13 +42,16 @@ import (
42
42
"io/ioutil"
43
43
"net/http"
44
44
"net/url"
45
+ "strings"
45
46
"time"
46
47
47
48
"golang.org/x/oauth2"
48
49
)
49
50
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"
52
55
)
53
56
54
57
type accessBoundary struct {
@@ -105,6 +108,18 @@ type DownscopingConfig struct {
105
108
// access (or set of accesses) that the new token has to a given resource.
106
109
// There can be a maximum of 10 AccessBoundaryRules.
107
110
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 )
108
123
}
109
124
110
125
// A downscopingTokenSource is used to retrieve a downscoped token with restricted
@@ -114,6 +129,9 @@ type downscopingTokenSource struct {
114
129
ctx context.Context
115
130
// config holds the information necessary to generate a downscoped Token.
116
131
config DownscopingConfig
132
+ // identityBindingEndpoint is the identity binding endpoint with the
133
+ // configured universe domain.
134
+ identityBindingEndpoint string
117
135
}
118
136
119
137
// NewTokenSource returns a configured downscopingTokenSource.
@@ -135,7 +153,11 @@ func NewTokenSource(ctx context.Context, conf DownscopingConfig) (oauth2.TokenSo
135
153
return nil , fmt .Errorf ("downscope: all rules must provide at least one permission: %+v" , val )
136
154
}
137
155
}
138
- return downscopingTokenSource {ctx : ctx , config : conf }, nil
156
+ return downscopingTokenSource {
157
+ ctx : ctx ,
158
+ config : conf ,
159
+ identityBindingEndpoint : conf .identityBindingEndpoint (),
160
+ }, nil
139
161
}
140
162
141
163
// Token() uses a downscopingTokenSource to generate an oauth2 Token.
@@ -171,7 +193,7 @@ func (dts downscopingTokenSource) Token() (*oauth2.Token, error) {
171
193
form .Add ("options" , string (b ))
172
194
173
195
myClient := oauth2 .NewClient (dts .ctx , nil )
174
- resp , err := myClient .PostForm (identityBindingEndpoint , form )
196
+ resp , err := myClient .PostForm (dts . identityBindingEndpoint , form )
175
197
if err != nil {
176
198
return nil , fmt .Errorf ("unable to generate POST Request %v" , err )
177
199
}
0 commit comments