Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/Middleware/InjectionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Libresign\Middleware\Attribute\CanSignRequestUuid;
use OCA\Libresign\Middleware\Attribute\PrivateValidation;
use OCA\Libresign\Middleware\Attribute\RequireFileAccess;
Expand All @@ -32,6 +31,8 @@
use OCA\Libresign\Service\Policy\Provider\ValidationAccess\ValidationAccessPolicy;
use OCA\Libresign\Service\SignFileService;
use OCA\Libresign\Service\UuidResolverService;
use OCA\Libresign\Service\Validation\SignerValidator;
use OCA\Libresign\Service\Validation\SigningRequestValidator;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\ContentSecurityPolicy;
Expand Down Expand Up @@ -59,7 +60,8 @@ public function __construct(
private IRequest $request,
private ISession $session,
private IUserSession $userSession,
private ValidateHelper $validateHelper,
private SigningRequestValidator $signingRequestValidator,
private SignerValidator $signerValidator,
private SignRequestMapper $signRequestMapper,
private CertificateEngineFactory $certificateEngineFactory,
private FileMapper $fileMapper,
Expand Down Expand Up @@ -278,7 +280,7 @@ private function getLoggedIn(): void {
// TRANSLATORS: Error shown when an anonymous user tries to create a signature request, an action allowed only for authenticated users with permission.
throw new \Exception($this->l10n->t('You are not allowed to create signature requests'), Http::STATUS_UNPROCESSABLE_ENTITY);
}
$this->validateHelper->canRequestSign($user);
$this->signingRequestValidator->canRequestSign($user);
}

private function requireSigner(): void {
Expand All @@ -291,7 +293,7 @@ private function requireSigner(): void {
if ($isIdDocApproval) {
$this->uuidResolverService->resolveUuidForUser($uuid, $user);
} else {
$this->validateHelper->validateSigner($uuid, $user);
$this->signerValidator->validateSigner($uuid, $user);
}
} catch (LibresignException $e) {
throw new LibresignException($e->getMessage());
Expand All @@ -302,7 +304,7 @@ private function requireSignerUuid(): void {
$uuid = $this->getUuidFromRequest();

try {
$this->validateHelper->validateSignerUuid($uuid);
$this->signerValidator->validateSignerUuid($uuid);
} catch (LibresignException $e) {
throw new LibresignException($e->getMessage());
}
Expand Down
14 changes: 8 additions & 6 deletions lib/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Handler\SignEngine\Pkcs12Handler;
use OCA\Libresign\Helper\FileUploadHelper;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Libresign\Service\Crl\CrlService;
use OCA\Libresign\Service\Policy\PolicyAuthorizationService;
use OCA\Libresign\Service\Policy\RequestSignAuthorizationService;
use OCA\Libresign\Service\Validation\FileInputValidator;
use OCA\Libresign\Service\Validation\IdentityDocumentValidator;
use OCA\Settings\Mailer\NewUserMailHelper;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\Db\DoesNotExistException;
Expand Down Expand Up @@ -69,7 +70,8 @@ public function __construct(
private NewUserMailHelper $newUserMail,
private IdentifyMethodService $identifyMethodService,
private IdentifyMethodMapper $identifyMethodMapper,
private ValidateHelper $validateHelper,
private IdentityDocumentValidator $identityDocumentValidator,
private FileInputValidator $fileInputValidator,
private IURLGenerator $urlGenerator,
private Pkcs12Handler $pkcs12Handler,
private IGroupManager $groupManager,
Expand Down Expand Up @@ -213,7 +215,7 @@ public function getConfig(?IUser $user = null): array {
$info['identificationDocumentsFlow'] = $this->idDocsPolicyService->isIdentificationDocumentsEnabled($user);
$info['hasSignatureFile'] = $this->hasSignatureFile($user);
$info['phoneNumber'] = $this->getPhoneNumber($user);
$info['isApprover'] = $this->validateHelper->userCanApproveValidationDocuments($user, false);
$info['isApprover'] = $this->identityDocumentValidator->userCanApproveValidationDocuments($user, false);
$info['id_docs_filters'] = $this->getUserConfigIdDocsFilters($user);
$info['id_docs_sort'] = $this->getUserConfigIdDocsSort($user);
$info['crl_filters'] = $this->getUserConfigCrlFilters($user);
Expand Down Expand Up @@ -349,7 +351,7 @@ private function getUserConfigCrlSort(?IUser $user): array {
}

private function getUserConfigIdDocsSort(?IUser $user): array {
if (!$user || !$this->validateHelper->userCanApproveValidationDocuments($user, false)) {
if (!$user || !$this->identityDocumentValidator->userCanApproveValidationDocuments($user, false)) {
return ['sortBy' => null, 'sortOrder' => null];
}

Expand Down Expand Up @@ -517,10 +519,10 @@ private function getFileRaw(array $data): string {
// TRANSLATORS Error when uploading a visible signature element file that is empty.
throw new \Exception($this->l10n->t('Empty file'));
}
$this->validateHelper->validateBase64($content, ValidateHelper::TYPE_VISIBLE_ELEMENT_USER);
$this->fileInputValidator->validateBase64($content, FileInputValidator::TYPE_VISIBLE_ELEMENT_USER);
return $content;
}
$this->validateHelper->validateBase64($data['file']['base64'], ValidateHelper::TYPE_VISIBLE_ELEMENT_USER);
$this->fileInputValidator->validateBase64($data['file']['base64'], FileInputValidator::TYPE_VISIBLE_ELEMENT_USER);
$withMime = explode(',', (string)$data['file']['base64']);
if (count($withMime) === 2) {
$content = base64_decode($withMime[1]);
Expand Down
20 changes: 12 additions & 8 deletions lib/Service/RequestSignatureService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\DocMdpHandler;
use OCA\Libresign\Helper\FileUploadHelper;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Libresign\Service\DocMdp\ConfigService as DocMdpConfigService;
use OCA\Libresign\Service\Envelope\EnvelopeFileRelocator;
use OCA\Libresign\Service\Envelope\EnvelopeService;
Expand All @@ -28,6 +27,9 @@
use OCA\Libresign\Service\Policy\FilePolicyApplier;
use OCA\Libresign\Service\SignerGeolocation\SignerGeolocationPolicyService;
use OCA\Libresign\Service\SignRequest\SignRequestService;
use OCA\Libresign\Service\Validation\FileInputValidator;
use OCA\Libresign\Service\Validation\SignerValidator;
use OCA\Libresign\Service\Validation\SigningRequestValidator;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\Node;
Expand All @@ -54,7 +56,9 @@ public function __construct(
protected FileElementMapper $fileElementMapper,
protected FolderService $folderService,
protected IMimeTypeDetector $mimeTypeDetector,
protected ValidateHelper $validateHelper,
protected FileInputValidator $fileInputValidator,
protected SigningRequestValidator $signingRequestValidator,
protected SignerValidator $signerValidator,
protected IClientService $client,
protected DocMdpHandler $docMdpHandler,
protected LoggerInterface $logger,
Expand Down Expand Up @@ -441,7 +445,7 @@ private function removeExtensionFromName(string $name, array $metadata): string
}

private function deleteIdentifyMethodIfNotExits(array $signers, FileEntity $file): void {
$normalizedSigners = $this->validateHelper->normalizeRequestSigners($signers);
$normalizedSigners = $this->signerValidator->normalizeRequestSigners($signers);
$signRequests = $this->signRequestMapper->getByFileId($file->getId());
foreach ($signRequests as $key => $signRequest) {
$identifyMethods = $this->identifyMethod->getIdentifyMethodsFromSignRequestId($signRequest->getId());
Expand Down Expand Up @@ -482,7 +486,7 @@ private function identifyMethodExists(array $signers, IIdentifyMethod $identifyM
private function associateToSigners(array $data, FileEntity $file): array {
$return = [];
if (!empty($data['signers'])) {
$normalizedSigners = $this->validateHelper->normalizeRequestSigners($data['signers']);
$normalizedSigners = $this->signerValidator->normalizeRequestSigners($data['signers']);
$this->deleteIdentifyMethodIfNotExits($normalizedSigners, $file);
$this->identifyMethod->clearCache();

Expand Down Expand Up @@ -571,15 +575,15 @@ private function saveVisibleElements(array $data, FileEntity $file): array {
public function validateNewRequestToFile(array $data): void {
$this->validateNewFile($data);
$this->validateSigners($data);
$this->validateHelper->validateFileStatus($data);
$this->signingRequestValidator->validateFileStatus($data);
}

public function validateNewFile(array $data): void {
if (empty($data['name'])) {
// TRANSLATORS Error shown when creating a signature request without a document file name.
throw new \Exception($this->l10n->t('File name is required'));
}
$this->validateHelper->validateNewFile($data);
$this->fileInputValidator->validateNewFile($data);
}

public function validateSigners(array $data): void {
Expand All @@ -596,8 +600,8 @@ public function validateSigners(array $data): void {
throw new \Exception($this->l10n->t('Signers list needs to be an array'));
}

$this->validateHelper->validateIdentifySigners($data);
$normalizedSigners = $this->validateHelper->normalizeRequestSigners($data['signers']);
$this->signerValidator->validateIdentifySigners($data);
$normalizedSigners = $this->signerValidator->normalizeRequestSigners($data['signers']);

foreach ($normalizedSigners as $signer) {
$this->identifyMethod->setAllEntityData($signer);
Expand Down
18 changes: 11 additions & 7 deletions lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
use OCA\Libresign\Handler\SignEngine\SignEngineFactory;
use OCA\Libresign\Handler\SignEngine\SignEngineHandler;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Libresign\Service\Envelope\EnvelopeStatusDeterminer;
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\IToken;
Expand All @@ -49,6 +48,9 @@
use OCA\Libresign\Service\Policy\Provider\Footer\FooterPolicyValue;
use OCA\Libresign\Service\SignRequest\SignRequestService;
use OCA\Libresign\Service\SignRequest\StatusService;
use OCA\Libresign\Service\Validation\IdentityDocumentValidator;
use OCA\Libresign\Service\Validation\SignerValidator;
use OCA\Libresign\Service\Validation\SigningRequestValidator;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Utility\ITimeFactory;
Expand Down Expand Up @@ -97,7 +99,9 @@ public function __construct(
private IClientService $client,
protected LoggerInterface $logger,
private IAppConfig $appConfig,
protected ValidateHelper $validateHelper,
protected IdentityDocumentValidator $identityDocumentValidator,
protected SigningRequestValidator $signingRequestValidator,
protected SignerValidator $signerValidator,
private SignerElementsService $signerElementsService,
private IUserSession $userSession,
private IDateTimeZone $dateTimeZone,
Expand Down Expand Up @@ -1311,7 +1315,7 @@ public function requestCode(
}

private function getOrCreateApproverSignRequest(FileEntity $file, IUser $user): ?SignRequestEntity {
if (!$this->validateHelper->userCanApproveValidationDocuments($user, false)) {
if (!$this->identityDocumentValidator->userCanApproveValidationDocuments($user, false)) {
return null;
}

Expand Down Expand Up @@ -1370,7 +1374,7 @@ private function findSignRequestByIdentifyMethod(array $signRequests, IUser $use
}

public function getSignRequestToSign(FileEntity $libresignFile, ?string $signRequestUuid, ?IUser $user): SignRequestEntity {
$this->validateHelper->fileCanBeSigned($libresignFile);
$this->signingRequestValidator->fileCanBeSigned($libresignFile);
try {
if (!empty($signRequestUuid)) {
$signRequest = $this->getSignRequestByUuid($signRequestUuid);
Expand Down Expand Up @@ -1615,7 +1619,7 @@ private function createSignedFile(File $originalFile, string $content): File {
* @throws DoesNotExistException
*/
public function getSignRequestByUuid(string $uuid): SignRequestEntity {
$this->validateHelper->validateUuidFormat($uuid);
$this->signerValidator->validateUuidFormat($uuid);
return $this->signRequestMapper->getByUuid($uuid);
}

Expand Down Expand Up @@ -1683,11 +1687,11 @@ public function getNextcloudFiles(FileEntity $fileData): array {
}

public function validateSigner(string $uuid, ?IUser $user = null): void {
$this->validateHelper->validateSigner($uuid, $user);
$this->signerValidator->validateSigner($uuid, $user);
}

public function validateRenewSigner(string $uuid, ?IUser $user = null): void {
$this->validateHelper->validateRenewSigner($uuid, $user);
$this->signerValidator->validateRenewSigner($uuid, $user);
}

public function getSignerData(?IUser $user, ?SignRequestEntity $signRequest = null): array {
Expand Down
12 changes: 8 additions & 4 deletions tests/php/Unit/Middleware/InjectionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Exception\PageException;
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Libresign\Middleware\Attribute\PrivateValidation;
use OCA\Libresign\Middleware\Attribute\RequireSignRequestUuid;
use OCA\Libresign\Middleware\InjectionMiddleware;
Expand All @@ -23,6 +22,8 @@
use OCA\Libresign\Service\Policy\Provider\ValidationAccess\ValidationAccessPolicy;
use OCA\Libresign\Service\SignFileService;
use OCA\Libresign\Service\UuidResolverService;
use OCA\Libresign\Service\Validation\SignerValidator;
use OCA\Libresign\Service\Validation\SigningRequestValidator;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
Expand Down Expand Up @@ -67,7 +68,8 @@ final class InjectionMiddlewareTest extends \OCA\Libresign\Tests\Unit\TestCase {
private IRequest&MockObject $request;
private ISession&MockObject $session;
private IUserSession&MockObject $userSession;
private ValidateHelper&MockObject $validateHelper;
private SigningRequestValidator&MockObject $signingRequestValidator;
private SignerValidator&MockObject $signerValidator;
private SignRequestMapper&MockObject $signRequestMapper;
private CertificateEngineFactory $certificateEngineFactory;
private FileMapper&MockObject $fileMapper;
Expand All @@ -88,7 +90,8 @@ public function setUp(): void {
$this->request = $this->createMock(IRequest::class);
$this->session = $this->createMock(ISession::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->validateHelper = $this->createMock(ValidateHelper::class);
$this->signingRequestValidator = $this->createMock(SigningRequestValidator::class);
$this->signerValidator = $this->createMock(SignerValidator::class);
$this->signRequestMapper = $this->createMock(SignRequestMapper::class);
$this->certificateEngineFactory = $this->createMock(CertificateEngineFactory::class);
$this->fileMapper = $this->createMock(FileMapper::class);
Expand All @@ -114,7 +117,8 @@ public function getInjectionMiddleware(): InjectionMiddleware {
$this->request,
$this->session,
$this->userSession,
$this->validateHelper,
$this->signingRequestValidator,
$this->signerValidator,
$this->signRequestMapper,
$this->certificateEngineFactory,
$this->fileMapper,
Expand Down
12 changes: 8 additions & 4 deletions tests/php/Unit/Service/AccountServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Handler\SignEngine\Pkcs12Handler;
use OCA\Libresign\Helper\FileUploadHelper;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\Crl\CrlService;
use OCA\Libresign\Service\FolderService;
Expand All @@ -38,6 +37,8 @@
use OCA\Libresign\Service\RequestSignatureService;
use OCA\Libresign\Service\SignerElementsService;
use OCA\Libresign\Service\SignFileService;
use OCA\Libresign\Service\Validation\FileInputValidator;
use OCA\Libresign\Service\Validation\IdentityDocumentValidator;
use OCA\Settings\Mailer\NewUserMailHelper;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
Expand Down Expand Up @@ -80,7 +81,8 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
private NewUserMailHelper&MockObject $newUserMail;
private IdentifyMethodService&MockObject $identifyMethodService;
private IdentifyMethodMapper&MockObject $identifyMethodMapper;
private ValidateHelper&MockObject $validateHelper;
private IdentityDocumentValidator&MockObject $identityDocumentValidator;
private FileInputValidator&MockObject $fileInputValidator;
private IURLGenerator&MockObject $urlGenerator;
private IGroupManager&MockObject $groupManager;
private ISubAdmin&MockObject $subAdmin;
Expand Down Expand Up @@ -120,7 +122,8 @@ public function setUp(): void {
$this->newUserMail = $this->createMock(NewUserMailHelper::class);
$this->identifyMethodService = $this->createMock(IdentifyMethodService::class);
$this->identifyMethodMapper = $this->createMock(IdentifyMethodMapper::class);
$this->validateHelper = $this->createMock(ValidateHelper::class);
$this->identityDocumentValidator = $this->createMock(IdentityDocumentValidator::class);
$this->fileInputValidator = $this->createMock(FileInputValidator::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->pkcs12Handler = $this->createMock(Pkcs12Handler::class);
$this->groupManager = $this->createMock(IGroupManager::class);
Expand Down Expand Up @@ -158,7 +161,8 @@ private function getService(): AccountService {
$this->newUserMail,
$this->identifyMethodService,
$this->identifyMethodMapper,
$this->validateHelper,
$this->identityDocumentValidator,
$this->fileInputValidator,
$this->urlGenerator,
$this->pkcs12Handler,
$this->groupManager,
Expand Down
Loading
Loading