|
| 1 | +package authorization |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + fuzz "github.com/AdaLogics/go-fuzz-headers" |
| 10 | + "github.com/golang-jwt/jwt" |
| 11 | + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" |
| 12 | +) |
| 13 | + |
| 14 | +// generateExpiredFakeJWTToken generates a fake JWT token with expiration time set to the past |
| 15 | +func generateExpiredFakeJWTToken(username string) string { |
| 16 | + token := jwt.New(jwt.SigningMethodHS256) |
| 17 | + claims := token.Claims.(jwt.MapClaims) |
| 18 | + claims["username"] = username |
| 19 | + claims["exp"] = time.Now().Add(-time.Hour).Unix() // Set expiration time to 1 hour ago |
| 20 | + signedToken, _ := token.SignedString([]byte("your-secret-key")) // Sign the token with a secret key |
| 21 | + return signedToken |
| 22 | +} |
| 23 | + |
| 24 | +// generateFakeJWTTokenWithInvalidSignature generates a fake JWT token with an invalid signature |
| 25 | +func generateFakeJWTTokenWithInvalidSignature(username string) string { |
| 26 | + token := jwt.New(jwt.SigningMethodHS256) |
| 27 | + claims := token.Claims.(jwt.MapClaims) |
| 28 | + claims["username"] = username |
| 29 | + claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Set expiration time to 24 hours from now |
| 30 | + signedToken, _ := token.SignedString([]byte("invalid-secret-key")) // Sign the token with an invalid secret key |
| 31 | + return signedToken |
| 32 | +} |
| 33 | + |
| 34 | +// generateFakeJWTToken generates a fake JWT token with predefined claims |
| 35 | +func generateFakeJWTToken(username string) string { |
| 36 | + token := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{ |
| 37 | + "username": username, |
| 38 | + "exp": time.Now().Add(time.Hour * 24).Unix(), // Set expiration time to 24 hours from now |
| 39 | + }) |
| 40 | + |
| 41 | + signedToken, _ := token.SignedString([]byte(utils.Config.JwtSecret)) // No signature is needed for testing |
| 42 | + return signedToken |
| 43 | +} |
| 44 | + |
| 45 | +func FuzzGetUsername(f *testing.F) { |
| 46 | + f.Fuzz(func(t *testing.T, input string) { |
| 47 | + // Create a fake JWT token with predefined claims |
| 48 | + |
| 49 | + // Invalid token format check |
| 50 | + _, err := GetUsername(input) |
| 51 | + if err == nil { |
| 52 | + t.Error("Expected error for invalid token format") |
| 53 | + } |
| 54 | + |
| 55 | + // Generating fake jwt token for testing |
| 56 | + token := generateFakeJWTToken(base64.StdEncoding.EncodeToString([]byte(input))) |
| 57 | + |
| 58 | + // Run the test with the fake JWT token |
| 59 | + username, err := GetUsername(token) |
| 60 | + if err != nil { |
| 61 | + t.Errorf("Error encountered: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + // Decode the username back from base64 |
| 65 | + decodedUsername, err := base64.StdEncoding.DecodeString(username) |
| 66 | + if err != nil { |
| 67 | + t.Errorf("Error decoding username: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Check if the decoded username matches the input string |
| 71 | + if string(decodedUsername) != input { |
| 72 | + t.Errorf("Expected username: %s, got: %s", input, username) |
| 73 | + } |
| 74 | + |
| 75 | + // Additional checks |
| 76 | + // Expiration check |
| 77 | + expiredToken := generateExpiredFakeJWTToken(input) |
| 78 | + _, err = GetUsername(expiredToken) |
| 79 | + if err == nil { |
| 80 | + t.Error("Expected error for expired token") |
| 81 | + } |
| 82 | + |
| 83 | + // Token signature check (invalid secret key) |
| 84 | + invalidSignatureToken := generateFakeJWTTokenWithInvalidSignature(input) |
| 85 | + _, err = GetUsername(invalidSignatureToken) |
| 86 | + if err == nil { |
| 87 | + t.Error("Expected error for token with invalid signature") |
| 88 | + } |
| 89 | + |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +// generateJWTToken generates a JWT token with the given claims |
| 94 | +func generateJWTTokenFromClaims(claims jwt.MapClaims) (string, error) { |
| 95 | + // Set expiration time to 24 hours from now |
| 96 | + claims["exp"] = time.Now().Add(time.Hour * 24).Unix() |
| 97 | + |
| 98 | + // Create a new token with the claims |
| 99 | + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
| 100 | + |
| 101 | + // Sign the token with a secret key |
| 102 | + tokenString, err := token.SignedString([]byte(utils.Config.JwtSecret)) |
| 103 | + if err != nil { |
| 104 | + return "", fmt.Errorf("failed to sign JWT token: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + return tokenString, nil |
| 108 | +} |
| 109 | + |
| 110 | +func FuzzUserValidateJWT(f *testing.F) { |
| 111 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 112 | + fuzzConsumer := fuzz.NewConsumer(data) |
| 113 | + inputClaims := &jwt.MapClaims{} |
| 114 | + err := fuzzConsumer.GenerateStruct(inputClaims) |
| 115 | + if err != nil { |
| 116 | + return |
| 117 | + } |
| 118 | + // Generate a JWT token with fuzzed claims |
| 119 | + tokenString, err := generateJWTTokenFromClaims(*inputClaims) |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("Error generating JWT token: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + // Run the test with the generated JWT token |
| 125 | + claims, err := UserValidateJWT(tokenString) |
| 126 | + if err != nil { |
| 127 | + t.Errorf("Error encountered: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + // Optionally, check if claims are nil when there's an error |
| 131 | + if claims == nil && err == nil { |
| 132 | + t.Errorf("Claims are nil while no error is returned") |
| 133 | + } |
| 134 | + |
| 135 | + }) |
| 136 | +} |
0 commit comments