Skip to content

Commit 3501dcb

Browse files
committed
Merge branch 'master' of github.com:golang/oauth2
2 parents 429a0e6 + 9f33145 commit 3501dcb

File tree

6 files changed

+37
-33
lines changed

6 files changed

+37
-33
lines changed

facebook/facebook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ import (
1111

1212
// Endpoint is Facebook's OAuth 2.0 endpoint.
1313
var Endpoint = oauth2.Endpoint{
14-
AuthURL: "https://www.facebook.com/v3.1/dialog/oauth",
15-
TokenURL: "https://graph.facebook.com/v3.1/oauth/access_token",
14+
AuthURL: "https://www.facebook.com/v3.2/dialog/oauth",
15+
TokenURL: "https://graph.facebook.com/v3.2/oauth/access_token",
1616
}

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
}

internal/token.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,12 @@ type tokenJSON struct {
6363
TokenType string `json:"token_type"`
6464
RefreshToken string `json:"refresh_token"`
6565
ExpiresIn expirationTime `json:"expires_in"` // at least PayPal returns string, while most return number
66-
Expires expirationTime `json:"expires"` // broken Facebook spelling of expires_in
6766
}
6867

6968
func (e *tokenJSON) expiry() (t time.Time) {
7069
if v := e.ExpiresIn; v != 0 {
7170
return time.Now().Add(time.Duration(v) * time.Second)
7271
}
73-
if v := e.Expires; v != 0 {
74-
return time.Now().Add(time.Duration(v) * time.Second)
75-
}
7672
return
7773
}
7874

@@ -264,12 +260,6 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
264260
Raw: vals,
265261
}
266262
e := vals.Get("expires_in")
267-
if e == "" || e == "null" {
268-
// TODO(jbd): Facebook's OAuth2 implementation is broken and
269-
// returns expires_in field in expires. Remove the fallback to expires,
270-
// when Facebook fixes their implementation.
271-
e = vals.Get("expires")
272-
}
273263
expires, _ := strconv.Atoi(e)
274264
if expires != 0 {
275265
token.Expiry = time.Now().Add(time.Duration(expires) * time.Second)

oauth2_test.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -275,26 +275,26 @@ const day = 24 * time.Hour
275275
func TestExchangeRequest_JSONResponse_Expiry(t *testing.T) {
276276
seconds := int32(day.Seconds())
277277
for _, c := range []struct {
278-
name string
279-
expires string
280-
want bool
278+
name string
279+
expires string
280+
want bool
281+
nullExpires bool
281282
}{
282-
{"normal", fmt.Sprintf(`"expires_in": %d`, seconds), true},
283-
{"paypal", fmt.Sprintf(`"expires_in": "%d"`, seconds), true},
284-
{"facebook", fmt.Sprintf(`"expires": %d`, seconds), true},
285-
{"issue_239", fmt.Sprintf(`"expires_in": null, "expires": %d`, seconds), true},
286-
287-
{"wrong_type", `"expires": false`, false},
288-
{"wrong_type2", `"expires": {}`, false},
289-
{"wrong_value", `"expires": "zzz"`, false},
283+
{"normal", fmt.Sprintf(`"expires_in": %d`, seconds), true, false},
284+
{"paypal", fmt.Sprintf(`"expires_in": "%d"`, seconds), true, false},
285+
{"issue_239", fmt.Sprintf(`"expires_in": null`), true, true},
286+
287+
{"wrong_type", `"expires_in": false`, false, false},
288+
{"wrong_type2", `"expires_in": {}`, false, false},
289+
{"wrong_value", `"expires_in": "zzz"`, false, false},
290290
} {
291291
t.Run(c.name, func(t *testing.T) {
292-
testExchangeRequest_JSONResponse_expiry(t, c.expires, c.want)
292+
testExchangeRequest_JSONResponse_expiry(t, c.expires, c.want, c.nullExpires)
293293
})
294294
}
295295
}
296296

297-
func testExchangeRequest_JSONResponse_expiry(t *testing.T, exp string, want bool) {
297+
func testExchangeRequest_JSONResponse_expiry(t *testing.T, exp string, want, nullExpires bool) {
298298
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
299299
w.Header().Set("Content-Type", "application/json")
300300
w.Write([]byte(fmt.Sprintf(`{"access_token": "90d", "scope": "user", "token_type": "bearer", %s}`, exp)))
@@ -303,7 +303,7 @@ func testExchangeRequest_JSONResponse_expiry(t *testing.T, exp string, want bool
303303
conf := newConf(ts.URL)
304304
t1 := time.Now().Add(day)
305305
tok, err := conf.Exchange(context.Background(), "exchange-code")
306-
t2 := time.Now().Add(day)
306+
t2 := t1.Add(day)
307307

308308
if got := (err == nil); got != want {
309309
if want {
@@ -319,8 +319,12 @@ func testExchangeRequest_JSONResponse_expiry(t *testing.T, exp string, want bool
319319
t.Fatalf("Token invalid. Got: %#v", tok)
320320
}
321321
expiry := tok.Expiry
322+
323+
if nullExpires && expiry.IsZero() {
324+
return
325+
}
322326
if expiry.Before(t1) || expiry.After(t2) {
323-
t.Errorf("Unexpected value for Expiry: %v (shold be between %v and %v)", expiry, t1, t2)
327+
t.Errorf("Unexpected value for Expiry: %v (should be between %v and %v)", expiry, t1, t2)
324328
}
325329
}
326330

0 commit comments

Comments
 (0)