Skip to content

Commit 8d6d45b

Browse files
quartzmocodyoss
authored andcommitted
google: add Credentials.UniverseDomain to support TPC
Read and expose universe_domain from service account JSON files in CredentialsFromJSONWithParams to support TPC in 1p clients. Change-Id: I3518a0ec8be5ff7235b946cffd88b26ac8d303cf Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/531715 Run-TryBot: Cody Oss <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cody Oss <[email protected]>
1 parent 43b6a7b commit 8d6d45b

File tree

3 files changed

+74
-13
lines changed

3 files changed

+74
-13
lines changed

google/default.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import (
1919
"golang.org/x/oauth2/authhandler"
2020
)
2121

22-
const adcSetupURL = "https://cloud.google.com/docs/authentication/external/set-up-adc"
22+
const (
23+
adcSetupURL = "https://cloud.google.com/docs/authentication/external/set-up-adc"
24+
universeDomainDefault = "googleapis.com"
25+
)
2326

2427
// Credentials holds Google credentials, including "Application Default Credentials".
2528
// For more details, see:
@@ -37,6 +40,18 @@ type Credentials struct {
3740
// environment and not with a credentials file, e.g. when code is
3841
// running on Google Cloud Platform.
3942
JSON []byte
43+
44+
// universeDomain is the default service domain for a given Cloud universe.
45+
universeDomain string
46+
}
47+
48+
// UniverseDomain returns the default service domain for a given Cloud universe.
49+
// The default value is "googleapis.com".
50+
func (c *Credentials) UniverseDomain() string {
51+
if c.universeDomain == "" {
52+
return universeDomainDefault
53+
}
54+
return c.universeDomain
4055
}
4156

4257
// DefaultCredentials is the old name of Credentials.
@@ -200,15 +215,17 @@ func CredentialsFromJSONWithParams(ctx context.Context, jsonData []byte, params
200215
if err := json.Unmarshal(jsonData, &f); err != nil {
201216
return nil, err
202217
}
218+
203219
ts, err := f.tokenSource(ctx, params)
204220
if err != nil {
205221
return nil, err
206222
}
207223
ts = newErrWrappingTokenSource(ts)
208224
return &Credentials{
209-
ProjectID: f.ProjectID,
210-
TokenSource: ts,
211-
JSON: jsonData,
225+
ProjectID: f.ProjectID,
226+
TokenSource: ts,
227+
JSON: jsonData,
228+
universeDomain: f.UniverseDomain,
212229
}, nil
213230
}
214231

google/default_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package google
6+
7+
import (
8+
"context"
9+
"testing"
10+
)
11+
12+
var jwtJSONKeyUniverseDomain = []byte(`{
13+
"type": "service_account",
14+
"project_id": "fake_project",
15+
"universe_domain": "example.com",
16+
"private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
17+
"private_key": "super secret key",
18+
"client_email": "[email protected]",
19+
"client_id": "gopher.apps.googleusercontent.com",
20+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
21+
"token_uri": "https://oauth2.googleapis.com/token",
22+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
23+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gopher%40fake_project.iam.gserviceaccount.com"
24+
}`)
25+
26+
func TestCredentialsFromJSONWithParams_UniverseDomain(t *testing.T) {
27+
ctx := context.Background()
28+
scope := "https://www.googleapis.com/auth/cloud-platform"
29+
params := CredentialsParams{
30+
Scopes: []string{scope},
31+
}
32+
creds, err := CredentialsFromJSONWithParams(ctx, jwtJSONKeyUniverseDomain, params)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
37+
if want := "fake_project"; creds.ProjectID != want {
38+
t.Fatalf("got %q, want %q", creds.ProjectID, want)
39+
}
40+
if want := "example.com"; creds.UniverseDomain() != want {
41+
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
42+
}
43+
}

google/google.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222

2323
// Endpoint is Google's OAuth 2.0 default endpoint.
2424
var Endpoint = oauth2.Endpoint{
25-
AuthURL: "https://accounts.google.com/o/oauth2/auth",
26-
TokenURL: "https://oauth2.googleapis.com/token",
25+
AuthURL: "https://accounts.google.com/o/oauth2/auth",
26+
TokenURL: "https://oauth2.googleapis.com/token",
2727
DeviceAuthURL: "https://oauth2.googleapis.com/device/code",
28-
AuthStyle: oauth2.AuthStyleInParams,
28+
AuthStyle: oauth2.AuthStyleInParams,
2929
}
3030

3131
// MTLSTokenURL is Google's OAuth 2.0 default mTLS endpoint.
@@ -109,12 +109,13 @@ type credentialsFile struct {
109109
Type string `json:"type"`
110110

111111
// Service Account fields
112-
ClientEmail string `json:"client_email"`
113-
PrivateKeyID string `json:"private_key_id"`
114-
PrivateKey string `json:"private_key"`
115-
AuthURL string `json:"auth_uri"`
116-
TokenURL string `json:"token_uri"`
117-
ProjectID string `json:"project_id"`
112+
ClientEmail string `json:"client_email"`
113+
PrivateKeyID string `json:"private_key_id"`
114+
PrivateKey string `json:"private_key"`
115+
AuthURL string `json:"auth_uri"`
116+
TokenURL string `json:"token_uri"`
117+
ProjectID string `json:"project_id"`
118+
UniverseDomain string `json:"universe_domain"`
118119

119120
// User Credential fields
120121
// (These typically come from gcloud auth.)

0 commit comments

Comments
 (0)