Skip to content

Commit 622c5d5

Browse files
shaporcodyoss
authored andcommitted
google/google: set JWT Audience in JWTConfigFromJSON()
Add support to set JWT Audience in JWTConfigFromJSON() to allow setting the audience field from the JSON config, rather than only allowing it the default value of the token_uri. Previous change 272766 (approved but abandoned). Change-Id: I14d46f3628df0a04801949bf99520b210e778f99 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/406836 Reviewed-by: Cody Oss <codyoss@google.com> Run-TryBot: Cody Oss <codyoss@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
1 parent 9780585 commit 622c5d5

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

‎google/google.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func (f *credentialsFile) jwtConfig(scopes []string, subject string) *jwt.Config
139139
Scopes: scopes,
140140
TokenURL: f.TokenURL,
141141
Subject: subject, // This is the user email to impersonate
142+
Audience: f.Audience,
142143
}
143144
if cfg.TokenURL == "" {
144145
cfg.TokenURL = JWTTokenURL

‎google/google_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ var jwtJSONKey = []byte(`{
3737
"client_email": "gopher@developer.gserviceaccount.com",
3838
"client_id": "gopher.apps.googleusercontent.com",
3939
"token_uri": "https://accounts.google.com/o/gophers/token",
40-
"type": "service_account"
40+
"type": "service_account",
41+
"audience": "https://testservice.googleapis.com/"
4142
}`)
4243

4344
var jwtJSONKeyNoTokenURL = []byte(`{
@@ -48,6 +49,15 @@ var jwtJSONKeyNoTokenURL = []byte(`{
4849
"type": "service_account"
4950
}`)
5051

52+
var jwtJSONKeyNoAudience = []byte(`{
53+
"private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
54+
"private_key": "super secret key",
55+
"client_email": "gopher@developer.gserviceaccount.com",
56+
"client_id": "gopher.apps.googleusercontent.com",
57+
"token_uri": "https://accounts.google.com/o/gophers/token",
58+
"type": "service_account"
59+
}`)
60+
5161
func TestConfigFromJSON(t *testing.T) {
5262
conf, err := ConfigFromJSON(webJSONKey, "scope1", "scope2")
5363
if err != nil {
@@ -103,6 +113,9 @@ func TestJWTConfigFromJSON(t *testing.T) {
103113
if got, want := conf.TokenURL, "https://accounts.google.com/o/gophers/token"; got != want {
104114
t.Errorf("TokenURL = %q; want %q", got, want)
105115
}
116+
if got, want := conf.Audience, "https://testservice.googleapis.com/"; got != want {
117+
t.Errorf("Audience = %q; want %q", got, want)
118+
}
106119
}
107120

108121
func TestJWTConfigFromJSONNoTokenURL(t *testing.T) {
@@ -114,3 +127,13 @@ func TestJWTConfigFromJSONNoTokenURL(t *testing.T) {
114127
t.Errorf("TokenURL = %q; want %q", got, want)
115128
}
116129
}
130+
131+
func TestJWTConfigFromJSONNoAudience(t *testing.T) {
132+
conf, err := JWTConfigFromJSON(jwtJSONKeyNoAudience, "scope1", "scope2")
133+
if err != nil {
134+
t.Fatal(err)
135+
}
136+
if got, want := conf.Audience, ""; got != want {
137+
t.Errorf("Audience = %q; want %q", got, want)
138+
}
139+
}

0 commit comments

Comments
 (0)