aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/golang.org/x/oauth2/google/appengine_gen1.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/oauth2/google/appengine_gen1.go')
-rw-r--r--vendor/golang.org/x/oauth2/google/appengine_gen1.go77
1 files changed, 77 insertions, 0 deletions
diff --git a/vendor/golang.org/x/oauth2/google/appengine_gen1.go b/vendor/golang.org/x/oauth2/google/appengine_gen1.go
new file mode 100644
index 0000000..83dacac
--- /dev/null
+++ b/vendor/golang.org/x/oauth2/google/appengine_gen1.go
@@ -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
9package google
10
11import (
12 "context"
13 "sort"
14 "strings"
15 "sync"
16
17 "golang.org/x/oauth2"
18 "google.golang.org/appengine"
19)
20
21func init() {
22 appengineTokenFunc = appengine.AccessToken
23 appengineAppIDFunc = appengine.AppID
24}
25
26// See comment on AppEngineTokenSource in appengine.go.
27func 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.
38var (
39 aeTokensMu sync.Mutex
40 aeTokens = make(map[string]*tokenLock) // key is space-separated scopes
41)
42
43type tokenLock struct {
44 mu sync.Mutex // guards t; held while fetching or updating t
45 t *oauth2.Token
46}
47
48type gaeTokenSource struct {
49 ctx context.Context
50 scopes []string
51 key string // to aeTokens map; space-separated scopes
52}
53
54func (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}