-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBaseHttpLoggingConfiguration.php
95 lines (85 loc) · 2.74 KB
/
BaseHttpLoggingConfiguration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace Core\Logger\Configuration;
use Core\Logger\LoggerConstants;
class BaseHttpLoggingConfiguration
{
private $logBody;
private $logHeaders;
private $headersToInclude;
private $headersToExclude;
private $headersToUnmask;
/**
* Construct an instance of ResponseConfig for logging
*
* @param bool $logBody
* @param bool $logHeaders
* @param string[] $headersToInclude
* @param string[] $headersToExclude
* @param string[] $headersToUnmask
*/
public function __construct(
bool $logBody,
bool $logHeaders,
array $headersToInclude,
array $headersToExclude,
array $headersToUnmask
) {
$this->logBody = $logBody;
$this->logHeaders = $logHeaders;
$this->headersToInclude = array_map('strtolower', $headersToInclude);
$this->headersToExclude = empty($headersToInclude) ? array_map('strtolower', $headersToExclude) : [];
$this->headersToUnmask = array_merge(
array_map('strtolower', LoggerConstants::NON_SENSITIVE_HEADERS),
array_map('strtolower', $headersToUnmask)
);
}
/**
* Indicates whether to log the body.
*/
public function shouldLogBody(): bool
{
return $this->logBody;
}
/**
* Indicates whether to log the headers.
*/
public function shouldLogHeaders(): bool
{
return $this->logHeaders;
}
/**
* Select the headers from the list of provided headers for logging.
*
* @param string[] $headers
* @param bool $maskSensitiveHeaders
*
* @return string[]
*/
public function getLoggableHeaders(array $headers, bool $maskSensitiveHeaders): array
{
$sensitiveHeaders = [];
$filteredHeaders = array_filter($headers, function ($key) use ($maskSensitiveHeaders, &$sensitiveHeaders) {
$lowerCaseKey = strtolower(strval($key));
if ($maskSensitiveHeaders && $this->isSensitiveHeader($lowerCaseKey)) {
$sensitiveHeaders[$key] = '**Redacted**';
}
if (
(empty($this->headersToInclude) || in_array($lowerCaseKey, $this->headersToInclude, true)) &&
(empty($this->headersToExclude) || !in_array($lowerCaseKey, $this->headersToExclude, true))
) {
return true;
}
unset($sensitiveHeaders[$key]);
return false;
}, ARRAY_FILTER_USE_KEY);
return array_merge($filteredHeaders, $sensitiveHeaders);
}
private function isSensitiveHeader($headerKey): bool
{
if (in_array($headerKey, $this->headersToUnmask, true)) {
return false;
}
return true;
}
}