Skip to content

Commit

Permalink
Merge pull request #2245 from LibreSign/feature/split-identify-and-si…
Browse files Browse the repository at this point in the history
…gn-methods

Split identify and sign methods
  • Loading branch information
vitormattos authored Feb 3, 2024
2 parents 3ba3197 + 7924fc2 commit a2f3012
Show file tree
Hide file tree
Showing 47 changed files with 1,345 additions and 1,219 deletions.
23 changes: 12 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions lib/Controller/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCA\Libresign\Service\AccountFileService;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\SessionService;
use OCA\Libresign\Service\SignerElementsService;
use OCA\Libresign\Service\SignFileService;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\ApiController;
Expand Down Expand Up @@ -68,6 +69,7 @@ public function __construct(
private AccountFileService $accountFileService,
private AccountFileMapper $accountFileMapper,
protected SignFileService $signFileService,
private SignerElementsService $signerElementsService,
private Pkcs12Handler $pkcs12Handler,
private Chain $loginChain,
private IURLGenerator $urlGenerator,
Expand Down Expand Up @@ -280,8 +282,8 @@ public function createSignatureElement(array $elements, string $uuid): JSONRespo
'elements' =>
(
$this->userSession->getUser() instanceof IUser
? $this->accountService->getUserElements($this->userSession->getUser()->getUID())
: $this->accountService->getElementsFromSession($this->sessionService->getSessionId())
? $this->signerElementsService->getUserElements($this->userSession->getUser()->getUID())
: $this->signerElementsService->getElementsFromSessionAsArray()
),
],
Http::STATUS_OK
Expand All @@ -298,8 +300,8 @@ public function getSignatureElements(): JSONResponse {
'elements' =>
(
$userId
? $this->accountService->getUserElements($userId)
: $this->accountService->getElementsFromSession($this->sessionService->getSessionId())
? $this->signerElementsService->getUserElements($userId)
: $this->signerElementsService->getElementsFromSessionAsArray()
)
],
Http::STATUS_OK
Expand Down Expand Up @@ -328,8 +330,8 @@ public function getSignatureElementPreview(int $fileId) {
}
$preview = $this->preview->getPreview(
file: $node,
width: AccountService::ELEMENT_SIGN_WIDTH,
height: AccountService::ELEMENT_SIGN_HEIGHT,
width: SignerElementsService::ELEMENT_SIGN_WIDTH,
height: SignerElementsService::ELEMENT_SIGN_HEIGHT,
);
$response = new FileDisplayResponse($preview, Http::STATUS_OK, [
'Content-Type' => $preview->getMimeType(),
Expand All @@ -343,7 +345,7 @@ public function getSignatureElement(int $elementId): JSONResponse {
$userId = $this->userSession->getUser()->getUID();
try {
return new JSONResponse(
$this->accountService->getUserElementByElementId($userId, $elementId),
$this->signerElementsService->getUserElementByElementId($userId, $elementId),
Http::STATUS_OK
);
} catch (\Throwable $th) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/FileElementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function delete(string $uuid, int $elementId): JSONResponse {
'uuid' => $uuid,
'userManager' => $this->userSession->getUser()
]);
$this->validateHelper->validateUserIsOwnerOfPdfVisibleElement($elementId, $this->userSession->getUser()->getUID());
$this->validateHelper->validateAuthenticatedUserIsOwnerOfPdfVisibleElement($elementId, $this->userSession->getUser()->getUID());
$this->fileElementService->deleteVisibleElement($elementId);
$return = [];
$statusCode = Http::STATUS_OK;
Expand Down
42 changes: 9 additions & 33 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
use OCA\Libresign\Middleware\Attribute\RequireSignRequestUuid;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\FileService;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\TokenService;
use OCA\Libresign\Service\IdentifyMethodService;
use OCA\Libresign\Service\RequestSignatureService;
use OCA\Libresign\Service\SessionService;
use OCA\Libresign\Service\SignatureMethodService;
use OCA\Libresign\Service\SignerElementsService;
use OCA\Libresign\Service\SignFileService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
Expand All @@ -56,7 +57,6 @@
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Util;
use Wobeto\EmailBlur\Blur;

class PageController extends AEnvironmentPageAwareController {
public function __construct(
Expand All @@ -67,9 +67,9 @@ public function __construct(
private AccountService $accountService,
protected SignFileService $signFileService,
protected RequestSignatureService $requestSignatureService,
private SignerElementsService $signerElementsService,
protected IL10N $l10n,
private IdentifyMethodService $identifyMethodService,
private SignatureMethodService $signatureMethodService,
private IAppConfig $appConfig,
private FileService $fileService,
private ValidateHelper $validateHelper,
Expand Down Expand Up @@ -148,9 +148,6 @@ public function sign($uuid): TemplateResponse {
$this->getSignRequestEntity(),
)
);
$this->initialState->provideInitialState('identifyMethods',
$this->signFileService->getAvailableIdentifyMethodsFromSignRequest($this->getSignRequestEntity())
);
$this->initialState->provideInitialState('filename', $this->getFileEntity()->getName());
$file = $this->fileService
->setFile($this->getFileEntity())
Expand All @@ -165,10 +162,9 @@ public function sign($uuid): TemplateResponse {
$this->initialState->provideInitialState('visibleElements', $file['visibleElements']);
$this->initialState->provideInitialState('signers', $file['signers']);
$this->provideSignerSignatues();
$signatureMethods = $this->signatureMethodService->getMethods();
$this->provideBlurredEmail($signatureMethods, $this->userSession->getUser()?->getEMailAddress());
$signatureMethods = $this->identifyMethodService->getSignMethodsOfIdentifiedFactors($this->getSignRequestEntity()->getId());
$this->initialState->provideInitialState('signature_methods', $signatureMethods);
$this->initialState->provideInitialState('token_length', SignatureMethodService::TOKEN_LENGTH);
$this->initialState->provideInitialState('token_length', TokenService::TOKEN_LENGTH);
$this->initialState->provideInitialState('description', $this->getSignRequestEntity()->getDescription() ?? '');
$this->initialState->provideInitialState('pdf',
$this->signFileService->getFileUrl('url', $this->getFileEntity(), $this->getNextcloudFile(), $uuid)
Expand All @@ -187,32 +183,13 @@ public function sign($uuid): TemplateResponse {
private function provideSignerSignatues(): void {
$signatures = [];
if ($this->userSession->getUser()) {
$signatures = $this->accountService->getUserElements($this->userSession->getUser()->getUID());
$signatures = $this->signerElementsService->getUserElements($this->userSession->getUser()->getUID());
} else {
$signatures = $this->accountService->getElementsFromSession($this->sessionService->getSessionId());
$signatures = $this->signerElementsService->getElementsFromSessionAsArray();
}
$this->initialState->provideInitialState('user_signatures', $signatures);
}

private function provideBlurredEmail(array $signatureMethods, ?string $email): void {
if (empty($email)) {
foreach ($signatureMethods as $id => $method) {
if ($id === IdentifyMethodService::IDENTIFY_EMAIL) {
$identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromSignRequestId($this->getSignRequestEntity()->getId());
if (isset($identifyMethods[IdentifyMethodService::IDENTIFY_EMAIL])) {
$method = current($identifyMethods[IdentifyMethodService::IDENTIFY_EMAIL]);
$email = $method->getEntity()->getIdentifierValue();
break;
}
}
}
}
if (!empty($email)) {
$blur = new Blur($email);
$this->initialState->provideInitialState('blurred_email', $blur->make());
}
}

/**
* Show signature page
*/
Expand Down Expand Up @@ -255,10 +232,9 @@ public function signAccountFile($uuid): TemplateResponse {
$this->initialState->provideInitialState('visibleElements', []);
$this->initialState->provideInitialState('signers', []);
$this->provideSignerSignatues();
$signatureMethods = $this->signatureMethodService->getMethods();
$this->provideBlurredEmail($signatureMethods, $this->userSession->getUser()?->getEMailAddress());
$signatureMethods = $this->identifyMethodService->getSignMethodsOfIdentifiedFactors($this->getSignRequestEntity()->getId());
$this->initialState->provideInitialState('signature_methods', $signatureMethods);
$this->initialState->provideInitialState('token_length', SignatureMethodService::TOKEN_LENGTH);
$this->initialState->provideInitialState('token_length', TokenService::TOKEN_LENGTH);
$this->initialState->provideInitialState('description', '');
$nextcloudFile = $this->signFileService->getNextcloudFile($fileEntity->getNodeId());
$this->initialState->provideInitialState('pdf',
Expand Down
19 changes: 12 additions & 7 deletions lib/Controller/SignFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use OCA\Libresign\Middleware\Attribute\RequireManager;
use OCA\Libresign\Middleware\Attribute\RequireSigner;
use OCA\Libresign\Service\FileService;
use OCA\Libresign\Service\SignatureMethodService;
use OCA\Libresign\Service\SignFileService;
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
use OCP\AppFramework\Http;
Expand All @@ -57,7 +56,6 @@ public function __construct(
protected IUserSession $userSession,
private ValidateHelper $validateHelper,
protected SignFileService $signFileService,
protected SignatureMethodService $signatureMethodService,
private FileService $fileService,
protected LoggerInterface $logger
) {
Expand All @@ -67,13 +65,15 @@ public function __construct(
#[NoAdminRequired]
#[NoCSRFRequired]
#[RequireManager]
#[PublicPage]
public function signUsingFileId(int $fileId, string $method, array $elements = [], string $identifyValue = '', string $token = ''): JSONResponse {
return $this->sign($fileId, null, $method, $elements, $identifyValue, $token);
}

#[NoAdminRequired]
#[NoCSRFRequired]
#[RequireSigner]
#[PublicPage]
public function signUsingUuid(string $uuid, string $method, array $elements = [], string $identifyValue = '', string $token = ''): JSONResponse {
return $this->sign(null, $uuid, $method, $elements, $identifyValue, $token);
}
Expand All @@ -83,16 +83,16 @@ public function sign(int $fileId = null, string $signRequestUuid = null, string
$user = $this->userSession->getUser();
$this->validateHelper->canSignWithIdentificationDocumentStatus(
$user,
$this->fileService->getIdentificationDocumentsStatus($user->getUID())
$this->fileService->getIdentificationDocumentsStatus($user?->getUID())
);
$libreSignFile = $this->signFileService->getLibresignFile($fileId, $signRequestUuid);
$signRequest = $this->signFileService->getSignRequestToSign($libreSignFile, $user);
$signRequest = $this->signFileService->getSignRequestToSign($libreSignFile, $signRequestUuid, $user);
$this->validateHelper->validateVisibleElementsRelation($elements, $signRequest, $user);
$this->validateHelper->validateCredentials($signRequest, $user, $method, $identifyValue, $token);
if ($method === 'password') {
$this->signFileService->setPassword($identifyValue);
} else {
$this->signFileService->setSignWithoutPassword(false);
$this->signFileService->setSignWithoutPassword(true);
}
$this->signFileService
->setLibreSignFile($libreSignFile)
Expand Down Expand Up @@ -170,17 +170,22 @@ public function signRenew(string $method): JSONResponse {
#[NoAdminRequired]
#[NoCSRFRequired]
#[RequireSigner]
#[PublicPage]
public function getCodeUsingUuid(string $uuid): JSONResponse {
return $this->getCode($uuid);
}

#[NoAdminRequired]
#[NoCSRFRequired]
#[RequireSigner]
#[PublicPage]
public function getCodeUsingFileId(int $fileId): JSONResponse {
return $this->getCode(null, $fileId);
}

/**
* @todo validate if can request code
*/
private function getCode(string $uuid = null, int $fileId = null): JSONResponse {
try {
try {
Expand All @@ -192,12 +197,12 @@ private function getCode(string $uuid = null, int $fileId = null): JSONResponse
} catch (\Throwable $th) {
throw new LibresignException($this->l10n->t('Invalid data to sign file'), 1);
}
$this->validateHelper->canRequestCode();
$libreSignFile = $this->fileMapper->getById($signRequest->getFileId());
$this->validateHelper->fileCanBeSigned($libreSignFile);
$this->signFileService->requestCode(
signRequest: $signRequest,
method: $this->request->getParam('method', ''),
identifyMethodName: $this->request->getParam('identifyMethod', ''),
signMethodName: $this->request->getParam('signMethod', ''),
identify: $this->request->getParam('identify', ''),
);
$message = $this->l10n->t('The code to sign file was successfully requested.');
Expand Down
20 changes: 4 additions & 16 deletions lib/DataObjects/VisibleElementAssoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,18 @@
namespace OCA\Libresign\DataObjects;

use OCA\Libresign\Db\FileElement;
use OCA\Libresign\Db\UserElement;

class VisibleElementAssoc {
/** @var FileElement */
private $fileElement;
/** @var UserElement */
private $userElement;
/** @var string */
private $tempFile;

public function __construct(FileElement $fileElement, UserElement $userElement, string $tempFile) {
$this->fileElement = $fileElement;
$this->userElement = $userElement;
$this->tempFile = $tempFile;
public function __construct(
private FileElement $fileElement,
private string $tempFile,
) {
}

public function getFileElement(): FileElement {
return $this->fileElement;
}

public function getUserElement(): UserElement {
return $this->userElement;
}

public function getTempFile(): string {
return $this->tempFile;
}
Expand Down
Loading

0 comments on commit a2f3012

Please sign in to comment.