class ROTP::TOTP
def at(time)
-
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)
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)
-
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
-
(Integer)
- the OTP as an integer
def now generate_otp(timecode(Time.now)) end
def provisioning_uri(name = nil)
-
(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)
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)
-
(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