2020-09-14 23:09:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AuthenticationEvent < ApplicationRecord
|
2020-09-18 20:09:50 -04:00
|
|
|
include UsageStatistics
|
|
|
|
|
2021-01-07 16:10:18 -05:00
|
|
|
TWO_FACTOR = 'two-factor'
|
|
|
|
TWO_FACTOR_U2F = 'two-factor-via-u2f-device'
|
|
|
|
TWO_FACTOR_WEBAUTHN = 'two-factor-via-webauthn-device'
|
|
|
|
STANDARD = 'standard'
|
2020-11-17 16:09:19 -05:00
|
|
|
STATIC_PROVIDERS = [TWO_FACTOR, TWO_FACTOR_U2F, TWO_FACTOR_WEBAUTHN, STANDARD].freeze
|
|
|
|
|
2020-09-14 23:09:24 -04:00
|
|
|
belongs_to :user, optional: true
|
|
|
|
|
|
|
|
validates :provider, :user_name, :result, presence: true
|
2020-09-24 11:09:51 -04:00
|
|
|
validates :ip_address, ip_address: true
|
2020-09-14 23:09:24 -04:00
|
|
|
|
|
|
|
enum result: {
|
|
|
|
failed: 0,
|
|
|
|
success: 1
|
|
|
|
}
|
2020-09-18 20:09:50 -04:00
|
|
|
|
|
|
|
scope :for_provider, ->(provider) { where(provider: provider) }
|
2022-08-17 05:11:44 -04:00
|
|
|
scope :ldap, -> { where('provider LIKE ?', 'ldap%') }
|
2020-09-18 20:09:50 -04:00
|
|
|
|
|
|
|
def self.providers
|
2020-11-17 16:09:19 -05:00
|
|
|
STATIC_PROVIDERS | Devise.omniauth_providers.map(&:to_s)
|
2020-09-18 20:09:50 -04:00
|
|
|
end
|
2022-07-01 11:08:30 -04:00
|
|
|
|
|
|
|
def self.initial_login_or_known_ip_address?(user, ip_address)
|
|
|
|
!where(user_id: user).exists? ||
|
|
|
|
where(user_id: user, ip_address: ip_address).success.exists?
|
|
|
|
end
|
2020-09-14 23:09:24 -04:00
|
|
|
end
|