oauth2: perform caching ops at the transport level

This commit is contained in:
Burcu Dogan 2014-06-17 16:09:09 +02:00
parent 5c1195ac3f
commit 4337573be3
4 changed files with 16 additions and 36 deletions

View File

@ -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
}

13
jwt.go
View File

@ -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
}

View File

@ -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 == "" {

View File

@ -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