This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
272 lines (242 loc) · 7.04 KB
/
helper.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
package helper
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"os"
"path/filepath"
"time"
"github.com/golang-jwt/jwt/v4"
)
// Global variables for test token creation
var (
DefaultTokenClaims = map[string]interface{}{
"iss": "https://dummy.ega.nbis.se",
"sub": "dummy",
"exp": time.Now().Add(time.Hour * 2).Unix(),
"pilot": "dummy-pilot",
}
WrongUserClaims = map[string]interface{}{
"sub": "c5773f41d17d27bd53b1e6794aedc32d7906e779@elixir-europe.org",
"aud": "15137645-3153-4d49-9ddb-594027cd4ca7",
"azp": "15137645-3153-4d49-9ddb-594027cd4ca7",
"scope": "ga4gh_passport_v1 openid",
"iss": "https://dummy.ega.nbis.se",
"iat": time.Now().Unix(),
"exp": time.Now().Add(time.Hour * 2).Unix(),
"jti": "5e6c6d24-42eb-408e-ba20-203904e388a1",
}
ExpiredClaims = map[string]interface{}{
"sub": "dummy",
"aud": "15137645-3153-4d49-9ddb-594027cd4ca7",
"azp": "15137645-3153-4d49-9ddb-594027cd4ca7",
"scope": "ga4gh_passport_v1 openid",
"iss": "https://dummy.ega.nbis.se",
"exp": time.Now().Add(-time.Hour * 2).Unix(),
"iat": time.Now().Unix(),
"jti": "5e6c6d24-42eb-408e-ba20-203904e388a1",
}
ExpiredAndWrongUserClaims = map[string]interface{}{
"sub": "c5773f41d17d27bd53b1e6794aedc32d7906e779@elixir-europe.org",
"aud": "15137645-3153-4d49-9ddb-594027cd4ca7",
"azp": "15137645-3153-4d49-9ddb-594027cd4ca7",
"scope": "ga4gh_passport_v1 openid",
"iss": "https://dummy.ega.nbis.se",
"exp": time.Now().Add(-time.Hour * 2).Unix(),
"iat": time.Now().Unix(),
"jti": "5e6c6d24-42eb-408e-ba20-203904e388a1",
}
NonValidClaims = map[string]interface{}{
"sub": "c5773f41d17d27bd53b1e6794aedc32d7906e779@elixir-europe.org",
"aud": "15137645-3153-4d49-9ddb-594027cd4ca7",
"azp": "15137645-3153-4d49-9ddb-594027cd4ca7",
"scope": "ga4gh_passport_v1 openid",
"iss": "https://dummy.ega.nbis.se",
"exp": time.Now().Add(time.Hour * 2).Unix(),
"iat": time.Now().Add(time.Hour * 2).Unix(),
"jti": "5e6c6d24-42eb-408e-ba20-203904e388a1",
}
WrongTokenAlgClaims = map[string]interface{}{
"iss": "Online JWT Builder",
"iat": time.Now().Unix(),
"exp": time.Now().Add(time.Hour * 2).Unix(),
"aud": "4e9416a7-3515-447a-b848-d4ac7a57f",
"sub": "pleasefix@snurre-in-the-house.org",
"auth_time": "1632207224",
"jti": "cc847f9c-7608-4b4f-9c6f-6e734813355f",
}
)
// MakeFolder creates a folder and subfolders for the keys pair
// Returns the two paths
func MakeFolder(path string) (string, string, error) {
prKeyPath := path + "/private-key"
pubKeyPath := path + "/public-key"
err := os.MkdirAll(prKeyPath, 0750)
if err != nil {
// fmt.Errorf("error creating directory: %v", err)
return " ", " ", err
}
err = os.MkdirAll(pubKeyPath, 0750)
if err != nil {
// fmt.Errorf("error creatin directory: %w", err)
return " ", " ", err
}
return prKeyPath, pubKeyPath, nil
}
// ParsePrivateRSAKey reads and parses the RSA private key
func ParsePrivateRSAKey(path, keyName string) (*rsa.PrivateKey, error) {
keyPath := path + keyName
prKey, err := os.ReadFile(filepath.Clean(keyPath))
if err != nil {
return nil, err
}
prKeyParsed, err := jwt.ParseRSAPrivateKeyFromPEM(prKey)
if err != nil {
return nil, err
}
return prKeyParsed, nil
}
// CreateRSAkeys creates the RSA key pair
func CreateRSAkeys(prPath, pubPath string) error {
privatekey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
publickey := &privatekey.PublicKey
// dump private key to file
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privatekey)
privateKeyBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privateKeyBytes,
}
privatePem, err := os.Create(prPath + "/dummy.ega.nbis.se")
if err != nil {
return err
}
err = pem.Encode(privatePem, privateKeyBlock)
if err != nil {
return err
}
// dump public key to file
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publickey)
if err != nil {
return err
}
publicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
publicPem, err := os.Create(pubPath + "/dummy.ega.nbis.se.pub")
if err != nil {
return err
}
err = pem.Encode(publicPem, publicKeyBlock)
if err != nil {
return err
}
return nil
}
// CreateRSAToken creates an RSA token
func CreateRSAToken(key *rsa.PrivateKey, headerAlg, headerType string, tokenClaims map[string]interface{}) (string, error) {
token := jwt.New(jwt.SigningMethodRS256)
token.Header["alg"] = headerAlg
token.Header["typ"] = headerType
claims := make(jwt.MapClaims)
for key, value := range tokenClaims {
claims[key] = value
}
token.Claims = claims
tokenString, err := token.SignedString(key)
if err != nil {
return "no-token", err
}
return tokenString, nil
}
// CreateECToken creates an EC token
func CreateECToken(key *ecdsa.PrivateKey, headerAlg, headerType string, tokenClaims map[string]interface{}) (string, error) {
token := jwt.New(jwt.SigningMethodES256)
token.Header["alg"] = headerAlg
token.Header["typ"] = headerType
claims := make(jwt.MapClaims)
for key, value := range tokenClaims {
claims[key] = value
}
token.Claims = claims
tokenString, err := token.SignedString(key)
if err != nil {
return "no-token", err
}
return tokenString, nil
}
// CreateHSToken creates an HS token
func CreateHSToken(key []byte, headerAlg, headerType string, tokenClaims map[string]interface{}) (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
token.Header["alg"] = headerAlg
token.Header["typ"] = headerType
claims := make(jwt.MapClaims)
for key, value := range tokenClaims {
claims[key] = value
}
token.Claims = claims
tokenString, err := token.SignedString(key)
if err != nil {
return "no-token", err
}
return tokenString, nil
}
// ParsePrivateECKey reads and parses the EC private key
func ParsePrivateECKey(path, keyName string) (*ecdsa.PrivateKey, error) {
keyPath := path + keyName
prKey, err := os.ReadFile(filepath.Clean(keyPath))
if err != nil {
return nil, err
}
prKeyParsed, err := jwt.ParseECPrivateKeyFromPEM(prKey)
if err != nil {
return nil, err
}
return prKeyParsed, nil
}
// CreateECkeys creates the EC key pair
func CreateECkeys(prPath, pubPath string) error {
privatekey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return err
}
publickey := &privatekey.PublicKey
// dump private key to file
privateKeyBytes, _ := x509.MarshalECPrivateKey(privatekey)
privateKeyBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKeyBytes,
}
privatePem, err := os.Create(prPath + "/dummy.ega.nbis.se")
if err != nil {
return err
}
err = pem.Encode(privatePem, privateKeyBlock)
if err != nil {
return err
}
// dump public key to file
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publickey)
if err != nil {
return err
}
publicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
publicPem, err := os.Create(pubPath + "/dummy.ega.nbis.se.pub")
if err != nil {
return err
}
err = pem.Encode(publicPem, publicKeyBlock)
if err != nil {
return err
}
return nil
}