Skip to content

Commit a9edb4e

Browse files
maxdinechclaude
andcommitted
refactor(otp): remove verbose logging and unused function
Clean up OTP protection code for production: - Remove verbose Info-level logging from verification flow - Keep only Warning for max attempts reached and Error logs - Remove unused clearOTPAttempts() function - Keep code lean and production-ready 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9161761 commit a9edb4e

File tree

1 file changed

+7
-46
lines changed

1 file changed

+7
-46
lines changed

internal/api/verify.go

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -731,17 +731,8 @@ func (a *API) verifyUserAndToken(conn *storage.Connection, params *VerifyParams,
731731

732732
// OTP Protection: Check if token is invalidated before attempting verification
733733
tokenType := getTokenTypeForVerification(params.Type)
734-
logrus.WithFields(logrus.Fields{
735-
"params_type": params.Type,
736-
"token_type": tokenType,
737-
"user_id": user.ID.String(),
738-
}).Info("OTP Protection: Getting token type for verification")
739734
if tokenType != "" {
740735
invalidated, err := checkOTPTokenInvalidated(conn, user.ID.String(), tokenType)
741-
logrus.WithFields(logrus.Fields{
742-
"invalidated": invalidated,
743-
"error": err,
744-
}).Info("OTP Protection: Checked if token is invalidated")
745736
if err == nil && invalidated {
746737
return nil, apierrors.NewForbiddenError(apierrors.ErrorCodeOTPExpired, "Token has been invalidated due to too many failed attempts. Please request a new verification code.")
747738
}
@@ -797,17 +788,9 @@ func (a *API) verifyUserAndToken(conn *storage.Connection, params *VerifyParams,
797788
// OTP Protection: Record attempt in a separate autonomous transaction
798789
// This ensures the attempt count persists even if the main transaction rolls back
799790
if tokenType != "" {
800-
logrus.WithFields(logrus.Fields{
801-
"user_id": user.ID.String(),
802-
"token_type": tokenType,
803-
"is_valid": isValid,
804-
}).Info("OTP Protection: Recording OTP attempt")
805-
// Use a new connection to create an autonomous transaction
806791
if err := recordOTPAttemptAutonomous(config, user.ID.String(), tokenType, isValid); err != nil {
807792
// Log error but don't fail the request
808793
logrus.WithError(err).Warn("Failed to record OTP attempt")
809-
} else {
810-
logrus.Info("OTP Protection: Successfully recorded OTP attempt")
811794
}
812795
}
813796

@@ -911,30 +894,20 @@ func recordOTPAttemptAutonomous(config *conf.GlobalConfiguration, userID string,
911894

912895
// recordOTPAttempt records a failed OTP verification attempt and invalidates token after max failures
913896
func recordOTPAttempt(conn *storage.Connection, userID string, tokenType string, isValid bool) error {
914-
logrus.WithFields(logrus.Fields{
915-
"user_id": userID,
916-
"token_type": tokenType,
917-
"is_valid": isValid,
918-
}).Info("recordOTPAttempt called")
919-
920897
// If token is valid, reset attempts
921898
if isValid {
922-
logrus.Info("Token valid - resetting attempt count")
923899
err := conn.RawQuery(`
924900
UPDATE auth.one_time_tokens
925901
SET attempt_count = 0, invalidated_at = NULL
926902
WHERE user_id = $1 AND token_type = $2::auth.one_time_token_type
927903
`, userID, tokenType).Exec()
928904
if err != nil {
929-
logrus.WithError(err).Error("Failed to reset attempt count")
930-
} else {
931-
logrus.Info("Successfully reset attempt count")
905+
logrus.WithError(err).Error("Failed to reset OTP attempt count")
932906
}
933907
return err
934908
}
935909

936910
// Token is invalid - increment attempt count
937-
logrus.Info("Token invalid - incrementing attempt count")
938911
var attemptCount int
939912
err := conn.RawQuery(`
940913
UPDATE auth.one_time_tokens
@@ -944,38 +917,26 @@ func recordOTPAttempt(conn *storage.Connection, userID string, tokenType string,
944917
`, userID, tokenType).First(&attemptCount)
945918

946919
if err != nil {
947-
logrus.WithError(err).Error("Failed to increment attempt count")
920+
logrus.WithError(err).Error("Failed to increment OTP attempt count")
948921
return err
949922
}
950923

951-
logrus.WithField("attempt_count", attemptCount).Info("Incremented attempt count")
952-
953924
// If max attempts reached, invalidate the token
954925
if attemptCount >= maxOTPVerificationAttempts {
955-
logrus.Warn("Max attempts reached - invalidating token")
926+
logrus.WithFields(logrus.Fields{
927+
"user_id": userID,
928+
"token_type": tokenType,
929+
}).Warn("OTP max attempts reached - token invalidated")
956930
err = conn.RawQuery(`
957931
UPDATE auth.one_time_tokens
958932
SET invalidated_at = NOW()
959933
WHERE user_id = $1 AND token_type = $2::auth.one_time_token_type
960934
`, userID, tokenType).Exec()
961935
if err != nil {
962-
logrus.WithError(err).Error("Failed to invalidate token")
963-
} else {
964-
logrus.Info("Successfully invalidated token")
936+
logrus.WithError(err).Error("Failed to invalidate OTP token")
965937
}
966938
return err
967939
}
968940

969-
logrus.Info("Attempt recorded successfully")
970941
return nil
971942
}
972-
973-
// clearOTPAttempts resets attempt tracking when a new OTP is generated
974-
func clearOTPAttempts(conn *storage.Connection, userID string, tokenType string) error {
975-
err := conn.RawQuery(`
976-
UPDATE auth.one_time_tokens
977-
SET attempt_count = 0, invalidated_at = NULL
978-
WHERE user_id = $1 AND token_type = $2::auth.one_time_token_type
979-
`, userID, tokenType).Exec()
980-
return err
981-
}

0 commit comments

Comments
 (0)