mirror of https://github.com/docusealco/docuseal
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
520 B
28 lines
520 B
# frozen_string_literal: true
|
|
|
|
module EmailVerificationCodes
|
|
DRIFT_BEHIND = 5.minutes
|
|
|
|
module_function
|
|
|
|
def generate(value)
|
|
totp = ROTP::TOTP.new(build_totp_secret(value))
|
|
|
|
totp.at(Time.current)
|
|
end
|
|
|
|
def verify(code, value)
|
|
totp = ROTP::TOTP.new(build_totp_secret(value))
|
|
|
|
totp.verify(code, drift_behind: DRIFT_BEHIND)
|
|
end
|
|
|
|
def build_totp_secret(value)
|
|
ROTP::Base32.encode(
|
|
Digest::SHA1.digest(
|
|
[Rails.application.secret_key_base, value].join(':')
|
|
)
|
|
)
|
|
end
|
|
end
|