Class: Datadog::AppSec::Contrib::Devise::DataExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/appsec/contrib/devise/data_extractor.rb

Overview

Extracts user identification data from Devise resources. Supports both regular and anonymized data extraction modes.

Constant Summary collapse

PRIORITY_ORDERED_ID_KEYS =
[:id, 'id', :uuid, 'uuid'].freeze
PRIORITY_ORDERED_LOGIN_KEYS =
[:email, 'email', :username, 'username', :login, 'login'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(mode:) ⇒ DataExtractor

Returns a new instance of DataExtractor.



15
16
17
18
# File 'lib/datadog/appsec/contrib/devise/data_extractor.rb', line 15

def initialize(mode:)
  @mode = mode
  @devise_scopes = {}
end

Instance Method Details

#extract_id(object) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/datadog/appsec/contrib/devise/data_extractor.rb', line 20

def extract_id(object)
  return if object.nil?

  if object.respond_to?(:[])
    id = object[PRIORITY_ORDERED_ID_KEYS.find { |key| object[key] }]
    scope = find_devise_scope(object)

    id = "#{scope}:#{id}" if id && scope
    return transform(id)
  end

  id = object.id if object.respond_to?(:id)
  id ||= object.uuid if object.respond_to?(:uuid)

  scope = find_devise_scope(object)
  id = "#{scope}:#{id}" if id && scope

  transform(id)
end

#extract_login(object) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/datadog/appsec/contrib/devise/data_extractor.rb', line 40

def (object)
  return if object.nil?

  if object.respond_to?(:[])
     = object[PRIORITY_ORDERED_LOGIN_KEYS.find { |key| object[key] }]
    return transform()
  end

   = object.email if object.respond_to?(:email)
   ||= object.username if object.respond_to?(:username)
   ||= object. if object.respond_to?(:login)

  transform()
end