Skip to content

Commit

Permalink
remove cache
Browse files Browse the repository at this point in the history
  • Loading branch information
proppy committed Jul 9, 2014
1 parent e62c791 commit 93ad3f4
Show file tree
Hide file tree
Showing 11 changed files with 10 additions and 262 deletions.
55 changes: 0 additions & 55 deletions cache.go

This file was deleted.

74 changes: 0 additions & 74 deletions cache_test.go

This file was deleted.

31 changes: 0 additions & 31 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,6 @@ func Example_config() {
// start making authenticated requests.
client := http.Client{Transport: t}
client.Get("...")

// Alternatively, you can initiate a new transport
// with tokens from a cache.
cache := oauth2.NewFileCache("/path/to/file")
// NewTransportWithCache will try to read the cached
// token, if any error occurs, it returns the error.
// If a token is available at the cache, initiates
// a new transport authorized and authenticated with
// the read token. If token expires, and a new access
// token is retrieved, it writes the newly fetched
// token to the cache.
t, err = conf.NewTransportWithCache(cache)
if err != nil {
log.Fatal(err)
}
client = http.Client{Transport: t}
client.Get("...")
}

func Example_jWTConfig() {
Expand Down Expand Up @@ -95,18 +78,4 @@ func Example_jWTConfig() {
// request will be made on the behalf of [email protected].
client = http.Client{Transport: conf.NewTransportWithUser("[email protected]")}
client.Get("...")

// Alternatively you can iniate a transport with
// a token read from the cache.
// If the existing access token expires, and a new access token is
// retrieved, the newly fetched token will be written to the cache.
cache := oauth2.NewFileCache("/path/to/file")
t, err := conf.NewTransportWithCache(cache)
if err != nil {
log.Fatal(err)
}
client = http.Client{Transport: t}
// The following request will be authorized by the token
// retrieved from the cache.
client.Get("...")
}
9 changes: 2 additions & 7 deletions google/appengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package google
import (
"strings"

"appengine"
"github.com/golang/oauth2"

"appengine"
)

// AppEngineConfig represents a configuration for an
Expand All @@ -28,12 +29,6 @@ func (c *AppEngineConfig) NewTransport() oauth2.Transport {
return oauth2.NewAuthorizedTransport(c, nil)
}

// NewTransport returns a token-caching transport that authorizes
// the requests with the application's service account.
func (c *AppEngineConfig) NewTransportWithCache(cache oauth2.Cache) (oauth2.Transport, error) {
return oauth2.NewAuthorizedTransportWithCache(c, cache)
}

// FetchToken fetches a new access token for the provided scopes.
func (c *AppEngineConfig) FetchToken(existing *oauth2.Token) (*oauth2.Token, error) {
token, expiry, err := appengine.AccessToken(c.context, strings.Join(c.scopes, " "))
Expand Down
7 changes: 0 additions & 7 deletions google/appenginevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
type AppEngineConfig struct {
context appengine.Context
scopes []string
cache oauth2.Cache
}

// NewAppEngineConfig creates a new AppEngineConfig for the
Expand All @@ -29,12 +28,6 @@ func (c *AppEngineConfig) NewTransport() oauth2.Transport {
return oauth2.NewAuthorizedTransport(c, nil)
}

// NewTransport returns a token-caching transport that authorizes
// the requests with the application's service account.
func (c *AppEngineConfig) NewTransportWithCache(cache oauth2.Cache) (oauth2.Transport, error) {
return oauth2.NewAuthorizedTransportWithCache(c, cache)
}

// FetchToken fetches a new access token for the provided scopes.
func (c *AppEngineConfig) FetchToken(existing *oauth2.Token) (*oauth2.Token, error) {
token, expiry, err := appengine.AccessToken(c.context, strings.Join(c.scopes, " "))
Expand Down
31 changes: 0 additions & 31 deletions google/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,6 @@ func Example_webServer() {
}
client := http.Client{Transport: t}
client.Get("...")

// Alternatively you can initiate a new transport
// with a token from a cache.
cache := oauth2.NewFileCache("/path/to/file")
// NewTransportWithCache will try to read the cached
// token, if any error occurs, it returns the error.
// If a token is available at the cache, initiates
// a new transport authorized and authenticated with
// the read token. If token expires, and a new access
// token is retrieved, it writes the newly fetched
// token to the cache.
t, err = config.NewTransportWithCache(cache)
if err != nil {
log.Fatal(err)
}
client = http.Client{Transport: t}
client.Get("...")
}

func Example_serviceAccounts() {
Expand Down Expand Up @@ -92,20 +75,6 @@ func Example_serviceAccounts() {
// request will be made on the behalf of [email protected].
client = http.Client{Transport: config.NewTransportWithUser("[email protected]")}
client.Get("...")

// Alternatively you can iniate a transport with
// a token read from the cache.
// If the existing access token expires, and a new access token is
// retrieved, the newly fetched token will be written to the cache.
cache := oauth2.NewFileCache("/path/to/file")
t, err := config.NewTransportWithCache(cache)
if err != nil {
log.Fatal(err)
}
client = http.Client{Transport: t}
// The following request will be authorized by the token
// retrieved from the cache.
client.Get("...")
}

func Example_appEngine() {
Expand Down
7 changes: 0 additions & 7 deletions jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ func (c *JWTConfig) NewTransportWithUser(user string) Transport {
return NewAuthorizedTransport(c, &Token{Subject: user})
}

// NewTransportWithCache initializes a transport by reading the initial
// 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) {
return NewAuthorizedTransportWithCache(c, cache)
}

// fetchToken retrieves a new access token and updates the existing token
// with the newly fetched credentials.
func (c *JWTConfig) FetchToken(existing *Token) (token *Token, err error) {
Expand Down
7 changes: 0 additions & 7 deletions oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,6 @@ func (c *Config) NewTransportWithCode(exchangeCode string) (Transport, error) {
return NewAuthorizedTransport(c, token), nil
}

// NewTransportWithCache initializes a transport by reading the initial
// 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) {
return NewAuthorizedTransportWithCache(c, cache)
}

// FetchToken retrieves a new access token and updates the existing token
// with the newly fetched credentials. If existing token doesn't
// contain a refresh token, it returns an error.
Expand Down
2 changes: 1 addition & 1 deletion oauth2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestExchangePayload(t *testing.T) {
return nil, errors.New("no response")
},
}
conf.Exchange("exchange-code")
conf.exchange("exchange-code")
}

func TestExchangingTransport(t *testing.T) {
Expand Down
19 changes: 0 additions & 19 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ type Transport interface {
}

type authorizedTransport struct {
// Cache to persist changes to the token that
// authorizes the current transport.
cache Cache
fetcher TokenFetcher
token *Token

Expand All @@ -93,19 +90,6 @@ func NewAuthorizedTransport(fetcher TokenFetcher, token *Token) Transport {
return &authorizedTransport{fetcher: fetcher, token: token}
}

// NewAuthorizedTransportWithCache creates a new transport that uses
// the provided token fetcher and cache. Before constructing the new
// transport, it will try to read from the cache to see if there
// is an existing 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) {
Expand Down Expand Up @@ -176,9 +160,6 @@ func (t *authorizedTransport) RefreshToken() error {
}

t.token = token
if t.cache != nil {
return t.cache.Write(token)
}

return nil
}
Expand Down
Loading

7 comments on commit 93ad3f4

@johnl
Copy link

@johnl johnl commented on 93ad3f4 Aug 2, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, can I ask why this was removed? I'm seeing a lot of people trying to make oauth2 do this kind of thing and having trouble (me included). What's the right way to cache tokens now? Thanks!

@adg
Copy link
Contributor

@adg adg commented on 93ad3f4 Aug 2, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was removed because it's not an API that we want to support. We need to revisit caching and design a nicer way of doing it. I think it would be something like a CachingTokenSource whose constructor takes a TokenCache.

cc @broady @bradfitz

@mayeco
Copy link

@mayeco mayeco commented on 93ad3f4 Aug 23, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnl I think is better to remove it, I dont use this, for me is better to cache the tokens in Memcache that in files.

@rakyll
Copy link
Contributor

@rakyll rakyll commented on 93ad3f4 Sep 28, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adg There is no point of polluting the APIs. Users must wrap the RoundTripper to cache. We must document it by providing a sample. If you would like to contribute a sample, I can review.

@broady
Copy link
Contributor

@broady broady commented on 93ad3f4 Sep 28, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A wrapper for TokenSource can provide caching. +1 for a code sample.

ReuseTokenSource is a good example.

https://github.com/golang/oauth2/blob/master/oauth2.go#L310

@bradfitz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to a bug.

@rakyll
Copy link
Contributor

@rakyll rakyll commented on 93ad3f4 Sep 28, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #84.

Please sign in to comment.