forked from Mirrors/oauth2
Add public TokenRefresher
This commit is contained in:
parent
e067960af8
commit
d87fd3250e
31
oauth2.go
31
oauth2.go
|
@ -260,6 +260,37 @@ func (c *Config) TokenSource(ctx context.Context, t *Token) TokenSource {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TokenRefresher is a TokenSource that makes "grant_type"=="refresh_token"
|
||||||
|
// HTTP requests to renew a token using a RefreshToken.
|
||||||
|
type TokenRefresher struct {
|
||||||
|
ctx context.Context // used to get HTTP requests
|
||||||
|
conf *Config
|
||||||
|
refreshToken string
|
||||||
|
}
|
||||||
|
|
||||||
|
// WARNING: Token is not safe for concurrent access, as it
|
||||||
|
// updates the tokenRefresher's refreshToken field.
|
||||||
|
// Within this package, it is used by reuseTokenSource which
|
||||||
|
// synchronizes calls to this method with its own mutex.
|
||||||
|
func (tf *TokenRefresher) Token() (*Token, error) {
|
||||||
|
if tf.refreshToken == "" {
|
||||||
|
return nil, errors.New("oauth2: token expired and refresh token is not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
tk, err := retrieveToken(tf.ctx, tf.conf, url.Values{
|
||||||
|
"grant_type": {"refresh_token"},
|
||||||
|
"refresh_token": {tf.refreshToken},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if tf.refreshToken != tk.RefreshToken {
|
||||||
|
tf.refreshToken = tk.RefreshToken
|
||||||
|
}
|
||||||
|
return tk, err
|
||||||
|
}
|
||||||
|
|
||||||
// tokenRefresher is a TokenSource that makes "grant_type"=="refresh_token"
|
// tokenRefresher is a TokenSource that makes "grant_type"=="refresh_token"
|
||||||
// HTTP requests to renew a token using a RefreshToken.
|
// HTTP requests to renew a token using a RefreshToken.
|
||||||
type tokenRefresher struct {
|
type tokenRefresher struct {
|
||||||
|
|
Loading…
Reference in New Issue