class ROTP::TOTP

def at(time)

Parameters:
  • time (Time/Integer) -- the time to generate an OTP for, integer unix timestamp or Time object
def at(time)
  generate_otp(timecode(time))
end

def get_timecodes(at, drift_behind, drift_ahead)

Get back an array of timecodes for a period
def get_timecodes(at, drift_behind, drift_ahead)
  now = timeint(at)
  timecode_start = timecode(now - drift_behind)
  timecode_end = timecode(now + drift_ahead)
  (timecode_start..timecode_end).step(1).to_a
end

def initialize(s, options = {})

Options Hash: (**options)
  • interval (Integer) -- the time interval in seconds for OTP
def initialize(s, options = {})
  @interval = options[:interval] || DEFAULT_INTERVAL
  @issuer = options[:issuer]
  super
end

def now

Returns:
  • (Integer) - the OTP as an integer
def now
  generate_otp(timecode(Time.now))
end

def provisioning_uri(name = nil)

Returns:
  • (String) - provisioning URI

Parameters:
  • name (String) -- of the account
def provisioning_uri(name = nil)
  OTP::URI.new(self, account_name: name || @name).to_s
end

def timecode(time)

def timecode(time)
  timeint(time) / interval
end

def timeint(time)

Ensure UTC int
def timeint(time)
  return time.to_i unless time.class == Time
  time.utc.to_i
end

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

Returns:
  • (Integer, nil) - the last successful timestamp

Parameters:
  • at (Time) -- time at which to generate and verify a particular
  • after (Integer) -- prevent token reuse, last login timestamp
  • drift_ahead (Integer) -- how many seconds to look ahead
  • drift_behind (Integer) -- how many seconds to look back
  • otp (String) -- the one time password to verify
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