-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_sql.go
175 lines (151 loc) · 4.05 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
163
164
165
166
167
168
169
170
171
172
173
174
175
package jwk
import (
"database/sql"
"encoding/json"
"github.com/Sirupsen/logrus"
"github.com/jmoiron/sqlx"
"github.com/ory-am/hydra/pkg"
"github.com/pkg/errors"
"github.com/rubenv/sql-migrate"
"github.com/square/go-jose"
)
type SQLManager struct {
DB *sqlx.DB
Cipher *AEAD
}
var migrations = &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
{
Id: "1",
Up: []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)
)`,
},
Down: []string{
"DROP TABLE hydra_jwk",
},
},
},
}
type sqlData struct {
Set string `db:"sid"`
KID string `db:"kid"`
Version int `db:"version"`
Key string `db:"keydata"`
}
func (s *SQLManager) CreateSchemas() error {
migrate.SetTable("hydra_jwk_migration")
n, err := migrate.Exec(s.DB.DB, s.DB.DriverName(), migrations, migrate.Up)
if err != nil {
return errors.Wrapf(err, "Could not migrate sql schema, applied %d migrations", n)
}
logrus.Infof("Applied %d migrations %s!", n, s.DB.DriverName())
return nil
}
func (m *SQLManager) AddKey(set string, key *jose.JsonWebKey) error {
out, err := json.Marshal(key)
if err != nil {
return errors.WithStack(err)
}
encrypted, err := m.Cipher.Encrypt(out)
if err != nil {
return errors.WithStack(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.WithStack(err)
}
return nil
}
func (m *SQLManager) AddKeySet(set string, keys *jose.JsonWebKeySet) error {
tx, err := m.DB.Beginx()
if err != nil {
return errors.WithStack(err)
}
for _, key := range keys.Keys {
out, err := json.Marshal(key)
if err != nil {
return errors.WithStack(err)
}
encrypted, err := m.Cipher.Encrypt(out)
if err != nil {
return errors.WithStack(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.WithStack(err)
}
}
if err := tx.Commit(); err != nil {
return errors.WithStack(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.WithStack(err)
}
key, err := m.Cipher.Decrypt(d.Key)
if err != nil {
return nil, errors.WithStack(err)
}
var c jose.JsonWebKey
if err := json.Unmarshal(key, &c); err != nil {
return nil, errors.WithStack(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.WithStack(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.WithStack(err)
}
var c jose.JsonWebKey
if err := json.Unmarshal(key, &c); err != nil {
return nil, errors.WithStack(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.WithStack(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.WithStack(err)
}
return nil
}