-
Notifications
You must be signed in to change notification settings - Fork 9
/
jwt_verifier_test.go
273 lines (250 loc) · 8.94 KB
/
jwt_verifier_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
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
// Copyright 2024 Thales Group
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gose
import (
"testing"
"time"
"crypto/x509"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/ThalesGroup/gose/jose"
)
const (
/* The below jwt contains the following generated @ https://jwt.io/:
header:
- typ: JWT
- alg: PS256/ES256
- kid: 12345
claims:
- sub: 1234567890,
- aud: test,
- jti: 31c8767d-6732-4db4-b488-21cf34e5412d,
- iat: 1514977397,
- exp: 1514980997,
- nbf: 1514980997
*/
rsaValidJwtPayload = "eyJhbGciOiJQUzI1NiIsInR5cCI6IkpXVCIsICJraWQiOiIxMjM0NSJ9Cg.eyJzdWIiOiIxMjM0NTY3ODkwIiwiYXVkIjoidGVzdCIsImp0aSI6IjMxYzg3NjdkLTY3MzItNGRiNC1iNDg4LTIxY2YzNGU1NDEyZCIsImlhdCI6MTUxNDk3NzM5NywibmJmIjoxNTE0OTc3Mzk3LCJleHAiOjE1MTQ5ODA5OTd9Cg"
rsaValidJwtSignature = "q0BpUU1b3rMraJR0dWGdpEffEBjmDhwT9O1AnSjvDGM"
rsaValidJwt = rsaValidJwtPayload + "." + rsaValidJwtSignature
rsaValidJwtAlg = jose.AlgPS256
validJwtKid = "12345"
validJwtSub = "1234567890"
validJwtAud = "test"
validJwtJti = "31c8767d-6732-4db4-b488-21cf34e5412d"
validJwtIat int64 = 1514977397
validJwtNbf int64 = 1514977397
validJwtExp int64 = 1514980997
)
func unixStartTime() time.Time {
return time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
}
type MockedTrustKeyStore struct {
mock.Mock
}
func (store *MockedTrustKeyStore) Add(issuer string, jwk jose.Jwk) error {
args := store.Called(issuer, jwk)
return args.Error(0)
}
func (store *MockedTrustKeyStore) Remove(issuer, kid string) bool {
args := store.Called(issuer, kid)
return args.Bool(0)
}
func (store *MockedTrustKeyStore) Get(issuer, kid string) (VerificationKey, error) {
args := store.Called(issuer, kid)
return args.Get(0).(VerificationKey), args.Error(1)
}
type MockedVerificationKey struct {
mock.Mock
}
func (key *MockedVerificationKey) Algorithm() jose.Alg {
return key.Called().Get(0).(jose.Alg)
}
func (key *MockedVerificationKey) Kid() string {
return key.Called().Get(0).(string)
}
func (key *MockedVerificationKey) Jwk() (jose.Jwk, error) {
args := key.Called()
return args.Get(0).(jose.Jwk), args.Error(1)
}
func (key *MockedVerificationKey) Marshal() (string, error) {
args := key.Called()
return args.String(0), args.Error(1)
}
func (key *MockedVerificationKey) MarshalPem() (string, error) {
args := key.Called()
return args.String(0), args.Error(1)
}
func (key *MockedVerificationKey) Certificates() []*x509.Certificate {
return key.Called().Get(0).([]*x509.Certificate)
}
func (key *MockedVerificationKey) Verify(operation jose.KeyOps, data []byte, signature []byte) bool {
args := key.Called(operation, data, signature)
return args.Bool(0)
}
func TestNewJwtVerifier(t *testing.T) {
// Setup
ks := MockedTrustKeyStore{}
// Act
verifier := NewJwtVerifier(&ks)
// Assert
require.NotNil(t, verifier)
assert.Equal(t, &ks, verifier.store)
}
//
//func TestJwtVerifierImpl_Verify(t *testing.T) {
// // Setup
// defer monkey.Patch(time.Now, func() time.Time {
// return unixStartTime().Add(time.Duration(validJwtNbf) * time.Second)
// }).Unpatch()
// key := MockedVerificationKey{}
// signatureBytes, err := base64.RawURLEncoding.DecodeString(rsaValidJwtSignature)
// require.NoError(t, err)
// key.On("Verify", jose.KeyOpsVerify, []byte(rsaValidJwtPayload), signatureBytes).Return(true)
// key.On("Algorithm").Return(rsaValidJwtAlg)
// key.On("Kid").Return(validJwtKid)
// ks := MockedTrustKeyStore{}
// ks.On("Get", mock.Anything, validJwtKid).Return(&key, nil)
// verifier := NewJwtVerifier(&ks)
//
// // Act
// kid, claimSet, err := verifier.Verify(rsaValidJwt, []string{"test"})
//
// // Assert
// require.NoError(t, err)
// require.NotNil(t, claimSet)
// assert.Equal(t, validJwtKid, kid)
// assert.Equal(t, validJwtAud, claimSet.Audiences.Aud[0])
// assert.Equal(t, validJwtSub, claimSet.Subject)
// assert.NotEmpty(t, validJwtJti, claimSet.JwtID)
// assert.Equal(t, validJwtIat, claimSet.IssuedAt)
// assert.Equal(t, validJwtExp, claimSet.Expiration)
// assert.Equal(t, validJwtNbf, claimSet.NotBefore)
//}
//
//func TestJwtVerifierImpl_Verify_FailsWithInvalidSignature(t *testing.T) {
// // Setup
// defer monkey.Patch(time.Now, func() time.Time {
// return unixStartTime().Add(time.Duration(validJwtNbf) * time.Second)
// }).Unpatch()
// key := MockedVerificationKey{}
// signatureBytes, err := base64.RawURLEncoding.DecodeString(rsaValidJwtSignature)
// require.NoError(t, err)
// key.On("Verify", jose.KeyOpsVerify, []byte(rsaValidJwtPayload), signatureBytes).Return(false)
// key.On("Algorithm").Return(rsaValidJwtAlg)
// ks := MockedTrustKeyStore{}
// ks.On("Verifier", validJwtKid).Return(&key)
// ks.On("Get", mock.Anything, validJwtKid).Return(&key, nil)
// verifier := NewJwtVerifier(&ks)
//
// // Act
// _, claimSet, err := verifier.Verify(rsaValidJwt, []string{"test"})
//
// // Assert
// require.Equal(t, ErrInvalidSignature, err)
// require.Nil(t, claimSet)
//}
//
//func TestJwtVerifierImpl_Verify_FailsWithNbfViolation(t *testing.T) {
// // Setup
// defer monkey.Patch(time.Now, func() time.Time {
// return unixStartTime().Add(time.Duration(validJwtNbf-10) * time.Second)
// }).Unpatch()
// key := MockedVerificationKey{}
// signatureBytes, err := base64.RawURLEncoding.DecodeString(rsaValidJwtSignature)
// require.NoError(t, err)
// key.On("Verify", jose.KeyOpsVerify, []byte(rsaValidJwtPayload), signatureBytes).Return(true)
// key.On("Algorithm").Return(rsaValidJwtAlg)
// ks := MockedTrustKeyStore{}
// ks.On("Verifier", validJwtKid).Return(&key)
// ks.On("Get", mock.Anything, validJwtKid).Return(&key, nil)
// verifier := NewJwtVerifier(&ks)
//
// // Act
// kid, claimSet, err := verifier.Verify(rsaValidJwt, []string{"test"})
//
// // Assert
// require.Equal(t, ErrInvalidJwtTimeframe, err)
// require.Empty(t, kid)
// require.Nil(t, claimSet)
//}
//
//func TestJwtVerifierImpl_Verify_FailsWithExpViolation(t *testing.T) {
// // Setup
// defer monkey.Patch(time.Now, func() time.Time {
// return unixStartTime().Add(time.Duration(validJwtExp+10) * time.Second)
// }).Unpatch()
// key := MockedVerificationKey{}
// signatureBytes, err := base64.RawURLEncoding.DecodeString(rsaValidJwtSignature)
// require.NoError(t, err)
// key.On("Verify", jose.KeyOpsVerify, []byte(rsaValidJwtPayload), signatureBytes).Return(true)
// key.On("Algorithm").Return(rsaValidJwtAlg)
// ks := MockedTrustKeyStore{}
// ks.On("Verifier", validJwtKid).Return(&key)
// ks.On("Get", mock.Anything, validJwtKid).Return(&key, nil)
// verifier := NewJwtVerifier(&ks)
//
// // Act
// kid, claimSet, err := verifier.Verify(rsaValidJwt, []string{"test"})
//
// // Assert
// require.Equal(t, ErrInvalidJwtTimeframe, err)
// require.Empty(t, kid)
// require.Nil(t, claimSet)
////}
//
//func TestJwtVerifierImpl_Verify_FailsWithNoKnownAudience(t *testing.T) {
// testcases := []struct {
// audiences []string
// seen []string
// }{
// {
// audiences: []string{"unknown"},
// seen: []string{"test"},
// },
// {
// audiences: []string{},
// seen: []string{},
// },
// }
// // Setup
// defer monkey.Patch(time.Now, func() time.Time {
// return unixStartTime().Add(time.Duration(validJwtNbf) * time.Second)
// }).Unpatch()
// key := MockedVerificationKey{}
// signatureBytes, err := base64.RawURLEncoding.DecodeString(rsaValidJwtSignature)
// require.NoError(t, err)
// key.On("Verify", jose.KeyOpsVerify, []byte(rsaValidJwtPayload), signatureBytes).Return(false)
// key.On("Algorithm").Return(rsaValidJwtAlg)
// ks := MockedTrustKeyStore{}
// ks.On("Verifier", validJwtKid).Return(&key)
// ks.On("Get", mock.Anything, validJwtKid).Return(&key, nil)
// verifier := NewJwtVerifier(&ks)
//
// for _, test := range testcases {
// // Act
// _, _, err := verifier.Verify(rsaValidJwt, test.audiences)
//
// // Assert
// ee := &InvalidFormat{fmt.Sprintf("no expected audience | expected %s | seen %s", test.audiences, test.seen)}
// assert.Equal(t, ee, err)
// }
//}