@@ -232,6 +232,20 @@ func TestJWTFetch_AssertionPayload(t *testing.T) {
232232 "private1" : "claim1" ,
233233 },
234234 },
235+ {
236+ 237+ PrivateKey : dummyPrivateKey ,
238+ PrivateKeyID : "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ,
239+ TokenURL : ts .URL ,
240+ Audiences : []string {"https://api.example.com" },
241+ },
242+ {
243+ 244+ PrivateKey : dummyPrivateKey ,
245+ PrivateKeyID : "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ,
246+ TokenURL : ts .URL ,
247+ Audiences : []string {"https://api.example.com" , "https://other.example.com" },
248+ },
235249 } {
236250 t .Run (conf .Email , func (t * testing.T ) {
237251 _ , err := conf .TokenSource (context .Background ()).Token ()
@@ -259,13 +273,27 @@ func TestJWTFetch_AssertionPayload(t *testing.T) {
259273 // Scope should NOT be in the JWT claim set according to RFC 7521
260274 if claimSet .Scope != "" {
261275 t .Errorf ("payload scope should be empty but got %q; scopes should be sent as request parameter" , claimSet .Scope )
276+ } // Check audience handling per RFC 7519
277+ var expectedAud interface {}
278+ if len (conf .Audiences ) > 0 {
279+ if len (conf .Audiences ) == 1 {
280+ expectedAud = conf .Audiences [0 ]
281+ } else {
282+ // When JSON unmarshals an array, it becomes []interface{}
283+ expectedAudSlice := make ([]interface {}, len (conf .Audiences ))
284+ for i , aud := range conf .Audiences {
285+ expectedAudSlice [i ] = aud
286+ }
287+ expectedAud = expectedAudSlice
288+ }
289+ } else if conf .Audience != "" {
290+ expectedAud = conf .Audience
291+ } else {
292+ expectedAud = conf .TokenURL
262293 }
263- aud := conf .TokenURL
264- if conf .Audience != "" {
265- aud = conf .Audience
266- }
267- if got , want := claimSet .Aud , aud ; got != want {
268- t .Errorf ("payload audience = %q; want %q" , got , want )
294+
295+ if ! reflect .DeepEqual (claimSet .Aud , expectedAud ) {
296+ t .Errorf ("payload audience = %v (type %T); want %v (type %T)" , claimSet .Aud , claimSet .Aud , expectedAud , expectedAud )
269297 }
270298 if got , want := claimSet .Sub , conf .Subject ; got != want {
271299 t .Errorf ("payload subject = %q; want %q" , got , want )
0 commit comments