Skip to content

Commit 2e1fba0

Browse files
committed
Replace memory storage from karlseguin/ccache to hashicorp/golang-lru
1 parent 3db8d46 commit 2e1fba0

File tree

5 files changed

+52
-35
lines changed

5 files changed

+52
-35
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/ProtocolONE/authone-jwt-verifier-golang
33
require (
44
github.com/dgrijalva/jwt-go v3.2.0+incompatible
55
github.com/go-redis/redis v6.15.1+incompatible
6-
github.com/karlseguin/ccache v2.0.2+incompatible
6+
github.com/hashicorp/golang-lru v0.5.1
77
github.com/labstack/echo v3.3.5+incompatible
88
github.com/labstack/gommon v0.2.8
99
github.com/lestrrat-go/jwx v0.0.0-20180928232350-0d477e6a1f0e

go.sum

+16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
23
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
34
github.com/go-redis/redis v6.15.1+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
5+
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
6+
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
7+
github.com/karlseguin/ccache v2.0.2+incompatible h1:MpSlLlHgG3vPWTAIJsSYlyAQsHwfQ2HzgUlbJFh9Ufk=
48
github.com/karlseguin/ccache v2.0.2+incompatible/go.mod h1:CM9tNPzT6EdRh14+jiW8mEF9mkNZuuE51qmgGYUB93w=
9+
github.com/labstack/echo v3.3.5+incompatible h1:9PfxPUmasKzeJor9uQTaXLT6WUG/r+vSTmvXxvv3JO4=
510
github.com/labstack/echo v3.3.5+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
11+
github.com/labstack/gommon v0.2.8 h1:JvRqmeZcfrHC5u6uVleB4NxxNbzx6gpbJiQknDbKQu0=
612
github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4=
13+
github.com/lestrrat-go/jwx v0.0.0-20180928232350-0d477e6a1f0e h1:BsBWIgqA7BFb5sdQeFVQqXYL0P9ZwiNYvL3nywtEmnY=
714
github.com/lestrrat-go/jwx v0.0.0-20180928232350-0d477e6a1f0e/go.mod h1:iEoxlYfZjvoGpuWwxUz+eR5e6KTJGsaRcy/YNA/UnBk=
15+
github.com/lestrrat-go/pdebug v0.0.0-20180220043849-39f9a71bcabe h1:S7XSBlgc/eI2v47LkPPVa+infH3FuTS4tPJbqCtJovo=
816
github.com/lestrrat-go/pdebug v0.0.0-20180220043849-39f9a71bcabe/go.mod h1:zvUY6gZZVL2nu7NM+/3b51Z/hxyFZCZxV0hvfZ3NJlg=
17+
github.com/mattn/go-colorable v0.1.0 h1:v2XXALHHh6zHfYTJ+cSkwtyffnaOyR1MXaA91mTrb8o=
918
github.com/mattn/go-colorable v0.1.0/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
19+
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
1020
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
21+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
1122
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1223
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1324
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1425
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
26+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
1527
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
28+
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 h1:gKMu1Bf6QINDnvyZuTaACm9ofY+PRh+5vFz4oxBZeF8=
1629
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4/go.mod h1:50wTf68f99/Zt14pr046Tgt3Lp2vLyFZKzbFXTOabXw=
30+
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2 h1:NwxKRvbkH5MsNkvOtPZi3/3kmI8CAzs3mtv+GLQMkNo=
1731
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
32+
golang.org/x/net v0.0.0-20190206173232-65e2d4e15006 h1:bfLnR+k0tq5Lqt6dflRLcZiz6UaXCMt3vhYJ1l4FQ80=
1833
golang.org/x/net v0.0.0-20190206173232-65e2d4e15006/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
34+
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890 h1:uESlIz09WIHT2I+pasSXcpLYqYK8wHcdCetU3VuMBJE=
1935
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
2036
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=

jwtverifier.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func NewJwtVerifier(config Config, options ...interface{}) *JwtVerifier {
131131
}
132132

133133
if j.storage == nil {
134-
j.storage = memory.NewStorage(memory.MaxSize, memory.PruneLimit, memory.PromoteLimit)
134+
j.storage = memory.NewStorage(memory.MaxSize)
135135
}
136136

137137
return j

storage/memory/memory_storage.go

+28-27
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,59 @@ package memory
22

33
import (
44
"errors"
5-
"fmt"
65
"github.com/ProtocolONE/authone-jwt-verifier-golang/storage"
7-
"github.com/karlseguin/ccache"
6+
lru "github.com/hashicorp/golang-lru"
87
"time"
98
)
109

1110
const (
11+
ErrorTokenAdd = "unbale to add token"
1212
ErrorTokenNotExists = "token not exists"
1313
ErrorTokenIsExpired = "token is expired"
1414
MaxSize = 5000
15-
PruneLimit = 500
16-
PromoteLimit = 3
1715
)
1816

19-
type tokenStorageMemory struct {
20-
cache *ccache.Cache
17+
type entry struct {
18+
value []byte
19+
duration time.Duration
2120
}
2221

23-
func NewStorage(maxSize int64, pruneLimit uint32, promoteLimit int32) storage.Adapter {
24-
conf := ccache.Configure()
25-
conf.MaxSize(maxSize)
26-
conf.ItemsToPrune(pruneLimit)
27-
conf.GetsPerPromote(promoteLimit)
22+
type tokenStorageMemory struct {
23+
cache *lru.Cache
24+
}
2825

26+
func NewStorage(maxSize int) storage.Adapter {
27+
l, _ := lru.New(maxSize)
2928
return tokenStorageMemory{
30-
cache: ccache.New(conf),
29+
cache: l,
3130
}
3231
}
3332

3433
func (tsm tokenStorageMemory) Set(token string, expire int64, introspect []byte) error {
35-
duration := time.Unix(expire, 0).Sub(time.Now())
36-
tsm.cache.Set(token, introspect, duration)
34+
e := &entry{
35+
value: introspect,
36+
duration: time.Unix(expire, 0).Sub(time.Now()),
37+
}
38+
if false == tsm.cache.Add(token, e) {
39+
return errors.New(ErrorTokenAdd)
40+
}
3741
return nil
3842
}
3943

4044
func (tsm tokenStorageMemory) Get(token string) ([]byte, error) {
41-
item := tsm.cache.Get(token)
42-
if item == nil {
43-
return nil, errors.New(ErrorTokenNotExists)
44-
}
45-
if item.Expired() {
46-
_ = tsm.Delete(token)
47-
return nil, errors.New(ErrorTokenIsExpired)
45+
if e, ok := tsm.cache.Get(token); ok {
46+
entry := e.(*entry)
47+
exp := time.Now().Add(entry.duration)
48+
if exp.Before(time.Now()) {
49+
_ = tsm.Delete(token)
50+
return nil, errors.New(ErrorTokenIsExpired)
51+
}
52+
return entry.value, nil
4853
}
49-
v := fmt.Sprintf("%s", item.Value())
50-
return []byte(v), nil
54+
return nil, errors.New(ErrorTokenNotExists)
5155
}
5256

5357
func (tsm tokenStorageMemory) Delete(token string) error {
54-
if tsm.cache.Delete(token) == false {
55-
return fmt.Errorf("unable to delete token [%s] from cache", token)
56-
}
57-
58+
tsm.cache.Remove(token)
5859
return nil
5960
}

storage/memory/memory_storage_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestSetAndGetToken(t *testing.T) {
11-
st := createStorage(1, 1)
11+
st := createStorage(1)
1212
tName := fmt.Sprintf("%d", time.Now().UnixNano())
1313
exp := time.Now().Add(5 * time.Second).Unix()
1414
token := []byte(tName)
@@ -26,7 +26,7 @@ func TestSetAndGetToken(t *testing.T) {
2626
}
2727

2828
func TestExpireToken(t *testing.T) {
29-
st := createStorage(1, 1)
29+
st := createStorage(1)
3030
tName := fmt.Sprintf("%d", time.Now().UnixNano())
3131
exp := time.Now().Add(time.Second).Unix() - 1
3232
token := []byte(tName)
@@ -46,7 +46,7 @@ func TestExpireToken(t *testing.T) {
4646
}
4747

4848
func TestGetUnExistsToken(t *testing.T) {
49-
st := createStorage(1, 1)
49+
st := createStorage(1)
5050
tName := "unexiststoken"
5151
_, err := st.Get(tName)
5252
if err == nil {
@@ -60,7 +60,7 @@ func TestGetUnExistsToken(t *testing.T) {
6060
}
6161

6262
func TestDeleteToken(t *testing.T) {
63-
st := createStorage(1, 1)
63+
st := createStorage(1)
6464
tName := fmt.Sprintf("%d", time.Now().UnixNano())
6565
exp := time.Now().Add(5 * time.Second).Unix()
6666
token := []byte(tName)
@@ -76,6 +76,6 @@ func TestDeleteToken(t *testing.T) {
7676
}
7777
}
7878

79-
func createStorage(maxSize int, itemsToPrune int) storage.Adapter {
80-
return NewStorage(int64(maxSize), uint32(itemsToPrune), PromoteLimit)
79+
func createStorage(maxSize int) storage.Adapter {
80+
return NewStorage(maxSize)
8181
}

0 commit comments

Comments
 (0)