require 'securerandom'
require 'digest/sha1'
require 'debci/db'
module Debci
class Key < ActiveRecord::Base
belongs_to :user, class_name: 'Debci::User'
attr_accessor :key
before_create do |key|
key.key = SecureRandom.uuid
key.encrypted_key = key.class.encrypt(key.key)
end
def self.reset!(user)
find_by(user: user)&.destroy
create!(user: user)
end
def self.authenticate(key)
entry = find_by(encrypted_key: encrypt(key))
entry&.user || nil
end
def self.encrypt(key)
Digest::SHA1.hexdigest(key)
end
end
end