/
refreshable_token_source.go
51 lines (43 loc) · 1.15 KB
/
refreshable_token_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package aponoapi
import (
"context"
"sync"
"golang.org/x/oauth2"
)
// TokenUpdateFunc is a function that accepts an oauth2 Token upon refresh, and
// returns an error if it should not be used.
type TokenUpdateFunc func(*oauth2.Token) error
func NewRefreshableTokenSource(
ctx context.Context,
cfg oauth2.Config,
token *oauth2.Token,
f TokenUpdateFunc,
) oauth2.TokenSource {
return &refreshableTokenSource{
new: cfg.TokenSource(ctx, token),
t: token,
f: f,
}
}
// refreshableTokenSource is essentially `oauth2.reuseTokenSource` with `TokenUpdateFunc` added.
type refreshableTokenSource struct {
new oauth2.TokenSource
mu sync.Mutex // guards t
t *oauth2.Token
f TokenUpdateFunc // called when token refreshed so new refresh token can be persisted
}
// Token returns the current token if it's still valid, else will
// refresh the current token (using r.Context for HTTP client information) and return the new one.
func (s *refreshableTokenSource) Token() (*oauth2.Token, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.t.Valid() {
return s.t, nil
}
t, err := s.new.Token()
if err != nil {
return nil, err
}
s.t = t
return t, s.f(t)
}