Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature sign using uuuid #41

Merged
merged 22 commits into from
Feb 15, 2021
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ Nextcloud app to sign PDF documents

## Setup

### Java and JSignPDF

Add the follow to Nextcloud PHP container Dockerfile

```Dockerfile
# Install Java and JsignPDF
RUN mkdir -p /usr/share/man/man1
RUN apt-get install -y default-jre
RUN curl -OL https://sourceforge.net/projects/jsignpdf/files/stable/JSignPdf%201.6.4/JSignPdf-1.6.4.zip \
&& unzip JSignPdf-1.6.4.zip -d /opt \
&& rm JSignPdf-1.6.4.zip
```

### With CFSS server

Up a cfssl server using this code:
Expand Down
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'verb' => 'OPTIONS', 'requirements' => ['path' => '.+'], ],
['name' => 'webhook#register', 'url' => '/api/0.1/webhook/register', 'verb' => 'POST'],
['name' => 'libresign#sign', 'url' => '/api/0.1/sign', 'verb' => 'POST'],
['name' => 'libresign#signUsingUuid', 'url' => '/api/0.1/sign/{uuid}', 'verb' => 'POST'],
['name' => 'account#createToSign', 'url' => '/api/0.1/account/create/{uuid}', 'verb' => 'POST'],
['name' => 'signature#generate', 'url' => '/api/0.1/signature/generate', 'verb' => 'POST'],
['name' => 'signature#hasRootCert', 'url' => '/api/0.1/signature/has-root-cert', 'verb' => 'GET'],
Expand Down
23 changes: 13 additions & 10 deletions lib/Controller/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace OCA\Libresign\Controller;

use OC\Files\Filesystem;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\FileUser;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Service\AccountService;
use OCP\AppFramework\ApiController;
Expand Down Expand Up @@ -42,6 +42,7 @@ public function __construct(
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
* @PublicPage
* @return JSONResponse
*/
public function createToSign(string $uuid, string $email, string $password, string $signPassword) {
Expand All @@ -56,6 +57,7 @@ public function createToSign(string $uuid, string $email, string $password, stri
$this->account->createToSign($uuid, $email, $password, $signPassword);
$fileUser = $this->account->getFileUserByUuid($uuid);
$fileData = $this->fileMapper->getById($fileUser->getLibresignFileId());
Filesystem::initMountPoints($fileData->getUserId());
$fileToSign = $this->root->getById($fileData->getFileId());
if (count($fileToSign) < 1) {
return new JSONResponse(
Expand All @@ -67,6 +69,15 @@ public function createToSign(string $uuid, string $email, string $password, stri
);
}
$fileToSign = $fileToSign[0];
$data = [
'message' => $this->l10n->t('Success'),
'action' => JSActions::ACTION_SIGN,
'pdf' => [
'base64' => base64_encode($fileToSign->getContent())
],
'filename' => $fileData->getName(),
'description' => $fileData->getDescription()
];
} catch (\Throwable $th) {
return new JSONResponse(
[
Expand All @@ -77,15 +88,7 @@ public function createToSign(string $uuid, string $email, string $password, stri
);
}
return new JSONResponse(
[
'message' => $this->l10n->t('Success'),
'action' => JSActions::ACTION_SIGN,
'pdf' => [
'base64' => $fileToSign->getContent()
],
'filename' => $fileData->getName(),
'description' => $fileData->getDescription()
],
$data,
Http::STATUS_OK
);
}
Expand Down
98 changes: 94 additions & 4 deletions lib/Controller/LibresignController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

namespace OCA\Libresign\Controller;

use OC\Files\Filesystem;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\JLibresignHandler;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\LibresignService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\IRootFolder;
use OCP\IL10N;
use OCP\IRequest;

class LibresignController extends Controller {
Expand All @@ -15,16 +25,40 @@ class LibresignController extends Controller {
/** @var LibresignService */
private $service;

/** @var FileUserMapper */
private $fileUserMapper;
/** @var FileMapper */
private $fileMapper;
/** @var IRootFolder */
private $root;
/** @var IL10N */
private $l10n;
/** @var AccountService */
private $account;
/** @var JLibresignHandler */
private $libresignHandler;
/** @var string */
private $userId;

public function __construct(
IRequest $request,
LibresignService $service,
FileUserMapper $fileUserMapper,
FileMapper $fileMapper,
IRootFolder $root,
IL10N $l10n,
AccountService $account,
JLibresignHandler $libresignHandler,
$userId
) {
parent::__construct(Application::APP_ID, $request);
$this->service = $service;
$this->fileUserMapper = $fileUserMapper;
$this->fileMapper = $fileMapper;
$this->root = $root;
$this->l10n = $l10n;
$this->account = $account;
$this->libresignHandler = $libresignHandler;
$this->userId = $userId;
}

Expand All @@ -39,7 +73,7 @@ public function sign(
string $outputFolderPath = null,
string $certificatePath = null,
string $password = null
): DataResponse {
): JSONResponse {
try {
$this->checkParams([
'inputFilePath' => $inputFilePath,
Expand All @@ -50,9 +84,65 @@ public function sign(

$fileSigned = $this->service->sign($inputFilePath, $outputFolderPath, $certificatePath, $password);

return new DataResponse(['fileSigned' => $fileSigned->getInternalPath()]);
return new JSONResponse(
['fileSigned' => $fileSigned->getInternalPath()],
HTTP::STATUS_OK
);
} catch (\Exception $exception) {
return $this->handleErrors($exception);
return new JSONResponse(
[
'action' => JSActions::ACTION_DO_NOTHING,
'errors' => [$this->l10n->t($exception->getMessage())]
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function signUsingUuid(string $uuid, string $password): JSONResponse {
try {
$fileUser = $this->fileUserMapper->getByUuidAndUserId($uuid, $this->userId);
$fileData = $this->fileMapper->getById($fileUser->getLibresignFileId());
Filesystem::initMountPoints($fileData->getuserId());
$inputFile = $this->root->getById($fileData->getFileId());
if (count($inputFile) < 1) {
throw new LibresignException($this->l10n->t('File not found'));
}
$inputFile = $inputFile[0];
$signedFilePath = preg_replace(
'/' . $inputFile->getExtension() . '$/',
$this->l10n->t('signed').'.'.$inputFile->getExtension(),
$inputFile->getPath()
);
if ($this->root->nodeExists($signedFilePath)) {
$signedFile = $this->root->get($signedFilePath);
$inputFile = $signedFilePath;
}
$certificatePath = $this->account->getPfx($fileUser->getUserId());
list(, $signedContent) = $this->libresignHandler->signExistingFile($inputFile, $certificatePath, $password);
if (!$signedFile) {
$signedFile = $this->root->newFile($signedFilePath);
}
$signedFile->putContent($signedContent);
return new JSONResponse(
[
'action' => JSActions::ACTION_DO_NOTHING,
'message' => $this->l10n->t('File signed')
],
Http::STATUS_OK
);
} catch (\Throwable $th) {
return new JSONResponse(
[
'action' => JSActions::ACTION_DO_NOTHING,
'errors' => [$this->l10n->t('Invalid data to sign file')]
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
}
}
2 changes: 2 additions & 0 deletions lib/Db/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* @method int getId()
* @method void setFileId(int $fileId)
* @method int getFileId()
* @method void setUserId(int $userId)
* @method int getUserId()
* @method void setCreatedAt(string $createdAt)
* @method string getCreatedAt()
* @method void setDescription(string $description)
Expand Down
15 changes: 15 additions & 0 deletions lib/Db/FileUserMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,19 @@ public function getByUuid(string $uuid) {

return $this->findEntity($qb);
}

public function getByUuidAndUserId(string $uuid, string $userId) {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('uuid', $qb->createNamedParameter($uuid, IQueryBuilder::PARAM_STR))
)
->andWhere(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
);

return $this->findEntity($qb);
}
}
2 changes: 2 additions & 0 deletions lib/Handler/JLibresignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function signExistingFile(
->setPdf($inputFile->getContent())
->setPassword($password)
->setTempPath('/tmp/')
->setIsUseJavaInstalled(true)
->setjSignPdfJarPath('/opt/jsignpdf-1.6.4/JSignPdf.jar')
;

$jSignPdf = new JSignPDF($param);
Expand Down
24 changes: 15 additions & 9 deletions lib/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class AccountService {
private $fileUserMapper;
/** @var IUserManager */
protected $userManager;
/** @var SignatureService */
private $signature;
/** @var FolderService */
private $folder;
/** @var IConfig */
Expand All @@ -32,12 +30,13 @@ class AccountService {
private $newUserMail;
/** @var CfsslHandler */
private $cfsslHandler;
/** @var string */
private $pdfFilename = 'signature.pfx';

public function __construct(
IL10N $l10n,
FileUserMapper $fileUserMapper,
IUserManager $userManager,
SignatureService $signature,
FolderService $folder,
IConfig $config,
NewUserMailHelper $newUserMail,
Expand All @@ -46,7 +45,6 @@ public function __construct(
$this->l10n = $l10n;
$this->fileUserMapper = $fileUserMapper;
$this->userManager = $userManager;
$this->signature = $signature;
$this->folder = $folder;
$this->config = $config;
$this->newUserMail = $newUserMail;
Expand Down Expand Up @@ -127,17 +125,25 @@ public function createToSign($uuid, $uid, $password, $signPassword) {
private function savePfx($uid, $content) {
Filesystem::initMountPoints($uid);
$folder = $this->folder->getFolderForUser();
$filename = 'signature.pfx';
if ($folder->nodeExists($filename)) {
$node = $folder->get($filename);
if ($folder->nodeExists($this->pdfFilename)) {
$node = $folder->get($this->pdfFilename);
if (!$node instanceof File) {
throw new LibresignException("path {$filename} already exists and is not a file!", 400);
throw new LibresignException("path {$this->pdfFilename} already exists and is not a file!", 400);
}
$node->putContent($content);
return $node;
}

$file = $folder->newFile($filename);
$file = $folder->newFile($this->pdfFilename);
$file->putContent($content);
}

public function getPfx($uid) {
Filesystem::initMountPoints($uid);
$folder = $this->folder->getFolderForUser();
if (!$folder->nodeExists($this->pdfFilename)) {
throw new LibresignException("Signature file not found!", 400);
}
return $folder->get($this->pdfFilename);
}
}
2 changes: 1 addition & 1 deletion lib/Service/FolderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
IRootFolder $root,
IConfig $config,
IL10N $l10n,
string $userId
?string $userId
) {
$this->root = $root;
$this->config = $config;
Expand Down
Loading