forked from johnsto/go-passwordless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_mem.go
125 lines (112 loc) · 2.62 KB
/
store_mem.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
package passwordless
import (
"sync"
"time"
"github.com/pzduniak/mcf"
"context"
)
// MemStore is a Store that keeps tokens in memory, expiring them periodically
// when they expire.
type MemStore struct {
mut sync.Mutex
data map[string]memToken
cleaner *time.Ticker
quitCleaner chan (struct{})
}
type memToken struct {
UID string
HashedToken []byte
Expires time.Time
}
// NewMemStore creates and returns a new `MemStore`
func NewMemStore() *MemStore {
ct := time.NewTicker(time.Second)
ms := &MemStore{
data: make(map[string]memToken),
quitCleaner: make(chan struct{}),
cleaner: ct,
}
// Run cleaner periodically
go func(quit chan struct{}) {
ticker:
for {
select {
case <-ct.C:
// Run clean cycle
ms.Clean()
case <-quit:
// Release resources
ct.Stop()
break ticker
}
}
}(ms.quitCleaner)
return ms
}
func (s *MemStore) Store(ctx context.Context, token, uid string,
ttl time.Duration) error {
hashToken, err := mcf.Create([]byte(token))
if err != nil {
return err
}
s.mut.Lock()
defer s.mut.Unlock()
s.data[uid] = memToken{
UID: uid,
HashedToken: hashToken,
Expires: time.Now().Add(ttl),
}
return nil
}
func (s *MemStore) Exists(ctx context.Context, uid string) (bool, time.Time, error) {
if t, ok := s.data[uid]; !ok {
// No known token for this user
return false, time.Time{}, nil
} else if time.Now().After(t.Expires) {
// Token exists, but expired
return false, time.Time{}, nil
} else {
// Token exists and is still valid
return true, t.Expires, nil
}
}
func (s *MemStore) Verify(ctx context.Context, token, uid string) (bool, error) {
if t, ok := s.data[uid]; !ok {
// No token in database
return false, ErrTokenNotFound
} else if time.Now().After(t.Expires) {
// Token exists but has expired
return false, ErrTokenNotFound
} else if valid, err := mcf.Verify([]byte(token), t.HashedToken); err != nil {
// Couldn't validate token
return false, err
} else if !valid {
// Token does not validate against hashed token
return false, nil
} else {
// Token is valid!
return true, nil
}
}
func (s *MemStore) Delete(ctx context.Context, uid string) error {
s.mut.Lock()
defer s.mut.Unlock()
delete(s.data, uid)
return nil
}
// Clean removes expired entries from the store.
func (s *MemStore) Clean() {
s.mut.Lock()
defer s.mut.Unlock()
for uid, token := range s.data {
if time.Now().After(token.Expires) {
delete(s.data, uid)
}
}
}
// Release disposes of the MemStore and any released resources
func (s *MemStore) Release() {
s.cleaner.Stop()
close(s.quitCleaner)
s.data = nil
}