Class: Worldline::Connect::SDK::Logging::Obfuscation::BodyObfuscator

Inherits:
Object
  • Object
show all
Defined in:
lib/worldline/connect/sdk/logging/obfuscation/body_obfuscator.rb

Overview

A class that can be used to obfuscate properties in JSON bodies.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(additional_rules = nil) ⇒ BodyObfuscator

Creates a new body obfuscator. This will contain some pre-defined obfuscation rules, as well as any provided custom rules



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
# File 'lib/worldline/connect/sdk/logging/obfuscation/body_obfuscator.rb', line 16

def initialize(additional_rules = nil)
  @obfuscation_rules = {
    "cardNumber" => Obfuscation.obfuscate_all_but_last(4),
    "expiryDate" => Obfuscation.obfuscate_all_but_last(2),
    "cvv" => Obfuscation.obfuscate_all,
    "iban" => Obfuscation.obfuscate_all_but_last(4),
    "accountNumber" => Obfuscation.obfuscate_all_but_last(4),
    "reformattedAccountNumber" => Obfuscation.obfuscate_all_but_last(4),
    "bin" => Obfuscation.obfuscate_all_but_first(6),
    "value" => Obfuscation.obfuscate_all,
    "keyId" => Obfuscation.obfuscate_with_fixed_length(8),
    "secretKey" => Obfuscation.obfuscate_with_fixed_length(8),
    "publicKey" => Obfuscation.obfuscate_with_fixed_length(8),
    "userAuthenticationToken" => Obfuscation.obfuscate_with_fixed_length(8),
    "encryptedPayload" => Obfuscation.obfuscate_with_fixed_length(8),
    "decryptedPayload" => Obfuscation.obfuscate_with_fixed_length(8),
    "encryptedCustomerInput" => Obfuscation.obfuscate_with_fixed_length(8),
  }
  if additional_rules
    additional_rules.each do |name, rule|
      @obfuscation_rules[name] = rule
    end
  end

  @property_pattern = build_property_pattern(@obfuscation_rules.keys)
end

Class Method Details

.default_obfuscatorBodyObfuscator



94
95
96
# File 'lib/worldline/connect/sdk/logging/obfuscation/body_obfuscator.rb', line 94

def self.default_obfuscator
  DEFAULT_OBFUSCATOR
end

Instance Method Details

#obfuscate_body(body) ⇒ String

Obfuscates the given body as necessary.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/worldline/connect/sdk/logging/obfuscation/body_obfuscator.rb', line 74

def obfuscate_body(body)
  return nil if body.nil?
  return '' if body.empty?

  body.gsub(@property_pattern) do
    m = Regexp.last_match
    property_name = m[2]
    value = m[4] || m[5]
    # copy value 'cause it's part of m[0]
    m[0].sub(value, obfuscate_value(property_name, value.dup))
  end
end