Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor JWT Class into JWTDecoder and JWTSigner, Improve Type Safety and Test Coverage #4

Merged
merged 7 commits into from
Jan 6, 2025

Conversation

mosquito
Copy link
Owner

@mosquito mosquito commented Jan 6, 2025

This pull request introduces significant architectural improvements to the JWT implementation, enhancing clarity, separation of concerns, and test robustness. The monolithic JWT class is split into two distinct classes: JWTDecoder and JWTSigner, each tailored for their specific responsibilities. Additionally, tests have been streamlined, parameterized, and modernized for better coverage and maintainability.


Key Changes:

1. Refactor JWT Class

  • Split into JWTDecoder and JWTSigner:
    • JWTDecoder: Handles JWT token decoding.
    • JWTSigner: Inherits from JWTDecoder, adds encoding functionality using a private key.
  • Use @dataclass for Immutability and Cleaner Initialization:
    • Leverages dataclasses.field to manage internal fields securely.
  • Improved Method Clarity:
    • Removed _date_to_timestamp method duplication.
    • Refactored logic into a reusable date_to_timestamp function.
  • Added Overloaded Factory Method (JWT)
    • Dynamically returns either JWTDecoder or JWTSigner based on the provided key type.

2. Test Suite Enhancements:

  • Removed Unnecessary Mocks:
    • Direct argument passing replaces mock.patch usage.
  • Parameterized Tests:
    • test_rsa_verify and test_rsa_verify_bad_key now use pytest.mark.parametrize for better coverage across different formats (jwk, pem, base64).
  • Improved Exception Assertions:
    • More precise exception types are asserted (AttributeError instead of RuntimeError, TypeError instead of ValueError).

3. Minor Fixes and Improvements:

  • Cleaned up imports.
  • Removed deprecated or redundant code blocks.
  • Improved error messages for invalid key types in the factory function.

Backward Compatibility:

  • This refactor introduces breaking changes for users who relied on the previous JWT class structure.
  • However, the new interface is cleaner, type-safe, and more explicit.

Testing:

  • All tests have been updated to match the new architecture.
  • New edge cases have been added, particularly in key validation scenarios.
  • CI Pipeline passes successfully.

@mosquito mosquito merged commit 5ecd789 into master Jan 6, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant