-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
239 lines (220 loc) · 5.46 KB
/
cache.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Tideland Go JSON Web Token - Cache
//
// Copyright (C) 2016-2020 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package jwt // import "tideland.dev/go/jwt"
//--------------------
// IMPORTS
//--------------------
import (
"context"
"fmt"
"net/http"
"strings"
"time"
)
//--------------------
// CACHE ENTRY
//--------------------
// cacheEntry manages a token and its access time.
type cacheEntry struct {
token *JWT
accessed time.Time
}
//--------------------
// CACHE
//--------------------
// defaultTimeout is the default timeout for synchronous actions.
const defaultTimeout = 5 * time.Second
// Cache provides a caching for tokens so that these
// don't have to be decoded or verified multiple times.
type Cache struct {
ctx context.Context
entries map[string]*cacheEntry
ttl time.Duration
leeway time.Duration
interval time.Duration
maxEntries int
actionc chan func()
}
// NewCache creates a new JWT caching. The ttl value controls
// the time a cached token may be unused before cleanup. The
// leeway is used for the time validation of the token itself.
// The duration of the interval controls how often the background
// cleanup is running. Final configuration parameter is the maximum
// number of entries inside the cache. If these grow too fast the
// ttl will be temporarily reduced for cleanup.
func NewCache(ctx context.Context, ttl, leeway, interval time.Duration, maxEntries int) *Cache {
c := &Cache{
ctx: ctx,
entries: map[string]*cacheEntry{},
ttl: ttl,
leeway: leeway,
interval: interval,
maxEntries: maxEntries,
actionc: make(chan func(), 1),
}
go c.backend()
return c
}
// Get tries to retrieve a token from the cache.
func (c *Cache) Get(st string) (*JWT, error) {
var token *JWT
aerr := c.doSync(func() {
if c.entries == nil {
return
}
entry, ok := c.entries[st]
if !ok {
return
}
if !entry.token.IsValid(c.leeway) {
// Remove invalid token.
delete(c.entries, st)
}
entry.accessed = time.Now()
token = entry.token
}, defaultTimeout)
if aerr != nil {
return nil, aerr
}
return token, nil
}
// RequestDecode tries to retrieve a token from the cache by
// the requests authorization header. Otherwise it decodes it and
// puts it.
func (c *Cache) RequestDecode(req *http.Request) (*JWT, error) {
var token *JWT
var err error
aerr := c.doSync(func() {
var st string
if st, err = c.requestToken(req); err != nil {
return
}
if token, err = c.Get(st); err != nil {
return
}
if token, err = Decode(st); err != nil {
return
}
_, err = c.Put(token)
}, defaultTimeout)
if aerr != nil {
return nil, aerr
}
return token, err
}
// RequestVerify tries to retrieve a token from the cache by
// the requests authorization header. Otherwise it verifies it and
// puts it.
func (c *Cache) RequestVerify(req *http.Request, key Key) (*JWT, error) {
var token *JWT
var err error
aerr := c.doSync(func() {
var st string
if st, err = c.requestToken(req); err != nil {
return
}
if token, err = c.Get(st); err != nil {
return
}
if token, err = Verify(st, key); err != nil {
return
}
_, err = c.Put(token)
}, defaultTimeout)
if aerr != nil {
return nil, aerr
}
return token, err
}
// Put adds a token to the cache and return the total number of entries.
func (c *Cache) Put(token *JWT) (int, error) {
var l int
err := c.doSync(func() {
if c.entries == nil {
l = 0
return
}
if token.IsValid(c.leeway) {
c.entries[token.String()] = &cacheEntry{token, time.Now()}
lenEntries := len(c.entries)
if lenEntries > c.maxEntries {
ttl := int64(c.ttl) / int64(lenEntries) * int64(c.maxEntries)
c.cleanup(time.Duration(ttl))
}
}
l = len(c.entries)
}, defaultTimeout)
return l, err
}
// Cleanup manually tells the cache to cleanup.
func (c *Cache) Cleanup() error {
return c.doSync(func() {
if c.entries == nil {
return
}
c.cleanup(c.ttl)
}, defaultTimeout)
}
// requestToken retrieves an authentication token out of a request.
func (c *Cache) requestToken(req *http.Request) (string, error) {
authorization := req.Header.Get("Authorization")
if authorization == "" {
return "", fmt.Errorf("request contains no authorization header")
}
fields := strings.Fields(authorization)
if len(fields) != 2 || fields[0] != "Bearer" {
return "", fmt.Errorf("invalid authorization header: %q", authorization)
}
return fields[1], nil
}
// cleanup checks for invalid or unused tokens.
func (c *Cache) cleanup(ttl time.Duration) {
valids := map[string]*cacheEntry{}
now := time.Now()
for key, entry := range c.entries {
if entry.token.IsValid(c.leeway) {
if entry.accessed.Add(ttl).After(now) {
// Everything fine.
valids[key] = entry
}
}
}
c.entries = valids
}
// doSync performs a function in the backend synchronously.
func (c *Cache) doSync(action func(), timeout time.Duration) error {
donec := make(chan struct{})
c.actionc <- func() {
action()
close(donec)
}
select {
case <-donec:
return nil
case <-time.After(timeout):
return fmt.Errorf("cache action timeout")
}
}
// backend is the goroutine of the cache.
func (c *Cache) backend() {
ticker := time.NewTicker(c.interval)
for {
select {
case <-c.ctx.Done():
c.entries = map[string]*cacheEntry{}
ticker.Stop()
return
case action := <-c.actionc:
action()
case <-ticker.C:
if c.entries != nil {
c.cleanup(c.ttl)
}
}
}
}
// EOF