44 lines
999 B
PHP
44 lines
999 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\OceanpaymentKlarna\Model;
|
|
|
|
use Magento\Framework\Encryption\EncryptorInterface;
|
|
|
|
class SecretValueDecoder
|
|
{
|
|
private const MAX_DECRYPTION_DEPTH = 3;
|
|
|
|
public function __construct(private readonly EncryptorInterface $encryptor)
|
|
{
|
|
}
|
|
|
|
public function decode(string $value): string
|
|
{
|
|
$decoded = trim($value);
|
|
for ($depth = 0; $depth < self::MAX_DECRYPTION_DEPTH; $depth++) {
|
|
if (!$this->isEncrypted($decoded)) {
|
|
break;
|
|
}
|
|
|
|
try {
|
|
$next = $this->encryptor->decrypt($decoded);
|
|
} catch (\Throwable) {
|
|
break;
|
|
}
|
|
|
|
if ($next === '' || hash_equals($decoded, $next)) {
|
|
break;
|
|
}
|
|
$decoded = trim($next);
|
|
}
|
|
|
|
return $decoded;
|
|
}
|
|
|
|
private function isEncrypted(string $value): bool
|
|
{
|
|
return preg_match('/^\d+:\d+:/', $value) === 1;
|
|
}
|
|
}
|