forked from linuxfoundation/lfx-v2-auth-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwt_parser_test.go
More file actions
387 lines (339 loc) · 9.84 KB
/
jwt_parser_test.go
File metadata and controls
387 lines (339 loc) · 9.84 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright The Linux Foundation and each contributor to LFX.
// SPDX-License-Identifier: MIT
package auth0
import (
"context"
"crypto/rand"
"crypto/rsa"
"testing"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/linuxfoundation/lfx-v2-auth-service/internal/domain/model"
"github.com/linuxfoundation/lfx-v2-auth-service/pkg/constants"
"github.com/linuxfoundation/lfx-v2-auth-service/pkg/httpclient"
)
func TestJWTVerification(t *testing.T) {
// Generate a test RSA key pair
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("Failed to generate RSA key: %v", err)
}
publicKey := &privateKey.PublicKey
// Create JWT verification config
jwtVerify := &JWTVerificationConfig{
PublicKey: publicKey,
ExpectedIssuer: "https://test.auth0.com/",
ExpectedAudience: "https://test.auth0.com/api/v2/",
}
tests := []struct {
name string
token string
expectError bool
}{
{
name: "valid JWT with signature verification",
token: createValidJWT(t, privateKey),
expectError: false,
},
{
name: "invalid signature",
token: createInvalidSignatureJWT(t),
expectError: true,
},
{
name: "expired JWT",
token: createExpiredJWT(t, privateKey),
expectError: true,
},
{
name: "wrong issuer",
token: createWrongIssuerJWT(t, privateKey),
expectError: true,
},
{
name: "wrong audience",
token: createWrongAudienceJWT(t, privateKey),
expectError: true,
},
{
name: "missing required scope",
token: createMissingScopeJWT(t, privateKey),
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
user := &model.User{
Token: tt.token,
}
claims, err := jwtVerify.JWTVerify(ctx, user.Token, constants.UserUpdateMetadataRequiredScope)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if claims != nil {
user.UserID = claims.Subject
}
if user.UserID != "test-user-123" {
t.Errorf("Expected user ID 'test-user-123', got '%s'", user.UserID)
}
}
})
}
}
func TestMetadataLookupWithJWTVerification(t *testing.T) {
// Generate a test RSA key pair
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("Failed to generate RSA key: %v", err)
}
publicKey := &privateKey.PublicKey
// Create JWT verification config
jwtConfig := &JWTVerificationConfig{
PublicKey: publicKey,
ExpectedIssuer: "https://test.auth0.com/",
ExpectedAudience: "https://test.auth0.com/api/v2/",
}
// Create Auth0 config
config := Config{
Domain: "test.auth0.com",
JWTVerificationConfig: jwtConfig,
}
// Create user reader writer
httpConfig := httpclient.Config{}
userRW := &userReaderWriter{
config: config,
httpClient: httpclient.NewClient(httpConfig),
errorResponse: NewErrorResponse(),
}
tests := []struct {
name string
token string
expectError bool
}{
{
name: "valid JWT for metadata lookup",
token: createValidMetadataJWT(t, privateKey),
expectError: false,
},
{
name: "invalid signature for metadata lookup",
token: createInvalidSignatureJWT(t),
expectError: true,
},
{
name: "missing read scope for metadata lookup",
token: createMissingReadScopeJWT(t, privateKey),
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
user, err := userRW.MetadataLookup(ctx, tt.token, "read:current_user")
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if user == nil {
t.Error("Expected user but got nil")
} else if user.UserID != "test-user-123" {
t.Errorf("Expected user ID 'test-user-123', got '%s'", user.UserID)
}
}
})
}
}
func createValidJWT(t *testing.T, privateKey *rsa.PrivateKey) string {
now := time.Now()
claims := jwt.MapClaims{
"sub": "test-user-123",
"iss": "https://test.auth0.com/",
"aud": "https://test.auth0.com/api/v2/",
"exp": now.Add(time.Hour).Unix(),
"iat": now.Unix(),
"scope": "read:current_user update:current_user_metadata",
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(privateKey)
if err != nil {
t.Fatalf("Failed to sign token: %v", err)
}
return tokenString
}
func createValidMetadataJWT(t *testing.T, privateKey *rsa.PrivateKey) string {
now := time.Now()
claims := jwt.MapClaims{
"sub": "test-user-123",
"iss": "https://test.auth0.com/",
"aud": "https://test.auth0.com/api/v2/",
"exp": now.Add(time.Hour).Unix(),
"iat": now.Unix(),
"scope": "read:current_user",
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(privateKey)
if err != nil {
t.Fatalf("Failed to sign token: %v", err)
}
return tokenString
}
func createInvalidSignatureJWT(t *testing.T) string {
// Create a token with a different key (invalid signature)
wrongKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("Failed to generate wrong key: %v", err)
}
now := time.Now()
claims := jwt.MapClaims{
"sub": "test-user-123",
"iss": "https://test.auth0.com/",
"aud": "https://test.auth0.com/api/v2/",
"exp": now.Add(time.Hour).Unix(),
"iat": now.Unix(),
"scope": "read:current_user update:current_user_metadata",
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(wrongKey)
if err != nil {
t.Fatalf("Failed to sign token with wrong key: %v", err)
}
return tokenString
}
func createExpiredJWT(t *testing.T, privateKey *rsa.PrivateKey) string {
now := time.Now()
claims := jwt.MapClaims{
"sub": "test-user-123",
"iss": "https://test.auth0.com/",
"aud": "https://test.auth0.com/api/v2/",
"exp": now.Add(-time.Hour).Unix(), // Expired 1 hour ago
"iat": now.Add(-2 * time.Hour).Unix(),
"scope": "read:current_user update:current_user_metadata",
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(privateKey)
if err != nil {
t.Fatalf("Failed to sign expired token: %v", err)
}
return tokenString
}
func createWrongIssuerJWT(t *testing.T, privateKey *rsa.PrivateKey) string {
now := time.Now()
claims := jwt.MapClaims{
"sub": "test-user-123",
"iss": "https://wrong.auth0.com/", // Wrong issuer
"aud": "https://test.auth0.com/api/v2/",
"exp": now.Add(time.Hour).Unix(),
"iat": now.Unix(),
"scope": "read:current_user update:current_user_metadata",
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(privateKey)
if err != nil {
t.Fatalf("Failed to sign token with wrong issuer: %v", err)
}
return tokenString
}
func createWrongAudienceJWT(t *testing.T, privateKey *rsa.PrivateKey) string {
now := time.Now()
claims := jwt.MapClaims{
"sub": "test-user-123",
"iss": "https://test.auth0.com/",
"aud": "https://wrong.auth0.com/api/v2/", // Wrong audience
"exp": now.Add(time.Hour).Unix(),
"iat": now.Unix(),
"scope": "read:current_user update:current_user_metadata",
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(privateKey)
if err != nil {
t.Fatalf("Failed to sign token with wrong audience: %v", err)
}
return tokenString
}
func createMissingScopeJWT(t *testing.T, privateKey *rsa.PrivateKey) string {
now := time.Now()
claims := jwt.MapClaims{
"sub": "test-user-123",
"iss": "https://test.auth0.com/",
"aud": "https://test.auth0.com/api/v2/",
"exp": now.Add(time.Hour).Unix(),
"iat": now.Unix(),
"scope": "read:current_user", // Missing update:current_user_metadata scope
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(privateKey)
if err != nil {
t.Fatalf("Failed to sign token with missing scope: %v", err)
}
return tokenString
}
func createMissingReadScopeJWT(t *testing.T, privateKey *rsa.PrivateKey) string {
now := time.Now()
claims := jwt.MapClaims{
"sub": "test-user-123",
"iss": "https://test.auth0.com/",
"aud": "https://test.auth0.com/api/v2/",
"exp": now.Add(time.Hour).Unix(),
"iat": now.Unix(),
"scope": "update:current_user_metadata", // Missing read:current_user scope
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(privateKey)
if err != nil {
t.Fatalf("Failed to sign token with missing read scope: %v", err)
}
return tokenString
}
func TestMetadataLookupWithoutJWTVerificationConfig(t *testing.T) {
// Create Auth0 config without JWT verification config
config := Config{
Domain: "test.auth0.com",
// JWTVerificationConfig is nil
}
// Create user reader writer
httpConfig := httpclient.Config{}
userRW := &userReaderWriter{
config: config,
httpClient: httpclient.NewClient(httpConfig),
errorResponse: NewErrorResponse(),
}
tests := []struct {
name string
token string
expectError bool
}{
{
name: "missing JWT verification config for metadata lookup",
token: "any-token",
expectError: false, // Now handled as username lookup with M2M token
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
user, err := userRW.MetadataLookup(ctx, tt.token)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if user == nil {
t.Error("Expected user but got nil")
}
}
})
}
}