2015-08-26 20:42:02 -04:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-09-03 14:50:43 -04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-06-17 09:53:08 -04:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
2015-02-26 00:53:51 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-10-03 01:44:50 -04:00
|
|
|
"time"
|
2014-09-02 17:06:51 -04:00
|
|
|
|
2015-02-26 00:53:51 -05:00
|
|
|
"golang.org/x/net/context"
|
2014-12-11 02:30:13 -05:00
|
|
|
"golang.org/x/oauth2"
|
2014-11-06 19:36:41 -05:00
|
|
|
)
|
2014-10-03 01:44:50 -04:00
|
|
|
|
2015-09-15 20:49:47 -04:00
|
|
|
// Set at init time by appenginevm_hook.go. If true, we are on App Engine Managed VMs.
|
|
|
|
var appengineVM bool
|
|
|
|
|
2015-02-26 00:53:51 -05:00
|
|
|
// Set at init time by appengine_hook.go. If nil, we're not on App Engine.
|
|
|
|
var appengineTokenFunc func(c context.Context, scopes ...string) (token string, expiry time.Time, err error)
|
|
|
|
|
2016-11-07 16:06:47 -05:00
|
|
|
// Set at init time by appengine_hook.go. If nil, we're not on App Engine.
|
|
|
|
var appengineAppIDFunc func(c context.Context) string
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
// AppEngineTokenSource returns a token source that fetches tokens
|
|
|
|
// issued to the current App Engine application's service account.
|
|
|
|
// If you are implementing a 3-legged OAuth 2.0 flow on App Engine
|
|
|
|
// that involves user accounts, see oauth2.Config instead.
|
|
|
|
//
|
2015-01-22 00:09:47 -05:00
|
|
|
// The provided context must have come from appengine.NewContext.
|
2015-02-26 00:53:51 -05:00
|
|
|
func AppEngineTokenSource(ctx context.Context, scope ...string) oauth2.TokenSource {
|
|
|
|
if appengineTokenFunc == nil {
|
|
|
|
panic("google: AppEngineTokenSource can only be used on App Engine.")
|
|
|
|
}
|
|
|
|
scopes := append([]string{}, scope...)
|
|
|
|
sort.Strings(scopes)
|
2014-12-11 02:30:13 -05:00
|
|
|
return &appEngineTokenSource{
|
2015-02-26 00:53:51 -05:00
|
|
|
ctx: ctx,
|
|
|
|
scopes: scopes,
|
|
|
|
key: strings.Join(scopes, " "),
|
2014-08-31 18:13:59 -04:00
|
|
|
}
|
2014-06-17 09:53:08 -04:00
|
|
|
}
|
|
|
|
|
2015-02-26 00:53:51 -05:00
|
|
|
// aeTokens helps the fetched tokens to be reused until their expiration.
|
|
|
|
var (
|
|
|
|
aeTokensMu sync.Mutex
|
|
|
|
aeTokens = make(map[string]*tokenLock) // key is space-separated scopes
|
|
|
|
)
|
|
|
|
|
|
|
|
type tokenLock struct {
|
|
|
|
mu sync.Mutex // guards t; held while fetching or updating t
|
|
|
|
t *oauth2.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
type appEngineTokenSource struct {
|
|
|
|
ctx context.Context
|
|
|
|
scopes []string
|
|
|
|
key string // to aeTokens map; space-separated scopes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *appEngineTokenSource) Token() (*oauth2.Token, error) {
|
|
|
|
if appengineTokenFunc == nil {
|
|
|
|
panic("google: AppEngineTokenSource can only be used on App Engine.")
|
|
|
|
}
|
|
|
|
|
|
|
|
aeTokensMu.Lock()
|
|
|
|
tok, ok := aeTokens[ts.key]
|
2014-12-11 02:30:13 -05:00
|
|
|
if !ok {
|
2015-02-26 00:53:51 -05:00
|
|
|
tok = &tokenLock{}
|
|
|
|
aeTokens[ts.key] = tok
|
|
|
|
}
|
|
|
|
aeTokensMu.Unlock()
|
|
|
|
|
|
|
|
tok.mu.Lock()
|
|
|
|
defer tok.mu.Unlock()
|
|
|
|
if tok.t.Valid() {
|
|
|
|
return tok.t, nil
|
|
|
|
}
|
|
|
|
access, exp, err := appengineTokenFunc(ts.ctx, ts.scopes...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tok.t = &oauth2.Token{
|
|
|
|
AccessToken: access,
|
|
|
|
Expiry: exp,
|
2014-10-03 01:44:50 -04:00
|
|
|
}
|
2015-02-26 00:53:51 -05:00
|
|
|
return tok.t, nil
|
2014-09-02 17:06:51 -04:00
|
|
|
}
|