forked from Mirrors/oauth2
Authorized transport should read from cache and write to cache.
This commit is contained in:
parent
227bfbf02f
commit
2e00ad50b1
11
transport.go
11
transport.go
|
@ -104,6 +104,12 @@ func NewAuthorizedTransport(fetcher TokenFetcher, token *Token) Transport {
|
||||||
// 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) {
|
||||||
token := t.Token()
|
token := t.Token()
|
||||||
|
cache := t.fetcher.Cache()
|
||||||
|
|
||||||
|
if token == nil && cache != nil {
|
||||||
|
// Try to read from cache initially
|
||||||
|
token, _ := cache.Read()
|
||||||
|
}
|
||||||
if token == nil || token.Expired() {
|
if token == nil || token.Expired() {
|
||||||
// Check if the token is refreshable.
|
// Check if the token is refreshable.
|
||||||
// If token is refreshable, don't return an error,
|
// If token is refreshable, don't return an error,
|
||||||
|
@ -169,6 +175,11 @@ func (t *authorizedTransport) RefreshToken() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.token = token
|
t.token = token
|
||||||
|
cache := t.fetcher.Cache()
|
||||||
|
if cache != nil {
|
||||||
|
cache.Write(token)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue