-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_redis.go
143 lines (111 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 jwk
import (
"encoding/json"
"fmt"
"github.com/ory-am/hydra/pkg"
"github.com/pkg/errors"
"github.com/square/go-jose"
"gopkg.in/redis.v5"
)
type RedisManager struct {
DB *redis.Client
Cipher *AEAD
KeyPrefix string
}
func (m *RedisManager) redisJWKKey(set string) string {
return m.KeyPrefix + fmt.Sprintf("hydra:jwk:%s", set)
}
func (m *RedisManager) addKey(set string, key *jose.JsonWebKey, pipe *redis.Pipeline) error {
payload, err := json.Marshal(key)
if err != nil {
return err
}
encrypted, err := m.Cipher.Encrypt(payload)
if err != nil {
return errors.Wrap(err, "")
}
pipe.HSet(m.redisJWKKey(set), key.KeyID, encrypted)
return nil
}
func (m *RedisManager) AddKey(set string, key *jose.JsonWebKey) error {
pipe := m.DB.Pipeline()
defer pipe.Close()
if err := m.addKey(set, key, pipe); err != nil {
return errors.Wrap(err, "")
}
if _, err := pipe.Exec(); err != nil {
return errors.Wrap(err, "")
}
return nil
}
func (m *RedisManager) AddKeySet(set string, keys *jose.JsonWebKeySet) error {
pipe := m.DB.Pipeline()
defer pipe.Close()
for _, key := range keys.Keys {
if err := m.addKey(set, &key, pipe); err != nil {
return errors.Wrap(err, "")
}
}
if _, err := pipe.Exec(); err != nil {
return errors.Wrap(err, "")
}
return nil
}
func (m *RedisManager) getKeySet(set string) (*jose.JsonWebKeySet, error) {
var keySet jose.JsonWebKeySet
iter := m.DB.HScan(m.redisJWKKey(set), 0, "", 0).Iterator()
for iter.Next() {
if !iter.Next() {
break
}
encryptedJWK := iter.Val()
jwk, err := m.Cipher.Decrypt(encryptedJWK)
if err != nil {
return nil, err
}
var key jose.JsonWebKey
if err := json.Unmarshal(jwk, &key); err != nil {
return nil, err
}
keySet.Keys = append(keySet.Keys, key)
}
if len(keySet.Keys) == 0 {
return nil, pkg.ErrNotFound
}
if err := iter.Err(); err != nil {
return nil, err
}
return &keySet, nil
}
func (m *RedisManager) GetKey(set, kid string) (*jose.JsonWebKeySet, error) {
encryptedJWK, err := m.DB.HGet(m.redisJWKKey(set), kid).Result()
if err == redis.Nil {
return nil, errors.Wrap(pkg.ErrNotFound, "")
} else if err != nil {
return nil, errors.Wrap(err, "")
}
jwk, err := m.Cipher.Decrypt(encryptedJWK)
if err != nil {
return nil, err
}
var key jose.JsonWebKey
if err := json.Unmarshal(jwk, &key); err != nil {
return nil, errors.Wrap(err, "")
}
return &jose.JsonWebKeySet{
Keys: []jose.JsonWebKey{key},
}, nil
}
func (m *RedisManager) GetKeySet(set string) (*jose.JsonWebKeySet, error) {
keys, err := m.getKeySet(set)
if err != nil {
return nil, errors.Wrap(err, "")
}
return keys, nil
}
func (m *RedisManager) DeleteKey(set, kid string) error {
return m.DB.HDel(m.redisJWKKey(set), kid).Err()
}
func (m *RedisManager) DeleteKeySet(set string) error {
return m.DB.Del(m.redisJWKKey(set)).Err()
}