Skip to content

Commit deefa7e

Browse files
quartzmogopherbot
authored andcommitted
google/downscope: add DownscopingConfig.UniverseDomain to support TPC
Change-Id: I3669352b382414ea640ca176afa4071995fc5ff1 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/557135 Reviewed-by: Cody Oss <[email protected]> TryBot-Bypass: Cody Oss <[email protected]> Auto-Submit: Cody Oss <[email protected]>
1 parent 39adbb7 commit deefa7e

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

google/downscope/downscoping.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -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

5457
type 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
}

google/downscope/downscoping_test.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,43 @@ func Test_DownscopedTokenSource(t *testing.T) {
3838
w.Write([]byte(standardRespBody))
3939

4040
}))
41-
new := []AccessBoundaryRule{
41+
myTok := oauth2.Token{AccessToken: "Mellon"}
42+
tmpSrc := oauth2.StaticTokenSource(&myTok)
43+
rules := []AccessBoundaryRule{
4244
{
4345
AvailableResource: "test1",
4446
AvailablePermissions: []string{"Perm1", "Perm2"},
4547
},
4648
}
47-
myTok := oauth2.Token{AccessToken: "Mellon"}
48-
tmpSrc := oauth2.StaticTokenSource(&myTok)
49-
dts := downscopingTokenSource{context.Background(), DownscopingConfig{tmpSrc, new}}
50-
identityBindingEndpoint = ts.URL
49+
dts := downscopingTokenSource{
50+
ctx: context.Background(),
51+
config: DownscopingConfig{
52+
RootSource: tmpSrc,
53+
Rules: rules,
54+
},
55+
identityBindingEndpoint: ts.URL,
56+
}
5157
_, err := dts.Token()
5258
if err != nil {
5359
t.Fatalf("NewDownscopedTokenSource failed with error: %v", err)
5460
}
5561
}
62+
63+
func Test_DownscopingConfig(t *testing.T) {
64+
tests := []struct {
65+
universeDomain string
66+
want string
67+
}{
68+
{"", "https://sts.googleapis.com/v1/token"},
69+
{"googleapis.com", "https://sts.googleapis.com/v1/token"},
70+
{"example.com", "https://sts.example.com/v1/token"},
71+
}
72+
for _, tt := range tests {
73+
c := DownscopingConfig{
74+
UniverseDomain: tt.universeDomain,
75+
}
76+
if got := c.identityBindingEndpoint(); got != tt.want {
77+
t.Errorf("got %q, want %q", got, tt.want)
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)