Fix jti consistency for token creation #1255
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🐛 Bug-fix PR
Closes #1254
📌 Summary
This PR fixes a bug where the jti (JWT ID) value in the generated JWT token does not match the jti stored in the
email_api_tokensdatabase table. This mismatch will cause issues with token validation, revocation, and audit logging.The fix ensures that a single jti is generated and reused both for token creation and database insertion, maintaining consistency and referential integrity.
🔁 Reproduction Steps
🐞 Root Cause
The jti - JWT ID was being generated inside the
_generate_tokenfunction and included in the token payload, but this same ID was not used when inserting the token record into the database. Instead, the database assigned a separate, random jti value to the record. As a result, the token and its corresponding database entry ended up with different jti values, breaking token validation and tracking mechanisms.To fix this, the jti must be generated before both token creation and database insertion, ensuring the same ID is consistently used in both places.
💡 Fix Description
Refactored the token creation logic to generate the jti before both token generation and database insertion.
The same jti is now explicitly passed to both the _generate_token function and the database record creation, ensuring consistency.
🧪 Verification
make lintmake testmake coverage📐 MCP Compliance (if relevant)
✅ Checklist
make black isort pre-commit)