forked from Mirrors/oauth2
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 <codyoss@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cody Oss <codyoss@google.com>
This commit is contained in:
parent
43b6a7ba19
commit
8d6d45b6cd
|
@ -19,7 +19,10 @@ import (
|
||||||
"golang.org/x/oauth2/authhandler"
|
"golang.org/x/oauth2/authhandler"
|
||||||
)
|
)
|
||||||
|
|
||||||
const adcSetupURL = "https://cloud.google.com/docs/authentication/external/set-up-adc"
|
const (
|
||||||
|
adcSetupURL = "https://cloud.google.com/docs/authentication/external/set-up-adc"
|
||||||
|
universeDomainDefault = "googleapis.com"
|
||||||
|
)
|
||||||
|
|
||||||
// Credentials holds Google credentials, including "Application Default Credentials".
|
// Credentials holds Google credentials, including "Application Default Credentials".
|
||||||
// For more details, see:
|
// For more details, see:
|
||||||
|
@ -37,6 +40,18 @@ type Credentials struct {
|
||||||
// environment and not with a credentials file, e.g. when code is
|
// environment and not with a credentials file, e.g. when code is
|
||||||
// running on Google Cloud Platform.
|
// running on Google Cloud Platform.
|
||||||
JSON []byte
|
JSON []byte
|
||||||
|
|
||||||
|
// universeDomain is the default service domain for a given Cloud universe.
|
||||||
|
universeDomain string
|
||||||
|
}
|
||||||
|
|
||||||
|
// UniverseDomain returns the default service domain for a given Cloud universe.
|
||||||
|
// The default value is "googleapis.com".
|
||||||
|
func (c *Credentials) UniverseDomain() string {
|
||||||
|
if c.universeDomain == "" {
|
||||||
|
return universeDomainDefault
|
||||||
|
}
|
||||||
|
return c.universeDomain
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultCredentials is the old name of Credentials.
|
// DefaultCredentials is the old name of Credentials.
|
||||||
|
@ -200,6 +215,7 @@ func CredentialsFromJSONWithParams(ctx context.Context, jsonData []byte, params
|
||||||
if err := json.Unmarshal(jsonData, &f); err != nil {
|
if err := json.Unmarshal(jsonData, &f); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ts, err := f.tokenSource(ctx, params)
|
ts, err := f.tokenSource(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -209,6 +225,7 @@ func CredentialsFromJSONWithParams(ctx context.Context, jsonData []byte, params
|
||||||
ProjectID: f.ProjectID,
|
ProjectID: f.ProjectID,
|
||||||
TokenSource: ts,
|
TokenSource: ts,
|
||||||
JSON: jsonData,
|
JSON: jsonData,
|
||||||
|
universeDomain: f.UniverseDomain,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
// Copyright 2023 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package google
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var jwtJSONKeyUniverseDomain = []byte(`{
|
||||||
|
"type": "service_account",
|
||||||
|
"project_id": "fake_project",
|
||||||
|
"universe_domain": "example.com",
|
||||||
|
"private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
|
||||||
|
"private_key": "super secret key",
|
||||||
|
"client_email": "gopher@developer.gserviceaccount.com",
|
||||||
|
"client_id": "gopher.apps.googleusercontent.com",
|
||||||
|
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||||
|
"token_uri": "https://oauth2.googleapis.com/token",
|
||||||
|
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||||
|
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gopher%40fake_project.iam.gserviceaccount.com"
|
||||||
|
}`)
|
||||||
|
|
||||||
|
func TestCredentialsFromJSONWithParams_UniverseDomain(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
scope := "https://www.googleapis.com/auth/cloud-platform"
|
||||||
|
params := CredentialsParams{
|
||||||
|
Scopes: []string{scope},
|
||||||
|
}
|
||||||
|
creds, err := CredentialsFromJSONWithParams(ctx, jwtJSONKeyUniverseDomain, params)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if want := "fake_project"; creds.ProjectID != want {
|
||||||
|
t.Fatalf("got %q, want %q", creds.ProjectID, want)
|
||||||
|
}
|
||||||
|
if want := "example.com"; creds.UniverseDomain() != want {
|
||||||
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
||||||
|
}
|
||||||
|
}
|
|
@ -115,6 +115,7 @@ type credentialsFile struct {
|
||||||
AuthURL string `json:"auth_uri"`
|
AuthURL string `json:"auth_uri"`
|
||||||
TokenURL string `json:"token_uri"`
|
TokenURL string `json:"token_uri"`
|
||||||
ProjectID string `json:"project_id"`
|
ProjectID string `json:"project_id"`
|
||||||
|
UniverseDomain string `json:"universe_domain"`
|
||||||
|
|
||||||
// User Credential fields
|
// User Credential fields
|
||||||
// (These typically come from gcloud auth.)
|
// (These typically come from gcloud auth.)
|
||||||
|
|
Loading…
Reference in New Issue