-
Notifications
You must be signed in to change notification settings - Fork 1
/
cachemap.go
163 lines (139 loc) · 4.2 KB
/
cachemap.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package jwt
import (
"github.com/lestrrat-go/jwx/jwt"
"github.com/sirupsen/logrus"
"context"
"sync"
"time"
)
// CacheMap is a mapped implementation of Cache, which allows storing
// JWTs by a key (for example a tenant UUID). As a bonus, the map is
// concurrency safe.
type CacheMap struct {
jwtMap map[string]*Cache
lock *sync.RWMutex
name string
logger LoggerContract
headroom time.Duration
tokenFunc func(ctx context.Context, key string) (string, error)
parseOptions []jwt.ParseOption
rejectUnparsable bool
}
// NewCacheMap returns a new mapped JWT cache.
func NewCacheMap(opts ...MapOption) *CacheMap {
//default
mapConfig := &mapConfig{
name: "",
headroom: time.Second,
logger: logrus.StandardLogger(),
tokenFunc: func(ctx context.Context, key string) (s string, e error) {
return "", ErrNotImplemented
},
parseOptions: nil,
rejectUnparsable: false,
}
//apply opts
for _, opt := range opts {
opt(mapConfig)
}
return &CacheMap{
jwtMap: map[string]*Cache{},
lock: &sync.RWMutex{},
name: mapConfig.name,
logger: mapConfig.logger,
headroom: mapConfig.headroom,
tokenFunc: mapConfig.tokenFunc,
parseOptions: mapConfig.parseOptions,
rejectUnparsable: mapConfig.rejectUnparsable,
}
}
type mapConfig struct {
name string
logger LoggerContract
headroom time.Duration
tokenFunc func(ctx context.Context, key string) (string, error)
parseOptions []jwt.ParseOption
rejectUnparsable bool
}
// MapOption represents an option for the mapped cache.
type MapOption func(*mapConfig)
// MapName sets the name of the cache.
// The default is an empty string.
func MapName(name string) MapOption {
return func(c *mapConfig) {
c.name = name
}
}
// MapLogger sets the logger to be used.
// The default is the logrus default logger.
func MapLogger(logger LoggerContract) MapOption {
return func(c *mapConfig) {
c.logger = logger
}
}
// MapHeadroom sets the headroom on how much earlier the cached
// tokens should be considered expired.
// The default is 1 second.
func MapHeadroom(headroom time.Duration) MapOption {
return func(c *mapConfig) {
c.headroom = headroom
}
}
// MapTokenFunction set the function which is called to retrieve a new
// JWT when required.
// The default always returns an error with "not implemented".
func MapTokenFunction(tokenFunc func(ctx context.Context, key string) (string, error)) MapOption {
return func(c *mapConfig) {
c.tokenFunc = tokenFunc
}
}
// MapParseOptions set the parse options which are used to parse
// a JWT. This can be used to implement signature validation for example.
//
// The default empty.
func MapParseOptions(parseOptions ...jwt.ParseOption) MapOption {
return func(c *mapConfig) {
c.parseOptions = parseOptions
}
}
// MapRejectUnparsable sets if the cache should reject (and return
// the accompanying error) token which are not parsable.
// Note, unparsable can mean a failed signature check.
//
// The default is false.
func MapRejectUnparsable(rejectUnparsable bool) MapOption {
return func(c *mapConfig) {
c.rejectUnparsable = rejectUnparsable
}
}
// EnsureToken returns either the cached token if existing and still valid,
// or calls the internal token function to fetch a new token. If an error
// occurs in the latter case, it is passed trough.
func (cacheMap *CacheMap) EnsureToken(ctx context.Context, key string) (string, error) {
readLock := cacheMap.lock.RLocker()
writeLock := cacheMap.lock
readLock.Lock()
cache, exists := cacheMap.jwtMap[key]
if !exists {
// Trade read lock for write lock
readLock.Unlock()
writeLock.Lock()
cacheMap.jwtMap[key] = NewCache(
Name(cacheMap.name+" for "+key),
Headroom(cacheMap.headroom),
Logger(cacheMap.logger),
TokenFunction(func(ctx context.Context) (string, error) {
return cacheMap.tokenFunc(ctx, key)
}),
ParseOptions(cacheMap.parseOptions...),
RejectUnparsable(cacheMap.rejectUnparsable),
)
cache = cacheMap.jwtMap[key]
// Trade write lock for read lock
writeLock.Unlock()
readLock.Lock()
}
// Ensure that we unlock, even if the key function misbehaves
defer readLock.Unlock()
return cache.EnsureToken(ctx)
}