2019-10-01 22:08:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserSecurityKey < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
2023-08-24 02:27:38 -04:00
|
|
|
MAX_KEYS_PER_USER = 50
|
|
|
|
MAX_NAME_LENGTH = 300
|
2019-10-01 22:08:41 -04:00
|
|
|
|
|
|
|
scope :second_factors,
|
|
|
|
-> { where(factor_type: UserSecurityKey.factor_types[:second_factor], enabled: true) }
|
|
|
|
|
2023-08-24 02:27:38 -04:00
|
|
|
validates :name, length: { maximum: MAX_NAME_LENGTH }, if: :name_changed?
|
|
|
|
validate :count_per_user_does_not_exceed_limit, on: :create
|
|
|
|
|
2019-10-01 22:08:41 -04:00
|
|
|
def self.factor_types
|
|
|
|
@factor_types ||= Enum.new(second_factor: 0, first_factor: 1, multi_factor: 2)
|
|
|
|
end
|
2023-08-24 02:27:38 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def count_per_user_does_not_exceed_limit
|
|
|
|
if UserSecurityKey.where(user_id: self.user_id).count >= MAX_KEYS_PER_USER
|
|
|
|
errors.add(:base, I18n.t("login.too_many_security_keys"))
|
|
|
|
end
|
|
|
|
end
|
2019-10-01 22:08:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_security_keys
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
2019-10-17 01:57:53 -04:00
|
|
|
# user_id :bigint not null
|
|
|
|
# credential_id :string not null
|
2019-10-01 22:08:41 -04:00
|
|
|
# public_key :string not null
|
2019-10-17 01:57:53 -04:00
|
|
|
# factor_type :integer default(0), not null
|
|
|
|
# enabled :boolean default(TRUE), not null
|
2023-08-24 02:27:38 -04:00
|
|
|
# name :string(300) not null
|
2019-10-01 22:08:41 -04:00
|
|
|
# last_used :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2019-10-17 01:57:53 -04:00
|
|
|
# index_user_security_keys_on_credential_id (credential_id) UNIQUE
|
|
|
|
# index_user_security_keys_on_factor_type (factor_type)
|
|
|
|
# index_user_security_keys_on_factor_type_and_enabled (factor_type,enabled)
|
|
|
|
# index_user_security_keys_on_last_used (last_used)
|
|
|
|
# index_user_security_keys_on_public_key (public_key)
|
|
|
|
# index_user_security_keys_on_user_id (user_id)
|
|
|
|
#
|
|
|
|
# Foreign Keys
|
|
|
|
#
|
|
|
|
# fk_rails_... (user_id => users.id)
|
2019-10-01 22:08:41 -04:00
|
|
|
#
|