Method: ROTP::TOTP#verify

Defined in:
lib/rotp/totp.rb

#verify(otp, drift_ahead: 0, drift_behind: 0, after: nil, at: Time.now) ⇒ Integer?

Verifies the OTP passed in against the current time OTP and adjacent intervals up to drift. Excludes OTPs from after and earlier. Returns time value of matching OTP code for use in subsequent call.

Parameters:

  • otp (String)

    the one time password to verify

  • drift_behind (Integer) (defaults to: 0)

    how many seconds to look back

  • drift_ahead (Integer) (defaults to: 0)

    how many seconds to look ahead

  • after (Integer) (defaults to: nil)

    prevent token reuse, last login timestamp

  • at (Time) (defaults to: Time.now)

    time at which to generate and verify a particular otp. default Time.now

Returns:

  • (Integer, nil)

    the last successful timestamp interval



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rotp/totp.rb', line 39

def verify(otp, drift_ahead: 0, drift_behind: 0, after: nil, at: Time.now)
  timecodes = get_timecodes(at, drift_behind, drift_ahead)

  timecodes = timecodes.select { |t| t > timecode(after) } if after

  result = nil
  timecodes.each do |t|
    result = t * interval if super(otp, generate_otp(t))
  end
  result
end