forked from lestrrat-go/jwx
/
main.go
399 lines (380 loc) · 9.06 KB
/
main.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
package main
import (
"bytes"
"fmt"
"go/format"
"log"
"os"
"sort"
"strconv"
"github.com/pkg/errors"
)
func main() {
if err := _main(); err != nil {
log.Printf("%s", err)
os.Exit(1)
}
}
func _main() error {
typs := []typ{
{
name: `CompressionAlgorithm`,
comment: `CompressionAlgorithm represents the compression algorithms as described in https://tools.ietf.org/html/rfc7518#section-7.3`,
filename: `compression.go`,
elements: []element{
{
name: `NoCompress`,
value: ``,
comment: `No compression`,
},
{
name: `Deflate`,
value: `DEF`,
comment: `DEFLATE (RFC 1951)`,
},
},
},
{
name: `ContentEncryptionAlgorithm`,
comment: `ContentEncryptionAlgorithm represents the various encryption algorithms as described in https://tools.ietf.org/html/rfc7518#section-5`,
filename: `content_encryption.go`,
elements: []element{
{
name: `A128CBC_HS256`,
value: `A128CBC-HS256`,
comment: `AES-CBC + HMAC-SHA256 (128)`,
},
{
name: `A192CBC_HS384`,
value: `A192CBC-HS384`,
comment: `AES-CBC + HMAC-SHA384 (192)`,
},
{
name: `A256CBC_HS512`,
value: `A256CBC-HS512`,
comment: `AES-CBC + HMAC-SHA512 (256)`,
},
{
name: `A128GCM`,
value: `A128GCM`,
comment: `AES-GCM (128)`,
},
{
name: `A192GCM`,
value: `A192GCM`,
comment: `AES-GCM (192)`,
},
{
name: `A256GCM`,
value: `A256GCM`,
comment: `AES-GCM (256)`,
},
},
},
{
name: `KeyType`,
comment: `KeyType represents the key type ("kty") that are supported`,
filename: "key_type.go",
elements: []element{
{
name: `InvalidKeyType`,
value: ``,
comment: `Invalid KeyType`,
invalid: true,
},
{
name: `EC`,
value: `EC`,
comment: `Elliptic Curve`,
},
{
name: `RSA`,
value: `RSA`,
comment: `RSA`,
},
{
name: `OctetSeq`,
value: `oct`,
comment: `Octet sequence (used to represent symmetric keys)`,
},
},
},
{
name: `EllipticCurveAlgorithm`,
comment: ` EllipticCurveAlgorithm represents the algorithms used for EC keys`,
filename: `elliptic.go`,
elements: []element{
{
name: `P256`,
value: `P-256`,
},
{
name: `P384`,
value: `P-384`,
},
{
name: `P521`,
value: `P-521`,
},
},
},
{
name: `SignatureAlgorithm`,
comment: `SignatureAlgorithm represents the various signature algorithms as described in https://tools.ietf.org/html/rfc7518#section-3.1`,
filename: `signature.go`,
elements: []element{
{
name: `NoSignature`,
value: "none",
},
{
name: `HS256`,
value: "HS256",
comment: `HMAC using SHA-256`,
},
{
name: `HS384`,
value: `HS384`,
comment: `HMAC using SHA-384`,
},
{
name: `HS512`,
value: "HS512",
comment: `HMAC using SHA-512`,
},
{
name: `RS256`,
value: `RS256`,
comment: `RSASSA-PKCS-v1.5 using SHA-256`,
},
{
name: `RS384`,
value: `RS384`,
comment: `RSASSA-PKCS-v1.5 using SHA-384`,
},
{
name: `RS512`,
value: `RS512`,
comment: `RSASSA-PKCS-v1.5 using SHA-512`,
},
{
name: `ES256`,
value: `ES256`,
comment: `ECDSA using P-256 and SHA-256`,
},
{
name: `ES384`,
value: `ES384`,
comment: `ECDSA using P-384 and SHA-384`,
},
{
name: `ES512`,
value: "ES512",
comment: `ECDSA using P-521 and SHA-512`,
},
{
name: `PS256`,
value: `PS256`,
comment: `RSASSA-PSS using SHA256 and MGF1-SHA256`,
},
{
name: `PS384`,
value: `PS384`,
comment: `RSASSA-PSS using SHA384 and MGF1-SHA384`,
},
{
name: `PS512`,
value: `PS512`,
comment: `RSASSA-PSS using SHA512 and MGF1-SHA512`,
},
},
},
{
name: `KeyEncryptionAlgorithm`,
comment: `KeyEncryptionAlgorithm represents the various encryption algorithms as described in https://tools.ietf.org/html/rfc7518#section-4.1`,
filename: `key_encryption.go`,
elements: []element{
{
name: `RSA1_5`,
value: "RSA1_5",
comment: `RSA-PKCS1v1.5`,
},
{
name: `RSA_OAEP`,
value: "RSA-OAEP",
comment: `RSA-OAEP-SHA1`,
},
{
name: `RSA_OAEP_256`,
value: "RSA-OAEP-256",
comment: `RSA-OAEP-SHA256`,
},
{
name: `A128KW`,
value: "A128KW",
comment: `AES key wrap (128)`,
},
{
name: `A192KW`,
value: "A192KW",
comment: `AES key wrap (192)`,
},
{
name: `A256KW`,
value: "A256KW",
comment: `AES key wrap (256)`,
},
{
name: `DIRECT`,
value: "dir",
comment: `Direct encryption`,
},
{
name: `ECDH_ES`,
value: "ECDH-ES",
comment: `ECDH-ES`,
},
{
name: `ECDH_ES_A128KW`,
value: "ECDH-ES+A128KW",
comment: `ECDH-ES + AES key wrap (128)`,
},
{
name: `ECDH_ES_A192KW`,
value: "ECDH-ES+A192KW",
comment: `ECDH-ES + AES key wrap (192)`,
},
{
name: `ECDH_ES_A256KW`,
value: "ECDH-ES+A256KW",
comment: `ECDH-ES + AES key wrap (256)`,
},
{
name: `A128GCMKW`,
value: "A128GCMKW",
comment: `AES-GCM key wrap (128)`,
},
{
name: `A192GCMKW`,
value: "A192GCMKW",
comment: `AES-GCM key wrap (192)`,
},
{
name: `A256GCMKW`,
value: "A256GCMKW",
comment: `AES-GCM key wrap (256)`,
},
{
name: `PBES2_HS256_A128KW`,
value: "PBES2-HS256+A128KW",
comment: `PBES2 + HMAC-SHA256 + AES key wrap (128)`,
},
{
name: `PBES2_HS384_A192KW`,
value: "PBES2-HS384+A192KW",
comment: `PBES2 + HMAC-SHA384 + AES key wrap (192)`,
},
{
name: `PBES2_HS512_A256KW`,
value: "PBES2-HS512+A256KW",
comment: `PBES2 + HMAC-SHA512 + AES key wrap (256)`,
},
},
},
}
sort.Slice(typs, func(i, j int) bool {
return typs[i].name < typs[j].name
})
for _, t := range typs {
sort.Slice(t.elements, func(i, j int) bool {
return t.elements[i].name < t.elements[j].name
})
if err := t.Generate(); err != nil {
return errors.Wrap(err, `failed to generate file`)
}
}
return nil
}
type typ struct {
name string
comment string
filename string
elements []element
}
type element struct {
name string
value string
comment string
invalid bool
}
func (t typ) Generate() error {
var buf bytes.Buffer
fmt.Fprintf(&buf, "// this file was auto-generated by internal/cmd/gentypes/main.go: DO NOT EDIT")
fmt.Fprintf(&buf, "\n\npackage jwa")
fmt.Fprintf(&buf, "\n\nimport (")
for _, pkg := range []string{"github.com/pkg/errors"} {
fmt.Fprintf(&buf, "\n%s", strconv.Quote(pkg))
}
fmt.Fprintf(&buf, "\n)")
fmt.Fprintf(&buf, "\n\n// %s", t.comment)
fmt.Fprintf(&buf, "\ntype %s string", t.name)
fmt.Fprintf(&buf, "\n\n// Supported values for %s", t.name)
fmt.Fprintf(&buf, "\nconst (")
for _, e := range t.elements {
fmt.Fprintf(&buf, "\n%s %s = %s", e.name, t.name, strconv.Quote(e.value))
if len(e.comment) > 0 {
fmt.Fprintf(&buf, " // %s", e.comment)
}
}
fmt.Fprintf(&buf, "\n)") // end const
fmt.Fprintf(&buf, "\n\n// Accept is used when conversion from values given by")
fmt.Fprintf(&buf, "\n// outside sources (such as JSON payloads) is required")
fmt.Fprintf(&buf, "\nfunc (v *%s) Accept(value interface{}) error {", t.name)
fmt.Fprintf(&buf, "\nvar tmp %s", t.name)
fmt.Fprintf(&buf, "\nswitch x := value.(type) {")
fmt.Fprintf(&buf, "\ncase string:")
fmt.Fprintf(&buf, "\ntmp = %s(x)", t.name)
fmt.Fprintf(&buf, "\ncase %s:", t.name)
fmt.Fprintf(&buf, "\ntmp = x")
fmt.Fprintf(&buf, "\ndefault:")
fmt.Fprintf(&buf, "\nreturn errors.Errorf(`invalid type for jwa.%s: %%T`, value)", t.name)
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\nswitch tmp {")
fmt.Fprintf(&buf, "\ncase ")
var valids []element
for _, e := range t.elements {
if e.invalid {
continue
}
valids = append(valids, e)
}
for i, e := range valids {
fmt.Fprintf(&buf, "%s", e.name)
if i < len(valids)-1 {
fmt.Fprintf(&buf, ", ")
}
}
fmt.Fprintf(&buf, ":")
fmt.Fprintf(&buf, "\ndefault:")
fmt.Fprintf(&buf, "\nreturn errors.Errorf(`invalid jwa.%s value`)", t.name)
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n\n*v = tmp")
fmt.Fprintf(&buf, "\nreturn nil")
fmt.Fprintf(&buf, "\n}") // func (v *%s) Accept(v interface{})
fmt.Fprintf(&buf, "\n\n// String returns the string representation of a %s", t.name)
fmt.Fprintf(&buf, "\nfunc (v %s) String() string {", t.name)
fmt.Fprintf(&buf, "\nreturn string(v)")
fmt.Fprintf(&buf, "\n}")
formatted, err := format.Source(buf.Bytes())
if err != nil {
os.Stdout.Write(buf.Bytes())
return errors.Wrap(err, `failed to format source`)
}
f, err := os.Create(t.filename)
if err != nil {
return errors.Wrapf(err, `failed to create %s`, t.filename)
}
defer f.Close()
f.Write(formatted)
return nil
}