Skip to content

Commit 9f33145

Browse files
sbussbradfitz
authored andcommitted
google: Support scopes for ComputeTokenSource
Scopes have been added as a query parameter to the metadata server. Change-Id: Ife68db01beeca386e558edd424fa11da508b7287 GitHub-Last-Rev: 1cb4a6e GitHub-Pull-Request: #376 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/170106 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent c85d3e9 commit 9f33145

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

google/default.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ func DefaultTokenSource(ctx context.Context, scope ...string) (oauth2.TokenSourc
7373
// 4. On Google Compute Engine, Google App Engine standard second generation runtimes
7474
// (>= Go 1.11), and Google App Engine flexible environment, it fetches
7575
// credentials from the metadata server.
76-
// (In this final case any provided scopes are ignored.)
7776
func FindDefaultCredentials(ctx context.Context, scopes ...string) (*Credentials, error) {
7877
// First, try the environment variable.
7978
const envVar = "GOOGLE_APPLICATION_CREDENTIALS"
@@ -109,7 +108,7 @@ func FindDefaultCredentials(ctx context.Context, scopes ...string) (*Credentials
109108
id, _ := metadata.ProjectID()
110109
return &DefaultCredentials{
111110
ProjectID: id,
112-
TokenSource: ComputeTokenSource(""),
111+
TokenSource: ComputeTokenSource("", scopes...),
113112
}, nil
114113
}
115114

google/example_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ func ExampleComputeTokenSource() {
126126
// Fetch from Google Compute Engine's metadata server to retrieve
127127
// an access token for the provided account.
128128
// If no account is specified, "default" is used.
129-
Source: google.ComputeTokenSource(""),
129+
// If no scopes are specified, a set of default scopes
130+
// are automatically granted.
131+
Source: google.ComputeTokenSource("", "https://www.googleapis.com/auth/bigquery"),
130132
},
131133
}
132134
client.Get("...")

google/google.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/json"
1010
"errors"
1111
"fmt"
12+
"net/url"
1213
"strings"
1314
"time"
1415

@@ -151,14 +152,16 @@ func (f *credentialsFile) tokenSource(ctx context.Context, scopes []string) (oau
151152
// from Google Compute Engine (GCE)'s metadata server. It's only valid to use
152153
// this token source if your program is running on a GCE instance.
153154
// If no account is specified, "default" is used.
155+
// If no scopes are specified, a set of default scopes are automatically granted.
154156
// Further information about retrieving access tokens from the GCE metadata
155157
// server can be found at https://cloud.google.com/compute/docs/authentication.
156-
func ComputeTokenSource(account string) oauth2.TokenSource {
157-
return oauth2.ReuseTokenSource(nil, computeSource{account: account})
158+
func ComputeTokenSource(account string, scope ...string) oauth2.TokenSource {
159+
return oauth2.ReuseTokenSource(nil, computeSource{account: account, scopes: scope})
158160
}
159161

160162
type computeSource struct {
161163
account string
164+
scopes []string
162165
}
163166

164167
func (cs computeSource) Token() (*oauth2.Token, error) {
@@ -169,7 +172,13 @@ func (cs computeSource) Token() (*oauth2.Token, error) {
169172
if acct == "" {
170173
acct = "default"
171174
}
172-
tokenJSON, err := metadata.Get("instance/service-accounts/" + acct + "/token")
175+
tokenURI := "instance/service-accounts/" + acct + "/token"
176+
if len(cs.scopes) > 0 {
177+
v := url.Values{}
178+
v.Set("scopes", strings.Join(cs.scopes, ","))
179+
tokenURI = tokenURI + "?" + v.Encode()
180+
}
181+
tokenJSON, err := metadata.Get(tokenURI)
173182
if err != nil {
174183
return nil, err
175184
}

0 commit comments

Comments
 (0)