Class: ROTP::TOTP
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
Attributes inherited from OTP
Instance Method Summary collapse
-
#at(time) ⇒ Object
Accepts either a Unix timestamp integer or a Time object.
-
#initialize(s, options = {}) ⇒ TOTP
constructor
A new instance of TOTP.
-
#now ⇒ Integer
Generate the current time OTP.
-
#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.
-
#verify(otp, time = Time.now) ⇒ Object
Verifies the OTP passed in against the current time OTP.
-
#verify_with_drift(otp, drift, time = Time.now) ⇒ Object
Verifies the OTP passed in against the current time OTP and adjacent intervals up to
drift
.
Methods inherited from OTP
Constructor Details
#initialize(s, options = {}) ⇒ TOTP
Returns a new instance of TOTP.
8 9 10 11 |
# File 'lib/rotp/totp.rb', line 8 def initialize(s, = {}) @interval = [:interval] || 30 super end |
Instance Attribute Details
#interval ⇒ Object (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
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 |
#now ⇒ Integer
Generate the current time OTP
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
52 53 54 |
# File 'lib/rotp/totp.rb', line 52 def provisioning_uri(name) "otpauth://totp/#{URI.encode(name)}?secret=#{secret}" end |
#verify(otp, time = Time.now) ⇒ Object
Verifies the OTP passed in against the current time OTP
31 32 33 |
# File 'lib/rotp/totp.rb', line 31 def verify(otp, time = Time.now) super(otp, self.at(time)) end |
#verify_with_drift(otp, drift, time = Time.now) ⇒ Object
Verifies the OTP passed in against the current time OTP and adjacent intervals up to drift
.
40 41 42 43 44 45 |
# File 'lib/rotp/totp.rb', line 40 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 |