From 4337573be3ff7c56f73bf00ad346a007ceff8f3e Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Tue, 17 Jun 2014 16:09:09 +0200 Subject: [PATCH] oauth2: perform caching ops at the transport level --- google/google.go | 7 ------- jwt.go | 13 +------------ oauth2.go | 16 +--------------- transport.go | 16 ++++++++++++++-- 4 files changed, 16 insertions(+), 36 deletions(-) diff --git a/google/google.go b/google/google.go index 8703fc2..1b56433 100644 --- a/google/google.go +++ b/google/google.go @@ -146,10 +146,3 @@ func (c *ComputeEngineConfig) FetchToken(existing *oauth2.Token) (token *oauth2. } return } - -// Cache returns nil. On Google Compute Engine, access tokens are -// retrieved from the metaserver, no other persistence layer is -// required. -func (c *ComputeEngineConfig) Cache() oauth2.Cache { - return nil -} diff --git a/jwt.go b/jwt.go index 284df3a..bb1aeb0 100644 --- a/jwt.go +++ b/jwt.go @@ -56,7 +56,6 @@ type JWTConfig struct { opts *JWTOptions aud string signature []byte - cache Cache } // NewTransport creates a transport that is authorize with the @@ -75,12 +74,7 @@ func (c *JWTConfig) NewTransportWithUser(user string) Transport { // token from the provided cache. If a token refreshing occurs, it // writes the newly fetched token back to the cache. func (c *JWTConfig) NewTransportWithCache(cache Cache) (Transport, error) { - token, err := cache.Read() - if err != nil { - return nil, err - } - c.cache = cache - return NewAuthorizedTransport(c, token), nil + return NewAuthorizedTransportWithCache(c, cache) } // fetchToken retrieves a new access token and updates the existing token @@ -151,8 +145,3 @@ func (c *JWTConfig) FetchToken(existing *Token) (token *Token, err error) { token.Expiry = time.Now().Add(time.Duration(b.ExpiresIn) * time.Second) return } - -// Cache returns a cache if specified, otherwise nil. -func (c *JWTConfig) Cache() Cache { - return c.cache -} diff --git a/oauth2.go b/oauth2.go index 606816f..b87de7d 100644 --- a/oauth2.go +++ b/oauth2.go @@ -64,8 +64,6 @@ type TokenFetcher interface { // If the implementation doesn't know how to retrieve a new token, // it returns an error. FetchToken(existing *Token) (*Token, error) - // Cache returns the Cache implementation to read/persist user tokens. - Cache() Cache } // Options represents options to provide OAuth 2.0 client credentials @@ -130,8 +128,6 @@ type Config struct { authURL string // TokenURL is the URL used to retrieve OAuth tokens. tokenURL string - - cache Cache } // AuthCodeURL returns a URL to OAuth 2.0 provider's consent page @@ -187,12 +183,7 @@ func (c *Config) NewTransportWithCode(exchangeCode string) (Transport, error) { // token from the provided cache. If a token refreshing occurs, it // writes the newly fetched token back to the cache. func (c *Config) NewTransportWithCache(cache Cache) (Transport, error) { - token, err := cache.Read() - if err != nil { - return nil, err - } - c.cache = cache - return NewAuthorizedTransport(c, token), nil + return NewAuthorizedTransportWithCache(c, cache) } // Exchange exchanges the exchange code with the OAuth 2.0 provider @@ -225,11 +216,6 @@ func (c *Config) FetchToken(existing *Token) (*Token, error) { return existing, err } -// Cache returns a cache if specified, otherwise nil. -func (c *Config) Cache() Cache { - return c.cache -} - // Checks if all required configuration fields have non-zero values. func (c *Config) validate() error { if c.opts.ClientID == "" { diff --git a/transport.go b/transport.go index f83d522..800245a 100644 --- a/transport.go +++ b/transport.go @@ -76,6 +76,9 @@ type Transport interface { } type authorizedTransport struct { + // Cache to persist changes to the token that + // authorizes the current transport. + cache Cache fetcher TokenFetcher token *Token @@ -90,6 +93,15 @@ func NewAuthorizedTransport(fetcher TokenFetcher, token *Token) Transport { return &authorizedTransport{fetcher: fetcher, token: token} } +func NewAuthorizedTransportWithCache(fetcher TokenFetcher, cache Cache) (transport Transport, err error) { + var token *Token + if token, err = cache.Read(); err != nil { + return + } + transport = &authorizedTransport{fetcher: fetcher, cache: cache, token: token} + return +} + // RoundTrip authorizes the request with the existing token. // If token is expired, tries to refresh/fetch a new token. func (t *authorizedTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) { @@ -160,8 +172,8 @@ func (t *authorizedTransport) RefreshToken() error { } t.token = token - if c := t.fetcher.Cache(); c != nil { - c.Write(token) + if t.cache != nil { + t.cache.Write(token) } return nil