1: | <?php |
2: | namespace Worldline\Connect\Sdk\Logging; |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | class HeaderObfuscator |
10: | { |
11: | |
12: | protected $valueObfuscator; |
13: | |
14: | |
15: | private $customRules = array(); |
16: | |
17: | public function __construct() |
18: | { |
19: | $this->valueObfuscator = new ValueObfuscator(); |
20: | } |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | public function obfuscateHeaders(array $headers) |
27: | { |
28: | foreach ($headers as $headerName => &$headerValue) { |
29: | $headerValue = $this->obfuscateHeaderValue($headerName, $headerValue); |
30: | } |
31: | return $headers; |
32: | } |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | protected function obfuscateHeaderValue($key, $value) |
40: | { |
41: | $lowerKey = mb_strtolower(strval($key), 'UTF-8'); |
42: | if (isset($this->customRules[$lowerKey])) { |
43: | return $this->replaceHeaderValueWithCustomRule($value, $this->customRules[$lowerKey]); |
44: | } |
45: | switch ($lowerKey) { |
46: | case 'authorization': |
47: | case 'www-authenticate': |
48: | case 'proxy-authenticate': |
49: | case 'proxy-authorization': |
50: | case 'x-gcs-authentication-token': |
51: | case 'x-gcs-callerpassword': |
52: | return $this->replaceHeaderValueWithFixedNumberOfCharacters($value, 8); |
53: | default: |
54: | return $value; |
55: | } |
56: | } |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | protected function replaceHeaderValueWithFixedNumberOfCharacters($value, $numberOfCharacters) |
64: | { |
65: | if (is_array($value)) { |
66: | return array_fill(0, count($value), $this->valueObfuscator->obfuscateFixedLength($numberOfCharacters)); |
67: | } else { |
68: | return $this->valueObfuscator->obfuscateFixedLength($numberOfCharacters); |
69: | } |
70: | } |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | protected function replaceHeaderValueWithCustomRule($value, callable $customRule) |
78: | { |
79: | if (is_array($value)) { |
80: | return array_map(function ($v) use ($customRule) { |
81: | return call_user_func($customRule, $v, $this->valueObfuscator); |
82: | }, $value); |
83: | } else { |
84: | return call_user_func($customRule, $value, $this->valueObfuscator); |
85: | } |
86: | } |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | public function setCustomRule($headerName, callable $customRule) |
93: | { |
94: | $lowerName = mb_strtolower(strval($headerName), 'UTF-8'); |
95: | $this->customRules[$lowerName] = $customRule; |
96: | } |
97: | } |
98: | |