Skip to content

Commit 62ee278

Browse files
committed
Fix expire token in memory storage
1 parent 9cfb6c9 commit 62ee278

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

storage/memory/memory_storage.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const (
1515

1616
type entry struct {
1717
value []byte
18-
duration time.Duration
18+
duration time.Time
1919
}
2020

2121
type tokenStorageMemory struct {
@@ -32,7 +32,7 @@ func NewStorage(maxSize int) storage.Adapter {
3232
func (tsm tokenStorageMemory) Set(token string, expire int64, introspect []byte) error {
3333
e := &entry{
3434
value: introspect,
35-
duration: time.Unix(expire, 0).Sub(time.Now()),
35+
duration: time.Unix(expire, 0),
3636
}
3737
tsm.cache.Add(token, e)
3838
return nil
@@ -41,8 +41,7 @@ func (tsm tokenStorageMemory) Set(token string, expire int64, introspect []byte)
4141
func (tsm tokenStorageMemory) Get(token string) ([]byte, error) {
4242
if e, ok := tsm.cache.Get(token); ok {
4343
entry := e.(*entry)
44-
exp := time.Now().Add(entry.duration)
45-
if exp.Before(time.Now()) {
44+
if entry.duration.Before(time.Now()) {
4645
_ = tsm.Delete(token)
4746
return nil, errors.New(ErrorTokenIsExpired)
4847
}

storage/memory/memory_storage_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ func TestSetAndGetToken(t *testing.T) {
2828
func TestExpireToken(t *testing.T) {
2929
st := createStorage(1)
3030
tName := fmt.Sprintf("%d", time.Now().UnixNano())
31-
exp := time.Now().Add(time.Second).Unix() - 1
31+
exp := time.Now().Add(time.Second).Unix() + 2
3232
token := []byte(tName)
3333
if err := st.Set(tName, exp, token); err != nil {
3434
t.Log("Unable to add token to the memory")
3535
}
3636

37+
time.Sleep(3 * time.Second)
38+
3739
_, err := st.Get(tName)
3840
if err == nil {
3941
t.Error("An expired token should not be received")

0 commit comments

Comments
 (0)