module RailsOtp

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/rails_otp.rb, line 19
def self.configure
  yield(configuration)
end
reset() click to toggle source
# File lib/rails_otp.rb, line 15
def self.reset
  @configuration = Configuration.new
end

Public Instance Methods

generate_otp() click to toggle source
# File lib/rails_otp/otp.rb, line 4
def generate_otp
  self.verified_status = false
  #TODO: add expiry time to config
  self.otp_expiry_time = RailsOtp::configuration.otp_expiry_time #code is valid only for 5 minutes
  self.otp_code = SecureRandom.hex(4)
end
generate_otp!() click to toggle source
# File lib/rails_otp/otp.rb, line 11
def generate_otp!
  generate_otp
  save
end
regenerate_otp() click to toggle source
# File lib/rails_otp/otp.rb, line 20
def regenerate_otp
  generate_otp
end
regenerate_otp!() click to toggle source
# File lib/rails_otp/otp.rb, line 24
def regenerate_otp!
  regenerate_otp
  save
end
verify_otp(otp) click to toggle source
# File lib/rails_otp/otp.rb, line 16
def verify_otp(otp)
  otp_code_valid?(otp) && code_not_expired
end

Private Instance Methods

code_not_expired() click to toggle source
# File lib/rails_otp/otp.rb, line 51
def code_not_expired
  if !expired?
    true
  else
    errors.add(:base, "this otp code has been expired")
    false
  end
end
expired?() click to toggle source

function to check whether otp is expired or not

# File lib/rails_otp/otp.rb, line 37
def expired?
  self.otp_expiry_time < Time.now
end
matches?(otp) click to toggle source

check whether both generated and entered otp match or not

# File lib/rails_otp/otp.rb, line 32
def matches?(otp)
  otp == self.otp_code 
end
otp_code_valid?(otp) click to toggle source

custom messages for invalid code and code expiry time.

# File lib/rails_otp/otp.rb, line 42
def otp_code_valid?(otp)
  if matches?(otp)
    true
  else
    errors.add(:base, "you have entered a invalid code")
    false
  end
end