-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_sql.go
162 lines (138 loc) · 3.68 KB
/
manager_sql.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
158
159
160
161
162
package jwk
import (
"database/sql"
"encoding/json"
"github.com/jmoiron/sqlx"
"github.com/ory-am/hydra/pkg"
"github.com/pkg/errors"
"github.com/square/go-jose"
)
type SQLManager struct {
DB *sqlx.DB
Cipher *AEAD
}
var sqlSchema = []string{
`CREATE TABLE IF NOT EXISTS hydra_jwk (
sid varchar(255) NOT NULL,
kid varchar(255) NOT NULL,
version int NOT NULL DEFAULT 0,
keydata text NOT NULL,
PRIMARY KEY (sid, kid)
)`,
}
type sqlData struct {
Set string `db:"sid"`
KID string `db:"kid"`
Version int `db:"version"`
Key string `db:"keydata"`
}
func (s *SQLManager) CreateSchemas() error {
for _, query := range sqlSchema {
if _, err := s.DB.Exec(query); err != nil {
return errors.Wrapf(err, "Could not create schema:\n%s", query)
}
}
return nil
}
func (m *SQLManager) AddKey(set string, key *jose.JsonWebKey) error {
out, err := json.Marshal(key)
if err != nil {
return errors.Wrap(err, "")
}
encrypted, err := m.Cipher.Encrypt(out)
if err != nil {
return errors.Wrap(err, "")
}
if _, err = m.DB.NamedExec(`INSERT INTO hydra_jwk (sid, kid, version, keydata) VALUES (:sid, :kid, :version, :keydata)`, &sqlData{
Set: set,
KID: key.KeyID,
Version: 0,
Key: encrypted,
}); err != nil {
return errors.Wrap(err, "")
}
return nil
}
func (m *SQLManager) AddKeySet(set string, keys *jose.JsonWebKeySet) error {
tx, err := m.DB.Beginx()
if err != nil {
return errors.Wrap(err, "")
}
for _, key := range keys.Keys {
out, err := json.Marshal(key)
if err != nil {
return errors.Wrap(err, "")
}
encrypted, err := m.Cipher.Encrypt(out)
if err != nil {
return errors.Wrap(err, "")
}
if _, err = tx.NamedExec(`INSERT INTO hydra_jwk (sid, kid, version, keydata) VALUES (:sid, :kid, :version, :keydata)`, &sqlData{
Set: set,
KID: key.KeyID,
Version: 0,
Key: encrypted,
}); err != nil {
return errors.Wrap(err, "")
}
}
if err := tx.Commit(); err != nil {
return errors.Wrap(err, "")
}
return nil
}
func (m *SQLManager) GetKey(set, kid string) (*jose.JsonWebKeySet, error) {
var d sqlData
if err := m.DB.Get(&d, m.DB.Rebind("SELECT * FROM hydra_jwk WHERE sid=? AND kid=?"), set, kid); err == sql.ErrNoRows {
return nil, errors.Wrap(pkg.ErrNotFound, "")
} else if err != nil {
return nil, errors.Wrap(err, "")
}
key, err := m.Cipher.Decrypt(d.Key)
if err != nil {
return nil, errors.Wrap(err, "")
}
var c jose.JsonWebKey
if err := json.Unmarshal(key, &c); err != nil {
return nil, errors.Wrap(err, "")
}
return &jose.JsonWebKeySet{
Keys: []jose.JsonWebKey{c},
}, nil
}
func (m *SQLManager) GetKeySet(set string) (*jose.JsonWebKeySet, error) {
var ds []sqlData
if err := m.DB.Select(&ds, m.DB.Rebind("SELECT * FROM hydra_jwk WHERE sid=?"), set); err == sql.ErrNoRows {
return nil, errors.Wrap(pkg.ErrNotFound, "")
} else if err != nil {
return nil, errors.Wrap(err, "")
}
if len(ds) == 0 {
return nil, errors.Wrap(pkg.ErrNotFound, "")
}
keys := &jose.JsonWebKeySet{Keys: []jose.JsonWebKey{}}
for _, d := range ds {
key, err := m.Cipher.Decrypt(d.Key)
if err != nil {
return nil, errors.Wrap(err, "")
}
var c jose.JsonWebKey
if err := json.Unmarshal(key, &c); err != nil {
return nil, errors.Wrap(err, "")
}
keys.Keys = append(keys.Keys, c)
}
return keys, nil
}
func (m *SQLManager) DeleteKey(set, kid string) error {
if _, err := m.DB.Exec(m.DB.Rebind(`DELETE FROM hydra_jwk WHERE sid=? AND kid=?`), set, kid); err != nil {
return errors.Wrap(err, "")
}
return nil
}
func (m *SQLManager) DeleteKeySet(set string) error {
if _, err := m.DB.Exec(m.DB.Rebind(`DELETE FROM hydra_jwk WHERE sid=?`), set); err != nil {
return errors.Wrap(err, "")
}
return nil
}