Module: Datadog::AppSec::Contrib::Devise::Patcher::AuthenticatablePatch

Defined in:
lib/datadog/appsec/contrib/devise/patcher/authenticatable_patch.rb

Overview

Hook in devise validate method

Instance Method Summary collapse

Instance Method Details

#validate(resource, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/datadog/appsec/contrib/devise/patcher/authenticatable_patch.rb', line 15

def validate(resource, &block)
  result = super
  return result unless AppSec.enabled?

  track_user_events_configuration = Datadog.configuration.appsec.track_user_events

  return result unless track_user_events_configuration.enabled

  automated_track_user_events_mode = track_user_events_configuration.mode

  appsec_scope = Datadog::AppSec.active_scope

  return result unless appsec_scope

  devise_resource = resource ? Resource.new(resource) : nil

  event_information = Event.new(devise_resource, automated_track_user_events_mode)

  if result
    if event_information.user_id
      Datadog.logger.debug { 'User Login Event success' }
    else
      Datadog.logger.debug { 'User Login Event success, but can\'t extract user ID. Tracking empty event' }
    end

    Tracking.(
      appsec_scope.trace,
      appsec_scope.service_entry_span,
      user_id: event_information.user_id,
      **event_information.to_h
    )

    return result
  end

  user_exists = nil

  if resource
    user_exists = true
    Datadog.logger.debug { 'User Login Event failure users exists' }
  else
    user_exists = false
    Datadog.logger.debug { 'User Login Event failure user do not exists' }
  end

  Tracking.(
    appsec_scope.trace,
    appsec_scope.service_entry_span,
    user_id: event_information.user_id,
    user_exists: user_exists,
    **event_information.to_h
  )

  result
end