Class List Method List File List

# Class: ROTP::TOTP

Inherits:
  OTP
  - Object
  - OTP
  - ROTP::TOTP
  show all
Defined in:
  lib/rotp/totp.rb

## Instance Attribute Summary (collapse)

- - (Object) interval readonly

  Returns the value of attribute interval.

- - (Object) issuer readonly

  Returns the value of attribute issuer.

### Attributes inherited from OTP

#digest, #digits, #secret

## Instance Method Summary (collapse)

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

  Accepts either a Unix timestamp integer or a Time object.

- - (TOTP) initialize(s, options = {}) constructor

  A new instance of TOTP.

- - (Integer) now(padding = true)

  Generate the current time OTP.

- - (String) provisioning_uri(name)

  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, time = Time.now)

  Verifies the OTP passed in against the current time OTP.

- - (Object) verify_with_drift(otp, drift, time = Time.now)

  Verifies the OTP passed in against the current time OTP and adjacent intervals up to `drift`.

### Methods inherited from OTP

#generate_otp

## Constructor Details

### - (`TOTP`) initialize(s, options = {})

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.

    9
    10
    11
    12
    13

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

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

## Instance Attribute Details

### - (`Object`) interval (readonly)

Returns the value of attribute interval

    5
    6
    7

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

    def interval
      @interval
    end

### - (`Object`) issuer (readonly)

Returns the value of attribute issuer

    5
    6
    7

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

    def issuer
      @issuer
    end

## Instance Method Details

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

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

- [Boolean] (`Hash`) —

  a customizable set of options

    19
    20
    21
    22
    23
    24

    # File 'lib/rotp/totp.rb', line 19

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

### - (`Integer`) now(padding = true)

Generate the current time OTP

Returns:

- (`Integer`) —

  the OTP as an integer

    28
    29
    30

    # File 'lib/rotp/totp.rb', line 28

    def now(padding=true)
      generate_otp(timecode(Time.now), padding)
    end

### - (`String`) provisioning_uri(name)

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

    55
    56
    57
    58

    # File 'lib/rotp/totp.rb', line 55

    def provisioning_uri(name)
      encode_params("otpauth://totp/#{URI.encode(name)}",
                    :secret => secret, :period => (interval==30 ? nil : interval), :issuer => issuer)
    end

### - (`Object`) verify(otp, time = Time.now)

Verifies the OTP passed in against the current time OTP

Parameters:

- otp (`String/Integer`) —

  the OTP to check against

    34
    35
    36

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

    def verify(otp, time = Time.now)
      super(otp, self.at(time))
    end

### - (`Object`) verify_with_drift(otp, drift, time = Time.now)

Verifies the OTP passed in against the current time OTP and adjacent intervals up to `drift`.

Parameters:

- otp (`String`) —

  the OTP to check against

- drift (`Integer`) —

  the number of seconds that the client and server are allowed to drift apart

    43
    44
    45
    46
    47
    48

    # File 'lib/rotp/totp.rb', line 43

    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