Skip to content

Commit b92585a

Browse files
authored
Merge branch 'master' into claims-request-parameter-squashed
2 parents 5641e21 + c453e0c commit b92585a

21 files changed

+159
-122
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ See godoc for further documentation and examples.
2424

2525
In change 96e89be (March 2015), we removed the `oauth2.Context2` type in favor
2626
of the [`context.Context`](https://golang.org/x/net/context#Context) type from
27-
the `golang.org/x/net/context` package
27+
the `golang.org/x/net/context` package. Later replaced by the standard `context` package
28+
of the [`context.Context`](https://golang.org/pkg/context#Context) type.
29+
2830

2931
This means it's no longer possible to use the "Classic App Engine"
3032
`appengine.Context` type with the `oauth2` package. (You're using
@@ -44,7 +46,7 @@ with the `oauth2` package.
4446

4547
```go
4648
import (
47-
"golang.org/x/net/context"
49+
"context"
4850
"golang.org/x/oauth2"
4951
"golang.org/x/oauth2/google"
5052
newappengine "google.golang.org/appengine"

clientcredentials/clientcredentials.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
package clientcredentials // import "golang.org/x/oauth2/clientcredentials"
1515

1616
import (
17+
"context"
1718
"fmt"
1819
"net/http"
1920
"net/url"
2021
"strings"
2122

22-
"golang.org/x/net/context"
2323
"golang.org/x/oauth2"
2424
"golang.org/x/oauth2/internal"
2525
)

google/appengine.go

+19-70
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,34 @@
55
package google
66

77
import (
8-
"sort"
9-
"strings"
10-
"sync"
8+
"context"
119
"time"
1210

13-
"golang.org/x/net/context"
1411
"golang.org/x/oauth2"
1512
)
1613

17-
// appengineFlex is set at init time by appengineflex_hook.go. If true, we are on App Engine Flex.
18-
var appengineFlex bool
19-
20-
// Set at init time by appengine_hook.go. If nil, we're not on App Engine.
14+
// Set at init time by appengine_gen1.go. If nil, we're not on App Engine standard first generation (<= Go 1.9) or App Engine flexible.
2115
var appengineTokenFunc func(c context.Context, scopes ...string) (token string, expiry time.Time, err error)
2216

23-
// Set at init time by appengine_hook.go. If nil, we're not on App Engine.
17+
// Set at init time by appengine_gen1.go. If nil, we're not on App Engine standard first generation (<= Go 1.9) or App Engine flexible.
2418
var appengineAppIDFunc func(c context.Context) string
2519

26-
// AppEngineTokenSource returns a token source that fetches tokens
27-
// issued to the current App Engine application's service account.
28-
// If you are implementing a 3-legged OAuth 2.0 flow on App Engine
29-
// that involves user accounts, see oauth2.Config instead.
20+
// AppEngineTokenSource returns a token source that fetches tokens from either
21+
// the current application's service account or from the metadata server,
22+
// depending on the App Engine environment. See below for environment-specific
23+
// details. If you are implementing a 3-legged OAuth 2.0 flow on App Engine that
24+
// involves user accounts, see oauth2.Config instead.
25+
//
26+
// First generation App Engine runtimes (<= Go 1.9):
27+
// AppEngineTokenSource returns a token source that fetches tokens issued to the
28+
// current App Engine application's service account. The provided context must have
29+
// come from appengine.NewContext.
3030
//
31-
// The provided context must have come from appengine.NewContext.
31+
// Second generation App Engine runtimes (>= Go 1.11) and App Engine flexible:
32+
// AppEngineTokenSource is DEPRECATED on second generation runtimes and on the
33+
// flexible environment. It delegates to ComputeTokenSource, and the provided
34+
// context and scopes are not used. Please use DefaultTokenSource (or ComputeTokenSource,
35+
// which DefaultTokenSource will use in this case) instead.
3236
func AppEngineTokenSource(ctx context.Context, scope ...string) oauth2.TokenSource {
33-
if appengineTokenFunc == nil {
34-
panic("google: AppEngineTokenSource can only be used on App Engine.")
35-
}
36-
scopes := append([]string{}, scope...)
37-
sort.Strings(scopes)
38-
return &appEngineTokenSource{
39-
ctx: ctx,
40-
scopes: scopes,
41-
key: strings.Join(scopes, " "),
42-
}
43-
}
44-
45-
// aeTokens helps the fetched tokens to be reused until their expiration.
46-
var (
47-
aeTokensMu sync.Mutex
48-
aeTokens = make(map[string]*tokenLock) // key is space-separated scopes
49-
)
50-
51-
type tokenLock struct {
52-
mu sync.Mutex // guards t; held while fetching or updating t
53-
t *oauth2.Token
54-
}
55-
56-
type appEngineTokenSource struct {
57-
ctx context.Context
58-
scopes []string
59-
key string // to aeTokens map; space-separated scopes
60-
}
61-
62-
func (ts *appEngineTokenSource) Token() (*oauth2.Token, error) {
63-
if appengineTokenFunc == nil {
64-
panic("google: AppEngineTokenSource can only be used on App Engine.")
65-
}
66-
67-
aeTokensMu.Lock()
68-
tok, ok := aeTokens[ts.key]
69-
if !ok {
70-
tok = &tokenLock{}
71-
aeTokens[ts.key] = tok
72-
}
73-
aeTokensMu.Unlock()
74-
75-
tok.mu.Lock()
76-
defer tok.mu.Unlock()
77-
if tok.t.Valid() {
78-
return tok.t, nil
79-
}
80-
access, exp, err := appengineTokenFunc(ts.ctx, ts.scopes...)
81-
if err != nil {
82-
return nil, err
83-
}
84-
tok.t = &oauth2.Token{
85-
AccessToken: access,
86-
Expiry: exp,
87-
}
88-
return tok.t, nil
37+
return appEngineTokenSource(ctx, scope...)
8938
}

google/appengine_gen1.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build appengine
6+
7+
// This file applies to App Engine first generation runtimes (<= Go 1.9).
8+
9+
package google
10+
11+
import (
12+
"sort"
13+
"strings"
14+
"sync"
15+
16+
"golang.org/x/net/context"
17+
"golang.org/x/oauth2"
18+
"google.golang.org/appengine"
19+
)
20+
21+
func init() {
22+
appengineTokenFunc = appengine.AccessToken
23+
appengineAppIDFunc = appengine.AppID
24+
}
25+
26+
// See comment on AppEngineTokenSource in appengine.go.
27+
func appEngineTokenSource(ctx context.Context, scope ...string) oauth2.TokenSource {
28+
scopes := append([]string{}, scope...)
29+
sort.Strings(scopes)
30+
return &gaeTokenSource{
31+
ctx: ctx,
32+
scopes: scopes,
33+
key: strings.Join(scopes, " "),
34+
}
35+
}
36+
37+
// aeTokens helps the fetched tokens to be reused until their expiration.
38+
var (
39+
aeTokensMu sync.Mutex
40+
aeTokens = make(map[string]*tokenLock) // key is space-separated scopes
41+
)
42+
43+
type tokenLock struct {
44+
mu sync.Mutex // guards t; held while fetching or updating t
45+
t *oauth2.Token
46+
}
47+
48+
type gaeTokenSource struct {
49+
ctx context.Context
50+
scopes []string
51+
key string // to aeTokens map; space-separated scopes
52+
}
53+
54+
func (ts *gaeTokenSource) Token() (*oauth2.Token, error) {
55+
aeTokensMu.Lock()
56+
tok, ok := aeTokens[ts.key]
57+
if !ok {
58+
tok = &tokenLock{}
59+
aeTokens[ts.key] = tok
60+
}
61+
aeTokensMu.Unlock()
62+
63+
tok.mu.Lock()
64+
defer tok.mu.Unlock()
65+
if tok.t.Valid() {
66+
return tok.t, nil
67+
}
68+
access, exp, err := appengineTokenFunc(ts.ctx, ts.scopes...)
69+
if err != nil {
70+
return nil, err
71+
}
72+
tok.t = &oauth2.Token{
73+
AccessToken: access,
74+
Expiry: exp,
75+
}
76+
return tok.t, nil
77+
}

google/appengine_gen2_flex.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !appengine
6+
7+
// This file applies to App Engine second generation runtimes (>= Go 1.11) and App Engine flexible.
8+
9+
package google
10+
11+
import (
12+
"log"
13+
"sync"
14+
15+
"golang.org/x/net/context"
16+
"golang.org/x/oauth2"
17+
)
18+
19+
var logOnce sync.Once // only spam about deprecation once
20+
21+
// See comment on AppEngineTokenSource in appengine.go.
22+
func appEngineTokenSource(ctx context.Context, scope ...string) oauth2.TokenSource {
23+
logOnce.Do(func() {
24+
log.Print("google: AppEngineTokenSource is deprecated on App Engine standard second generation runtimes (>= Go 1.11) and App Engine flexible. Please use DefaultTokenSource or ComputeTokenSource.")
25+
})
26+
return ComputeTokenSource("")
27+
}

google/appengine_hook.go

-14
This file was deleted.

google/appengineflex_hook.go

-11
This file was deleted.

google/default.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package google
66

77
import (
8+
"context"
89
"encoding/json"
910
"fmt"
1011
"io/ioutil"
@@ -14,7 +15,6 @@ import (
1415
"runtime"
1516

1617
"cloud.google.com/go/compute/metadata"
17-
"golang.org/x/net/context"
1818
"golang.org/x/oauth2"
1919
)
2020

@@ -59,15 +59,18 @@ func findDefaultCredentials(ctx context.Context, scopes []string) (*DefaultCrede
5959
return nil, fmt.Errorf("google: error getting credentials using well-known file (%v): %v", filename, err)
6060
}
6161

62-
// Third, if we're on Google App Engine use those credentials.
63-
if appengineTokenFunc != nil && !appengineFlex {
62+
// Third, if we're on a Google App Engine standard first generation runtime (<= Go 1.9)
63+
// use those credentials. App Engine standard second generation runtimes (>= Go 1.11)
64+
// and App Engine flexible use ComputeTokenSource and the metadata server.
65+
if appengineTokenFunc != nil {
6466
return &DefaultCredentials{
6567
ProjectID: appengineAppIDFunc(ctx),
6668
TokenSource: AppEngineTokenSource(ctx, scopes...),
6769
}, nil
6870
}
6971

70-
// Fourth, if we're on Google Compute Engine use the metadata server.
72+
// Fourth, if we're on Google Compute Engine, an App Engine standard second generation runtime,
73+
// or App Engine flexible, use the metadata server.
7174
if metadata.OnGCE() {
7275
id, _ := metadata.ProjectID()
7376
return &DefaultCredentials{

google/example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
package google_test
66

77
import (
8+
"context"
89
"fmt"
910
"io/ioutil"
1011
"log"
1112
"net/http"
1213

13-
"golang.org/x/net/context"
1414
"golang.org/x/oauth2"
1515
"golang.org/x/oauth2/google"
1616
"golang.org/x/oauth2/jwt"

google/go19.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
package google
88

99
import (
10-
"golang.org/x/net/context"
10+
"context"
11+
1112
"golang.org/x/oauth2"
1213
)
1314

@@ -40,8 +41,10 @@ type DefaultCredentials = Credentials
4041
// 2. A JSON file in a location known to the gcloud command-line tool.
4142
// On Windows, this is %APPDATA%/gcloud/application_default_credentials.json.
4243
// On other systems, $HOME/.config/gcloud/application_default_credentials.json.
43-
// 3. On Google App Engine it uses the appengine.AccessToken function.
44-
// 4. On Google Compute Engine and Google App Engine Managed VMs, it fetches
44+
// 3. On Google App Engine standard first generation runtimes (<= Go 1.9) it uses
45+
// the appengine.AccessToken function.
46+
// 4. On Google Compute Engine, Google App Engine standard second generation runtimes
47+
// (>= Go 1.11), and Google App Engine flexible environment, it fetches
4548
// credentials from the metadata server.
4649
// (In this final case any provided scopes are ignored.)
4750
func FindDefaultCredentials(ctx context.Context, scopes ...string) (*Credentials, error) {

google/google.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
package google
66

77
import (
8+
"context"
89
"encoding/json"
910
"errors"
1011
"fmt"
1112
"strings"
1213
"time"
1314

1415
"cloud.google.com/go/compute/metadata"
15-
"golang.org/x/net/context"
1616
"golang.org/x/oauth2"
1717
"golang.org/x/oauth2/jwt"
1818
)

google/not_go19.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
package google
88

99
import (
10-
"golang.org/x/net/context"
10+
"context"
11+
1112
"golang.org/x/oauth2"
1213
)
1314

@@ -35,8 +36,10 @@ type DefaultCredentials struct {
3536
// 2. A JSON file in a location known to the gcloud command-line tool.
3637
// On Windows, this is %APPDATA%/gcloud/application_default_credentials.json.
3738
// On other systems, $HOME/.config/gcloud/application_default_credentials.json.
38-
// 3. On Google App Engine it uses the appengine.AccessToken function.
39-
// 4. On Google Compute Engine and Google App Engine Managed VMs, it fetches
39+
// 3. On Google App Engine standard first generation runtimes (<= Go 1.9) it uses
40+
// the appengine.AccessToken function.
41+
// 4. On Google Compute Engine, Google App Engine standard second generation runtimes
42+
// (>= Go 1.11), and Google App Engine flexible environment, it fetches
4043
// credentials from the metadata server.
4144
// (In this final case any provided scopes are ignored.)
4245
func FindDefaultCredentials(ctx context.Context, scopes ...string) (*DefaultCredentials, error) {

0 commit comments

Comments
 (0)