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
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, 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.
9 10 11 12 13 |
# File 'lib/rotp/totp.rb', line 9 def initialize(s, = {}) @interval = [:interval] || DEFAULT_INTERVAL @issuer = [:issuer] super end |
Instance Attribute Details
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
5 6 7 |
# File 'lib/rotp/totp.rb', line 5 def interval @interval end |
#issuer ⇒ Object (readonly)
Returns the value of attribute issuer.
5 6 7 |
# File 'lib/rotp/totp.rb', line 5 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
18 19 20 |
# File 'lib/rotp/totp.rb', line 18 def at(time) generate_otp(timecode(time)) end |
#now ⇒ Integer
Generate the current time OTP
24 25 26 |
# File 'lib/rotp/totp.rb', line 24 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rotp/totp.rb', line 62 def provisioning_uri(name) # The format of this URI is documented at: # https://github.com/google/google-authenticator/wiki/Key-Uri-Format # For compatibility the issuer appears both before that account name and also in the # query string. issuer_string = issuer.nil? ? "" : "#{URI.encode(issuer)}:" params = { secret: secret, period: interval == 30 ? nil : interval, issuer: issuer, digits: digits == DEFAULT_DIGITS ? nil : digits, algorithm: digest.upcase == 'SHA1' ? nil : digest.upcase, } encode_params("otpauth://totp/#{issuer_string}#{URI.encode(name)}", params) 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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rotp/totp.rb', line 40 def verify(otp, drift_ahead: 0, drift_behind: 0, after: nil, at: Time.now) timecodes = get_timecodes(at, drift_behind, drift_ahead) if after timecodes = timecodes.select { |t| t > timecode(after) } end result = nil timecodes.each { |t| if (super(otp, self.generate_otp(t))) result = t * interval end } return result end |