-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmanager_sql.go
409 lines (373 loc) · 12.5 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright © 2015-2018 Aeneas Rekkas <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <[email protected]>
* @copyright 2015-2018 Aeneas Rekkas <[email protected]>
* @license Apache-2.0
*/
package client
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/jmoiron/sqlx"
"github.com/ory/fosite"
"github.com/ory/go-convenience/stringsx"
"github.com/ory/sqlcon"
"github.com/pkg/errors"
"github.com/rubenv/sql-migrate"
"gopkg.in/square/go-jose.v2"
)
var sharedMigrations = []*migrate.Migration{
{
Id: "1",
Up: []string{`CREATE TABLE IF NOT EXISTS hydra_client (
id varchar(255) NOT NULL PRIMARY KEY,
client_name text NOT NULL,
client_secret text NOT NULL,
redirect_uris text NOT NULL,
grant_types text NOT NULL,
response_types text NOT NULL,
scope text NOT NULL,
owner text NOT NULL,
policy_uri text NOT NULL,
tos_uri text NOT NULL,
client_uri text NOT NULL,
logo_uri text NOT NULL,
contacts text NOT NULL,
public boolean NOT NULL
)`},
Down: []string{
"DROP TABLE hydra_client",
},
},
{
Id: "2",
Up: []string{
`ALTER TABLE hydra_client ADD client_secret_expires_at INTEGER NOT NULL DEFAULT 0`,
},
Down: []string{
`ALTER TABLE hydra_client DROP COLUMN client_secret_expires_at`,
},
},
{
Id: "3",
Up: []string{
`ALTER TABLE hydra_client ADD sector_identifier_uri TEXT`,
`ALTER TABLE hydra_client ADD jwks TEXT`,
`ALTER TABLE hydra_client ADD jwks_uri TEXT`,
`ALTER TABLE hydra_client ADD request_uris TEXT`,
`ALTER TABLE hydra_client ADD token_endpoint_auth_method VARCHAR(25) NOT NULL DEFAULT ''`,
`ALTER TABLE hydra_client ADD request_object_signing_alg VARCHAR(10) NOT NULL DEFAULT ''`,
`ALTER TABLE hydra_client ADD userinfo_signed_response_alg VARCHAR(10) NOT NULL DEFAULT ''`,
},
Down: []string{
`ALTER TABLE hydra_client DROP COLUMN sector_identifier_uri`,
`ALTER TABLE hydra_client DROP COLUMN jwks`,
`ALTER TABLE hydra_client DROP COLUMN jwks_uri`,
`ALTER TABLE hydra_client DROP COLUMN token_endpoint_auth_method`,
`ALTER TABLE hydra_client DROP COLUMN request_uris`,
`ALTER TABLE hydra_client DROP COLUMN request_object_signing_alg`,
`ALTER TABLE hydra_client DROP COLUMN userinfo_signed_response_alg`,
},
},
{
Id: "5",
Up: []string{
`UPDATE hydra_client SET token_endpoint_auth_method='none' WHERE public=TRUE`,
`ALTER TABLE hydra_client DROP COLUMN public`,
},
Down: []string{
`ALTER TABLE hydra_client ADD public BOOLEAN NOT NULL DEFAULT FALSE`,
`UPDATE hydra_client SET public=TRUE WHERE token_endpoint_auth_method='none'`,
},
},
{
Id: "6",
Up: []string{
`ALTER TABLE hydra_client ADD subject_type VARCHAR(15) NOT NULL DEFAULT ''`,
},
Down: []string{
`ALTER TABLE hydra_client DROP COLUMN subject_type`,
},
},
}
var Migrations = map[string]*migrate.MemoryMigrationSource{
"mysql": {Migrations: []*migrate.Migration{
sharedMigrations[0],
sharedMigrations[1],
sharedMigrations[2],
{
Id: "4",
Up: []string{
`UPDATE hydra_client SET sector_identifier_uri='', jwks='', jwks_uri='', request_uris=''`,
`ALTER TABLE hydra_client MODIFY sector_identifier_uri TEXT NOT NULL`,
`ALTER TABLE hydra_client MODIFY jwks TEXT NOT NULL`,
`ALTER TABLE hydra_client MODIFY jwks_uri TEXT NOT NULL`,
`ALTER TABLE hydra_client MODIFY request_uris TEXT NOT NULL`,
},
Down: []string{
`ALTER TABLE hydra_client MODIFY sector_identifier_uri TEXT`,
`ALTER TABLE hydra_client MODIFY jwks TEXT`,
`ALTER TABLE hydra_client MODIFY jwks_uri TEXT`,
`ALTER TABLE hydra_client MODIFY request_uris TEXT`,
},
},
sharedMigrations[3],
sharedMigrations[4],
}},
"postgres": {Migrations: []*migrate.Migration{
sharedMigrations[0],
sharedMigrations[1],
sharedMigrations[2],
{
Id: "4",
Up: []string{
`UPDATE hydra_client SET sector_identifier_uri='', jwks='', jwks_uri='', request_uris=''`,
`ALTER TABLE hydra_client ALTER COLUMN sector_identifier_uri SET NOT NULL`,
`ALTER TABLE hydra_client ALTER COLUMN jwks SET NOT NULL`,
`ALTER TABLE hydra_client ALTER COLUMN jwks_uri SET NOT NULL`,
`ALTER TABLE hydra_client ALTER COLUMN request_uris SET NOT NULL`,
},
Down: []string{
`ALTER TABLE hydra_client ALTER COLUMN sector_identifier_uri DROP NOT NULL`,
`ALTER TABLE hydra_client ALTER COLUMN jwks DROP NOT NULL`,
`ALTER TABLE hydra_client ALTER COLUMN jwks_uri DROP NOT NULL`,
`ALTER TABLE hydra_client ALTER COLUMN request_uris DROP NOT NULL`,
},
},
sharedMigrations[3],
sharedMigrations[4],
}},
}
type SQLManager struct {
Hasher fosite.Hasher
DB *sqlx.DB
}
type sqlData struct {
ID string `db:"id"`
Name string `db:"client_name"`
Secret string `db:"client_secret"`
RedirectURIs string `db:"redirect_uris"`
GrantTypes string `db:"grant_types"`
ResponseTypes string `db:"response_types"`
Scope string `db:"scope"`
Owner string `db:"owner"`
PolicyURI string `db:"policy_uri"`
TermsOfServiceURI string `db:"tos_uri"`
ClientURI string `db:"client_uri"`
LogoURI string `db:"logo_uri"`
Contacts string `db:"contacts"`
SecretExpiresAt int `db:"client_secret_expires_at"`
SectorIdentifierURI string `db:"sector_identifier_uri"`
JSONWebKeysURI string `db:"jwks_uri"`
JSONWebKeys string `db:"jwks"`
TokenEndpointAuthMethod string `db:"token_endpoint_auth_method"`
RequestURIs string `db:"request_uris"`
SubjectType string `db:"subject_type"`
RequestObjectSigningAlgorithm string `db:"request_object_signing_alg"`
UserinfoSignedResponseAlg string `db:"userinfo_signed_response_alg"`
}
var sqlParams = []string{
"id",
"client_name",
"client_secret",
"redirect_uris",
"grant_types",
"response_types",
"scope",
"owner",
"policy_uri",
"tos_uri",
"client_uri",
"subject_type",
"logo_uri",
"contacts",
"client_secret_expires_at",
"sector_identifier_uri",
"jwks",
"jwks_uri",
"token_endpoint_auth_method",
"request_uris",
"request_object_signing_alg",
"userinfo_signed_response_alg",
}
func sqlDataFromClient(d *Client) (*sqlData, error) {
jwks := ""
if d.JSONWebKeys != nil {
out, err := json.Marshal(d.JSONWebKeys)
if err != nil {
return nil, errors.WithStack(err)
}
jwks = string(out)
}
return &sqlData{
ID: d.GetID(),
Name: d.Name,
Secret: d.Secret,
RedirectURIs: strings.Join(d.RedirectURIs, "|"),
GrantTypes: strings.Join(d.GrantTypes, "|"),
ResponseTypes: strings.Join(d.ResponseTypes, "|"),
Scope: d.Scope,
Owner: d.Owner,
PolicyURI: d.PolicyURI,
TermsOfServiceURI: d.TermsOfServiceURI,
ClientURI: d.ClientURI,
LogoURI: d.LogoURI,
Contacts: strings.Join(d.Contacts, "|"),
SecretExpiresAt: d.SecretExpiresAt,
SectorIdentifierURI: d.SectorIdentifierURI,
JSONWebKeysURI: d.JSONWebKeysURI,
JSONWebKeys: jwks,
TokenEndpointAuthMethod: d.TokenEndpointAuthMethod,
RequestObjectSigningAlgorithm: d.RequestObjectSigningAlgorithm,
RequestURIs: strings.Join(d.RequestURIs, "|"),
UserinfoSignedResponseAlg: d.UserinfoSignedResponseAlg,
SubjectType: d.SubjectType,
}, nil
}
func (d *sqlData) ToClient() (*Client, error) {
c := &Client{
ClientID: d.ID,
Name: d.Name,
Secret: d.Secret,
RedirectURIs: stringsx.Splitx(d.RedirectURIs, "|"),
GrantTypes: stringsx.Splitx(d.GrantTypes, "|"),
ResponseTypes: stringsx.Splitx(d.ResponseTypes, "|"),
Scope: d.Scope,
Owner: d.Owner,
PolicyURI: d.PolicyURI,
TermsOfServiceURI: d.TermsOfServiceURI,
ClientURI: d.ClientURI,
LogoURI: d.LogoURI,
Contacts: stringsx.Splitx(d.Contacts, "|"),
SecretExpiresAt: d.SecretExpiresAt,
SectorIdentifierURI: d.SectorIdentifierURI,
JSONWebKeysURI: d.JSONWebKeysURI,
TokenEndpointAuthMethod: d.TokenEndpointAuthMethod,
RequestObjectSigningAlgorithm: d.RequestObjectSigningAlgorithm,
RequestURIs: stringsx.Splitx(d.RequestURIs, "|"),
UserinfoSignedResponseAlg: d.UserinfoSignedResponseAlg,
SubjectType: d.SubjectType,
}
if d.JSONWebKeys != "" {
c.JSONWebKeys = new(jose.JSONWebKeySet)
if err := json.Unmarshal([]byte(d.JSONWebKeys), &c.JSONWebKeys); err != nil {
return nil, errors.WithStack(err)
}
}
return c, nil
}
func (m *SQLManager) CreateSchemas() (int, error) {
database := m.DB.DriverName()
switch database {
case "pgx", "pq":
database = "postgres"
}
migrate.SetTable("hydra_client_migration")
n, err := migrate.Exec(m.DB.DB, m.DB.DriverName(), Migrations[database], migrate.Up)
if err != nil {
return 0, errors.Wrapf(err, "Could not migrate sql schema, applied %d Migrations", n)
}
return n, nil
}
func (m *SQLManager) GetConcreteClient(id string) (*Client, error) {
var d sqlData
if err := m.DB.Get(&d, m.DB.Rebind("SELECT * FROM hydra_client WHERE id=?"), id); err != nil {
return nil, sqlcon.HandleError(err)
}
return d.ToClient()
}
func (m *SQLManager) GetClient(_ context.Context, id string) (fosite.Client, error) {
return m.GetConcreteClient(id)
}
func (m *SQLManager) UpdateClient(c *Client) error {
o, err := m.GetClient(context.Background(), c.GetID())
if err != nil {
return errors.WithStack(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)
}
s, err := sqlDataFromClient(c)
if err != nil {
return errors.WithStack(err)
}
var query []string
for _, param := range sqlParams {
query = append(query, fmt.Sprintf("%s=:%s", param, param))
}
if _, err := m.DB.NamedExec(fmt.Sprintf(`UPDATE hydra_client SET %s WHERE id=:id`, strings.Join(query, ", ")), s); err != nil {
return sqlcon.HandleError(err)
}
return nil
}
func (m *SQLManager) 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 *SQLManager) CreateClient(c *Client) error {
h, err := m.Hasher.Hash([]byte(c.Secret))
if err != nil {
return errors.WithStack(err)
}
c.Secret = string(h)
data, err := sqlDataFromClient(c)
if err != nil {
return errors.WithStack(err)
}
if _, err := m.DB.NamedExec(fmt.Sprintf(
"INSERT INTO hydra_client (%s) VALUES (%s)",
strings.Join(sqlParams, ", "),
":"+strings.Join(sqlParams, ", :"),
), data); err != nil {
return sqlcon.HandleError(err)
}
return nil
}
func (m *SQLManager) DeleteClient(id string) error {
if _, err := m.DB.Exec(m.DB.Rebind(`DELETE FROM hydra_client WHERE id=?`), id); err != nil {
return sqlcon.HandleError(err)
}
return nil
}
func (m *SQLManager) GetClients(limit, offset int) (clients map[string]Client, err error) {
d := make([]sqlData, 0)
clients = make(map[string]Client)
if err := m.DB.Select(&d, m.DB.Rebind("SELECT * FROM hydra_client ORDER BY id LIMIT ? OFFSET ?"), limit, offset); err != nil {
return nil, sqlcon.HandleError(err)
}
for _, k := range d {
c, err := k.ToClient()
if err != nil {
return nil, errors.WithStack(err)
}
clients[k.ID] = *c
}
return clients, nil
}