forked from Mirrors/oauth2
oauth2: perform caching ops at the transport level
This commit is contained in:
parent
5c1195ac3f
commit
4337573be3
|
@ -146,10 +146,3 @@ func (c *ComputeEngineConfig) FetchToken(existing *oauth2.Token) (token *oauth2.
|
||||||
}
|
}
|
||||||
return
|
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
13
jwt.go
|
@ -56,7 +56,6 @@ type JWTConfig struct {
|
||||||
opts *JWTOptions
|
opts *JWTOptions
|
||||||
aud string
|
aud string
|
||||||
signature []byte
|
signature []byte
|
||||||
cache Cache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTransport creates a transport that is authorize with the
|
// 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
|
// token from the provided cache. If a token refreshing occurs, it
|
||||||
// writes the newly fetched token back to the cache.
|
// writes the newly fetched token back to the cache.
|
||||||
func (c *JWTConfig) NewTransportWithCache(cache Cache) (Transport, error) {
|
func (c *JWTConfig) NewTransportWithCache(cache Cache) (Transport, error) {
|
||||||
token, err := cache.Read()
|
return NewAuthorizedTransportWithCache(c, cache)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
c.cache = cache
|
|
||||||
return NewAuthorizedTransport(c, token), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchToken retrieves a new access token and updates the existing token
|
// 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)
|
token.Expiry = time.Now().Add(time.Duration(b.ExpiresIn) * time.Second)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache returns a cache if specified, otherwise nil.
|
|
||||||
func (c *JWTConfig) Cache() Cache {
|
|
||||||
return c.cache
|
|
||||||
}
|
|
||||||
|
|
16
oauth2.go
16
oauth2.go
|
@ -64,8 +64,6 @@ type TokenFetcher interface {
|
||||||
// If the implementation doesn't know how to retrieve a new token,
|
// If the implementation doesn't know how to retrieve a new token,
|
||||||
// it returns an error.
|
// it returns an error.
|
||||||
FetchToken(existing *Token) (*Token, 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
|
// Options represents options to provide OAuth 2.0 client credentials
|
||||||
|
@ -130,8 +128,6 @@ type Config struct {
|
||||||
authURL string
|
authURL string
|
||||||
// TokenURL is the URL used to retrieve OAuth tokens.
|
// TokenURL is the URL used to retrieve OAuth tokens.
|
||||||
tokenURL string
|
tokenURL string
|
||||||
|
|
||||||
cache Cache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthCodeURL returns a URL to OAuth 2.0 provider's consent page
|
// 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
|
// token from the provided cache. If a token refreshing occurs, it
|
||||||
// writes the newly fetched token back to the cache.
|
// writes the newly fetched token back to the cache.
|
||||||
func (c *Config) NewTransportWithCache(cache Cache) (Transport, error) {
|
func (c *Config) NewTransportWithCache(cache Cache) (Transport, error) {
|
||||||
token, err := cache.Read()
|
return NewAuthorizedTransportWithCache(c, cache)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
c.cache = cache
|
|
||||||
return NewAuthorizedTransport(c, token), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exchange exchanges the exchange code with the OAuth 2.0 provider
|
// 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
|
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.
|
// Checks if all required configuration fields have non-zero values.
|
||||||
func (c *Config) validate() error {
|
func (c *Config) validate() error {
|
||||||
if c.opts.ClientID == "" {
|
if c.opts.ClientID == "" {
|
||||||
|
|
16
transport.go
16
transport.go
|
@ -76,6 +76,9 @@ type Transport interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type authorizedTransport struct {
|
type authorizedTransport struct {
|
||||||
|
// Cache to persist changes to the token that
|
||||||
|
// authorizes the current transport.
|
||||||
|
cache Cache
|
||||||
fetcher TokenFetcher
|
fetcher TokenFetcher
|
||||||
token *Token
|
token *Token
|
||||||
|
|
||||||
|
@ -90,6 +93,15 @@ func NewAuthorizedTransport(fetcher TokenFetcher, token *Token) Transport {
|
||||||
return &authorizedTransport{fetcher: fetcher, token: token}
|
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.
|
// RoundTrip authorizes the request with the existing token.
|
||||||
// If token is expired, tries to refresh/fetch a new token.
|
// If token is expired, tries to refresh/fetch a new token.
|
||||||
func (t *authorizedTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
func (t *authorizedTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
||||||
|
@ -160,8 +172,8 @@ func (t *authorizedTransport) RefreshToken() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.token = token
|
t.token = token
|
||||||
if c := t.fetcher.Cache(); c != nil {
|
if t.cache != nil {
|
||||||
c.Write(token)
|
t.cache.Write(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue