Class: ROTP::TOTP
Constant Summary
Constants inherited from OTP
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#issuer ⇒ Object
readonly
Returns the value of attribute issuer.
Attributes inherited from OTP
#digest, #digits, #name, #provisioning_params, #secret
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 = nil) ⇒ 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, drift_ahead: 0, drift_behind: 0, after: nil, at: Time.now) ⇒ Integer?
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 12 |
# File 'lib/rotp/totp.rb', line 8 def initialize(s, = {}) @interval = [:interval] || DEFAULT_INTERVAL @issuer = [:issuer] 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 |
#issuer ⇒ Object (readonly)
Returns the value of attribute issuer.
4 5 6 |
# File 'lib/rotp/totp.rb', line 4 def issuer @issuer 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
17 18 19 |
# File 'lib/rotp/totp.rb', line 17 def at(time) generate_otp(timecode(time)) end |
#now ⇒ Integer
Generate the current time OTP
23 24 25 |
# File 'lib/rotp/totp.rb', line 23 def now generate_otp(timecode(Time.now)) end |
#provisioning_uri(name = nil) ⇒ 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
56 57 58 |
# File 'lib/rotp/totp.rb', line 56 def provisioning_uri(name = nil) OTP::URI.new(self, account_name: name || @name).to_s end |
#verify(otp, drift_ahead: 0, drift_behind: 0, after: nil, at: Time.now) ⇒ Integer?
Verifies the OTP passed in against the current time OTP and adjacent intervals up to drift
. Excludes OTPs from ‘after` and earlier. Returns time value of matching OTP code for use in subsequent call.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rotp/totp.rb', line 39 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 |