2014-09-03 14:50:43 -04:00
|
|
|
// Copyright 2014 The oauth2 Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-16 14:23:54 -05:00
|
|
|
// +build appenginevm
|
2014-06-17 09:53:08 -04:00
|
|
|
|
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
2014-10-03 01:44:50 -04:00
|
|
|
"time"
|
2014-08-31 18:36:50 -04:00
|
|
|
|
2014-11-26 14:44:45 -05:00
|
|
|
"golang.org/x/oauth2"
|
2014-06-17 09:53:08 -04:00
|
|
|
"google.golang.org/appengine"
|
|
|
|
)
|
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
// You are required to provide a valid appengine.Context as context.
|
2015-01-16 14:23:54 -05:00
|
|
|
func AppEngineTokenSource(ctx oauth2.Context, scope ...string) oauth2.TokenSource {
|
2014-12-11 02:30:13 -05:00
|
|
|
return &appEngineTokenSource{
|
|
|
|
ctx: ctx,
|
|
|
|
scopes: scope,
|
|
|
|
fetcherFunc: aeVMFetcherFunc,
|
2014-08-31 18:13:59 -04:00
|
|
|
}
|
2014-06-17 09:53:08 -04:00
|
|
|
}
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
var aeVMFetcherFunc = func(ctx oauth2.Context, scope ...string) (string, time.Time, error) {
|
|
|
|
c, ok := ctx.(appengine.Context)
|
|
|
|
if !ok {
|
|
|
|
return "", time.Time{}, errInvalidContext
|
2014-10-03 01:44:50 -04:00
|
|
|
}
|
2014-12-11 02:30:13 -05:00
|
|
|
return appengine.AccessToken(c, scope...)
|
2014-09-02 17:06:51 -04:00
|
|
|
}
|