Repository navigation
feat(verification): hashed one-time codes with attempt counting - #289
Merged
Merged
Conversation
Add VerificationCode::issue(), check() and attemptsLeft() for flows where a leaked table must not give away live codes: - issue() stores an HMAC of the code, keyed by the app key, and hands the plain code back once on the instance (plainCode), defaulting to a 10-minute expiry and an 'active' status. - check() compares with hash_equals, counts wrong attempts in meta and locks the code on the last allowed one. Expired and locked codes report as such. - The creating hook keeps a code that was already set, so issue() is not overwritten; codes made the old way still get a random one and still check. Existing generators and their callers are unchanged. First user: the FleetOps public tracking page's one-time codes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #289 +/- ##
===========================================
Coverage 100.00% 100.00%
- Complexity 7931 7943 +12
===========================================
Files 438 438
Lines 25665 25698 +33
===========================================
+ Hits 25665 25698 +33
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR 1 of the customer tracking pages refactor. It adds hashed one-time codes to
VerificationCode, for flows where a leakedverification_codestable must not give away live codes.Why
VerificationCodestores codes in plain text and counts no attempts. The new public tracking page sends recipients a 6-digit code to unlock delivery details, so it needs codes that are stored hashed and can't be guessed indefinitely.Changes
VerificationCode::issue($subject, $for, $options):hash_hmac('sha256', code, app.key), the same keyingTwoFactorAuthuses.plainCodeproperty. It is never persisted.activestatus.metais merged, andhashedandattemptsare recorded.check($plainCode, $maxAttempts = 3): returnsvalid,invalid,expiredorlocked. It compares withhash_equals, counts wrong attempts in meta, and locks the code on the last allowed attempt. It also checks plain codes made the old way.attemptsLeft()andhashCode().creatinghook now keeps a code that was already set, soissue()isn't overwritten. Codes made the old way still get a random one.Existing generators (
generateFor,generateEmailVerificationFor,generateSmsVerificationFor) and their callers don't change.Tests
Three cases added to
VerificationCodeModelTest: