This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwx.go
279 lines (254 loc) · 5.52 KB
/
jwx.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
package otgo
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"errors"
"fmt"
"strings"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jwt"
)
// JWKSet ...
type JWKSet = jwk.Set
// Key ...
type Key = jwk.Key
// Token ...
type Token = jwt.Token
// NewToken ...
func NewToken() Token {
return jwt.New()
}
// ParseKeys ...
func ParseKeys(bs ...[]byte) ([]Key, error) {
keys := make([]Key, 0, len(bs))
for _, b := range bs {
k, err := jwk.ParseKey(b)
if err == nil {
err = validateKeys(k)
}
if err != nil {
return nil, err
}
keys = append(keys, k)
}
return keys, nil
}
// ParseKey ...
func ParseKey(s string) (Key, error) {
keys, err := ParseKeys([]byte(s))
if err != nil {
return nil, err
}
if len(keys) != 1 {
return nil, errors.New("no keys found")
}
return keys[0], nil
}
// ParseSet ...
func ParseSet(ss ...string) (*JWKSet, error) {
if len(ss) == 0 {
return nil, errors.New("otgo.ParseSet: empty string")
}
ks := &JWKSet{}
if strings.Contains(ss[0], `"keys"`) {
k, err := jwk.ParseString(ss[0])
if err == nil {
err = validateKeys(ks.Keys...)
}
if err != nil {
return nil, err
}
ks.Keys = k.Keys
} else {
bs := make([][]byte, 0, len(ss))
for _, s := range ss {
bs = append(bs, []byte(s))
}
keys, err := ParseKeys(bs...)
if err != nil {
return nil, err
}
ks.Keys = keys
}
if len(ks.Keys) == 0 {
return nil, errors.New("otgo.ParseSet: no keys found")
}
return ks, nil
}
// FetchKeys ...
func FetchKeys(ctx context.Context, jwkurl string, cli HTTPClient) (*JWKSet, error) {
ks := &jwk.Set{}
if cli == nil {
cli = DefaultHTTPClient
}
err := cli.Do(ctx, "GET", jwkurl, nil, nil, &ks)
if err == nil {
err = validateKeys(ks.Keys...)
}
if err != nil {
return nil, err
}
return ks, nil
}
// NewKeys ...
func NewKeys(keys ...Key) (*JWKSet, error) {
if err := validateKeys(keys...); err != nil {
return nil, err
}
return &jwk.Set{Keys: keys}, nil
}
// MustKeys ...
func MustKeys(keys ...Key) *JWKSet {
ks, err := NewKeys(keys...)
if err != nil {
panic(err)
}
return ks
}
// ToPublicKey ...
func ToPublicKey(k Key) (Key, error) {
switch key := k.(type) {
case jwk.RSAPrivateKey:
pub, err := key.PublicKey()
if err == nil {
err = copyParams(key, pub, "alg", "kid", "use", "key_ops")
}
if err != nil {
return nil, err
}
return pub, nil
case jwk.ECDSAPrivateKey:
pub, err := key.PublicKey()
if err == nil {
err = copyParams(key, pub, "alg", "kid", "use", "key_ops")
}
if err != nil {
return nil, err
}
return pub, nil
case jwk.RSAPublicKey, jwk.ECDSAPublicKey:
return key, nil
default:
return nil, fmt.Errorf("otgo.ToPublicKey: invalid key type %T", key)
}
}
// LookupPublicKeys ...
func LookupPublicKeys(ks *JWKSet) *JWKSet {
rs := &jwk.Set{Keys: make([]Key, 0)}
if ks != nil {
for _, k := range ks.Keys {
if pub, err := ToPublicKey(k); err == nil {
rs.Keys = append(rs.Keys, pub)
}
}
}
return rs
}
// LookupSigningKey ...
func LookupSigningKey(ks *JWKSet) (Key, error) {
if ks == nil || len(ks.Keys) == 0 {
return nil, errors.New("otgo.LookupSigningKey: no private keys exists")
}
key := ks.Keys[0]
if len(ks.Keys) > 1 {
key = ks.Keys[1]
}
switch key.(type) {
case jwk.RSAPrivateKey, jwk.ECDSAPrivateKey:
return key, nil
}
return nil, fmt.Errorf(`otgo.LookupSigningKey: invalid key type '%T'`, key)
}
// MustPrivateKey ...
func MustPrivateKey(alg string) Key {
key, err := NewPrivateKey(alg)
if err != nil {
panic(err)
}
return key
}
// NewPrivateKey ...
func NewPrivateKey(alg string) (Key, error) {
var key Key
var err error
switch jwa.SignatureAlgorithm(alg) {
case jwa.RS256, jwa.RS384, jwa.RS512, jwa.PS256, jwa.PS384, jwa.PS512:
key, err = newRSAPrivateKey()
case jwa.ES256:
key, err = newECDSAPrivateKey(elliptic.P256())
case jwa.ES384:
key, err = newECDSAPrivateKey(elliptic.P384())
case jwa.ES512:
key, err = newECDSAPrivateKey(elliptic.P521())
default:
err = fmt.Errorf("otgo.NewPrivateKey: invalid algorithm '%s'", alg)
}
if err != nil {
return nil, err
}
if err = key.Set("alg", alg); err != nil {
return nil, err
}
if err = jwk.AssignKeyID(key); err != nil {
return nil, err
}
return key, nil
}
// ValidateAlgorithm ...
func ValidateAlgorithm(alg string) bool {
switch jwa.SignatureAlgorithm(alg) {
case jwa.RS256, jwa.RS384, jwa.RS512, jwa.ES256, jwa.ES384, jwa.ES512, jwa.PS256, jwa.PS384, jwa.PS512:
return true
}
return false
}
func validateKeys(keys ...Key) error {
for _, k := range keys {
if alg := k.Algorithm(); !ValidateAlgorithm(alg) {
return fmt.Errorf("otgo.validateKeys: invalid algorithm '%s'", alg)
}
if kid := k.KeyID(); kid == "" {
return errors.New("otgo.validateKeys: kid required")
}
}
return nil
}
// The recommended RSA key-length is 2048 bits.
func newRSAPrivateKey() (Key, error) {
pk, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
key := jwk.NewRSAPrivateKey()
if err = key.FromRaw(pk); err != nil {
return nil, err
}
return key, nil
}
// newECDSAPrivateKey ...
func newECDSAPrivateKey(c elliptic.Curve) (Key, error) {
pk, err := ecdsa.GenerateKey(c, rand.Reader)
if err != nil {
return nil, err
}
key := jwk.NewECDSAPrivateKey()
if err = key.FromRaw(pk); err != nil {
return nil, err
}
return key, nil
}
func copyParams(src, dst Key, params ...string) error {
var err error
for _, k := range params {
if v, ok := src.Get(k); ok {
if err = dst.Set(k, v); err != nil {
return nil
}
}
}
return nil
}