mirror of
https://github.com/24eme/signaturepdf
synced 2024-06-03 06:22:13 +02:00
key and hash created in the javascript
This commit is contained in:
parent
12043408cc
commit
47ef75562f
12
app.php
12
app.php
|
@ -195,7 +195,7 @@ require_once 'lib/cryptography.class.php';
|
||||||
|
|
||||||
$f3->route('POST /share',
|
$f3->route('POST /share',
|
||||||
function($f3) {
|
function($f3) {
|
||||||
$hash = substr(hash('sha512', uniqid().rand()), 0, 20);
|
$hash = Web::instance()->slug($_POST['hash']);
|
||||||
$sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash;
|
$sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash;
|
||||||
$f3->set('UPLOADS', $sharingFolder."/");
|
$f3->set('UPLOADS', $sharingFolder."/");
|
||||||
if (!is_dir($f3->get('PDF_STORAGE_PATH'))) {
|
if (!is_dir($f3->get('PDF_STORAGE_PATH'))) {
|
||||||
|
@ -240,10 +240,9 @@ $f3->route('POST /share',
|
||||||
if(!$f3->get('DEBUG')) {
|
if(!$f3->get('DEBUG')) {
|
||||||
array_map('cryptographyClass::hardUnlink', glob($tmpfile."*.svg"));
|
array_map('cryptographyClass::hardUnlink', glob($tmpfile."*.svg"));
|
||||||
}
|
}
|
||||||
$symmetricKey = CryptographyClass::createSymmetricKey();
|
|
||||||
setcookie($hash, $symmetricKey, ['expires' => 0, 'samesite' => 'Strict', 'path' => "/"]);
|
|
||||||
|
|
||||||
$encryptor = new CryptographyClass($symmetricKey, $f3->get('PDF_STORAGE_PATH').$hash);
|
$symmetricKey = $_COOKIE[$hash];
|
||||||
|
$encryptor = new CryptographyClass($_COOKIE[$hash], $f3->get('PDF_STORAGE_PATH').$hash);
|
||||||
$encryptor->encrypt();
|
$encryptor->encrypt();
|
||||||
|
|
||||||
|
|
||||||
|
@ -258,11 +257,10 @@ $f3->route('GET /signature/@hash/pdf',
|
||||||
$hash = Web::instance()->slug($f3->get('PARAMS.hash'));
|
$hash = Web::instance()->slug($f3->get('PARAMS.hash'));
|
||||||
$sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash;
|
$sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash;
|
||||||
|
|
||||||
if (CryptographyClass::isSymmetricKeyValid($_COOKIE[$hash]) == false) {
|
$cryptor = new CryptographyClass(CryptographyClass::protectSymmetricKey($_COOKIE[$hash]), $f3->get('PDF_STORAGE_PATH').$hash);
|
||||||
|
if ($cryptor->decrypt() == false) {
|
||||||
$f3->error(403);
|
$f3->error(403);
|
||||||
}
|
}
|
||||||
$cryptor = new CryptographyClass($_COOKIE[$hash], $f3->get('PDF_STORAGE_PATH').$hash);
|
|
||||||
$cryptor->decrypt();
|
|
||||||
|
|
||||||
$files = scandir($sharingFolder);
|
$files = scandir($sharingFolder);
|
||||||
$originalFile = $sharingFolder.'/original.pdf';
|
$originalFile = $sharingFolder.'/original.pdf';
|
||||||
|
|
|
@ -42,7 +42,7 @@ class CryptographyClass
|
||||||
$result = shell_exec($command);
|
$result = shell_exec($command);
|
||||||
if ($result === false) {
|
if ($result === false) {
|
||||||
echo "Decypher failure";
|
echo "Decypher failure";
|
||||||
exit;
|
return $result;
|
||||||
}
|
}
|
||||||
$this->hardUnlink($file);
|
$this->hardUnlink($file);
|
||||||
}
|
}
|
||||||
|
@ -53,14 +53,13 @@ class CryptographyClass
|
||||||
if (!$element) {
|
if (!$element) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
print_r(['hu', $element]);
|
|
||||||
$eraser = str_repeat(0, strlen(file_get_contents($element)));
|
$eraser = str_repeat(0, strlen(file_get_contents($element)));
|
||||||
file_put_contents($element, $eraser);
|
file_put_contents($element, $eraser);
|
||||||
unlink($element);
|
unlink($element);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isSymmetricKeyValid($key) {
|
public static function protectSymmetricKey($key) {
|
||||||
return (bool)preg_match('/^[0-9a-zA-Z]{15}$/', $key);
|
return preg_replace('/[^0-9a-zA-Z]*/', '', $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function createSymmetricKey() {
|
public static function createSymmetricKey() {
|
||||||
|
|
|
@ -818,6 +818,10 @@ var createEventsListener = function() {
|
||||||
}
|
}
|
||||||
document.getElementById('input_svg_share').files = dataTransfer.files;
|
document.getElementById('input_svg_share').files = dataTransfer.files;
|
||||||
hasModifications = false;
|
hasModifications = false;
|
||||||
|
|
||||||
|
document.getElementById('input_pdf_hash').value = generatePdfHash();
|
||||||
|
document.getElementById('input_symmetric_key').value = generateSymmetricKey();
|
||||||
|
storeSymmetricKeyCookie(document.getElementById('input_pdf_hash').value, document.getElementById('input_symmetric_key').value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1111,7 +1115,6 @@ var pageSignature = async function(url) {
|
||||||
let filename = url.replace('/pdf/', '');
|
let filename = url.replace('/pdf/', '');
|
||||||
|
|
||||||
if(pdfHash) {
|
if(pdfHash) {
|
||||||
storeSymmetricKeyCookie();
|
|
||||||
let response = await fetch(url);
|
let response = await fetch(url);
|
||||||
if(response.status != 200) {
|
if(response.status != 200) {
|
||||||
return;
|
return;
|
||||||
|
@ -1165,13 +1168,36 @@ var pageSignature = async function(url) {
|
||||||
})
|
})
|
||||||
})();
|
})();
|
||||||
|
|
||||||
function storeSymmetricKeyCookie() {
|
function storeSymmetricKeyCookie(hash, symmetricKey) {
|
||||||
let symmetricKey = window.location.hash;
|
if (symmetricKey.length != 15) {
|
||||||
if (symmetricKey.length != 19) {
|
|
||||||
console.error("Erreur taille cle symmetrique.");
|
console.error("Erreur taille cle symmetrique.");
|
||||||
return;
|
return;
|
||||||
} else if (symmetricKey.substr(0, 4) != "#sk:") {
|
|
||||||
console.error("Erreur format cle symmetrique");
|
|
||||||
}
|
}
|
||||||
document.cookie = pdfHash + "=" + symmetricKey.substr(4, 15) + "; SameSite=Strict";
|
document.cookie = hash + "=" + symmetricKey + "; SameSite=Strict";
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateSymmetricKey() {
|
||||||
|
const length = 15;
|
||||||
|
const keySpace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||||
|
let key = '';
|
||||||
|
|
||||||
|
for (let i = 0; i < length; ++i) {
|
||||||
|
const randomIndex = Math.floor(Math.random() * keySpace.length);
|
||||||
|
key += keySpace.charAt(randomIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
function generatePdfHash() {
|
||||||
|
const length = 20;
|
||||||
|
const keySpace = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||||
|
let key = '';
|
||||||
|
|
||||||
|
for (let i = 0; i < length; ++i) {
|
||||||
|
const randomIndex = Math.floor(Math.random() * keySpace.length);
|
||||||
|
key += keySpace.charAt(randomIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return key;
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,7 +112,7 @@
|
||||||
</div>
|
</div>
|
||||||
<form id="form_pdf" action="<?php echo $REVERSE_PROXY_URL; ?>/signature/<?php echo $hash ?>/save" method="post" enctype="multipart/form-data" class="d-none d-sm-none d-md-block">
|
<form id="form_pdf" action="<?php echo $REVERSE_PROXY_URL; ?>/signature/<?php echo $hash ?>/save" method="post" enctype="multipart/form-data" class="d-none d-sm-none d-md-block">
|
||||||
<input id="input_svg" name="svg[]" type="file" class="d-none" />
|
<input id="input_svg" name="svg[]" type="file" class="d-none" />
|
||||||
<!-- <input id="symmetric_key" name="key" type="hidden" value="test" /> -->
|
|
||||||
<button class="btn btn-primary w-100 mt-2" disabled="disabled" type="submit" id="save"><i class="bi bi-cloud-upload"></i> <?php echo _("Transmit my signature"); ?></button>
|
<button class="btn btn-primary w-100 mt-2" disabled="disabled" type="submit" id="save"><i class="bi bi-cloud-upload"></i> <?php echo _("Transmit my signature"); ?></button>
|
||||||
</form>
|
</form>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
@ -198,7 +198,8 @@
|
||||||
<form id="form_sharing" clas action="<?php echo $REVERSE_PROXY_URL; ?>/share" method="post" enctype="multipart/form-data">
|
<form id="form_sharing" clas action="<?php echo $REVERSE_PROXY_URL; ?>/share" method="post" enctype="multipart/form-data">
|
||||||
<input id="input_pdf_share" name="pdf" type="file" class="d-none" />
|
<input id="input_pdf_share" name="pdf" type="file" class="d-none" />
|
||||||
<input id="input_svg_share" name="svg[]" type="file" class="d-none" />
|
<input id="input_svg_share" name="svg[]" type="file" class="d-none" />
|
||||||
<!-- <input id="symmetric_key" name="key" type="hidden" value="test" /> -->
|
<input id="input_pdf_hash" name="hash" type="hidden" value="" />
|
||||||
|
<input id="input_symmetric_key" name="key" type="hidden" value="" />
|
||||||
<button class="btn col-9 col-md-6 btn-primary" type="submit" id="save_share"><?php echo sprintf(_("%s Start sharing"), '<i class="bi bi-cloud-upload"></i>'); ?></button>
|
<button class="btn col-9 col-md-6 btn-primary" type="submit" id="save_share"><?php echo sprintf(_("%s Start sharing"), '<i class="bi bi-cloud-upload"></i>'); ?></button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue