class Ingenico::Connect::SDK::Logging::PropertyObfuscator

Class that obfuscates properties in the JSON body of a message

Public Class Methods

builder() click to toggle source
# File lib/ingenico/connect/sdk/logging/logging_util.rb, line 228
def self.builder
  Builder.new
end
new(obfuscators) click to toggle source
# File lib/ingenico/connect/sdk/logging/logging_util.rb, line 187
def initialize(obfuscators)
  # case sensitive
  super(obfuscators, false)
  @property_pattern = build_property_pattern(obfuscators.keys)
end

Public Instance Methods

obfuscate(body) click to toggle source
# File lib/ingenico/connect/sdk/logging/logging_util.rb, line 215
def obfuscate(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

Private Instance Methods

build_property_pattern(pn) click to toggle source
# File lib/ingenico/connect/sdk/logging/logging_util.rb, line 195
def build_property_pattern(pn)
  return /$^/ if pn.empty? # no possible match
  # Regex to create:
  # (["'])(X|Y|Z)\1\s*:\s*(?:(["'])(.*?)(?<!\\)\3|([^"'\s\[\{]\S*))
  # Groups:
  # 1: opening " or ' for the property name
  # 2: property name
  # 3: opening " or ' for the value
  # 4: quoted value
  # 5: non-quoted-value
  # The negative lookbehind is to allow escaped quotes to be part of
  # the value. What this does not allow currently is having values end
  # with a \ (which would be escaped to \\).
  regex = pn.inject("([\"'])(") { |r, p| "#{r}#{Regexp.quote(p)}|"}.chop <<
    ")\\1\\s*:\\s*(?:([\"'])(.*?)(?<!\\\\)\\3|([^\"'\\s\\[\\{]((?!,)\\S)*))"
  /#{regex}/m # dotall mode
end