forked from lestrrat-go/jwx
/
headers_gen.go
379 lines (355 loc) · 9.79 KB
/
headers_gen.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
// This file is auto-generated. DO NOT EDIT
package jwk
import (
"crypto/x509"
"fmt"
"github.com/lestrrat-go/jwx/jwa"
"github.com/pkg/errors"
)
const (
AlgorithmKey = "alg"
KeyIDKey = "kid"
KeyTypeKey = "kty"
KeyUsageKey = "use"
KeyOpsKey = "key_ops"
X509CertChainKey = "x5c"
X509CertThumbprintKey = "x5t"
X509CertThumbprintS256Key = "x5t#S256"
X509URLKey = "x5u"
)
type Headers interface {
Remove(string)
Get(string) (interface{}, bool)
Set(string, interface{}) error
PopulateMap(map[string]interface{}) error
ExtractMap(map[string]interface{}) error
Walk(func(string, interface{}) error) error
Algorithm() string
KeyID() string
KeyType() jwa.KeyType
KeyUsage() string
KeyOps() KeyOperationList
X509CertChain() []*x509.Certificate
X509CertThumbprint() string
X509CertThumbprintS256() string
X509URL() string
}
type StandardHeaders struct {
algorithm *string // https://tools.ietf.org/html/rfc7517#section-4.4
keyID *string // https://tools.ietf.org/html/rfc7515#section-4.1.4
keyType *jwa.KeyType // https://tools.ietf.org/html/rfc7517#section-4.1
keyUsage *string // https://tools.ietf.org/html/rfc7517#section-4.2
keyops KeyOperationList // https://tools.ietf.org/html/rfc7517#section-4.3
x509CertChain *CertificateChain // https://tools.ietf.org/html/rfc7515#section-4.1.6
x509CertThumbprint *string // https://tools.ietf.org/html/rfc7515#section-4.1.7
x509CertThumbprintS256 *string // https://tools.ietf.org/html/rfc7515#section-4.1.8
x509URL *string // https://tools.ietf.org/html/rfc7515#section-4.1.5
privateParams map[string]interface{}
}
func (h *StandardHeaders) Remove(s string) {
delete(h.privateParams, s)
}
func (h *StandardHeaders) Algorithm() string {
if v := h.algorithm; v != nil {
return *v
}
return ""
}
func (h *StandardHeaders) KeyID() string {
if v := h.keyID; v != nil {
return *v
}
return ""
}
func (h *StandardHeaders) KeyType() jwa.KeyType {
if v := h.keyType; v != nil {
return *v
}
return jwa.InvalidKeyType
}
func (h *StandardHeaders) KeyUsage() string {
if v := h.keyUsage; v != nil {
return *v
}
return ""
}
func (h *StandardHeaders) KeyOps() KeyOperationList {
return h.keyops
}
func (h *StandardHeaders) X509CertChain() []*x509.Certificate {
return h.x509CertChain.Get()
}
func (h *StandardHeaders) X509CertThumbprint() string {
if v := h.x509CertThumbprint; v != nil {
return *v
}
return ""
}
func (h *StandardHeaders) X509CertThumbprintS256() string {
if v := h.x509CertThumbprintS256; v != nil {
return *v
}
return ""
}
func (h *StandardHeaders) X509URL() string {
if v := h.x509URL; v != nil {
return *v
}
return ""
}
func (h *StandardHeaders) Get(name string) (interface{}, bool) {
switch name {
case AlgorithmKey:
v := h.algorithm
if v == nil {
return nil, false
}
return *v, true
case KeyIDKey:
v := h.keyID
if v == nil {
return nil, false
}
return *v, true
case KeyTypeKey:
v := h.keyType
if v == nil {
return nil, false
}
return *v, true
case KeyUsageKey:
v := h.keyUsage
if v == nil {
return nil, false
}
return *v, true
case KeyOpsKey:
v := h.keyops
if v == nil {
return nil, false
}
return v, true
case X509CertChainKey:
v := h.x509CertChain
if v == nil {
return nil, false
}
return v.Get(), true
case X509CertThumbprintKey:
v := h.x509CertThumbprint
if v == nil {
return nil, false
}
return *v, true
case X509CertThumbprintS256Key:
v := h.x509CertThumbprintS256
if v == nil {
return nil, false
}
return *v, true
case X509URLKey:
v := h.x509URL
if v == nil {
return nil, false
}
return *v, true
default:
v, ok := h.privateParams[name]
return v, ok
}
}
func (h *StandardHeaders) Set(name string, value interface{}) error {
switch name {
case AlgorithmKey:
switch v := value.(type) {
case string:
h.algorithm = &v
return nil
case fmt.Stringer:
s := v.String()
h.algorithm = &s
return nil
}
return errors.Errorf(`invalid value for %s key: %T`, AlgorithmKey, value)
case KeyIDKey:
if v, ok := value.(string); ok {
h.keyID = &v
return nil
}
return errors.Errorf(`invalid value for %s key: %T`, KeyIDKey, value)
case KeyTypeKey:
var acceptor jwa.KeyType
if err := acceptor.Accept(value); err != nil {
return errors.Wrapf(err, `invalid value for %s key`, KeyTypeKey)
}
h.keyType = &acceptor
return nil
case KeyUsageKey:
if v, ok := value.(string); ok {
h.keyUsage = &v
return nil
}
return errors.Errorf(`invalid value for %s key: %T`, KeyUsageKey, value)
case KeyOpsKey:
if err := h.keyops.Accept(value); err != nil {
return errors.Wrapf(err, `invalid value for %s key`, KeyOpsKey)
}
return nil
case X509CertChainKey:
var acceptor CertificateChain
if err := acceptor.Accept(value); err != nil {
return errors.Wrapf(err, `invalid value for %s key`, X509CertChainKey)
}
h.x509CertChain = &acceptor
return nil
case X509CertThumbprintKey:
if v, ok := value.(string); ok {
h.x509CertThumbprint = &v
return nil
}
return errors.Errorf(`invalid value for %s key: %T`, X509CertThumbprintKey, value)
case X509CertThumbprintS256Key:
if v, ok := value.(string); ok {
h.x509CertThumbprintS256 = &v
return nil
}
return errors.Errorf(`invalid value for %s key: %T`, X509CertThumbprintS256Key, value)
case X509URLKey:
if v, ok := value.(string); ok {
h.x509URL = &v
return nil
}
return errors.Errorf(`invalid value for %s key: %T`, X509URLKey, value)
default:
if h.privateParams == nil {
h.privateParams = map[string]interface{}{}
}
h.privateParams[name] = value
}
return nil
}
// PopulateMap populates a map with appropriate values that represent
// the headers as a JSON object. This exists primarily because JWKs are
// represented as flat objects instead of differentiating the different
// parts of the message in separate sub objects.
func (h StandardHeaders) PopulateMap(m map[string]interface{}) error {
for k, v := range h.privateParams {
m[k] = v
}
if v, ok := h.Get(AlgorithmKey); ok {
m[AlgorithmKey] = v
}
if v, ok := h.Get(KeyIDKey); ok {
m[KeyIDKey] = v
}
if v, ok := h.Get(KeyTypeKey); ok {
m[KeyTypeKey] = v
}
if v, ok := h.Get(KeyUsageKey); ok {
m[KeyUsageKey] = v
}
if v, ok := h.Get(KeyOpsKey); ok {
m[KeyOpsKey] = v
}
if v, ok := h.Get(X509CertChainKey); ok {
var chain []*x509.Certificate
if chain, ok = v.([]*x509.Certificate); !ok {
return errors.Errorf(`X509CertChain type is invalid: %T`, v)
}
encodedChain, err := marshalX509CertChain(chain)
if err != nil {
return err
}
m[X509CertChainKey] = encodedChain
}
if v, ok := h.Get(X509CertThumbprintKey); ok {
m[X509CertThumbprintKey] = v
}
if v, ok := h.Get(X509CertThumbprintS256Key); ok {
m[X509CertThumbprintS256Key] = v
}
if v, ok := h.Get(X509URLKey); ok {
m[X509URLKey] = v
}
return nil
}
// ExtractMap populates the appropriate values from a map that represent
// the headers as a JSON object. This exists primarily because JWKs are
// represented as flat objects instead of differentiating the different
// parts of the message in separate sub objects.
func (h *StandardHeaders) ExtractMap(m map[string]interface{}) (err error) {
if v, ok := m[AlgorithmKey]; ok {
if err := h.Set(AlgorithmKey, v); err != nil {
return errors.Wrapf(err, `failed to set value for key %s`, AlgorithmKey)
}
delete(m, AlgorithmKey)
}
if v, ok := m[KeyIDKey]; ok {
if err := h.Set(KeyIDKey, v); err != nil {
return errors.Wrapf(err, `failed to set value for key %s`, KeyIDKey)
}
delete(m, KeyIDKey)
}
if v, ok := m[KeyTypeKey]; ok {
if err := h.Set(KeyTypeKey, v); err != nil {
return errors.Wrapf(err, `failed to set value for key %s`, KeyTypeKey)
}
delete(m, KeyTypeKey)
}
if v, ok := m[KeyUsageKey]; ok {
if err := h.Set(KeyUsageKey, v); err != nil {
return errors.Wrapf(err, `failed to set value for key %s`, KeyUsageKey)
}
delete(m, KeyUsageKey)
}
if v, ok := m[KeyOpsKey]; ok {
if err := h.Set(KeyOpsKey, v); err != nil {
return errors.Wrapf(err, `failed to set value for key %s`, KeyOpsKey)
}
delete(m, KeyOpsKey)
}
if v, ok := m[X509CertChainKey]; ok {
if err := h.Set(X509CertChainKey, v); err != nil {
return errors.Wrapf(err, `failed to set value for key %s`, X509CertChainKey)
}
delete(m, X509CertChainKey)
}
if v, ok := m[X509CertThumbprintKey]; ok {
if err := h.Set(X509CertThumbprintKey, v); err != nil {
return errors.Wrapf(err, `failed to set value for key %s`, X509CertThumbprintKey)
}
delete(m, X509CertThumbprintKey)
}
if v, ok := m[X509CertThumbprintS256Key]; ok {
if err := h.Set(X509CertThumbprintS256Key, v); err != nil {
return errors.Wrapf(err, `failed to set value for key %s`, X509CertThumbprintS256Key)
}
delete(m, X509CertThumbprintS256Key)
}
if v, ok := m[X509URLKey]; ok {
if err := h.Set(X509URLKey, v); err != nil {
return errors.Wrapf(err, `failed to set value for key %s`, X509URLKey)
}
delete(m, X509URLKey)
}
// Fix: A nil map is different from a empty map as far as deep.equal is concerned
if len(m) > 0 {
h.privateParams = m
}
return nil
}
func (h StandardHeaders) Walk(f func(string, interface{}) error) error {
for _, key := range []string{AlgorithmKey, KeyIDKey, KeyTypeKey, KeyUsageKey, KeyOpsKey, X509CertChainKey, X509CertThumbprintKey, X509CertThumbprintS256Key, X509URLKey} {
if v, ok := h.Get(key); ok {
if err := f(key, v); err != nil {
return errors.Wrapf(err, `walk function returned error for %s`, key)
}
}
}
for k, v := range h.privateParams {
if err := f(k, v); err != nil {
return errors.Wrapf(err, `walk function returned error for %s`, k)
}
}
return nil
}