Sha256: b4981a70b077a77f1c06c32598652c2efe7644ce3d90ab265f400a29253f4f76

Contents?: true

Size: 945 Bytes

Versions: 4

Compression:

Stored size: 945 Bytes

Contents

class Visa::Token < ActiveRecord::Base
  self.table_name = 'visa_tokens'

  belongs_to :tokenable, polymorphic: true

  validates :tokenable,        presence: true
  validates :client_id,        presence: true, uniqueness: true
  validates :encrypted_secret, presence: true

  before_validation :set_client_id, on: :create
  before_validation :set_secret,    on: :create

  attr_reader :secret

  def self.find_by_credentials(client_id, secret)
    token = find_by client_id: client_id
    token && token.has_secret?(secret) ? token : nil
  end

  def has_secret?(secret)
    BCrypt::Password.new(encrypted_secret) == secret
  end

  private

  def encryption_cost
    Visa.encryption_cost || 10
  end

  def set_client_id
    self.client_id = SecureRandom.hex 8
  end

  def set_secret
    @secret               = SecureRandom.urlsafe_base64 31
    self.encrypted_secret = BCrypt::Password.create @secret,
      cost: encryption_cost
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
visa-0.1.0 app/models/visa/token.rb
visa-0.0.3 app/models/visa/token.rb
visa-0.0.2 app/models/visa/token.rb
visa-0.0.1 app/models/visa/token.rb