Skip to content

Commit 0f0a2f0

Browse files
authored
fix(internal): set scopes for new auth flow (#2525)
Also updated a test that relied on impl specific types before. Testing the TokenSource methods instead of NewClient as NewClient defers impl to NewTokenSource anyways. Fixes: #2523 Fixes: #2522
1 parent f49960d commit 0f0a2f0

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

idtoken/integration_test.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212
"testing"
1313

14-
"golang.org/x/oauth2"
1514
"golang.org/x/oauth2/google"
1615
"google.golang.org/api/idtoken"
1716
"google.golang.org/api/option"
@@ -49,28 +48,7 @@ func TestNewTokenSource(t *testing.T) {
4948
}
5049
}
5150

52-
func TestNewClient_WithCredentialFile(t *testing.T) {
53-
if testing.Short() {
54-
t.Skip("skipping integration test")
55-
}
56-
client, err := idtoken.NewClient(context.Background(), aud, option.WithCredentialsFile(os.Getenv(envCredentialFile)))
57-
if err != nil {
58-
t.Fatalf("unable to create Client: %v", err)
59-
}
60-
tok, err := client.Transport.(*oauth2.Transport).Source.Token()
61-
if err != nil {
62-
t.Fatalf("unable to retrieve Token: %v", err)
63-
}
64-
validTok, err := idtoken.Validate(context.Background(), tok.AccessToken, aud)
65-
if err != nil {
66-
t.Fatalf("token validation failed: %v", err)
67-
}
68-
if validTok.Audience != aud {
69-
t.Fatalf("got %q, want %q", validTok.Audience, aud)
70-
}
71-
}
72-
73-
func TestNewClient_WithCredentialJSON(t *testing.T) {
51+
func TestNewTokenSource_WithCredentialJSON(t *testing.T) {
7452
if testing.Short() {
7553
t.Skip("skipping integration test")
7654
}
@@ -79,14 +57,19 @@ func TestNewClient_WithCredentialJSON(t *testing.T) {
7957
if err != nil {
8058
t.Fatalf("unable to find default creds: %v", err)
8159
}
82-
client, err := idtoken.NewClient(ctx, aud, option.WithCredentialsJSON(creds.JSON))
60+
ts, err := idtoken.NewTokenSource(ctx, aud, option.WithCredentialsJSON(creds.JSON))
8361
if err != nil {
8462
t.Fatalf("unable to create Client: %v", err)
8563
}
86-
tok, err := client.Transport.(*oauth2.Transport).Source.Token()
64+
tok, err := ts.Token()
8765
if err != nil {
8866
t.Fatalf("unable to retrieve Token: %v", err)
8967
}
68+
req := &http.Request{Header: make(http.Header)}
69+
tok.SetAuthHeader(req)
70+
if !strings.HasPrefix(req.Header.Get("Authorization"), "Bearer ") {
71+
t.Fatalf("token should sign requests with Bearer Authorization header")
72+
}
9073
validTok, err := idtoken.Validate(context.Background(), tok.AccessToken, aud)
9174
if err != nil {
9275
t.Fatalf("token validation failed: %v", err)

internal/creds.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ func credsNewAuth(ctx context.Context, settings *DialSettings) (*google.Credenti
6464
useSelfSignedJWT = true
6565
}
6666

67+
if len(settings.Scopes) > 0 {
68+
scopes = make([]string, len(settings.Scopes))
69+
copy(scopes, settings.Scopes)
70+
}
6771
if len(settings.Audiences) > 0 {
6872
aud = settings.Audiences[0]
6973
}
7074
// Only default scopes if user did not also set an audience.
7175
if len(settings.Scopes) == 0 && aud == "" && len(settings.DefaultScopes) > 0 {
72-
scopes = make([]string, len(scopes))
76+
scopes = make([]string, len(settings.DefaultScopes))
7377
copy(scopes, settings.DefaultScopes)
7478
}
7579
if len(scopes) == 0 && aud == "" {

0 commit comments

Comments
 (0)