-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
391 lines (348 loc) · 12.3 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
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* 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 <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/
package jwk
import (
"encoding/json"
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/pkg/errors"
"gopkg.in/square/go-jose.v2"
)
const (
IDTokenKeyName = "hydra.openid.id-token"
KeyHandlerPath = "/keys"
WellKnownKeysPath = "/.well-known/jwks.json"
)
type Handler struct {
Manager Manager
Generators map[string]KeyGenerator
H herodot.Writer
WellKnownKeys []string
}
func NewHandler(
manager Manager,
generators map[string]KeyGenerator,
h herodot.Writer,
wellKnownKeys []string,
) *Handler {
return &Handler{
Manager: manager,
Generators: generators,
H: h,
WellKnownKeys: append(wellKnownKeys, IDTokenKeyName),
}
}
func (h *Handler) GetGenerators() map[string]KeyGenerator {
if h.Generators == nil || len(h.Generators) == 0 {
h.Generators = map[string]KeyGenerator{
"RS256": &RS256Generator{},
"ES512": &ECDSA512Generator{},
"HS256": &HS256Generator{},
"HS512": &HS512Generator{},
}
}
return h.Generators
}
func (h *Handler) SetRoutes(frontend, backend *httprouter.Router) {
frontend.GET(WellKnownKeysPath, h.WellKnown)
backend.GET(KeyHandlerPath+"/:set/:key", h.GetKey)
backend.GET(KeyHandlerPath+"/:set", h.GetKeySet)
backend.POST(KeyHandlerPath+"/:set", h.Create)
backend.PUT(KeyHandlerPath+"/:set/:key", h.UpdateKey)
backend.PUT(KeyHandlerPath+"/:set", h.UpdateKeySet)
backend.DELETE(KeyHandlerPath+"/:set/:key", h.DeleteKey)
backend.DELETE(KeyHandlerPath+"/:set", h.DeleteKeySet)
}
// swagger:route GET /.well-known/jwks.json oAuth2 wellKnown
//
// Get Well-Known JSON Web Keys
//
// Returns metadata for discovering important JSON Web Keys. Currently, this endpoint returns the public key for verifying OpenID Connect ID Tokens.
//
// 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
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) WellKnown(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var jwks jose.JSONWebKeySet
for _, set := range h.WellKnownKeys {
keys, err := h.Manager.GetKeySet(set)
if err != nil {
h.H.WriteError(w, r, err)
return
}
keys, err = FindKeysByPrefix(keys, "public")
if err != nil {
h.H.WriteError(w, r, err)
return
}
jwks.Keys = append(jwks.Keys, keys.Keys...)
}
h.H.Write(w, r, &jwks)
}
// swagger:route GET /keys/{set}/{kid} jsonWebKey getJsonWebKey
//
// Retrieve a JSON Web Key
//
// This endpoint can be used to retrieve JWKs 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
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) GetKey(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var setName = ps.ByName("set")
var keyName = ps.ByName("key")
keys, err := h.Manager.GetKey(setName, keyName)
if err != nil {
h.H.WriteError(w, r, err)
return
}
h.H.Write(w, r, keys)
}
// swagger:route GET /keys/{set} jsonWebKey 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
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) GetKeySet(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var setName = ps.ByName("set")
keys, err := h.Manager.GetKeySet(setName)
if err != nil {
h.H.WriteError(w, r, err)
return
}
h.H.Write(w, r, keys)
}
// swagger:route POST /keys/{set} jsonWebKey createJsonWebKeySet
//
// Generate a new 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:
// 200: jsonWebKeySet
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) Create(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var keyRequest createRequest
var set = ps.ByName("set")
if err := json.NewDecoder(r.Body).Decode(&keyRequest); err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
}
generator, found := h.GetGenerators()[keyRequest.Algorithm]
if !found {
h.H.WriteErrorCode(w, r, http.StatusBadRequest, errors.Errorf("Generator %s unknown", keyRequest.Algorithm))
return
}
keys, err := generator.Generate(keyRequest.KeyID, keyRequest.Use)
if err != nil {
h.H.WriteError(w, r, err)
return
}
if err := h.Manager.AddKeySet(set, keys); err != nil {
h.H.WriteError(w, r, err)
return
}
h.H.WriteCreated(w, r, fmt.Sprintf("%s://%s/keys/%s", r.URL.Scheme, r.URL.Host, set), keys)
}
// swagger:route PUT /keys/{set} jsonWebKey updateJsonWebKeySet
//
// 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
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) UpdateKeySet(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.H.WriteError(w, r, errors.WithStack(err))
return
}
if err := h.Manager.AddKeySet(set, &keySet); err != nil {
h.H.WriteError(w, r, err)
return
}
h.H.Write(w, r, &keySet)
}
// swagger:route PUT /keys/{set}/{kid} jsonWebKey updateJsonWebKey
//
// Update a 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
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) UpdateKey(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.H.WriteError(w, r, errors.WithStack(err))
return
}
if err := h.Manager.DeleteKey(set, key.KeyID); err != nil {
h.H.WriteError(w, r, err)
return
}
if err := h.Manager.AddKey(set, &key); err != nil {
h.H.WriteError(w, r, err)
return
}
h.H.Write(w, r, key)
}
// swagger:route DELETE /keys/{set} jsonWebKey deleteJsonWebKeySet
//
// Delete a 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
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) DeleteKeySet(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var setName = ps.ByName("set")
if err := h.Manager.DeleteKeySet(setName); err != nil {
h.H.WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// swagger:route DELETE /keys/{set}/{kid} jsonWebKey deleteJsonWebKey
//
// Delete a 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
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) DeleteKey(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var setName = ps.ByName("set")
var keyName = ps.ByName("key")
if err := h.Manager.DeleteKey(setName, keyName); err != nil {
h.H.WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}