From e0341422285e46441e0ea6ff1135f3c653564931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Mon, 24 Jun 2024 12:16:11 +0200 Subject: [PATCH] Added better mask function --- .../ProcessUploadOrderRequestHandler.php | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Message/CommandHandler/ProcessUploadOrderRequestHandler.php b/src/Message/CommandHandler/ProcessUploadOrderRequestHandler.php index faff08c..928f162 100644 --- a/src/Message/CommandHandler/ProcessUploadOrderRequestHandler.php +++ b/src/Message/CommandHandler/ProcessUploadOrderRequestHandler.php @@ -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); @@ -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), + ); + } }