-
Notifications
You must be signed in to change notification settings - Fork 0
/
gin_jwks_rsa.go
260 lines (216 loc) · 6.26 KB
/
gin_jwks_rsa.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
package gin_jwks_rsa
import (
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"fmt"
"github.com/gin-gonic/gin"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"io/ioutil"
)
const KeyUsageAsSignature = "sig"
// Config represents the available options for the middleware.
type Config struct {
key *jwk.Key
newPkOpts *NewKeyOptions
importPkOpts *ImportKeyOptions
}
type Options interface {
KeyId() string
}
// Structure used when the user generates a new private key
type NewKeyOptions struct {
keyId string
bits int
}
func (o *NewKeyOptions) KeyId() string {
return o.keyId
}
// Structure used when the user imports an existing private key
type ImportKeyOptions struct {
keyId string
privateKeyPemPath string
}
func (o *ImportKeyOptions) KeyId() string {
return o.keyId
}
// Config builder
type ConfigBuilder struct {
config *Config
}
// New key facet of the config builder
type ConfigNewKeyBuilder struct {
ConfigBuilder
}
func (n *ConfigBuilder) NewPrivateKey() *ConfigNewKeyBuilder {
return &ConfigNewKeyBuilder{*n}
}
// Import key face of the config builder
type ConfigImportKeyBuilder struct {
ConfigBuilder
}
func (n *ConfigBuilder) ImportPrivateKey() *ConfigImportKeyBuilder {
return &ConfigImportKeyBuilder{*n}
}
// Initialise a new config builder
func NewConfigBuilder() *ConfigBuilder {
return &ConfigBuilder{config: &Config{}}
}
// Initiate the import opts obj if nil
func (n *ConfigImportKeyBuilder) initiateImportOptsIfNil() {
if n.config.importPkOpts == nil {
n.config.importPkOpts = &ImportKeyOptions{}
}
}
// Add the private key path
func (n *ConfigImportKeyBuilder) WithPath(privateKeyPemPath string) *ConfigImportKeyBuilder {
n.initiateImportOptsIfNil()
n.config.importPkOpts.privateKeyPemPath = privateKeyPemPath
return n
}
// Add a key id to the private key
func (n *ConfigImportKeyBuilder) WithKeyId(keyId string) *ConfigImportKeyBuilder {
n.initiateImportOptsIfNil()
n.config.importPkOpts.keyId = keyId
return n
}
// Initiate the new opts obj if nil
func (n *ConfigNewKeyBuilder) initiateNewOptsIfNil() {
if n.config.newPkOpts == nil {
n.config.newPkOpts = &NewKeyOptions{}
}
}
// Add the key length
func (n *ConfigNewKeyBuilder) WithKeyLength(bits int) *ConfigNewKeyBuilder {
n.initiateNewOptsIfNil()
n.config.newPkOpts.bits = bits
return n
}
// Add a key id to the private key
func (n *ConfigNewKeyBuilder) WithKeyId(keyId string) *ConfigNewKeyBuilder {
n.initiateNewOptsIfNil()
n.config.newPkOpts.keyId = keyId
return n
}
// Build the config object in order to initiate the middleware
func (b *ConfigBuilder) Build() (*Config, error) {
var key jwk.Key
var opts Options
var err error
if b.config.newPkOpts != nil && b.config.importPkOpts != nil {
return nil, fmt.Errorf("cannot import and generate a new private key")
}
// generate a new private key
if b.config.newPkOpts != nil {
newPkOpts := b.config.newPkOpts
key, err = generatePrivateKey(*newPkOpts)
if err != nil {
return nil, fmt.Errorf("cannot generate new private key %v", err)
}
opts = newPkOpts
}
// import the private key
if b.config.importPkOpts != nil {
importPkOpts := b.config.importPkOpts
key, err = importPrivateKey(*importPkOpts)
if err != nil {
return nil, fmt.Errorf("cannot import private key %v", err)
}
opts = importPkOpts
}
if key == nil {
return nil, fmt.Errorf("generate or import a private key")
}
// add an id to the certificate according to RFC
err = key.Set(jwk.KeyIDKey, opts.KeyId())
if err != nil {
return nil, fmt.Errorf("cannot add an id property to the private key %v", err)
}
err = key.Set(jwk.KeyUsageKey, KeyUsageAsSignature)
if err != nil {
return nil, fmt.Errorf("cannot add an id property to the private key %v", err)
}
// cast to private key
if _, ok := key.(jwk.RSAPrivateKey); !ok {
return nil, fmt.Errorf("expected jwk.SymmetricKey, got %T", key)
}
// generate public key
_, err = key.PublicKey()
if err != nil {
return nil, fmt.Errorf("failed to create public key %v", err)
}
b.config.key = &key
return b.config, nil
}
// Generate a private key
func generatePrivateKey(opts NewKeyOptions) (jwk.Key, error) {
rawPrivateKey, err := rsa.GenerateKey(rand.Reader, opts.bits)
if err != nil {
return nil, fmt.Errorf("failed to generate new RSA private key: %s\n", err)
}
key, err := jwk.FromRaw(rawPrivateKey)
if err != nil {
return nil, fmt.Errorf("failed to create symmetric key: %s\n", err)
}
return key, nil
}
// Import a private key with pem format
func importPrivateKey(opts ImportKeyOptions) (jwk.Key, error) {
// import from path
keyData, err := ioutil.ReadFile(opts.privateKeyPemPath)
if err != nil {
return nil, fmt.Errorf("cannot read private key %v", err)
}
// check if it's a PEM file
key, err := jwk.ParseKey(keyData, jwk.WithPEM(true))
if err != nil {
return nil, fmt.Errorf("cannot parse private key %v", err)
}
return key, nil
}
// Refer to rfc for more information: https://www.rfc-editor.org/rfc/rfc7518#section-6.3.1
type JkwsResponse struct {
KeyTypeKey string `json:"kty"`
AlgorithmKey string `json:"alg"`
PubKeyExponentKey string `json:"e"`
PubKeyModulusKey string `json:"n"`
KeyUsageKey string `json:"use"`
KeyIDKey string `json:"kid"`
}
// Jkws middleware exposing the public key properties required in order to decrypt
// a jwt token
func Jkws(config Config) gin.HandlerFunc {
return func(c *gin.Context) {
if config.key == nil {
c.Error(fmt.Errorf("private key cannot be nil"))
c.AbortWithStatus(500)
return
}
// get private key and its properties
key := *config.key
// get public key
pubKey, _ := key.PublicKey()
// get public key exponent
E, _ := key.Get("e")
// get public key modulus
N, _ := key.Get("n")
// generate jkws response
res := JkwsResponse{
KeyTypeKey: pubKey.KeyType().String(),
AlgorithmKey: jwa.RS256.String(),
PubKeyExponentKey: EncodeToString(E.([]byte)),
PubKeyModulusKey: EncodeToString(N.([]byte)),
KeyUsageKey: key.KeyUsage(),
KeyIDKey: key.KeyID(),
}
// expose jkws response
c.JSON(200, gin.H{
"keys": []JkwsResponse{res},
})
}
}
// EncodeToString utility which converts []byte into a base64 string
func EncodeToString(src []byte) string {
return base64.RawURLEncoding.EncodeToString(src)
}