Class: AuthlogicMotp::CheckMOTP

Inherits:
Object
  • Object
show all
Defined in:
lib/authlogic_motp/session.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCheckMOTP

Returns a new instance of CheckMOTP.



117
118
119
# File 'lib/authlogic_motp/session.rb', line 117

def initialize
  @tmp_md5 = ''
end

Class Method Details

.generate_otp(time, secret, pin) ⇒ Object



134
135
136
# File 'lib/authlogic_motp/session.rb', line 134

def self.generate_otp(time,secret,pin)
  Digest::MD5.hexdigest(time.to_s.chop << secret << pin.to_s)[0,6]
end

.validate(secret, pin, otp, period = 3) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/authlogic_motp/session.rb', line 121

def self.validate(secret,pin,otp,period=3)
  maxperiod = period * 60 # in seconds
  time = Time.now.utc.to_i
  ((time - maxperiod)..(time + maxperiod)).each do |n|
    md5 = generate_otp(n,secret,pin)
    next if md5 == @tmp_md5
    @tmp_md5 = md5
    
    return true if md5.downcase == otp.chomp.downcase
  end
  false
end