-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
510 lines (449 loc) · 14.7 KB
/
handler.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package jwk
import (
"encoding/json"
"net/http"
"golang.org/x/sync/errgroup"
"github.com/ory/herodot"
"github.com/ory/x/httprouterx"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
"github.com/ory/x/urlx"
"github.com/ory/x/errorsx"
"github.com/ory/x/stringslice"
"github.com/ory/hydra/v2/x"
jose "github.com/go-jose/go-jose/v3"
"github.com/julienschmidt/httprouter"
)
const (
KeyHandlerPath = "/keys"
WellKnownKeysPath = "/.well-known/jwks.json"
)
type Handler struct {
r InternalRegistry
}
// JSON Web Key Set
//
// swagger:model jsonWebKeySet
//
//lint:ignore U1000 Used to generate Swagger and OpenAPI definitions
type jsonWebKeySet struct {
// List of JSON Web Keys
//
// The value of the "keys" parameter is an array of JSON Web Key (JWK)
// values. By default, the order of the JWK values within the array does
// not imply an order of preference among them, although applications
// of JWK Sets can choose to assign a meaning to the order for their
// purposes, if desired.
Keys []x.JSONWebKey `json:"keys"`
}
func NewHandler(r InternalRegistry) *Handler {
return &Handler{r: r}
}
func (h *Handler) SetRoutes(admin *httprouterx.RouterAdmin, public *httprouterx.RouterPublic, corsMiddleware func(http.Handler) http.Handler) {
public.Handler("OPTIONS", WellKnownKeysPath, corsMiddleware(http.HandlerFunc(h.handleOptions)))
public.Handler("GET", WellKnownKeysPath, corsMiddleware(http.HandlerFunc(h.discoverJsonWebKeys)))
admin.GET(KeyHandlerPath+"/:set/:key", h.getJsonWebKey)
admin.GET(KeyHandlerPath+"/:set", h.getJsonWebKeySet)
admin.POST(KeyHandlerPath+"/:set", h.createJsonWebKeySet)
admin.PUT(KeyHandlerPath+"/:set/:key", h.adminUpdateJsonWebKey)
admin.PUT(KeyHandlerPath+"/:set", h.setJsonWebKeySet)
admin.DELETE(KeyHandlerPath+"/:set/:key", h.deleteJsonWebKey)
admin.DELETE(KeyHandlerPath+"/:set", h.adminDeleteJsonWebKeySet)
}
// swagger:route GET /.well-known/jwks.json wellknown discoverJsonWebKeys
//
// # Discover Well-Known JSON Web Keys
//
// This endpoint returns JSON Web Keys required to verifying OpenID Connect ID Tokens and,
// if enabled, OAuth 2.0 JWT Access Tokens. This endpoint can be used with client libraries like
// [node-jwks-rsa](https://github.com/auth0/node-jwks-rsa) among others.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: jsonWebKeySet
// default: errorOAuth2
func (h *Handler) discoverJsonWebKeys(w http.ResponseWriter, r *http.Request) {
eg, ctx := errgroup.WithContext(r.Context())
wellKnownKeys := stringslice.Unique(h.r.Config().WellKnownKeys(ctx))
keys := make(chan *jose.JSONWebKeySet, len(wellKnownKeys))
for _, set := range wellKnownKeys {
set := set
eg.Go(func() error {
k, err := h.r.KeyManager().GetKeySet(ctx, set)
if errors.Is(err, x.ErrNotFound) {
h.r.Logger().Warnf("JSON Web Key Set %q does not exist yet, generating new key pair...", set)
k, err = h.r.KeyManager().GenerateAndPersistKeySet(ctx, set, uuid.Must(uuid.NewV4()).String(), string(jose.RS256), "sig")
if err != nil {
return err
}
} else if err != nil {
return err
}
keys <- ExcludePrivateKeys(k)
return nil
})
}
if err := eg.Wait(); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
close(keys)
var jwks jose.JSONWebKeySet
for k := range keys {
jwks.Keys = append(jwks.Keys, k.Keys...)
}
h.r.Writer().Write(w, r, &jwks)
}
// Get JSON Web Key Request
//
// swagger:parameters getJsonWebKey
//
//lint:ignore U1000 Used to generate Swagger and OpenAPI definitions
type getJsonWebKey struct {
// JSON Web Key Set ID
//
// in: path
// required: true
Set string `json:"set"`
// JSON Web Key ID
//
// in: path
// required: true
KID string `json:"kid"`
}
// swagger:route GET /admin/keys/{set}/{kid} jwk getJsonWebKey
//
// # Get JSON Web Key
//
// This endpoint returns a singular JSON Web Key contained in a set. It is identified by the set and the specific key ID (kid).
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: jsonWebKeySet
// default: errorOAuth2
func (h *Handler) getJsonWebKey(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var setName = ps.ByName("set")
var keyName = ps.ByName("key")
keys, err := h.r.KeyManager().GetKey(r.Context(), setName, keyName)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
keys = ExcludeOpaquePrivateKeys(keys)
h.r.Writer().Write(w, r, keys)
}
// Get JSON Web Key Set Parameters
//
// swagger:parameters getJsonWebKeySet
//
//lint:ignore U1000 Used to generate Swagger and OpenAPI definitions
type getJsonWebKeySet struct {
// JSON Web Key Set ID
//
// in: path
// required: true
Set string `json:"set"`
}
// swagger:route GET /admin/keys/{set} jwk getJsonWebKeySet
//
// # Retrieve a JSON Web Key Set
//
// This endpoint can be used to retrieve JWK Sets stored in ORY Hydra.
//
// A JSON Web Key (JWK) is a JavaScript Object Notation (JSON) data structure that represents a cryptographic key. A JWK Set is a JSON data structure that represents a set of JWKs. A JSON Web Key is identified by its set and key id. ORY Hydra uses this functionality to store cryptographic keys used for TLS and JSON Web Tokens (such as OpenID Connect ID tokens), and allows storing user-defined keys as well.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: jsonWebKeySet
// default: errorOAuth2
func (h *Handler) getJsonWebKeySet(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var setName = ps.ByName("set")
keys, err := h.r.KeyManager().GetKeySet(r.Context(), setName)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
keys = ExcludeOpaquePrivateKeys(keys)
h.r.Writer().Write(w, r, keys)
}
// Create JSON Web Key Set Request
//
// swagger:parameters createJsonWebKeySet
//
//lint:ignore U1000 Used to generate Swagger and OpenAPI definitions
type adminCreateJsonWebKeySet struct {
// The JSON Web Key Set ID
//
// in: path
// required: true
Set string `json:"set"`
// in: body
// required: true
Body createJsonWebKeySetBody
}
// Create JSON Web Key Set Request Body
//
// swagger:model createJsonWebKeySet
type createJsonWebKeySetBody struct {
// JSON Web Key Algorithm
//
// The algorithm to be used for creating the key. Supports `RS256`, `ES256`, `ES512`, `HS512`, and `HS256`.
//
// required: true
Algorithm string `json:"alg"`
// JSON Web Key Use
//
// The "use" (public key use) parameter identifies the intended use of
// the public key. The "use" parameter is employed to indicate whether
// a public key is used for encrypting data or verifying the signature
// on data. Valid values are "enc" and "sig".
// required: true
Use string `json:"use"`
// JSON Web Key ID
//
// The Key ID of the key to be created.
//
// required: true
KeyID string `json:"kid"`
}
// swagger:route POST /admin/keys/{set} jwk createJsonWebKeySet
//
// # Create JSON Web Key
//
// This endpoint is capable of generating JSON Web Key Sets for you. There a different strategies available, such as symmetric cryptographic keys (HS256, HS512) and asymetric cryptographic keys (RS256, ECDSA). If the specified JSON Web Key Set does not exist, it will be created.
//
// A JSON Web Key (JWK) is a JavaScript Object Notation (JSON) data structure that represents a cryptographic key. A JWK Set is a JSON data structure that represents a set of JWKs. A JSON Web Key is identified by its set and key id. ORY Hydra uses this functionality to store cryptographic keys used for TLS and JSON Web Tokens (such as OpenID Connect ID tokens), and allows storing user-defined keys as well.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 201: jsonWebKeySet
// default: errorOAuth2
func (h *Handler) createJsonWebKeySet(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var keyRequest createJsonWebKeySetBody
var set = ps.ByName("set")
if err := json.NewDecoder(r.Body).Decode(&keyRequest); err != nil {
h.r.Writer().WriteError(w, r, errorsx.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to decode the request body: %s", err)))
return
}
if keys, err := h.r.KeyManager().GenerateAndPersistKeySet(r.Context(), set, keyRequest.KeyID, keyRequest.Algorithm, keyRequest.Use); err == nil {
keys = ExcludeOpaquePrivateKeys(keys)
h.r.Writer().WriteCreated(w, r, urlx.AppendPaths(h.r.Config().IssuerURL(r.Context()), "/keys/"+set).String(), keys)
} else {
h.r.Writer().WriteError(w, r, err)
}
}
// Set JSON Web Key Set Request
//
// swagger:parameters setJsonWebKeySet
//
//lint:ignore U1000 Used to generate Swagger and OpenAPI definitions
type setJsonWebKeySet struct {
// The JSON Web Key Set ID
//
// in: path
// required: true
Set string `json:"set"`
// in: body
Body jsonWebKeySet
}
// swagger:route PUT /admin/keys/{set} jwk setJsonWebKeySet
//
// # Update a JSON Web Key Set
//
// Use this method if you do not want to let Hydra generate the JWKs for you, but instead save your own.
//
// A JSON Web Key (JWK) is a JavaScript Object Notation (JSON) data structure that represents a cryptographic key. A JWK Set is a JSON data structure that represents a set of JWKs. A JSON Web Key is identified by its set and key id. ORY Hydra uses this functionality to store cryptographic keys used for TLS and JSON Web Tokens (such as OpenID Connect ID tokens), and allows storing user-defined keys as well.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: jsonWebKeySet
// default: errorOAuth2
func (h *Handler) setJsonWebKeySet(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var keySet jose.JSONWebKeySet
var set = ps.ByName("set")
if err := json.NewDecoder(r.Body).Decode(&keySet); err != nil {
h.r.Writer().WriteError(w, r, errorsx.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to decode the request body: %s", err)))
return
}
if err := h.r.KeyManager().UpdateKeySet(r.Context(), set, &keySet); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
h.r.Writer().Write(w, r, &keySet)
}
// Set JSON Web Key Request
//
// swagger:parameters setJsonWebKey
//
//lint:ignore U1000 Used to generate Swagger and OpenAPI definitions
type setJsonWebKey struct {
// The JSON Web Key Set ID
//
// in: path
// required: true
Set string `json:"set"`
// JSON Web Key ID
//
// in: path
// required: true
KID string `json:"kid"`
// in: body
Body x.JSONWebKey
}
// swagger:route PUT /admin/keys/{set}/{kid} jwk setJsonWebKey
//
// # Set JSON Web Key
//
// Use this method if you do not want to let Hydra generate the JWKs for you, but instead save your own.
//
// A JSON Web Key (JWK) is a JavaScript Object Notation (JSON) data structure that represents a cryptographic key. A JWK Set is a JSON data structure that represents a set of JWKs. A JSON Web Key is identified by its set and key id. ORY Hydra uses this functionality to store cryptographic keys used for TLS and JSON Web Tokens (such as OpenID Connect ID tokens), and allows storing user-defined keys as well.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: jsonWebKey
// default: errorOAuth2
func (h *Handler) adminUpdateJsonWebKey(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var key jose.JSONWebKey
var set = ps.ByName("set")
if err := json.NewDecoder(r.Body).Decode(&key); err != nil {
h.r.Writer().WriteError(w, r, errorsx.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to decode the request body: %s", err)))
return
}
if err := h.r.KeyManager().UpdateKey(r.Context(), set, &key); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
h.r.Writer().Write(w, r, key)
}
// Delete JSON Web Key Set Parameters
//
// swagger:parameters deleteJsonWebKeySet
//
//lint:ignore U1000 Used to generate Swagger and OpenAPI definitions
type deleteJsonWebKeySet struct {
// The JSON Web Key Set
// in: path
// required: true
Set string `json:"set"`
}
// swagger:route DELETE /admin/keys/{set} jwk deleteJsonWebKeySet
//
// # Delete JSON Web Key Set
//
// Use this endpoint to delete a complete JSON Web Key Set and all the keys in that set.
//
// A JSON Web Key (JWK) is a JavaScript Object Notation (JSON) data structure that represents a cryptographic key. A JWK Set is a JSON data structure that represents a set of JWKs. A JSON Web Key is identified by its set and key id. ORY Hydra uses this functionality to store cryptographic keys used for TLS and JSON Web Tokens (such as OpenID Connect ID tokens), and allows storing user-defined keys as well.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 204: emptyResponse
// default: errorOAuth2
func (h *Handler) adminDeleteJsonWebKeySet(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var setName = ps.ByName("set")
if err := h.r.KeyManager().DeleteKeySet(r.Context(), setName); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// Delete JSON Web Key Parameters
//
// swagger:parameters deleteJsonWebKey
//
//lint:ignore U1000 Used to generate Swagger and OpenAPI definitions
type deleteJsonWebKey struct {
// The JSON Web Key Set
// in: path
// required: true
Set string `json:"set"`
// The JSON Web Key ID (kid)
//
// in: path
// required: true
KID string `json:"kid"`
}
// swagger:route DELETE /admin/keys/{set}/{kid} jwk deleteJsonWebKey
//
// # Delete JSON Web Key
//
// Use this endpoint to delete a single JSON Web Key.
//
// A JSON Web Key (JWK) is a JavaScript Object Notation (JSON) data structure that represents a cryptographic key. A
// JWK Set is a JSON data structure that represents a set of JWKs. A JSON Web Key is identified by its set and key id. ORY Hydra uses
// this functionality to store cryptographic keys used for TLS and JSON Web Tokens (such as OpenID Connect ID tokens),
// and allows storing user-defined keys as well.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 204: emptyResponse
// default: errorOAuth2
func (h *Handler) deleteJsonWebKey(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var setName = ps.ByName("set")
var keyName = ps.ByName("key")
if err := h.r.KeyManager().DeleteKey(r.Context(), setName, keyName); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// This function will not be called, OPTIONS request will be handled by cors
// this is just a placeholder.
func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) {}