Skip to content

Commit 040d27c

Browse files
Merge pull request #4962 from LibreSign/chore/validate-password-before-send-to-sign
chore: valdiate password before send to sign
2 parents 381e310 + 2fddb79 commit 040d27c

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

lib/Service/IdentifyMethod/SignatureMethod/Password.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace OCA\Libresign\Service\IdentifyMethod\SignatureMethod;
1010

11+
use OCA\Libresign\Exception\InvalidPasswordException;
1112
use OCA\Libresign\Exception\LibresignException;
1213
use OCA\Libresign\Handler\SignEngine\Pkcs12Handler;
1314
use OCA\Libresign\Service\IdentifyMethod\IdentifyService;
@@ -28,13 +29,21 @@ public function __construct(
2829

2930
public function validateToSign(): void {
3031
$this->validateToIdentify();
32+
try {
33+
$this->pkcs12Handler
34+
->setCertificate($this->pkcs12Handler->getPfxOfCurrentSigner($this->userSession->getUser()?->getUID()))
35+
->setPassword($this->codeSentByUser)
36+
->readCertificate();
37+
} catch (InvalidPasswordException $e) {
38+
throw new LibresignException($this->identifyService->getL10n()->t('Invalid user or password'));
39+
}
3140
}
3241

3342
public function validateToIdentify(): void {
3443
$this->pkcs12Handler->setPassword($this->codeSentByUser);
3544
$pfx = $this->pkcs12Handler->getPfxOfCurrentSigner($this->userSession->getUser()?->getUID());
3645
if (empty($pfx)) {
37-
throw new LibresignException($this->identifyService->getL10n()->t('Invalid password'));
46+
throw new LibresignException($this->identifyService->getL10n()->t('Invalid certificate'));
3847
}
3948
}
4049

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Libresign\Tests\Unit\Service;
10+
11+
use OCA\Libresign\AppInfo\Application;
12+
use OCA\Libresign\Exception\LibresignException;
13+
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
14+
use OCA\Libresign\Handler\FooterHandler;
15+
use OCA\Libresign\Handler\SignEngine\Pkcs12Handler;
16+
use OCA\Libresign\Service\FolderService;
17+
use OCA\Libresign\Service\IdentifyMethod\IdentifyService;
18+
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\Password;
19+
use OCP\IAppConfig;
20+
use OCP\IL10N;
21+
use OCP\ITempManager;
22+
use OCP\IUserSession;
23+
use OCP\L10N\IFactory as IL10NFactory;
24+
use PHPUnit\Framework\Attributes\DataProvider;
25+
use PHPUnit\Framework\MockObject\MockObject;
26+
27+
final class PasswordTest extends \OCA\Libresign\Tests\Unit\TestCase {
28+
private IdentifyService&MockObject $identifyService;
29+
private Pkcs12Handler&MockObject $pkcs12Handler;
30+
private IUserSession&MockObject $userSession;
31+
private IAppConfig $appConfig;
32+
private FolderService&MockObject $folderService;
33+
private CertificateEngineFactory&MockObject $certificateEngineFactory;
34+
private IL10N $l10n;
35+
private FooterHandler&MockObject $footerHandler;
36+
private ITempManager $tempManager;
37+
38+
public function setUp(): void {
39+
$this->identifyService = $this->createMock(IdentifyService::class);
40+
$this->appConfig = $this->getMockAppConfig();
41+
$this->folderService = $this->createMock(FolderService::class);
42+
$this->certificateEngineFactory = $this->createMock(CertificateEngineFactory::class);
43+
$this->l10n = \OCP\Server::get(IL10NFactory::class)->get(Application::APP_ID);
44+
$this->footerHandler = $this->createMock(FooterHandler::class);
45+
$this->tempManager = \OCP\Server::get(ITempManager::class);
46+
$this->userSession = $this->createMock(IUserSession::class);
47+
$this->pkcs12Handler = $this->getPkcs12Instance();
48+
}
49+
50+
private function getClass(): Password {
51+
return new Password(
52+
$this->identifyService,
53+
$this->pkcs12Handler,
54+
$this->userSession,
55+
);
56+
}
57+
58+
/**
59+
* @return Pkcs12Handler&MockObject
60+
*/
61+
private function getPkcs12Instance(array $methods = []) {
62+
return $this->getMockBuilder(Pkcs12Handler::class)
63+
->setConstructorArgs([
64+
$this->folderService,
65+
$this->appConfig,
66+
$this->certificateEngineFactory,
67+
$this->l10n,
68+
$this->footerHandler,
69+
$this->tempManager,
70+
])
71+
->onlyMethods($methods)
72+
->getMock();
73+
}
74+
75+
#[DataProvider('providerValidateToIdentify')]
76+
public function testValidateToIdentify(string $pfx, bool $shouldThrow): void {
77+
$this->pkcs12Handler = $this->getPkcs12Instance(['getPfxOfCurrentSigner']);
78+
$this->pkcs12Handler->method('getPfxOfCurrentSigner')->willReturn($pfx);
79+
80+
$password = $this->getClass();
81+
$password->setCodeSentByUser('senha');
82+
83+
if ($shouldThrow) {
84+
$this->expectException(LibresignException::class);
85+
$password->validateToIdentify();
86+
} else {
87+
$password->validateToIdentify();
88+
$this->expectNotToPerformAssertions();
89+
}
90+
}
91+
92+
public static function providerValidateToIdentify(): array {
93+
return [
94+
'valid pfx' => ['mock-pfx', false],
95+
'empty pfx' => ['', true],
96+
];
97+
}
98+
99+
#[DataProvider('providerValidateToSignWithError')]
100+
public function testValidateToSignWithError(bool $throwsException, string $pfx): void {
101+
$this->pkcs12Handler = $this->getPkcs12Instance(['getPfxOfCurrentSigner']);
102+
$this->pkcs12Handler->method('getPfxOfCurrentSigner')->willReturn($pfx);
103+
if ($throwsException) {
104+
$this->expectException(LibresignException::class);
105+
} else {
106+
$this->expectNotToPerformAssertions();
107+
}
108+
109+
$password = $this->getClass();
110+
$password->setCodeSentByUser('senha');
111+
$password->validateToSign();
112+
}
113+
114+
public static function providerValidateToSignWithError(): array {
115+
return [
116+
'Invalid certificate' => [true, ''],
117+
'throws InvalidPasswordException' => [true, 'mock-pfx'],
118+
];
119+
}
120+
}

0 commit comments

Comments
 (0)