oauth2/google/appengine.go

49 lines
1.3 KiB
Go
Raw Normal View History

2014-06-17 09:53:08 -04:00
// +build appengine
package google
import (
"strings"
"github.com/golang/oauth2"
2014-07-09 01:27:34 -04:00
"appengine"
"appengine/urlfetch"
2014-06-17 09:53:08 -04:00
)
2014-06-22 17:39:35 -04:00
// AppEngineConfig represents a configuration for an
// App Engine application's Google service account.
2014-06-17 09:53:08 -04:00
type AppEngineConfig struct {
context appengine.Context
scopes []string
}
2014-06-22 17:39:35 -04:00
// NewAppEngineConfig creates a new AppEngineConfig for the
// provided auth scopes.
2014-06-17 09:53:08 -04:00
func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig {
return &AppEngineConfig{context: context, scopes: scopes}
}
2014-06-22 17:39:35 -04:00
// NewTransport returns a transport that authorizes
// the requests with the application's service account.
2014-06-17 09:53:08 -04:00
func (c *AppEngineConfig) NewTransport() oauth2.Transport {
transport := &urlfetch.Transport{
Context: c.context,
Deadline: 0,
AllowInvalidServerCertificate: false,
}
return oauth2.NewAuthorizedTransport(transport, c, nil)
2014-06-17 09:53:08 -04:00
}
2014-06-22 17:39:35 -04:00
// FetchToken fetches a new access token for the provided scopes.
2014-06-17 09:53:08 -04:00
func (c *AppEngineConfig) FetchToken(existing *oauth2.Token) (*oauth2.Token, error) {
token, expiry, err := appengine.AccessToken(c.context, strings.Join(c.scopes, " "))
if err != nil {
return nil, err
}
return &oauth2.Token{
AccessToken: token,
Expiry: expiry,
}, nil
}