Class: ROTP::TOTP

Inherits:
OTP
  • Object
show all
Defined in:
lib/rotp/totp.rb

Instance Attribute Summary collapse

Attributes inherited from OTP

#digest, #digits, #secret

Instance Method Summary collapse

Methods inherited from OTP

#generate_otp

Constructor Details

#initialize(s, options = {}) ⇒ TOTP

Returns a new instance of TOTP.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • interval (Integer) — default: 30

    the time interval in seconds for OTP This defaults to 30 which is standard.



8
9
10
11
# File 'lib/rotp/totp.rb', line 8

def initialize(s, options = {})
  @interval = options[:interval] || 30
  super
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



4
5
6
# File 'lib/rotp/totp.rb', line 4

def interval
  @interval
end

Instance Method Details

#at(time) ⇒ Object

Accepts either a Unix timestamp integer or a Time object. Time objects will be adjusted to UTC automatically

Parameters:

  • time (Time/Integer)

    the time to generate an OTP for



16
17
18
19
20
21
# File 'lib/rotp/totp.rb', line 16

def at(time)
  unless time.class == Time
    time = Time.at(time.to_i)
  end
  generate_otp(timecode(time))
end

#nowInteger

Generate the current time OTP

Returns:

  • (Integer)

    the OTP as an integer



25
26
27
# File 'lib/rotp/totp.rb', line 25

def now
  generate_otp(timecode(Time.now))
end

#provisioning_uri(name) ⇒ String

Returns the provisioning URI for the OTP This can then be encoded in a QR Code and used to provision the Google Authenticator app

Parameters:

  • name (String)

    of the account

Returns:

  • (String)

    provisioning uri



34
35
36
# File 'lib/rotp/totp.rb', line 34

def provisioning_uri(name)
  "otpauth://totp/#{URI.encode(name)}?secret=#{secret}"
end