-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager_rethinkdb.go
157 lines (130 loc) · 3.12 KB
/
manager_rethinkdb.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package client
import (
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/go-errors/errors"
"github.com/ory-am/fosite"
"github.com/ory-am/fosite/hash"
"github.com/ory-am/hydra/pkg"
"github.com/pborman/uuid"
"golang.org/x/net/context"
r "gopkg.in/dancannon/gorethink.v2"
)
type RethinkManager struct {
Session *r.Session
Table r.Term
sync.RWMutex
Clients map[string]Client
Hasher hash.Hasher
}
func (m *RethinkManager) GetConcreteClient(id string) (*Client, error) {
m.RLock()
defer m.RUnlock()
c, ok := m.Clients[id]
if !ok {
return nil, errors.New(pkg.ErrNotFound)
}
return &c, nil
}
func (m *RethinkManager) GetClient(id string) (fosite.Client, error) {
return m.GetConcreteClient(id)
}
func (m *RethinkManager) Authenticate(id string, secret []byte) (*Client, error) {
m.RLock()
defer m.RUnlock()
c, ok := m.Clients[id]
if !ok {
return nil, errors.New(pkg.ErrNotFound)
}
if err := m.Hasher.Compare(c.GetHashedSecret(), secret); err != nil {
return nil, errors.New(err)
}
return &c, nil
}
func (m *RethinkManager) CreateClient(c *Client) error {
if c.ID == "" {
c.ID = uuid.New()
}
hash, err := m.Hasher.Hash([]byte(c.Secret))
if err != nil {
return errors.New(err)
}
c.Secret = string(hash)
if err := m.publishCreate(c); err != nil {
return err
}
return nil
}
func (m *RethinkManager) DeleteClient(id string) error {
if err := m.publishDelete(id); err != nil {
return err
}
return nil
}
func (m *RethinkManager) GetClients() (clients map[string]Client, err error) {
m.RLock()
defer m.RUnlock()
clients = make(map[string]Client)
for _, c := range m.Clients {
clients[c.ID] = c
}
return clients, nil
}
func (m *RethinkManager) ColdStart() error {
m.Clients = map[string]Client{}
clients, err := m.Table.Run(m.Session)
if err != nil {
return errors.New(err)
}
var client Client
m.Lock()
defer m.Unlock()
for clients.Next(&client) {
m.Clients[client.ID] = client
}
return nil
}
func (m *RethinkManager) publishCreate(client *Client) error {
if _, err := m.Table.Insert(client).RunWrite(m.Session); err != nil {
return errors.New(err)
}
return nil
}
func (m *RethinkManager) publishDelete(id string) error {
if _, err := m.Table.Get(id).Delete().RunWrite(m.Session); err != nil {
return errors.New(err)
}
return nil
}
func (m *RethinkManager) Watch(ctx context.Context) {
go pkg.Retry(time.Second*15, time.Minute, func() error {
clients, err := m.Table.Changes().Run(m.Session)
if err != nil {
return errors.New(err)
}
defer clients.Close()
var update map[string]*Client
for clients.Next(&update) {
logrus.Debug("Received update from RethinkDB Cluster in OAuth2 client manager.")
newVal := update["new_val"]
oldVal := update["old_val"]
m.Lock()
if newVal == nil && oldVal != nil {
delete(m.Clients, oldVal.GetID())
} else if newVal != nil && oldVal != nil {
delete(m.Clients, oldVal.GetID())
m.Clients[newVal.GetID()] = *newVal
} else {
m.Clients[newVal.GetID()] = *newVal
}
m.Unlock()
}
if clients.Err() != nil {
err = errors.New(clients.Err())
pkg.LogError(err)
return err
}
return nil
})
}