/
manager_redis.go
143 lines (114 loc) · 2.82 KB
/
manager_redis.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
package client
import (
"encoding/json"
"github.com/imdario/mergo"
"github.com/ory-am/fosite"
"github.com/ory-am/hydra/pkg"
"github.com/pborman/uuid"
"github.com/pkg/errors"
"gopkg.in/redis.v5"
)
type RedisManager struct {
DB *redis.Client
Hasher fosite.Hasher
KeyPrefix string
}
const redisClientTemplate = "hydra:client"
func (m *RedisManager) redisClientKey() string {
return m.KeyPrefix + redisClientTemplate
}
func (m *RedisManager) GetConcreteClient(id string) (*Client, error) {
resp, err := m.DB.HGet(m.redisClientKey(), id).Bytes()
if err == redis.Nil {
return nil, errors.Wrap(pkg.ErrNotFound, "")
} else if err != nil {
return nil, errors.WithStack(err)
}
var d Client
if err := json.Unmarshal(resp, &d); err != nil {
return nil, errors.WithStack(err)
}
return &d, nil
}
func (m *RedisManager) GetClient(id string) (fosite.Client, error) {
return m.GetConcreteClient(id)
}
func (m *RedisManager) UpdateClient(c *Client) error {
o, err := m.GetClient(c.ID)
if err != nil {
return err
}
if c.Secret == "" {
c.Secret = string(o.GetHashedSecret())
} else {
h, err := m.Hasher.Hash([]byte(c.Secret))
if err != nil {
return errors.WithStack(err)
}
c.Secret = string(h)
}
if err := mergo.Merge(c, o); err != nil {
return errors.WithStack(err)
}
s, err := json.Marshal(c)
if err != nil {
return errors.WithStack(err)
}
if err := m.DB.HSet(m.redisClientKey(), c.ID, string(s)).Err(); err != nil {
return errors.WithStack(err)
}
return nil
}
func (m *RedisManager) Authenticate(id string, secret []byte) (*Client, error) {
c, err := m.GetConcreteClient(id)
if err != nil {
return nil, errors.WithStack(err)
}
if err := m.Hasher.Compare(c.GetHashedSecret(), secret); err != nil {
return nil, errors.WithStack(err)
}
return c, nil
}
func (m *RedisManager) CreateClient(c *Client) error {
if c.ID == "" {
c.ID = uuid.New()
}
hash, err := m.Hasher.Hash([]byte(c.Secret))
if err != nil {
return errors.WithStack(err)
}
c.Secret = string(hash)
s, err := json.Marshal(c)
if err != nil {
return errors.WithStack(err)
}
if err := m.DB.HSetNX(m.redisClientKey(), c.ID, string(s)).Err(); err != nil {
return errors.WithStack(err)
}
return nil
}
func (m *RedisManager) DeleteClient(id string) error {
if _, err := m.DB.HDel(m.redisClientKey(), id).Result(); err != nil {
return errors.WithStack(err)
}
return nil
}
func (m *RedisManager) GetClients() (map[string]Client, error) {
clients := make(map[string]Client)
iter := m.DB.HScan(m.redisClientKey(), 0, "", 0).Iterator()
for iter.Next() {
if !iter.Next() {
break
}
resp := iter.Val()
var d Client
if err := json.Unmarshal([]byte(resp), &d); err != nil {
return nil, errors.WithStack(err)
}
clients[d.ID] = d
}
if err := iter.Err(); err != nil {
return nil, err
}
return clients, nil
}