/
manager_memory.go
71 lines (57 loc) · 1.27 KB
/
manager_memory.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
package connection
import (
"sync"
"github.com/go-errors/errors"
"github.com/ory-am/hydra/pkg"
)
type MemoryManager struct {
Connections map[string]Connection
sync.RWMutex
}
func NewMemoryManager() *MemoryManager {
return &MemoryManager{
Connections: make(map[string]Connection),
}
}
func (m *MemoryManager) Create(c *Connection) error {
m.Lock()
defer m.Unlock()
m.Connections[c.GetID()] = *c
return nil
}
func (m *MemoryManager) Delete(id string) error {
m.Lock()
defer m.Unlock()
delete(m.Connections, id)
return nil
}
func (m *MemoryManager) Get(id string) (*Connection, error) {
m.RLock()
defer m.RUnlock()
c, ok := m.Connections[id]
if !ok {
return nil, errors.New(pkg.ErrNotFound)
}
return &c, nil
}
func (m *MemoryManager) FindAllByLocalSubject(subject string) ([]Connection, error) {
m.RLock()
defer m.RUnlock()
var cs []Connection
for _, c := range m.Connections {
if c.GetLocalSubject() == subject {
cs = append(cs, c)
}
}
return cs, nil
}
func (m *MemoryManager) FindByRemoteSubject(provider, subject string) (*Connection, error) {
m.RLock()
defer m.RUnlock()
for _, c := range m.Connections {
if c.GetProvider() == provider && c.GetRemoteSubject() == subject {
return &c, nil
}
}
return nil, errors.New(pkg.ErrNotFound)
}