forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.go
83 lines (74 loc) · 1.59 KB
/
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
package oauth
import (
"fmt"
"sync"
"time"
"github.com/pydio/cells/common/proto/auth"
"github.com/pydio/cells/common/sql"
)
// MemDAO is a dev-util for storing tokens in memory
type MemDAO struct {
sql.DAO
tokens map[string]*auth.PersonalAccessToken
tLock *sync.Mutex
}
func NewMemDao() DAO {
m := &MemDAO{
tokens: make(map[string]*auth.PersonalAccessToken),
tLock: &sync.Mutex{},
}
return m
}
func (m *MemDAO) Load(accessToken string) (*auth.PersonalAccessToken, error) {
m.tLock.Lock()
defer m.tLock.Unlock()
if t, o := m.tokens[accessToken]; o {
return t, nil
} else {
return nil, fmt.Errorf("not.found")
}
}
func (m *MemDAO) Store(accessToken string, token *auth.PersonalAccessToken, _ bool) error {
m.tLock.Lock()
defer m.tLock.Unlock()
m.tokens[accessToken] = token
return nil
}
func (m *MemDAO) Delete(patUuid string) error {
m.tLock.Lock()
defer m.tLock.Unlock()
for k, v := range m.tokens {
if v.Uuid == patUuid {
delete(m.tokens, k)
}
}
return nil
}
func (m *MemDAO) List(byType auth.PatType, byUser string) ([]*auth.PersonalAccessToken, error) {
m.tLock.Lock()
defer m.tLock.Unlock()
var pp []*auth.PersonalAccessToken
for _, t := range m.tokens {
if byType != auth.PatType_ANY && t.Type != byType {
continue
}
if byUser != "" && t.UserLogin != byUser {
continue
}
pp = append(pp, t)
}
return pp, nil
}
func (m *MemDAO) PruneExpired() (int, error) {
m.tLock.Lock()
defer m.tLock.Unlock()
now := time.Now().Unix()
var count int
for k, t := range m.tokens {
if t.ExpiresAt < now {
count++
delete(m.tokens, k)
}
}
return count, nil
}