-
Notifications
You must be signed in to change notification settings - Fork 0
/
signingmethod.go
245 lines (198 loc) · 4.98 KB
/
signingmethod.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
package auth
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"gopkg.in/dgrijalva/jwt-go.v2"
)
// Size is the signature size
type Size int
// Size vars
const (
Size256 Size = 256
Size384 Size = 384
Size512 Size = 512
)
// SigningMethod generates public and private keys
type SigningMethod interface {
KID() interface{}
PublicKey(interface{}) interface{}
PrivateKey(interface{}) interface{}
Method() jwt.SigningMethod
}
type signingMethodHMAC struct {
secret []byte
method *jwt.SigningMethodHMAC
}
func (s *signingMethodHMAC) KID() interface{} {
kid := make([]byte, 10)
rand.Read(kid)
return base64.StdEncoding.EncodeToString(kid)
}
func (s *signingMethodHMAC) PublicKey(kid interface{}) interface{} {
hash := sha1.New()
binary.Write(hash, binary.BigEndian, s.secret)
binary.Write(hash, binary.BigEndian, kid)
return hash.Sum(nil)
}
func (s *signingMethodHMAC) PrivateKey(kid interface{}) interface{} {
return s.PublicKey(kid)
}
func (s *signingMethodHMAC) Method() jwt.SigningMethod {
return s.method
}
// SigningMethodHMAC implements SigningMethod based off a secret
func SigningMethodHMAC(secret []byte, size Size) SigningMethod {
var method *jwt.SigningMethodHMAC
switch size {
case Size256:
method = jwt.SigningMethodHS256
case Size384:
method = jwt.SigningMethodHS384
case Size512:
method = jwt.SigningMethodHS512
}
return &signingMethodHMAC{
secret: secret,
method: method,
}
}
// ECDSAKeyGen gets ecdsa keys based on an id
type ECDSAKeyGen interface {
KID() interface{}
Key(kid interface{}) *ecdsa.PrivateKey
}
type simpleECDSAKeyGen struct {
key *ecdsa.PrivateKey
}
func (s *simpleECDSAKeyGen) KID() interface{} {
return nil
}
func (s *simpleECDSAKeyGen) Key(kid interface{}) *ecdsa.PrivateKey {
return s.key
}
// SimpleECDSAKeyGen ignores the kid and returnes a consitent private key
func SimpleECDSAKeyGen(key *ecdsa.PrivateKey) ECDSAKeyGen {
return &simpleECDSAKeyGen{
key: key,
}
}
type signingMethodECDSA struct {
keygen ECDSAKeyGen
method *jwt.SigningMethodECDSA
}
// SigningMethodECDSA implements SigningMethod based off a private key
func SigningMethodECDSA(keygen ECDSAKeyGen, size Size) SigningMethod {
var method *jwt.SigningMethodECDSA
switch size {
case Size256:
method = jwt.SigningMethodES256
case Size384:
method = jwt.SigningMethodES384
case Size512:
method = jwt.SigningMethodES512
}
return &signingMethodECDSA{
keygen: keygen,
method: method,
}
}
func (s *signingMethodECDSA) KID() interface{} {
return s.keygen.KID()
}
func (s *signingMethodECDSA) PublicKey(kid interface{}) interface{} {
return &(s.keygen.Key(kid).PublicKey)
}
func (s *signingMethodECDSA) PrivateKey(kid interface{}) interface{} {
return s.keygen.Key(kid)
}
func (s *signingMethodECDSA) Method() jwt.SigningMethod {
return s.method
}
// RSAKeyGen gets rsa keys based on an id
type RSAKeyGen interface {
KID() interface{}
Key(kid interface{}) *rsa.PrivateKey
}
type simpleRSAKeyGen struct {
key *rsa.PrivateKey
}
func (s *simpleRSAKeyGen) KID() interface{} {
return nil
}
func (s *simpleRSAKeyGen) Key(kid interface{}) *rsa.PrivateKey {
return s.key
}
// SimpleRSAKeyGen ignores the kid and returnes a consitent private key
func SimpleRSAKeyGen(key *rsa.PrivateKey) RSAKeyGen {
return &simpleRSAKeyGen{
key: key,
}
}
type signingMethodRSA struct {
keygen RSAKeyGen
method *jwt.SigningMethodRSA
}
// SigningMethodRSA implements SigningMethod based off a private key
func SigningMethodRSA(keygen RSAKeyGen, size Size) SigningMethod {
var method *jwt.SigningMethodRSA
switch size {
case Size256:
method = jwt.SigningMethodRS256
case Size384:
method = jwt.SigningMethodRS384
case Size512:
method = jwt.SigningMethodRS512
}
return &signingMethodRSA{
keygen: keygen,
method: method,
}
}
func (s *signingMethodRSA) KID() interface{} {
return s.keygen.KID()
}
func (s *signingMethodRSA) PublicKey(kid interface{}) interface{} {
return &(s.keygen.Key(kid).PublicKey)
}
func (s *signingMethodRSA) PrivateKey(kid interface{}) interface{} {
return s.keygen.Key(kid)
}
func (s *signingMethodRSA) Method() jwt.SigningMethod {
return s.method
}
type signingMethodRSAPSS struct {
keygen RSAKeyGen
method *jwt.SigningMethodRSAPSS
}
// SigningMethodRSAPSS implements SigningMethod based off a private key
func SigningMethodRSAPSS(keygen RSAKeyGen, size Size) SigningMethod {
var method *jwt.SigningMethodRSAPSS
switch size {
case Size256:
method = jwt.SigningMethodPS256
case Size384:
method = jwt.SigningMethodPS384
case Size512:
method = jwt.SigningMethodPS512
}
return &signingMethodRSAPSS{
keygen: keygen,
method: method,
}
}
func (s *signingMethodRSAPSS) KID() interface{} {
return s.keygen.KID()
}
func (s *signingMethodRSAPSS) PublicKey(kid interface{}) interface{} {
return &(s.keygen.Key(kid).PublicKey)
}
func (s *signingMethodRSAPSS) PrivateKey(kid interface{}) interface{} {
return s.keygen.Key(kid)
}
func (s *signingMethodRSAPSS) Method() jwt.SigningMethod {
return s.method
}