Class List Method List File List

# Class: ROTP::HOTP

Inherits:
  OTP
  - Object
  - OTP
  - ROTP::HOTP
  show all
Defined in:
  lib/rotp/hotp.rb

## Instance Attribute Summary

### Attributes inherited from OTP

#digest, #digits, #secret

## Instance Method Summary (collapse)

- - (Object) at(count, padding = true)

  Generates the OTP for the given count.

- - (String) provisioning_uri(name, initial_count = 0)

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

- - (Object) verify(otp, counter)

  Verifies the OTP passed in against the current time OTP.

- - (Object) verify_with_retries(otp, initial_count, retries = 1)

  Verifies the OTP passed in against the current time OTP, with a given number of retries.

### Methods inherited from OTP

#generate_otp, #initialize

## Constructor Details

This class inherits a constructor from ROTP::OTP

## Instance Method Details

### - (`Object`) at(count, padding = true)

Generates the OTP for the given count

Parameters:

- count (`Integer`) —

  counter

- [Boolean] (`Hash`) —

  a customizable set of options

    7
    8
    9

    # File 'lib/rotp/hotp.rb', line 7

    def at(count, padding=true)
      generate_otp(count, padding)
    end

### - (`String`) provisioning_uri(name, initial_count = 0)

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

- initial_count (`Integer`) (defaults to: `0`) —

  starting counter value, defaults to 0

Returns:

- (`String`) —

  provisioning uri

    40
    41
    42

    # File 'lib/rotp/hotp.rb', line 40

    def provisioning_uri(name, initial_count=0)
      encode_params("otpauth://hotp/#{URI.encode(name)}", :secret=>secret, :counter=>initial_count)
    end

### - (`Object`) verify(otp, counter)

Verifies the OTP passed in against the current time OTP

Parameters:

- otp (`String/Integer`) —

  the OTP to check against

- counter (`Integer`) —

  the counter of the OTP

    14
    15
    16

    # File 'lib/rotp/hotp.rb', line 14

    def verify(otp, counter)
      super(otp, self.at(counter))
    end

### - (`Object`) verify_with_retries(otp, initial_count, retries = 1)

Verifies the OTP passed in against the current time OTP, with a given number of retries. Returns the counter that was verified successfully

Parameters:

- otp (`String/Integer`) —

  the OTP to check against

- initial (`Integer`) —

  counter the counter of the OTP

- number (`Integer`) —

  of retries

    23
    24
    25
    26
    27
    28
    29
    30
    31
    32

    # File 'lib/rotp/hotp.rb', line 23

    def verify_with_retries(otp, initial_count, retries = 1)
      return false if retries <= 0

      1.upto(retries) do |counter|
        current_counter = initial_count + counter
        return current_counter if verify(otp, current_counter)
      end

      false
    end