2020-09-15 03:09:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AuthenticationEvent < ApplicationRecord
|
2020-09-19 00:09:50 +00:00
|
|
|
include UsageStatistics
|
|
|
|
|
2021-01-07 21:10:18 +00: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 21:09:19 +00:00
|
|
|
STATIC_PROVIDERS = [TWO_FACTOR, TWO_FACTOR_U2F, TWO_FACTOR_WEBAUTHN, STANDARD].freeze
|
|
|
|
|
2020-09-15 03:09:24 +00:00
|
|
|
belongs_to :user, optional: true
|
|
|
|
|
|
|
|
validates :provider, :user_name, :result, presence: true
|
2020-09-24 15:09:51 +00:00
|
|
|
validates :ip_address, ip_address: true
|
2020-09-15 03:09:24 +00:00
|
|
|
|
|
|
|
enum result: {
|
|
|
|
failed: 0,
|
|
|
|
success: 1
|
|
|
|
}
|
2020-09-19 00:09:50 +00:00
|
|
|
|
|
|
|
scope :for_provider, ->(provider) { where(provider: provider) }
|
|
|
|
scope :ldap, -> { where('provider LIKE ?', 'ldap%')}
|
|
|
|
|
|
|
|
def self.providers
|
2020-11-17 21:09:19 +00:00
|
|
|
STATIC_PROVIDERS | Devise.omniauth_providers.map(&:to_s)
|
2020-09-19 00:09:50 +00:00
|
|
|
end
|
2022-07-01 15:08:30 +00: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-15 03:09:24 +00:00
|
|
|
end
|