/
memoryTokenStorage.go
69 lines (57 loc) · 1.33 KB
/
memoryTokenStorage.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
package storage
import (
"errors"
"sync"
)
var (
ErrTokenEntryExists = errors.New("token entry with this access string already exists")
ErrTokenEntryNotFound = errors.New("token entry not found")
)
type MemoryTokenStorage struct {
m *sync.RWMutex
tokens map[string]OAuth2Token
}
func NewMemoryTokenStorage() *MemoryTokenStorage {
return &MemoryTokenStorage{
m: new(sync.RWMutex),
tokens: make(map[string]OAuth2Token),
}
}
func (ts *MemoryTokenStorage) FindByAccessToken(at string) (OAuth2Token, error) {
ts.m.RLock()
defer ts.m.RUnlock()
token, found := ts.tokens[at]
if !found {
return nil, ErrTokenEntryNotFound
}
return token, nil
}
func (ts *MemoryTokenStorage) FindByCodeChallenge(cc string) (OAuth2Token, error) {
ts.m.RLock()
defer ts.m.RUnlock()
for _, t := range ts.tokens {
if t.GetCodeChallenge() == cc {
return t, nil
}
}
return nil, ErrTokenEntryNotFound
}
func (ts *MemoryTokenStorage) Add(t OAuth2Token) error {
ts.m.Lock()
defer ts.m.Unlock()
_, found := ts.tokens[t.GetAccessToken()]
if found {
return ErrTokenEntryExists
}
ts.tokens[t.GetAccessToken()] = t
return nil
}
func (ts *MemoryTokenStorage) Remove(t OAuth2Token) error {
ts.m.Lock()
defer ts.m.Unlock()
delete(ts.tokens, t.GetAccessToken())
return nil
}
func (_ *MemoryTokenStorage) Close() error {
return nil
}