Skip to content

Commit

Permalink
Added better mask function
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 24, 2024
1 parent 811bdcd commit e034142
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Message/CommandHandler/ProcessUploadOrderRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,8 @@ private static function stringifyMessage(RequestInterface|ResponseInterface|null
foreach ($message->getHeaders() as $name => $values) {
$value = implode(', ', $values);

if ('authorization' === strtolower($name) && strlen($value) > 8) {
$value = substr_replace(
$value,
str_repeat('*', strlen($value) - 8),
4,
-4,
);
if ('authorization' === strtolower($name)) {
$value = self::mask($value);
}

$result .= sprintf("%s: %s\n", $name, $value);
Expand All @@ -117,4 +112,22 @@ private static function stringifyMessage(RequestInterface|ResponseInterface|null

return $result;
}

/**
* Copied from here: https://stackoverflow.com/questions/44200823/replace-all-characters-of-a-string-with-asterisks-except-for-the-last-four-chara
*/
private static function mask(string $value): string
{
$length = strlen($value);

$visibleCount = (int) floor($length / 4);
$hiddenCount = $length - ($visibleCount * 2);

return sprintf(
'%s%s%s',
substr($value, 0, $visibleCount),
str_repeat('*', $hiddenCount),
substr($value, $visibleCount * -1, $visibleCount),
);
}
}

0 comments on commit e034142

Please sign in to comment.