Sha256: dd62135602b42f938f3048fed531efc81c990055482672855c4b119dcc9f55c8

Contents?: true

Size: 703 Bytes

Versions: 19

Compression:

Stored size: 703 Bytes

Contents

# frozen_string_literal: true

module QuoVadis
  class RecoveryCode < ActiveRecord::Base
    belongs_to :account

    has_secure_password :code
    before_validation { send("code=", self.class.generate_code) unless code }

    # Returns true and destroys this instance if the plaintext code is authentic, false otherwise.
    def authenticate_code(plaintext_code)
      !!(destroy if super)
    end

    private

    CODE_LENGTH = 11  # odd number

    # Returns a string of length CODE_LENGTH, with two hexadecimal groups
    # separated by a hyphen.
    def self.generate_code
      group_length = (CODE_LENGTH - 1) / 2
      SecureRandom.hex(group_length).insert(group_length, '-')
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
quo_vadis-2.2.4 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.2.2 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.2.1 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.2.0 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.11 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.10 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.9 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.8 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.7 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.6 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.5 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.4 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.3 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.2 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.1 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.1.0 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.0.2 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.0.1 app/models/quo_vadis/recovery_code.rb
quo_vadis-2.0.0 app/models/quo_vadis/recovery_code.rb