-
Notifications
You must be signed in to change notification settings - Fork 1
/
token_provider.go
71 lines (59 loc) · 1.56 KB
/
token_provider.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package tokenprovider
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"time"
"golang.org/x/oauth2"
)
var (
tokenCache = oauthTokenCacheType{
cache: map[string]*oauth2.Token{},
}
// timeNow makes it possible to test usage of time
timeNow = time.Now
)
type oauthTokenCacheType struct {
cache map[string]*oauth2.Token
sync.Mutex
}
// TokenProvider is anything that can return a token
type TokenProvider interface {
GetAccessToken(context.Context) (string, error)
}
type tokenSource interface {
getCacheKey() string
getToken(context.Context) (*oauth2.Token, error)
}
// tokenProviderImpl implements the TokenProvider interface
type tokenProviderImpl struct {
tokenSource
}
// GetAccessToken implements TokenProvider
func (provider *tokenProviderImpl) GetAccessToken(ctx context.Context) (string, error) {
tokenCache.Lock()
defer tokenCache.Unlock()
if cachedToken, found := tokenCache.cache[provider.getCacheKey()]; found {
if cachedToken.Expiry.After(timeNow().Add(time.Second * 10)) {
return cachedToken.AccessToken, nil
}
}
token, err := provider.getToken(ctx)
if err != nil {
return "", err
}
tokenCache.cache[provider.getCacheKey()] = token
return token.AccessToken, nil
}
func createCacheKey(authtype string, cfg *Config) string {
key := fmt.Sprintf("%v_%v_%v_%v_%v", authtype, cfg.DataSourceID, cfg.DataSourceUpdated.Unix(), cfg.RoutePath, cfg.RouteMethod)
if len(cfg.Scopes) == 0 {
return key
}
arr := make([]string, len(cfg.Scopes))
copy(arr, cfg.Scopes)
sort.Strings(arr)
return fmt.Sprintf("%v_%v", key, strings.Join(arr, "-"))
}