Class: OnlinePayments::SDK::Logging::Obfuscation::BodyObfuscator

Inherits:
Object
  • Object
show all
Defined in:
lib/onlinepayments/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

Parameters:

  • additional_rules (Hash) (defaults to: nil)

    An optional hash where the keys are property names and the values are functions that obfuscate a single value



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

def initialize(additional_rules = nil)
  @obfuscation_rules = {
    "additionalInfo" => Obfuscation.obfuscate_all,
    "cardholderName" => Obfuscation.obfuscate_all,
    "dateOfBirth" => Obfuscation.obfuscate_all,
    "emailAddress" => Obfuscation.obfuscate_all,
    "faxNumber" => Obfuscation.obfuscate_all,
    "firstName" => Obfuscation.obfuscate_all,
    "houseNumber" => Obfuscation.obfuscate_all,
    "mobilePhoneNumber" => Obfuscation.obfuscate_all,
    "passengerName" => Obfuscation.obfuscate_all,
    "phoneNumber" => Obfuscation.obfuscate_all,
    "street" => Obfuscation.obfuscate_all,
    "workPhoneNumber" => Obfuscation.obfuscate_all,
    "zip" => Obfuscation.obfuscate_all,
    "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

Returns:



106
107
108
# File 'lib/onlinepayments/sdk/logging/obfuscation/body_obfuscator.rb', line 106

def self.default_obfuscator
  DEFAULT_OBFUSCATOR
end

Instance Method Details

#obfuscate_body(body) ⇒ String

Obfuscates the given body as necessary.

Returns:

  • (String)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/onlinepayments/sdk/logging/obfuscation/body_obfuscator.rb', line 86

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