refactor otp

This commit is contained in:
Pete Matsyburka committed 2026-10-01 08:57:19 +03:00
1 parent a847e7aa9c
commit e187644716
9 files changed
+121 -37

No files matched your search

@@ -10,12 +10,10 @@ class SubmitFormEmail2fasController < ApplicationController
def create
RateLimit.call("verify-2fa-code-#{@submitter.id}", limit: 2, ttl: 45.seconds, enabled: true)
RateLimit.call("verify-2fa-code-1h-#{@submitter.id}", limit: 10, ttl: 1.hour, enabled: true)
RateLimit.call("verify-2fa-code-1d-#{@submitter.id}", limit: 20, ttl: 1.day, enabled: true)
value = [@submitter.email.downcase.strip, @submitter.slug].join(':')
value = [@submitter.email.downcase.squish, @submitter.slug].join(':')
if EmailVerificationCodes.verify(params[:one_time_code].to_s.gsub(/\D/, ''), value)
if OtpChallenges.verify(params[:one_time_code].to_s.gsub(/\D/, ''), value, purpose: 'form_email_2fa')
SubmissionEvents.create_with_tracking_data(@submitter, 'email_verified', request, { email: @submitter.email })
cookies.encrypted[:email_2fa_slug] =
+2 -1
View File
@@ -181,7 +181,8 @@ class SubmitterMailer < ApplicationMailer
def otp_verification_email(submitter, locale: nil)
@current_account = submitter.account
@submitter = submitter
@otp_code = EmailVerificationCodes.generate([submitter.email.downcase.strip, submitter.slug].join(':'))
@otp_code = OtpChallenges.generate([submitter.email.downcase.squish, submitter.slug].join(':'),
purpose: 'form_email_2fa')
assign_message_metadata('otp_verification_email', submitter)
+1 -1
View File
@@ -5,7 +5,7 @@ class TemplateMailer < ApplicationMailer
@current_account = template.account
@template = template
@otp_code = EmailVerificationCodes.generate([email.downcase.strip, template.slug].join(':'))
@otp_code = OtpChallenges.generate([email.downcase.squish, template.slug].join(':'), purpose: 'form_email_2fa')
assign_message_metadata('otp_verification_email', template)
+21
View File
@@ -0,0 +1,21 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: otp_challenges
#
# id :bigint not null, primary key
# attempts :integer default(0), not null
# expire_at :datetime not null
# key :string not null
# nonce :string not null
# verified_at :datetime
# version :integer not null
# created_at :datetime not null
#
# Indexes
#
# index_otp_challenges_on_key_and_version (key,version) UNIQUE
#
class OtpChallenge < ApplicationRecord
end