-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathSignFileService.php
More file actions
890 lines (786 loc) · 28.5 KB
/
SignFileService.php
File metadata and controls
890 lines (786 loc) · 28.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service;
use DateTime;
use DateTimeInterface;
use Exception;
use InvalidArgumentException;
use OC\AppFramework\Http as AppFrameworkHttp;
use OC\User\NoUserException;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\DataObjects\VisibleElementAssoc;
use OCA\Libresign\Db\File as FileEntity;
use OCA\Libresign\Db\FileElement;
use OCA\Libresign\Db\FileElementMapper;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\IdDocs;
use OCA\Libresign\Db\IdDocsMapper;
use OCA\Libresign\Db\IdentifyMethod;
use OCA\Libresign\Db\IdentifyMethodMapper;
use OCA\Libresign\Db\SignRequest as SignRequestEntity;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Db\UserElementMapper;
use OCA\Libresign\Events\SignedEventFactory;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\DocMdpHandler;
use OCA\Libresign\Handler\FooterHandler;
use OCA\Libresign\Handler\PdfTk\Pdf;
use OCA\Libresign\Handler\SignEngine\Pkcs12Handler;
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\IdentifyMethod\IIdentifyMethod;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\IToken;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\Http\Client\IClientService;
use OCP\IAppConfig;
use OCP\IDateTimeZone;
use OCP\IL10N;
use OCP\ITempManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Sabre\DAV\UUIDUtil;
class SignFileService {
private SignRequestEntity $signRequest;
private string $password = '';
private ?FileEntity $libreSignFile = null;
/** @var VisibleElementAssoc[] */
private $elements = [];
private bool $signWithoutPassword = false;
private ?File $fileToSign = null;
private string $userUniqueIdentifier = '';
private string $friendlyName = '';
private array $signers = [];
private ?IUser $user = null;
private ?SignEngineHandler $engine = null;
public function __construct(
protected IL10N $l10n,
private FileMapper $fileMapper,
private SignRequestMapper $signRequestMapper,
private IdDocsMapper $idDocsMapper,
private FooterHandler $footerHandler,
protected FolderService $folderService,
private IClientService $client,
private IUserManager $userManager,
protected LoggerInterface $logger,
private IAppConfig $appConfig,
protected ValidateHelper $validateHelper,
private SignerElementsService $signerElementsService,
private IRootFolder $root,
private IUserSession $userSession,
private IDateTimeZone $dateTimeZone,
private FileElementMapper $fileElementMapper,
private UserElementMapper $userElementMapper,
private IEventDispatcher $eventDispatcher,
protected ISecureRandom $secureRandom,
private IURLGenerator $urlGenerator,
private IdentifyMethodMapper $identifyMethodMapper,
private ITempManager $tempManager,
private IdentifyMethodService $identifyMethodService,
private ITimeFactory $timeFactory,
protected SignEngineFactory $signEngineFactory,
private SignedEventFactory $signedEventFactory,
private Pdf $pdf,
private DocMdpHandler $docMdpHandler,
private PdfSignatureDetectionService $pdfSignatureDetectionService,
private SequentialSigningService $sequentialSigningService,
) {
}
/**
* Can delete sing request
*/
public function canDeleteRequestSignature(array $data): void {
if (!empty($data['uuid'])) {
$signatures = $this->signRequestMapper->getByFileUuid($data['uuid']);
} elseif (!empty($data['file']['fileId'])) {
$signatures = $this->signRequestMapper->getByNodeId($data['file']['fileId']);
} else {
throw new \Exception($this->l10n->t('Please provide either UUID or File object'));
}
$signed = array_filter($signatures, fn ($s) => $s->getSigned());
if ($signed) {
throw new \Exception($this->l10n->t('Document already signed'));
}
array_walk($data['users'], function ($user) use ($signatures): void {
$exists = array_filter($signatures, function (SignRequestEntity $signRequest) use ($user) {
$identifyMethod = $this->identifyMethodService->getIdentifiedMethod($signRequest->getId());
if ($identifyMethod->getName() === 'email') {
return $identifyMethod->getEntity()->getIdentifierValue() === $user['email'];
}
return false;
});
if (!$exists) {
throw new \Exception($this->l10n->t('No signature was requested to %s', $user['email']));
}
});
}
public function notifyCallback(File $file): void {
$uri = $this->libreSignFile->getCallback();
if (!$uri) {
$uri = $this->appConfig->getValueString(Application::APP_ID, 'webhook_sign_url');
if (!$uri) {
return;
}
}
$options = [
'multipart' => [
[
'name' => 'uuid',
'contents' => $this->libreSignFile->getUuid(),
],
[
'name' => 'status',
'contents' => $this->libreSignFile->getStatus(),
],
[
'name' => 'file',
'contents' => $file->fopen('r'),
'filename' => $file->getName()
]
]
];
$this->client->newClient()->post($uri, $options);
}
/**
* @return static
*/
public function setLibreSignFile(FileEntity $libreSignFile): self {
$this->libreSignFile = $libreSignFile;
return $this;
}
public function setUserUniqueIdentifier(string $identifier): self {
$this->userUniqueIdentifier = $identifier;
return $this;
}
public function setFriendlyName(string $friendlyName): self {
$this->friendlyName = $friendlyName;
return $this;
}
/**
* @return static
*/
public function setSignRequest(SignRequestEntity $signRequest): self {
$this->signRequest = $signRequest;
return $this;
}
/**
* @return static
*/
public function setSignWithoutPassword(bool $signWithoutPassword = true): self {
$this->signWithoutPassword = $signWithoutPassword;
return $this;
}
/**
* @return static
*/
public function setPassword(?string $password = null): self {
$this->password = $password;
return $this;
}
public function setCurrentUser(?IUser $user): self {
$this->user = $user;
return $this;
}
public function setVisibleElements(array $list): self {
$fileElements = $this->fileElementMapper->getByFileIdAndSignRequestId($this->signRequest->getFileId(), $this->signRequest->getId());
$canCreateSignature = $this->signerElementsService->canCreateSignature();
foreach ($fileElements as $fileElement) {
$this->elements[] = $this->buildVisibleElementAssoc($fileElement, $list, $canCreateSignature);
}
return $this;
}
private function buildVisibleElementAssoc(FileElement $fileElement, array $list, bool $canCreateSignature): VisibleElementAssoc {
if (!$canCreateSignature) {
return new VisibleElementAssoc($fileElement);
}
$element = $this->array_find($list, fn (array $element): bool => ($element['documentElementId'] ?? '') === $fileElement->getId());
$nodeId = $this->getNodeId($element, $fileElement);
return $this->bindFileElementWithTempFile($fileElement, $nodeId);
}
private function getNodeId(?array $element, FileElement $fileElement): int {
if ($this->isValidElement($element)) {
return (int)$element['profileNodeId'];
}
return $this->retrieveUserElement($fileElement);
}
private function isValidElement(?array $element): bool {
if (is_array($element) && !empty($element['profileNodeId']) && is_int($element['profileNodeId'])) {
return true;
}
$this->logger->error('Invalid data provided for signing file.', ['element' => $element]);
throw new LibresignException($this->l10n->t('Invalid data to sign file'), 1);
}
private function retrieveUserElement(FileElement $fileElement): int {
try {
if (!$this->user instanceof IUser) {
throw new Exception('User not set');
}
$userElement = $this->userElementMapper->findOne([
'user_id' => $this->user->getUID(),
'type' => $fileElement->getType(),
]);
} catch (MultipleObjectsReturnedException|DoesNotExistException|Exception) {
throw new LibresignException($this->l10n->t('You need to define a visible signature or initials to sign this document.'));
}
return $userElement->getFileId();
}
private function bindFileElementWithTempFile(FileElement $fileElement, int $nodeId): VisibleElementAssoc {
try {
$node = $this->getNode($nodeId);
if (!$node) {
throw new \Exception('Node content is empty or unavailable.');
}
} catch (\Throwable) {
throw new LibresignException($this->l10n->t('You need to define a visible signature or initials to sign this document.'));
}
$tempFile = $this->tempManager->getTemporaryFile('_' . $nodeId . '.png');
$content = $node->getContent();
if (empty($content)) {
$this->logger->error('Failed to retrieve content for node.', ['nodeId' => $nodeId, 'fileElement' => $fileElement]);
throw new LibresignException($this->l10n->t('You need to define a visible signature or initials to sign this document.'));
}
file_put_contents($tempFile, $content);
return new VisibleElementAssoc($fileElement, $tempFile);
}
private function getNode(int $nodeId): ?File {
if ($this->user instanceof IUser) {
return $this->folderService->getFileById($nodeId);
}
$filesOfElementes = $this->signerElementsService->getElementsFromSession();
return $this->array_find($filesOfElementes, fn ($file) => $file->getId() === $nodeId);
}
/**
* Fallback to PHP < 8.4
*
* Reference: https://www.php.net/manual/en/function.array-find.php#130257
*
* @todo remove this after minor PHP version is >= 8.4
* @deprecated This method will be removed once the minimum PHP version is >= 8.4. Use native array_find instead.
*/
private function array_find(array $array, callable $callback): mixed {
foreach ($array as $key => $value) {
if ($callback($value, $key)) {
return $value;
}
}
return null;
}
/**
* @return VisibleElementAssoc[]
*/
public function getVisibleElements(): array {
return $this->elements;
}
public function sign(): File {
$this->validateDocMdpAllowsSignatures();
$signedFile = $this->getEngine()->sign();
$hash = $this->computeHash($signedFile);
$this->updateSignRequest($hash);
$this->updateLibreSignFile($hash);
$this->dispatchSignedEvent();
return $signedFile;
}
/**
* @throws LibresignException If the document has DocMDP level 1 (no changes allowed)
*/
protected function validateDocMdpAllowsSignatures(): void {
$resource = $this->getLibreSignFileAsResource();
try {
if (!$this->docMdpHandler->allowsAdditionalSignatures($resource)) {
throw new LibresignException(
$this->l10n->t('This document has been certified with no changes allowed, so no additional signatures can be added.'),
AppFrameworkHttp::STATUS_UNPROCESSABLE_ENTITY
);
}
} finally {
fclose($resource);
}
}
/**
* @return resource
* @throws LibresignException
*/
protected function getLibreSignFileAsResource() {
$fileToSign = $this->getNextcloudFile($this->libreSignFile);
$content = $fileToSign->getContent();
$resource = fopen('php://memory', 'r+');
if ($resource === false) {
throw new LibresignException('Failed to create temporary resource for PDF validation');
}
fwrite($resource, $content);
rewind($resource);
return $resource;
}
protected function computeHash(File $file): string {
return hash('sha256', $file->getContent());
}
protected function updateSignRequest(string $hash): void {
$lastSignedDate = $this->getEngine()->getLastSignedDate();
$this->signRequest->setSigned($lastSignedDate);
$this->signRequest->setSignedHash($hash);
$this->signRequest->setStatusEnum(\OCA\Libresign\Enum\SignRequestStatus::SIGNED);
$this->signRequestMapper->update($this->signRequest);
$this->sequentialSigningService->releaseNextOrder(
$this->signRequest->getFileId(),
$this->signRequest->getSigningOrder()
);
}
protected function updateLibreSignFile(string $hash): void {
$nodeId = $this->getEngine()->getInputFile()->getId();
$this->libreSignFile->setSignedNodeId($nodeId);
$this->libreSignFile->setSignedHash($hash);
$this->setNewStatusIfNecessary();
$this->fileMapper->update($this->libreSignFile);
}
protected function dispatchSignedEvent(): void {
$event = $this->signedEventFactory->make(
$this->signRequest,
$this->libreSignFile,
$this->getEngine()->getInputFile(),
);
$this->eventDispatcher->dispatchTyped($event);
}
protected function identifyEngine(File $file): SignEngineHandler {
return $this->signEngineFactory->resolve($file->getExtension());
}
protected function getSignatureParams(): array {
$certificateData = $this->readCertificate();
$signatureParams = $this->buildBaseSignatureParams($certificateData);
$signatureParams = $this->addEmailToSignatureParams($signatureParams, $certificateData);
$signatureParams = $this->addMetadataToSignatureParams($signatureParams);
return $signatureParams;
}
private function buildBaseSignatureParams(array $certificateData): array {
return [
'DocumentUUID' => $this->libreSignFile?->getUuid(),
'IssuerCommonName' => $certificateData['issuer']['CN'] ?? '',
'SignerCommonName' => $certificateData['subject']['CN'] ?? '',
'LocalSignerTimezone' => $this->dateTimeZone->getTimeZone()->getName(),
'LocalSignerSignatureDateTime' => (new DateTime('now', new \DateTimeZone('UTC')))
->format(DateTimeInterface::ATOM)
];
}
private function addEmailToSignatureParams(array $signatureParams, array $certificateData): array {
if (isset($certificateData['extensions']['subjectAltName'])) {
preg_match('/(?:email:)+(?<email>[^\s,]+)/', $certificateData['extensions']['subjectAltName'], $matches);
if ($matches && filter_var($matches['email'], FILTER_VALIDATE_EMAIL)) {
$signatureParams['SignerEmail'] = $matches['email'];
} elseif (filter_var($certificateData['extensions']['subjectAltName'], FILTER_VALIDATE_EMAIL)) {
$signatureParams['SignerEmail'] = $certificateData['extensions']['subjectAltName'];
}
}
if (empty($signatureParams['SignerEmail']) && $this->user instanceof IUser) {
$signatureParams['SignerEmail'] = $this->user->getEMailAddress();
}
if (empty($signatureParams['SignerEmail'])) {
$identifyMethod = $this->identifyMethodService->getIdentifiedMethod($this->signRequest->getId());
if ($identifyMethod->getName() === IdentifyMethodService::IDENTIFY_EMAIL) {
$signatureParams['SignerEmail'] = $identifyMethod->getEntity()->getIdentifierValue();
}
}
return $signatureParams;
}
private function addMetadataToSignatureParams(array $signatureParams): array {
$signRequestMetadata = $this->signRequest->getMetadata();
if (isset($signRequestMetadata['remote-address'])) {
$signatureParams['SignerIP'] = $signRequestMetadata['remote-address'];
}
if (isset($signRequestMetadata['user-agent'])) {
$signatureParams['SignerUserAgent'] = $signRequestMetadata['user-agent'];
}
return $signatureParams;
}
public function storeUserMetadata(array $metadata = []): self {
$collectMetadata = $this->appConfig->getValueBool(Application::APP_ID, 'collect_metadata', false);
if (!$collectMetadata || !$metadata) {
return $this;
}
$this->signRequest->setMetadata(array_merge(
$this->signRequest->getMetadata() ?? [],
$metadata,
));
$this->signRequestMapper->update($this->signRequest);
return $this;
}
/**
* @return SignRequestEntity[]
*/
protected function getSigners(): array {
if (empty($this->signers)) {
$this->signers = $this->signRequestMapper->getByFileId($this->signRequest->getFileId());
if ($this->signers) {
foreach ($this->signers as $key => $signer) {
if ($signer->getId() === $this->signRequest->getId()) {
$this->signers[$key] = $this->signRequest;
break;
}
}
}
}
return $this->signers;
}
protected function setNewStatusIfNecessary(): bool {
$newStatus = $this->evaluateStatusFromSigners();
if ($newStatus === null || $newStatus === $this->libreSignFile->getStatus()) {
return false;
}
$this->libreSignFile->setStatus($newStatus);
return true;
}
private function evaluateStatusFromSigners(): ?int {
$signers = $this->getSigners();
$total = count($signers);
if ($total === 0) {
return null;
}
$totalSigned = count(array_filter($signers, fn ($s) => $s->getSigned() !== null));
if ($totalSigned === $total) {
return FileEntity::STATUS_SIGNED;
}
if ($totalSigned > 0) {
return FileEntity::STATUS_PARTIAL_SIGNED;
}
return null;
}
private function getOrGeneratePfxContent(SignEngineHandler $engine): string {
if ($certificate = $engine->getCertificate()) {
return $certificate;
}
if ($this->signWithoutPassword) {
$tempPassword = $this->generateTemporaryPassword();
$this->setPassword($tempPassword);
$engine->generateCertificate(
[
'host' => $this->userUniqueIdentifier,
'uid' => $this->userUniqueIdentifier,
'name' => $this->friendlyName,
],
$tempPassword,
$this->friendlyName,
);
}
return $engine->getPfxOfCurrentSigner();
}
private function generateTemporaryPassword(): string {
$passwordEvent = new GenerateSecurePasswordEvent();
$this->eventDispatcher->dispatchTyped($passwordEvent);
return $passwordEvent->getPassword() ?? $this->secureRandom->generate(20);
}
protected function readCertificate(): array {
return $this->getEngine()
->readCertificate();
}
/**
* Get file to sign
*
* @throws LibresignException
*/
protected function getFileToSign(): File {
if ($this->fileToSign instanceof File) {
return $this->fileToSign;
}
$userId = $this->libreSignFile->getUserId();
$nodeId = $this->libreSignFile->getNodeId();
$originalFile = $this->root->getUserFolder($userId)->getFirstNodeById($nodeId);
if (!$originalFile instanceof File) {
throw new LibresignException($this->l10n->t('File not found'));
}
if ($this->isPdf($originalFile)) {
$this->fileToSign = $this->getPdfToSign($originalFile);
} else {
$this->fileToSign = $originalFile;
}
return $this->fileToSign;
}
private function isPdf(File $file): bool {
return strcasecmp($file->getExtension(), 'pdf') === 0;
}
protected function getEngine(): SignEngineHandler {
if (!$this->engine) {
$originalFile = $this->getFileToSign();
$this->engine = $this->identifyEngine($originalFile);
$this->configureEngine();
}
return $this->engine;
}
private function configureEngine(): void {
$this->engine
->setInputFile($this->getFileToSign())
->setCertificate($this->getOrGeneratePfxContent($this->engine))
->setPassword($this->password);
if ($this->engine::class === Pkcs12Handler::class) {
$this->engine
->setVisibleElements($this->getVisibleElements())
->setSignatureParams($this->getSignatureParams());
}
}
public function getLibresignFile(?int $nodeId, ?string $signRequestUuid = null): FileEntity {
try {
if ($nodeId) {
return $this->fileMapper->getByFileId($nodeId);
}
if ($signRequestUuid) {
$signRequest = $this->signRequestMapper->getByUuid($signRequestUuid);
return $this->fileMapper->getById($signRequest->getFileId());
}
throw new \Exception('Invalid arguments');
} catch (DoesNotExistException) {
throw new LibresignException($this->l10n->t('File not found'), 1);
}
}
public function renew(SignRequestEntity $signRequest, string $method): void {
$identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromSignRequestId($signRequest->getId());
if (empty($identifyMethods[$method])) {
throw new LibresignException($this->l10n->t('Invalid identification method'));
}
$signRequest->setUuid(UUIDUtil::getUUID());
$this->signRequestMapper->update($signRequest);
array_map(function (IIdentifyMethod $identifyMethod): void {
$entity = $identifyMethod->getEntity();
$entity->setAttempts($entity->getAttempts() + 1);
$entity->setLastAttemptDate($this->timeFactory->getDateTime());
$identifyMethod->save();
}, $identifyMethods[$method]);
}
public function requestCode(
SignRequestEntity $signRequest,
string $identifyMethodName,
string $signMethodName,
string $identify = '',
): void {
$identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromSignRequestId($signRequest->getId());
if (empty($identifyMethods[$identifyMethodName])) {
throw new LibresignException($this->l10n->t('Invalid identification method'));
}
foreach ($identifyMethods[$identifyMethodName] as $identifyMethod) {
try {
$signatureMethod = $identifyMethod->getEmptyInstanceOfSignatureMethodByName($signMethodName);
$signatureMethod->setEntity($identifyMethod->getEntity());
} catch (InvalidArgumentException) {
continue;
}
/** @var IToken $signatureMethod */
$identifier = $identify ?: $identifyMethod->getEntity()->getIdentifierValue();
$signatureMethod->requestCode($identifier, $identifyMethod->getEntity()->getIdentifierKey());
return;
}
throw new LibresignException($this->l10n->t('Sending authorization code not enabled.'));
}
public function getSignRequestToSign(FileEntity $libresignFile, ?string $signRequestUuid, ?IUser $user): SignRequestEntity {
$this->validateHelper->fileCanBeSigned($libresignFile);
try {
$signRequests = $this->signRequestMapper->getByFileId($libresignFile->getId());
if (!empty($signRequestUuid)) {
$signRequest = $this->getSignRequestByUuid($signRequestUuid);
} else {
$signRequest = array_reduce($signRequests, function (?SignRequestEntity $carry, SignRequestEntity $signRequest) use ($user): ?SignRequestEntity {
$identifyMethods = $this->identifyMethodMapper->getIdentifyMethodsFromSignRequestId($signRequest->getId());
$found = array_filter($identifyMethods, function (IdentifyMethod $identifyMethod) use ($user) {
if ($identifyMethod->getIdentifierKey() === IdentifyMethodService::IDENTIFY_EMAIL
&& $user
&& (
$identifyMethod->getIdentifierValue() === $user->getUID()
|| $identifyMethod->getIdentifierValue() === $user->getEMailAddress()
)
) {
return true;
}
if ($identifyMethod->getIdentifierKey() === IdentifyMethodService::IDENTIFY_ACCOUNT
&& $user
&& $identifyMethod->getIdentifierValue() === $user->getUID()
) {
return true;
}
return false;
});
if (count($found) > 0) {
return $signRequest;
}
return $carry;
});
}
if (!$signRequest) {
throw new DoesNotExistException('Sign request not found');
}
if ($signRequest->getSigned()) {
throw new LibresignException($this->l10n->t('File already signed by you'), 1);
}
return $signRequest;
} catch (DoesNotExistException) {
throw new LibresignException($this->l10n->t('Invalid data to sign file'), 1);
}
}
protected function getPdfToSign(File $originalFile): File {
$file = $this->getSignedFile();
if ($file instanceof File) {
return $file;
}
$originalContent = $originalFile->getContent();
if ($this->pdfSignatureDetectionService->hasSignatures($originalContent)) {
return $this->createSignedFile($originalFile, $originalContent);
}
$metadata = $this->footerHandler->getMetadata($originalFile, $this->libreSignFile);
$footer = $this->footerHandler
->setTemplateVar('uuid', $this->libreSignFile->getUuid())
->setTemplateVar('signers', array_map(fn (SignRequestEntity $signer) => [
'displayName' => $signer->getDisplayName(),
'signed' => $signer->getSigned()
? $signer->getSigned()->format(DateTimeInterface::ATOM)
: null,
], $this->getSigners()))
->getFooter($metadata['d']);
if ($footer) {
$stamp = $this->tempManager->getTemporaryFile('stamp.pdf');
file_put_contents($stamp, $footer);
$input = $this->tempManager->getTemporaryFile('input.pdf');
file_put_contents($input, $originalContent);
try {
$pdfContent = $this->pdf->applyStamp($input, $stamp);
} catch (RuntimeException $e) {
throw new LibresignException($e->getMessage());
}
} else {
$pdfContent = $originalContent;
}
return $this->createSignedFile($originalFile, $pdfContent);
}
protected function getSignedFile(): ?File {
$nodeId = $this->libreSignFile->getSignedNodeId();
if (!$nodeId) {
return null;
}
$fileToSign = $this->getNodeByIdUsingUid($this->libreSignFile->getUserId(), $nodeId);
if ($fileToSign->getOwner()->getUID() !== $this->libreSignFile->getUserId()) {
$fileToSign = $this->getNodeByIdUsingUid($fileToSign->getOwner()->getUID(), $nodeId);
}
return $fileToSign;
}
protected function getNodeByIdUsingUid(string $uid, int $nodeId): File {
try {
$fileToSign = $this->root->getUserFolder($uid)->getFirstNodeById($nodeId);
} catch (NoUserException) {
throw new LibresignException($this->l10n->t('User not found.'));
} catch (NotPermittedException) {
throw new LibresignException($this->l10n->t('You do not have permission for this action.'));
}
if (!$fileToSign instanceof File) {
throw new LibresignException($this->l10n->t('File not found'));
}
return $fileToSign;
}
private function createSignedFile(File $originalFile, string $content): File {
$filename = preg_replace(
'/' . $originalFile->getExtension() . '$/',
$this->l10n->t('signed') . '.' . $originalFile->getExtension(),
basename($originalFile->getPath())
);
$owner = $originalFile->getOwner()->getUID();
try {
/** @var \OCP\Files\Folder */
$parentFolder = $this->root->getUserFolder($owner)->getFirstNodeById($originalFile->getParentId());
return $parentFolder->newFile($filename, $content);
} catch (NotPermittedException) {
throw new LibresignException($this->l10n->t('You do not have permission for this action.'));
}
}
/**
* @throws DoesNotExistException
*/
public function getSignRequestByUuid(string $uuid): SignRequestEntity {
$this->validateHelper->validateUuidFormat($uuid);
return $this->signRequestMapper->getByUuid($uuid);
}
/**
* @throws DoesNotExistException
*/
public function getFile(int $signRequestId): FileEntity {
return $this->fileMapper->getById($signRequestId);
}
/**
* @throws DoesNotExistException
*/
public function getFileByUuid(string $uuid): FileEntity {
return $this->fileMapper->getByUuid($uuid);
}
public function getIdDocById(int $fileId): IdDocs {
return $this->idDocsMapper->getByFileId($fileId);
}
public function getNextcloudFile(FileEntity $fileData): File {
$fileToSign = $this->root->getUserFolder($fileData->getUserId())->getFirstNodeById($fileData->getNodeId());
if (!$fileToSign instanceof File) {
throw new LibresignException(json_encode([
'action' => JSActions::ACTION_DO_NOTHING,
'errors' => [['message' => $this->l10n->t('File not found')]],
]), AppFrameworkHttp::STATUS_NOT_FOUND);
}
return $fileToSign;
}
public function validateSigner(string $uuid, ?IUser $user = null): void {
$this->validateHelper->validateSigner($uuid, $user);
}
public function validateRenewSigner(string $uuid, ?IUser $user = null): void {
$this->validateHelper->validateRenewSigner($uuid, $user);
}
public function getSignerData(?IUser $user, ?SignRequestEntity $signRequest = null): array {
$return = ['user' => ['name' => null]];
if ($signRequest) {
$return['user']['name'] = $signRequest->getDisplayName();
} elseif ($user) {
$return['user']['name'] = $user->getDisplayName();
}
return $return;
}
public function getAvailableIdentifyMethodsFromSettings(): array {
$identifyMethods = $this->identifyMethodService->getIdentifyMethodsSettings();
$return = array_map(fn (array $identifyMethod): array => [
'mandatory' => $identifyMethod['mandatory'],
'identifiedAtDate' => null,
'validateCode' => false,
'method' => $identifyMethod['name'],
], $identifyMethods);
return $return;
}
/**
* @psalm-return array{file?: File, nodeId?: int, url?: string, base64?: string}
*/
public function getFileUrl(string $format, FileEntity $fileEntity, File $fileToSign, string $uuid): array {
$url = [];
switch ($format) {
case 'base64':
$url = ['base64' => base64_encode($fileToSign->getContent())];
break;
case 'url':
try {
$this->idDocsMapper->getByFileId($fileEntity->getId());
$url = ['url' => $this->urlGenerator->linkToRoute('libresign.page.getPdf', ['uuid' => $uuid])];
} catch (DoesNotExistException) {
$url = ['url' => $this->urlGenerator->linkToRoute('libresign.page.getPdfFile', ['uuid' => $uuid])];
}
break;
case 'nodeId':
$url = ['nodeId' => $fileToSign->getId()];
break;
case 'file':
$url = ['file' => $fileToSign];
break;
}
return $url;
}
}