This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt_test.go
196 lines (167 loc) · 4.87 KB
/
jwt_test.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
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"testing"
"time"
"github.com/golang-jwt/jwt/v4"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type JWTTests struct {
suite.Suite
TempDir string
ECKeyFile *os.File
ECPubFile *os.File
RSAKeyFile *os.File
RSAPubFile *os.File
}
func TestJWTTestSuite(t *testing.T) {
suite.Run(t, new(JWTTests))
}
func (suite *JWTTests) SetupTest() {
var err error
// Create a temporary directory for our config file
suite.TempDir, err = os.MkdirTemp(os.TempDir(), "sda-auth-test-")
if err != nil {
log.Fatal("Couldn't create temporary test directory", err)
}
// Create RSA private key file
suite.RSAKeyFile, err = os.CreateTemp(suite.TempDir, "rsakey-")
if err != nil {
log.Fatal("Cannot create temporary rsa key file", err)
}
RSAPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Error("Failed to generate RSA key")
}
var privateKeyBytes = x509.MarshalPKCS1PrivateKey(RSAPrivateKey)
privateKeyBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privateKeyBytes,
}
err = pem.Encode(suite.RSAKeyFile, privateKeyBlock)
if err != nil {
log.Error("Error writing RSA private key")
}
// Create RSA public key file
suite.RSAPubFile, err = os.CreateTemp(suite.TempDir, "rsapub-")
if err != nil {
log.Fatal("Cannot create temporary rsa pub file", err)
}
RSAPublicKey := &RSAPrivateKey.PublicKey
RSApublicKeyBytes, err := x509.MarshalPKIXPublicKey(RSAPublicKey)
if err != nil {
log.Error("Error marshal RSA public key")
}
RSApublicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: RSApublicKeyBytes,
}
err = pem.Encode(suite.RSAPubFile, RSApublicKeyBlock)
if err != nil {
log.Error("Error writing RSA public key")
}
// Create EC private key file
suite.ECKeyFile, err = os.CreateTemp(suite.TempDir, "eckey-")
if err != nil {
log.Fatal("Cannot create temporary ec key file", err)
}
ECPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Error("Failed to generate EC key")
}
privateKeyBytes, err = x509.MarshalECPrivateKey(ECPrivateKey)
if err != nil {
log.Error("Failed to marshal EC key")
}
privateKeyBlock = &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: privateKeyBytes,
}
err = pem.Encode(suite.ECKeyFile, privateKeyBlock)
if err != nil {
log.Error("Error writing EC private key")
}
// Create EC public key file
suite.ECPubFile, err = os.CreateTemp(suite.TempDir, "ecpub-")
if err != nil {
log.Fatal("Cannot create temporary ec pub file", err)
}
ECPublicKey := &ECPrivateKey.PublicKey
ECpublicKeyBytes, err := x509.MarshalPKIXPublicKey(ECPublicKey)
if err != nil {
log.Error("Error marshal EC public key")
}
ECpublicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: ECpublicKeyBytes,
}
err = pem.Encode(suite.ECPubFile, ECpublicKeyBlock)
if err != nil {
log.Error("Error writing EC public key")
}
}
func (suite *JWTTests) TearDownTest() {
os.Remove(suite.RSAKeyFile.Name())
os.Remove(suite.RSAPubFile.Name())
os.Remove(suite.ECKeyFile.Name())
os.Remove(suite.ECPubFile.Name())
os.Remove(suite.TempDir)
}
func (suite *JWTTests) TestGenerateJwtToken() {
type KeyAlgo struct {
Algorithm string
Keyfile string
Pubfile string
}
algorithms := []KeyAlgo{
{Algorithm: "RS256", Keyfile: suite.RSAKeyFile.Name(), Pubfile: suite.RSAPubFile.Name()},
{Algorithm: "ES256", Keyfile: suite.ECKeyFile.Name(), Pubfile: suite.ECPubFile.Name()},
}
claims := &Claims{
"test@foo.bar",
"",
jwt.RegisteredClaims{
IssuedAt: jwt.NewNumericDate(time.Now().UTC()),
Issuer: "http://local.issuer",
Subject: "test@foo.bar",
},
}
for _, test := range algorithms {
ts, expiration, err := generateJwtToken(claims, test.Keyfile, test.Algorithm)
assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), ts)
t, err := jwt.Parse(ts, func(t *jwt.Token) (interface{}, error) {
// Validate that the alg is what we expect: RSA or ES
_, okRSA := t.Method.(*jwt.SigningMethodRSA)
if okRSA {
pub, _ := os.ReadFile(test.Pubfile)
publicKey, _ := jwt.ParseRSAPublicKeyFromPEM(pub)
return publicKey, nil
}
_, okES := t.Method.(*jwt.SigningMethodECDSA)
if okES {
pub, _ := os.ReadFile(test.Pubfile)
publicKey, _ := jwt.ParseECPublicKeyFromPEM(pub)
return publicKey, nil
}
return nil, fmt.Errorf("unexpected signing method")
})
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), test.Algorithm, t.Method.Alg())
claims := t.Claims.(jwt.MapClaims)
assert.Equal(suite.T(), "http://local.issuer", claims["iss"])
assert.Equal(suite.T(), "test@foo.bar", claims["email"])
// check that the expiration string is a date
_, err = time.Parse("2006-01-02 15:04:05", expiration)
assert.Nil(suite.T(), err, "Couldn't parse expiration date for jwt")
}
}