From bf106852890b25f983bbd764d00510a0c35d3018 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Tue, 17 Jun 2014 15:53:08 +0200 Subject: [PATCH] Adding app engine implementations. --- google/appengine.go | 48 +++++++++++++++++++++++++++++++++++++++++++ google/appenginevm.go | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 google/appengine.go create mode 100644 google/appenginevm.go diff --git a/google/appengine.go b/google/appengine.go new file mode 100644 index 0000000..6bdf0a4 --- /dev/null +++ b/google/appengine.go @@ -0,0 +1,48 @@ +// +build appengine + +package google + +import ( + "strings" + + "appengine" + "github.com/golang/oauth2" +) + +type AppEngineConfig struct { + context appengine.Context + scopes []string + cache oauth2.Cache +} + +func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig { + return &AppEngineConfig{context: context, scopes: scopes} +} + +func (c *AppEngineConfig) NewTransport() oauth2.Transport { + return oauth2.NewAuthorizedTransport(c, nil) +} + +func (c *AppEngineConfig) NewTransportWithCache(cache oauth2.Cache) (oauth2.Transport, error) { + token, err := cache.Read() + if err != nil { + return nil, err + } + c.cache = cache + return oauth2.NewAuthorizedTransport(c, token), nil +} + +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 +} + +func (c *AppEngineConfig) Cache() oauth2.Cache { + return c.cache +} diff --git a/google/appenginevm.go b/google/appenginevm.go new file mode 100644 index 0000000..4a4fcdd --- /dev/null +++ b/google/appenginevm.go @@ -0,0 +1,48 @@ +// +build appenginevm + +package google + +import ( + "strings" + + "github.com/golang/oauth2" + "google.golang.org/appengine" +) + +type AppEngineConfig struct { + context appengine.Context + scopes []string + cache oauth2.Cache +} + +func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig { + return &AppEngineConfig{context: context, scopes: scopes} +} + +func (c *AppEngineConfig) NewTransport() oauth2.Transport { + return oauth2.NewAuthorizedTransport(c, nil) +} + +func (c *AppEngineConfig) NewTransportWithCache(cache oauth2.Cache) (oauth2.Transport, error) { + token, err := cache.Read() + if err != nil { + return nil, err + } + c.cache = cache + return oauth2.NewAuthorizedTransport(c, token), nil +} + +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 +} + +func (c *AppEngineConfig) Cache() oauth2.Cache { + return c.cache +}