From af59b71ce5f101588c2488dbfc99327a3e28c0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maia?= Date: Sun, 6 Sep 2026 08:57:09 -0300 Subject: [PATCH 1/2] refactor(jsignpdf): move the hash algorithm resolution to a dedicated class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hash algorithm that signs a document depends on the PDF version, so it is not a valid answer for the other hashes JSignPdf takes: reusing it would send SHA1 to a timestamp authority whenever the document is older than PDF 1.6. Issue #8145 asks for each hash to be resolved on its own, which is hard to guarantee while the rules live inside the handler as private methods. Move getHashAlgorithm(), getHashAlgorithmForPdfVersion(), validateHashAlgorithm() and requiresPdfVersionUpgradeForSha256() to HashAlgorithmResolver, where each hash gets its own entry point and can be tested in isolation. The handler keeps reading the PDF version, the only part that is about the document and not about the policy. No behavior change: the same version thresholds, the same fallback to SHA256 and the same supported algorithms. Signed-off-by: André Maia Assisted-by: Claude Code:claude-opus-5 --- .../SignEngine/HashAlgorithmResolver.php | 86 ++++++++++++++++++ lib/Handler/SignEngine/JSignPdfHandler.php | 52 +---------- .../SignEngine/HashAlgorithmResolverTest.php | 90 +++++++++++++++++++ .../SignEngine/JSignPdfHandlerTest.php | 38 +------- 4 files changed, 183 insertions(+), 83 deletions(-) create mode 100644 lib/Handler/SignEngine/HashAlgorithmResolver.php create mode 100644 tests/php/Unit/Handler/SignEngine/HashAlgorithmResolverTest.php diff --git a/lib/Handler/SignEngine/HashAlgorithmResolver.php b/lib/Handler/SignEngine/HashAlgorithmResolver.php new file mode 100644 index 0000000000..3925f692be --- /dev/null +++ b/lib/Handler/SignEngine/HashAlgorithmResolver.php @@ -0,0 +1,86 @@ +getConfiguredAlgorithm(); + /** + * Need to respect the follow code: + * https://github.com/intoolswetrust/jsignpdf/blob/JSignPdf_2_2_2/jsignpdf/src/main/java/net/sf/jsignpdf/types/HashAlgorithm.java#L46-L47 + */ + if ($pdfVersion === null) { + return $this->validate($configuredAlgorithm); + } + + return $this->forPdfVersion($pdfVersion, $configuredAlgorithm); + } + + /** + * PDFs older than 1.6 have to be upgraded before JSignPdf accepts SHA-256. + */ + public function requiresPdfVersionUpgradeForSha256(float $pdfVersion): bool { + if ($pdfVersion >= self::MIN_PDF_VERSION_SHA256) { + return false; + } + + return $this->getConfiguredAlgorithm() === self::DEFAULT_ALGORITHM; + } + + private function forPdfVersion(float $pdfVersion, string $configuredAlgorithm): string { + // Legacy compatibility: JSignPdf still requires SHA1 for very old PDFs (< 1.6). + // The policy still exposes SHA1 for supported legacy workflows, and the runtime + // must continue enforcing this fallback for ancient PDFs that JSignPdf cannot sign otherwise. + if ($pdfVersion < self::MIN_PDF_VERSION_SHA256) { + return 'SHA1'; + } + if ($pdfVersion < self::MIN_PDF_VERSION_SHA1_REJECT) { + return self::DEFAULT_ALGORITHM; + } + if ($configuredAlgorithm === 'SHA1') { + return self::DEFAULT_ALGORITHM; + } + + return $this->validate($configuredAlgorithm); + } + + private function validate(string $algorithm): string { + return in_array($algorithm, self::SUPPORTED_ALGORITHMS, true) ? $algorithm : self::DEFAULT_ALGORITHM; + } + + private function getConfiguredAlgorithm(): string { + return (string)$this->policyService->resolve(SignatureHashAlgorithmPolicy::KEY)->getEffectiveValue(); + } +} diff --git a/lib/Handler/SignEngine/JSignPdfHandler.php b/lib/Handler/SignEngine/JSignPdfHandler.php index 0becbc5ee5..c5f485f543 100644 --- a/lib/Handler/SignEngine/JSignPdfHandler.php +++ b/lib/Handler/SignEngine/JSignPdfHandler.php @@ -16,7 +16,6 @@ use OCA\Libresign\Helper\JavaHelper; use OCA\Libresign\Service\DocMdp\ConfigService as DocMdpConfigService; use OCA\Libresign\Service\Policy\PolicyService; -use OCA\Libresign\Service\Policy\Provider\SignatureHashAlgorithm\SignatureHashAlgorithmPolicy; use OCA\Libresign\Service\Policy\Provider\SignatureText\SignatureTextPolicyValue; use OCA\Libresign\Service\Policy\Provider\Tsa\TsaPolicy; use OCA\Libresign\Service\Policy\Provider\Tsa\TsaPolicyValue; @@ -33,9 +32,7 @@ class JSignPdfHandler extends Pkcs12Handler { private const float MIN_PDF_VERSION = 1.2; private const string TARGET_OLD_PDF_VERSION = '1.3'; - private const float MIN_PDF_VERSION_SHA256 = 1.6; private const string TARGET_PDF_VERSION_SHA256 = '1.6'; - private const float MIN_PDF_VERSION_SHA1_REJECT = 1.7; private const int PAGE_FIRST = 1; private const int SCALE_FACTOR_MIN = 5; @@ -55,6 +52,7 @@ public function __construct( protected CertificateEngineFactory $certificateEngineFactory, protected JavaHelper $javaHelper, private DocMdpConfigService $docMdpConfigService, + private HashAlgorithmResolver $hashAlgorithmResolver, ) { } @@ -151,21 +149,6 @@ private function createEmptyFile(string $path): void { fclose($file); } - private function getHashAlgorithm(string $pdfContent): string { - $configuredAlgorithm = (string)$this->policyService->resolve(SignatureHashAlgorithmPolicy::KEY)->getEffectiveValue(); - /** - * Need to respect the follow code: - * https://github.com/intoolswetrust/jsignpdf/blob/JSignPdf_2_2_2/jsignpdf/src/main/java/net/sf/jsignpdf/types/HashAlgorithm.java#L46-L47 - */ - $pdfVersion = $this->extractPdfVersion($pdfContent); - - if ($pdfVersion === null) { - return $this->validateHashAlgorithm($configuredAlgorithm); - } - - return $this->getHashAlgorithmForPdfVersion($pdfVersion, $configuredAlgorithm); - } - private function extractPdfVersion(string $content): ?float { if (!preg_match('/^%PDF-(?\d+(\.\d+)?)/', $content, $match)) { return null; @@ -173,27 +156,6 @@ private function extractPdfVersion(string $content): ?float { return (float)$match['version']; } - private function getHashAlgorithmForPdfVersion(float $pdfVersion, string $configuredAlgorithm): string { - // Legacy compatibility: JSignPdf still requires SHA1 for very old PDFs (< 1.6). - // The policy still exposes SHA1 for supported legacy workflows, and the runtime - // must continue enforcing this fallback for ancient PDFs that JSignPdf cannot sign otherwise. - if ($pdfVersion < 1.6) { - return 'SHA1'; - } - if ($pdfVersion < self::MIN_PDF_VERSION_SHA1_REJECT) { - return 'SHA256'; - } - if ($pdfVersion >= self::MIN_PDF_VERSION_SHA1_REJECT && $configuredAlgorithm === 'SHA1') { - return 'SHA256'; - } - return $this->validateHashAlgorithm($configuredAlgorithm); - } - - private function validateHashAlgorithm(string $algorithm): string { - $supportedAlgorithms = ['SHA1', 'SHA256', 'SHA384', 'SHA512', 'RIPEMD160']; - return in_array($algorithm, $supportedAlgorithms) ? $algorithm : 'SHA256'; - } - /** * Normalizes very old PDFs (1.0/1.1) to 1.3. * Rationale: JSignPDF enum PdfVersion only defines 1.2+; for 1.0/1.1, @@ -213,7 +175,7 @@ private function normalizePdfVersion(string $content): string { // Convert PDFs < 1.6 to 1.6 if using SHA-256 (the default hash algorithm) // This prevents "The chosen hash algorithm (SHA-256) requires a newer PDF version" error - if ($this->requiresPdfVersionUpgradeForSha256($version)) { + if ($this->hashAlgorithmResolver->requiresPdfVersionUpgradeForSha256($version)) { return $this->replacePdfVersion($content, self::TARGET_PDF_VERSION_SHA256); } @@ -224,14 +186,6 @@ private function isVeryOldPdfVersion(float $version): bool { return $version > 0 && $version < self::MIN_PDF_VERSION; } - private function requiresPdfVersionUpgradeForSha256(float $version): bool { - if ($version >= self::MIN_PDF_VERSION_SHA256) { - return false; - } - $hashAlgorithm = (string)$this->policyService->resolve(SignatureHashAlgorithmPolicy::KEY)->getEffectiveValue(); - return $hashAlgorithm === 'SHA256'; - } - private function replacePdfVersion(string $content, string $newVersion): string { return (string)preg_replace('/^%PDF-\d+(\.\d+)?/', '%PDF-' . $newVersion, $content, 1); } @@ -256,7 +210,7 @@ public function sign(): File { #[\Override] public function getSignedContent(): string { $normalizedPdf = $this->normalizePdfVersion($this->getInputFile()->getContent()); - $hashAlgorithm = $this->getHashAlgorithm($normalizedPdf); + $hashAlgorithm = $this->hashAlgorithmResolver->forSignature($this->extractPdfVersion($normalizedPdf)); $param = $this->getJSignParam(); $param->setCertificate($this->getCertificate()) ->setPdf($normalizedPdf) diff --git a/tests/php/Unit/Handler/SignEngine/HashAlgorithmResolverTest.php b/tests/php/Unit/Handler/SignEngine/HashAlgorithmResolverTest.php new file mode 100644 index 0000000000..e3cb6f9ebe --- /dev/null +++ b/tests/php/Unit/Handler/SignEngine/HashAlgorithmResolverTest.php @@ -0,0 +1,90 @@ +policyService = $this->createMock(PolicyService::class); + } + + private function getInstance(mixed $configuredAlgorithm): HashAlgorithmResolver { + $this->policyService + ->method('resolve') + ->with(SignatureHashAlgorithmPolicy::KEY) + ->willReturn( + (new ResolvedPolicy()) + ->setPolicyKey(SignatureHashAlgorithmPolicy::KEY) + ->setEffectiveValue($configuredAlgorithm) + ); + + return new HashAlgorithmResolver($this->policyService); + } + + #[DataProvider('providerSignatureHashAlgorithm')] + public function testForSignature(mixed $configuredAlgorithm, ?float $pdfVersion, string $expected): void { + $resolver = $this->getInstance($configuredAlgorithm); + + $this->assertSame($expected, $resolver->forSignature($pdfVersion)); + } + + public static function providerSignatureHashAlgorithm(): array { + return [ + // Unknown PDF version: only the configured algorithm decides. + 'unknown version keeps a supported algorithm' => ['SHA384', null, 'SHA384'], + 'unknown version keeps RIPEMD160' => ['RIPEMD160', null, 'RIPEMD160'], + 'unknown version falls back on an empty algorithm' => ['', null, 'SHA256'], + 'unknown version falls back on an unsupported algorithm' => ['XYZ', null, 'SHA256'], + 'unknown version falls back on an unset policy' => [null, null, 'SHA256'], + // JSignPdf only accepts SHA1 in PDFs older than 1.6. + 'PDF 1.0 is signed with SHA1' => ['SHA256', 1.0, 'SHA1'], + 'PDF 1.5 is signed with SHA1' => ['SHA512', 1.5, 'SHA1'], + // Between 1.6 and 1.7 JSignPdf only accepts SHA256. + 'PDF 1.6 is signed with SHA256' => ['SHA384', 1.6, 'SHA256'], + 'PDF 1.6 ignores an unsupported algorithm' => ['XYZ', 1.6, 'SHA256'], + // From 1.7 on the configured algorithm is used, except SHA1. + 'PDF 1.7 keeps the configured SHA384' => ['SHA384', 1.7, 'SHA384'], + 'PDF 1.7 keeps the configured SHA512' => ['SHA512', 1.7, 'SHA512'], + 'PDF 1.7 keeps the configured RIPEMD160' => ['RIPEMD160', 1.7, 'RIPEMD160'], + 'PDF 1.7 replaces SHA1 with SHA256' => ['SHA1', 1.7, 'SHA256'], + 'PDF 2.0 replaces SHA1 with SHA256' => ['SHA1', 2.0, 'SHA256'], + 'PDF 2.0 falls back on an unsupported algorithm' => ['XYZ', 2.0, 'SHA256'], + 'PDF 2.0 keeps the configured SHA512' => ['SHA512', 2.0, 'SHA512'], + ]; + } + + #[DataProvider('providerPdfVersionUpgrade')] + public function testRequiresPdfVersionUpgradeForSha256(mixed $configuredAlgorithm, float $pdfVersion, bool $expected): void { + $resolver = $this->getInstance($configuredAlgorithm); + + $this->assertSame($expected, $resolver->requiresPdfVersionUpgradeForSha256($pdfVersion)); + } + + public static function providerPdfVersionUpgrade(): array { + return [ + 'SHA256 in a PDF 1.2 needs the upgrade' => ['SHA256', 1.2, true], + 'SHA256 in a PDF 1.5 needs the upgrade' => ['SHA256', 1.5, true], + 'SHA256 in a PDF 1.6 does not need the upgrade' => ['SHA256', 1.6, false], + 'SHA256 in a PDF 1.7 does not need the upgrade' => ['SHA256', 1.7, false], + 'SHA1 in a PDF 1.5 does not need the upgrade' => ['SHA1', 1.5, false], + 'SHA512 in a PDF 1.5 does not need the upgrade' => ['SHA512', 1.5, false], + 'an unset policy in a PDF 1.5 does not need the upgrade' => [null, 1.5, false], + ]; + } +} diff --git a/tests/php/Unit/Handler/SignEngine/JSignPdfHandlerTest.php b/tests/php/Unit/Handler/SignEngine/JSignPdfHandlerTest.php index 2bfb503345..537797f60b 100644 --- a/tests/php/Unit/Handler/SignEngine/JSignPdfHandlerTest.php +++ b/tests/php/Unit/Handler/SignEngine/JSignPdfHandlerTest.php @@ -14,6 +14,7 @@ use OCA\Libresign\Enum\DocMdpLevel; use OCA\Libresign\Exception\LibresignException; use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory; +use OCA\Libresign\Handler\SignEngine\HashAlgorithmResolver; use OCA\Libresign\Handler\SignEngine\JSignPdfHandler; use OCA\Libresign\Helper\JavaHelper; use OCA\Libresign\Service\CaIdentifierService; @@ -132,6 +133,7 @@ private function getInstance(array $methods = []): JSignPdfHandler|MockObject { // Create mock factory if initialization failed in setUpBeforeClass $certificateEngineFactory = self::$certificateEngineFactory ?? $this->createMock(CertificateEngineFactory::class); + $hashAlgorithmResolver = new HashAlgorithmResolver($policyService); if (empty($methods)) { return new JSignPdfHandler( @@ -144,6 +146,7 @@ private function getInstance(array $methods = []): JSignPdfHandler|MockObject { $certificateEngineFactory, $this->javaHelper, $this->createMock(DocMdpConfigService::class), + $hashAlgorithmResolver, ); } return $this->getMockBuilder(JSignPdfHandler::class) @@ -157,6 +160,7 @@ private function getInstance(array $methods = []): JSignPdfHandler|MockObject { $certificateEngineFactory, $this->javaHelper, $this->createMock(DocMdpConfigService::class), + $hashAlgorithmResolver, ]) ->onlyMethods($methods) ->getMock(); @@ -198,40 +202,6 @@ private function setDocMdpConfigService(JSignPdfHandler $handler, DocMdpConfigSe $reflection->setValue($handler, $docMdpConfigService); } - #[DataProvider('providerGetHashAlgorithm')] - public function testGetHashAlgorithm(string $setting, string $content, string $expected): void { - if (self::$certificateEngineFactory === null || empty(self::$certificateContent)) { - $this->markTestSkipped('Certificate initialization failed'); - } - - $this->persistHashAlgorithmPolicy($setting); - $instance = $this->getInstance(['getInputFile']); - $file = $this->createMock(\OCP\Files\File::class); - $file->method('getContent')->willReturn($content); - $instance->method('getInputFile')->willReturn($file); - $actual = self::invokePrivate($instance, 'getHashAlgorithm', [$content]); - $this->assertEquals($expected, $actual); - } - - public static function providerGetHashAlgorithm(): array { - return [ - 'empty setting, PDF 1.6' => ['', '%PDF-1.6', 'SHA256'], - 'invalid PDF header' => ['', 'random data', 'SHA256'], - 'invalid setting, fallback to SHA256 on PDF 1.7' => ['XYZ', '%PDF-1.7', 'SHA256'], - 'null-like setting, PDF 1.5' => ['0', '%PDF-1.5', 'SHA1'], - 'default with PDF 1.0' => ['', '%PDF-1', 'SHA1'], - 'SHA1 with PDF 1.5' => ['', '%PDF-1.5', 'SHA1'], - 'SHA1 with PDF 1.6' => ['', '%PDF-1.6', 'SHA256'], - 'SHA1 with PDF 1.7' => ['', '%PDF-1.7', 'SHA256'], - 'SHA1 with PDF 2.0' => ['', '%PDF-2.0', 'SHA256'], - 'SHA384, PDF 1.6 (fallback)' => ['SHA384', '%PDF-1.6', 'SHA256'], - 'SHA384, PDF 1.7' => ['SHA384', '%PDF-1.7', 'SHA384'], - 'SHA512, PDF 1.6' => ['SHA512', '%PDF-1.6', 'SHA256'], - 'RIPEMD160, PDF 1.6 (unsupported)' => ['RIPEMD160', '%PDF-1.6', 'SHA256'], - 'RIPEMD160, PDF 1.7 (supported)' => ['RIPEMD160', '%PDF-1.7', 'RIPEMD160'], - ]; - } - #[DataProvider('providerExtractPdfVersion')] public function testExtractPdfVersion(string $content, ?float $expected): void { if (self::$certificateEngineFactory === null || empty(self::$certificateContent)) { From 0752fa1d7f58e1e47540de3f19ad8ddab479ae87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maia?= Date: Sun, 6 Sep 2026 11:33:22 -0300 Subject: [PATCH 2/2] refactor(jsignpdf): group the JSignPdf classes in their own namespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both the handler and the resolver carry rules that only make sense for JSignPdf — the PDF version thresholds come from its own HashAlgorithm enum — so they move to SignEngine/JSignPdf/ and the resolver keeps a name that does not need to repeat the engine. Pkcs12Handler used to build the handler class name at runtime from the configured engine ('...\SignEngine\' . ucfirst($property)), a string no static analysis, IDE rename or grep could follow: moving the class would only fail when a document was signed. It now resolves through an explicit map, so a name that is not an engine answers the same friendly error instead of reaching the container. Signed-off-by: André Maia Assisted-by: Claude Code:claude-opus-5 --- .../{ => JSignPdf}/HashAlgorithmResolver.php | 2 +- .../{ => JSignPdf}/JSignPdfHandler.php | 3 +- lib/Handler/SignEngine/Pkcs12Handler.php | 12 ++++- lib/SetupCheck/JSignPdfSetupCheck.php | 2 +- .../HashAlgorithmResolverTest.php | 4 +- .../{ => JSignPdf}/JSignPdfHandlerTest.php | 44 +++++++++---------- .../SetupCheck/JSignPdfSetupCheckTest.php | 2 +- 7 files changed, 39 insertions(+), 30 deletions(-) rename lib/Handler/SignEngine/{ => JSignPdf}/HashAlgorithmResolver.php (98%) rename lib/Handler/SignEngine/{ => JSignPdf}/JSignPdfHandler.php (99%) rename tests/php/Unit/Handler/SignEngine/{ => JSignPdf}/HashAlgorithmResolverTest.php (96%) rename tests/php/Unit/Handler/SignEngine/{ => JSignPdf}/JSignPdfHandlerTest.php (97%) diff --git a/lib/Handler/SignEngine/HashAlgorithmResolver.php b/lib/Handler/SignEngine/JSignPdf/HashAlgorithmResolver.php similarity index 98% rename from lib/Handler/SignEngine/HashAlgorithmResolver.php rename to lib/Handler/SignEngine/JSignPdf/HashAlgorithmResolver.php index 3925f692be..c9b1d231cf 100644 --- a/lib/Handler/SignEngine/HashAlgorithmResolver.php +++ b/lib/Handler/SignEngine/JSignPdf/HashAlgorithmResolver.php @@ -6,7 +6,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\Libresign\Handler\SignEngine; +namespace OCA\Libresign\Handler\SignEngine\JSignPdf; use OCA\Libresign\Service\Policy\PolicyService; use OCA\Libresign\Service\Policy\Provider\SignatureHashAlgorithm\SignatureHashAlgorithmPolicy; diff --git a/lib/Handler/SignEngine/JSignPdfHandler.php b/lib/Handler/SignEngine/JSignPdf/JSignPdfHandler.php similarity index 99% rename from lib/Handler/SignEngine/JSignPdfHandler.php rename to lib/Handler/SignEngine/JSignPdf/JSignPdfHandler.php index c5f485f543..dfbd76952c 100644 --- a/lib/Handler/SignEngine/JSignPdfHandler.php +++ b/lib/Handler/SignEngine/JSignPdf/JSignPdfHandler.php @@ -6,13 +6,14 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\Libresign\Handler\SignEngine; +namespace OCA\Libresign\Handler\SignEngine\JSignPdf; use Imagick; use ImagickPixel; use OCA\Libresign\AppInfo\Application; use OCA\Libresign\Exception\LibresignException; use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory; +use OCA\Libresign\Handler\SignEngine\Pkcs12Handler; use OCA\Libresign\Helper\JavaHelper; use OCA\Libresign\Service\DocMdp\ConfigService as DocMdpConfigService; use OCA\Libresign\Service\Policy\PolicyService; diff --git a/lib/Handler/SignEngine/Pkcs12Handler.php b/lib/Handler/SignEngine/Pkcs12Handler.php index 1259881921..498ae23b27 100644 --- a/lib/Handler/SignEngine/Pkcs12Handler.php +++ b/lib/Handler/SignEngine/Pkcs12Handler.php @@ -14,6 +14,7 @@ use OCA\Libresign\Handler\CertificateEngine\OrderCertificatesTrait; use OCA\Libresign\Handler\DocMdpHandler; use OCA\Libresign\Handler\FooterHandler; +use OCA\Libresign\Handler\SignEngine\JSignPdf\JSignPdfHandler; use OCA\Libresign\Service\CaIdentifierService; use OCA\Libresign\Service\Crl\CrlService; use OCA\Libresign\Service\FolderService; @@ -30,6 +31,11 @@ class Pkcs12Handler extends SignEngineHandler { use OrderCertificatesTrait; protected string $certificate = ''; + /** @var array> */ + private const ENGINE_HANDLERS = [ + 'jSignPdfHandler' => JSignPdfHandler::class, + 'phpNativeHandler' => PhpNativeHandler::class, + ]; private ?JSignPdfHandler $jSignPdfHandler = null; private ?PhpNativeHandler $phpNativeHandler = null; private string $rootCertificatePem = ''; @@ -379,11 +385,13 @@ private function enrichLeafWithNativeData( private function getHandler(): SignEngineHandler { $sign_engine = $this->appConfig->getValueString(Application::APP_ID, 'signature_engine', 'JSignPdf'); $property = lcfirst($sign_engine) . 'Handler'; - if (!property_exists($this, $property)) { + // Resolved through a class map instead of a name built at runtime, so + // moving a handler to another namespace cannot break this silently. + if (!isset(self::ENGINE_HANDLERS[$property])) { // TRANSLATORS API/config error when LibreSign's signature_engine setting names a backend that is not available (for example a mistyped JSignPdf/native engine). throw new LibresignException($this->l10n->t('Invalid Sign engine.'), 400); } - $classHandler = 'OCA\\Libresign\\Handler\\SignEngine\\' . ucfirst($property); + $classHandler = self::ENGINE_HANDLERS[$property]; if (!$this->$property instanceof $classHandler) { $this->$property = \OCP\Server::get($classHandler); } diff --git a/lib/SetupCheck/JSignPdfSetupCheck.php b/lib/SetupCheck/JSignPdfSetupCheck.php index 873a51eb47..53c9ab56cd 100644 --- a/lib/SetupCheck/JSignPdfSetupCheck.php +++ b/lib/SetupCheck/JSignPdfSetupCheck.php @@ -9,7 +9,7 @@ namespace OCA\Libresign\SetupCheck; use OCA\Libresign\AppInfo\Application; -use OCA\Libresign\Handler\SignEngine\JSignPdfHandler; +use OCA\Libresign\Handler\SignEngine\JSignPdf\JSignPdfHandler; use OCA\Libresign\Helper\JavaHelper; use OCA\Libresign\Service\Install\InstallService; use OCA\Libresign\Service\Install\SignSetupService; diff --git a/tests/php/Unit/Handler/SignEngine/HashAlgorithmResolverTest.php b/tests/php/Unit/Handler/SignEngine/JSignPdf/HashAlgorithmResolverTest.php similarity index 96% rename from tests/php/Unit/Handler/SignEngine/HashAlgorithmResolverTest.php rename to tests/php/Unit/Handler/SignEngine/JSignPdf/HashAlgorithmResolverTest.php index e3cb6f9ebe..754eaf5247 100644 --- a/tests/php/Unit/Handler/SignEngine/HashAlgorithmResolverTest.php +++ b/tests/php/Unit/Handler/SignEngine/JSignPdf/HashAlgorithmResolverTest.php @@ -6,9 +6,9 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\Libresign\Tests\Unit\Handler\SignEngine; +namespace OCA\Libresign\Tests\Unit\Handler\SignEngine\JSignPdf; -use OCA\Libresign\Handler\SignEngine\HashAlgorithmResolver; +use OCA\Libresign\Handler\SignEngine\JSignPdf\HashAlgorithmResolver; use OCA\Libresign\Service\Policy\Model\ResolvedPolicy; use OCA\Libresign\Service\Policy\PolicyService; use OCA\Libresign\Service\Policy\Provider\SignatureHashAlgorithm\SignatureHashAlgorithmPolicy; diff --git a/tests/php/Unit/Handler/SignEngine/JSignPdfHandlerTest.php b/tests/php/Unit/Handler/SignEngine/JSignPdf/JSignPdfHandlerTest.php similarity index 97% rename from tests/php/Unit/Handler/SignEngine/JSignPdfHandlerTest.php rename to tests/php/Unit/Handler/SignEngine/JSignPdf/JSignPdfHandlerTest.php index 537797f60b..8c2f2ab2dc 100644 --- a/tests/php/Unit/Handler/SignEngine/JSignPdfHandlerTest.php +++ b/tests/php/Unit/Handler/SignEngine/JSignPdf/JSignPdfHandlerTest.php @@ -6,7 +6,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\Libresign\Tests\Unit\Handler\SignEngine; +namespace OCA\Libresign\Tests\Unit\Handler\SignEngine\JSignPdf; use OCA\Libresign\AppInfo\Application; use OCA\Libresign\DataObjects\VisibleElementAssoc; @@ -14,8 +14,8 @@ use OCA\Libresign\Enum\DocMdpLevel; use OCA\Libresign\Exception\LibresignException; use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory; -use OCA\Libresign\Handler\SignEngine\HashAlgorithmResolver; -use OCA\Libresign\Handler\SignEngine\JSignPdfHandler; +use OCA\Libresign\Handler\SignEngine\JSignPdf\HashAlgorithmResolver; +use OCA\Libresign\Handler\SignEngine\JSignPdf\JSignPdfHandler; use OCA\Libresign\Helper\JavaHelper; use OCA\Libresign\Service\CaIdentifierService; use OCA\Libresign\Service\DocMdp\ConfigService as DocMdpConfigService; @@ -315,7 +315,7 @@ public function testSignAffectedParams( ); $this->signatureBackgroundService->method('getImagePath')->willReturn( - realpath(__DIR__ . '/../../../../../img/LibreSign.png') + realpath(__DIR__ . '/../../../../../../img/LibreSign.png') ); $this->persistSignatureStampPolicy( @@ -386,7 +386,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 0, 'urx' => 0, 'ury' => 0, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 100, 'signatureHeight' => 100, 'template' => '', @@ -404,7 +404,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 20, 'urx' => 30, 'ury' => 40, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 20, 'signatureHeight' => 20, 'template' => '', @@ -422,7 +422,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 20, 'urx' => 30, 'ury' => 40, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 20, 'signatureHeight' => 20, 'template' => 'aaaaa', @@ -440,7 +440,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 20, 'urx' => 30, 'ury' => 40, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 20, 'signatureHeight' => 20, 'template' => 'aaaaa', @@ -458,7 +458,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 20, 'urx' => 30, 'ury' => 40, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 20, 'signatureHeight' => 20, 'template' => 'aaaaa', @@ -476,7 +476,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 20, 'urx' => 30, 'ury' => 40, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 20, 'signatureHeight' => 20, 'template' => 'a"b $c \'d e', @@ -494,7 +494,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 20, 'urx' => 30, 'ury' => 40, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 20, 'signatureHeight' => 20, 'template' => '', @@ -512,7 +512,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 20, 'urx' => 30, 'ury' => 40, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 20, 'signatureHeight' => 20, 'template' => 'aaaaa', @@ -530,7 +530,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 100, 'urx' => 351, 'ury' => 200, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 350, 'signatureHeight' => 100, 'template' => 'aaaaa', @@ -548,7 +548,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 20, 'urx' => 30, 'ury' => 40, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 20, 'signatureHeight' => 20, 'template' => 'aaaaa', @@ -605,7 +605,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 20, 'urx' => 30, 'ury' => 40, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 20, 'signatureHeight' => 20, 'template' => '', @@ -623,7 +623,7 @@ public static function providerSignAffectedParams(): array { 'lly' => 20, 'urx' => 30, 'ury' => 40, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png'))], + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png'))], 'signatureWidth' => 0, 'signatureHeight' => 0, 'template' => '', @@ -647,7 +647,7 @@ public function testDocMdpAppliedOnlyOnFirstVisibleElement(): void { $this->signatureBackgroundService->method('getSignatureBackgroundType')->willReturn('deleted'); $this->signatureBackgroundService->method('getImagePath')->willReturn( - realpath(__DIR__ . '/../../../../../img/LibreSign.png') + realpath(__DIR__ . '/../../../../../../img/LibreSign.png') ); $this->persistSignatureStampPolicy('', SignerElementsService::RENDER_MODE_DESCRIPTION_ONLY, 10, SignatureTextPolicyValue::DEFAULT_SIGNATURE_FONT_SIZE, 100, 100); @@ -678,14 +678,14 @@ public function testDocMdpAppliedOnlyOnFirstVisibleElement(): void { 'lly' => 10, 'urx' => 110, 'ury' => 60, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png')), + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png')), self::getElement([ 'page' => 1, 'llx' => 120, 'lly' => 10, 'urx' => 220, 'ury' => 60, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png')), + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png')), ]); $jSignPdfHandler->setJSignPdf($mock); $jSignPdfHandler->setInputFile($inputFile); @@ -710,7 +710,7 @@ public function testDocMdpSkippedWhenSignatureExists(): void { $this->signatureBackgroundService->method('getSignatureBackgroundType')->willReturn('deleted'); $this->signatureBackgroundService->method('getImagePath')->willReturn( - realpath(__DIR__ . '/../../../../../img/LibreSign.png') + realpath(__DIR__ . '/../../../../../../img/LibreSign.png') ); $this->persistSignatureStampPolicy('', SignerElementsService::RENDER_MODE_DESCRIPTION_ONLY, 10, SignatureTextPolicyValue::DEFAULT_SIGNATURE_FONT_SIZE, 100, 100); @@ -741,7 +741,7 @@ public function testDocMdpSkippedWhenSignatureExists(): void { 'lly' => 10, 'urx' => 110, 'ury' => 60, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png')), + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png')), ]); $jSignPdfHandler->setJSignPdf($mock); $jSignPdfHandler->setInputFile($inputFile); @@ -1190,7 +1190,7 @@ public static function providerCertificationLevelWithoutVisibleElements(): array 'lly' => 10, 'urx' => 110, 'ury' => 60, - ], realpath(__DIR__ . '/../../../../../img/app-dark.png')); + ], realpath(__DIR__ . '/../../../../../../img/app-dark.png')); $tsa = ['url' => 'https://tsa.example.test/tsr']; return [ 'certification before the TSA options when the PDF has no signature' => [ diff --git a/tests/php/Unit/SetupCheck/JSignPdfSetupCheckTest.php b/tests/php/Unit/SetupCheck/JSignPdfSetupCheckTest.php index 9e56a4fdf3..efbdf37c6b 100644 --- a/tests/php/Unit/SetupCheck/JSignPdfSetupCheckTest.php +++ b/tests/php/Unit/SetupCheck/JSignPdfSetupCheckTest.php @@ -22,7 +22,7 @@ function is_dir(string $filename): bool { namespace OCA\Libresign\Tests\Unit\SetupCheck; -use OCA\Libresign\Handler\SignEngine\JSignPdfHandler; +use OCA\Libresign\Handler\SignEngine\JSignPdf\JSignPdfHandler; use OCA\Libresign\Helper\JavaHelper; use OCA\Libresign\Service\Install\InstallService; use OCA\Libresign\Service\Install\JSignPdfRelease;