class ROTP::TOTP

Attributes

interval[R]
issuer[R]

Public Class Methods

new(s, options = {}) click to toggle source

@option options [Integer] interval (30) the time interval in seconds for OTP

This defaults to 30 which is standard.
Calls superclass method ROTP::OTP.new
# File lib/rotp/totp.rb, line 9
def initialize(s, options = {})
  @interval = options[:interval] || DEFAULT_INTERVAL
  @issuer = options[:issuer]
  super
end

Public Instance Methods

at(time, padding=true) click to toggle source

Accepts either a Unix timestamp integer or a Time object. Time objects will be adjusted to UTC automatically @param [Time/Integer] time the time to generate an OTP for @option [Boolean] padding (true) Issue the number as a 0 padded string

# File lib/rotp/totp.rb, line 19
def at(time, padding=true)
  unless time.class == Time
    time = Time.at(time.to_i)
  end
  generate_otp(timecode(time), padding)
end
now(padding=true) click to toggle source

Generate the current time OTP @return [Integer] the OTP as an integer

# File lib/rotp/totp.rb, line 28
def now(padding=true)
  generate_otp(timecode(Time.now), padding)
end
provisioning_uri(name) click to toggle source

Returns the provisioning URI for the OTP This can then be encoded in a QR Code and used to provision the Google Authenticator app @param [String] name of the account @return [String] provisioning uri

# File lib/rotp/totp.rb, line 55
def provisioning_uri(name)
  encode_params("otpauth://totp/#{URI.encode(name)}",
                :secret => secret, :period => (interval==30 ? nil : interval), :issuer => issuer)
end
verify(otp, time = Time.now) click to toggle source

Verifies the OTP passed in against the current time OTP @param [String/Integer] otp the OTP to check against

Calls superclass method ROTP::OTP#verify
# File lib/rotp/totp.rb, line 34
def verify(otp, time = Time.now)
  super(otp, self.at(time))
end
verify_with_drift(otp, drift, time = Time.now) click to toggle source

Verifies the OTP passed in against the current time OTP and adjacent intervals up to drift. @param [String] otp the OTP to check against @param [Integer] drift the number of seconds that the client

and server are allowed to drift apart
# File lib/rotp/totp.rb, line 43
def verify_with_drift(otp, drift, time = Time.now)
  time = time.to_i
  times = (time-drift..time+drift).step(interval).to_a
  times << time + drift if times.last < time + drift
  times.any? { |ti| verify(otp, ti) }
end

Private Instance Methods

timecode(time) click to toggle source
# File lib/rotp/totp.rb, line 62
def timecode(time)
  time.utc.to_i / interval
end