Skip to content
Snippets Groups Projects

269 implement workflow for signing already uploaded document dev

Files
8
import { parseSMIME } from '../utilities/emailUtilities';
import { parseSMIME } from '../utilities/emailUtilities';
 
import {
 
stringToUtf8ByteArray,
 
utf8ByteArrayToString,
 
stringToUtf8Base64,
 
utf8Base64ToString,
 
base64ToByteArray,
 
byteArrayToBase64
 
} from '../utilities/stringUtilities';
const QRCode = require('qrcode');
const QRCode = require('qrcode');
const Penpal = require('penpal').default;
const Penpal = require('penpal').default;
@@ -16,6 +24,9 @@ import {
@@ -16,6 +24,9 @@ import {
decryptMessage,
decryptMessage,
encryptMessage, signEmail
encryptMessage, signEmail
} from '../utilities/signingUtilities';
} from '../utilities/signingUtilities';
 
import {
 
signPdf
 
} from '../utilities/pdfUtilities';
import CryptoData from '../CryptoData';
import CryptoData from '../CryptoData';
import Identity from '../Identity';
import Identity from '../Identity';
import {STATUS_DEVICE_REVOKED} from '../constants/statuses';
import {STATUS_DEVICE_REVOKED} from '../constants/statuses';
@@ -248,7 +259,23 @@ const handleIdentityLogin = (identity, uuid, token) => {
@@ -248,7 +259,23 @@ const handleIdentityLogin = (identity, uuid, token) => {
async function executeRestfulFunction(type, that, fn, config, ...args) {
async function executeRestfulFunction(type, that, fn, config, ...args) {
const { currentlyAuthenticatedIdentity, viamApi, currentlyLoadedIdentity } = window;
const { currentlyAuthenticatedIdentity, viamApi, currentlyLoadedIdentity } = window;
const response = await fn.apply(that, [config, ...args]);
let response;
 
try {
 
response = await fn.apply(that, [config, ...args]);
 
} catch (error) {
 
if (error.response) {
 
//Resposnse with status code != 2xx still has valid response
 
response = error.response;
 
} else {
 
//Connection error or similar
 
const data = {
 
"data": "",
 
"code": "999",
 
"status": error.message
 
};
 
return data;
 
}
 
}
const identity = currentlyAuthenticatedIdentity || currentlyLoadedIdentity;
const identity = currentlyAuthenticatedIdentity || currentlyLoadedIdentity;
const { code, status } = response.data;
const { code, status } = response.data;
@@ -272,8 +299,12 @@ async function executeRestfulFunction(type, that, fn, config, ...args) {
@@ -272,8 +299,12 @@ async function executeRestfulFunction(type, that, fn, config, ...args) {
const uuid = loginResponse.data.data["Uuid"];
const uuid = loginResponse.data.data["Uuid"];
const token = loginResponse.data.data["Session"];
const token = loginResponse.data.data["Session"];
handleIdentityLogin(identity, uuid, token);
handleIdentityLogin(identity, uuid, token);
const { data } = await fn.apply(that, [config, ...args]);
try {
return data;
response = await fn.apply(that, [config, ...args]);
 
} catch (error) {
 
response = error.response;
 
}
 
return response.data;
}
}
window.executeRestfulFunction = executeRestfulFunction;
window.executeRestfulFunction = executeRestfulFunction;
@@ -929,7 +960,82 @@ const connection = Penpal.connectToParent({
@@ -929,7 +960,82 @@ const connection = Penpal.connectToParent({
return encodeResponse("200", response.data, "Email signed");
return encodeResponse("200", response.data, "Email signed");
},
},
documentCreateDocument: async (path, passportUUID, contenttype) => {
signDocument: async (passportUUID, documentUUID, documentContentType) => {
 
 
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
 
 
if (
 
!authenticationPublicKey ||
 
!window.loadedIdentities[authenticationPublicKey] ||
 
!extendPinCodeTtl(authenticationPublicKey)
 
) {
 
return encodeResponse("400", "", "Identity not authenticated");
 
}
 
 
const certResponse = await getCertificateForPassport(passportUUID, true);
 
 
if (certResponse.code !== "200") {
 
return encodeResponse("400", "", certResponse.status);
 
}
 
 
const {
 
x509Certificate: passportCertificate,
 
privateKey: passportPrivateKey,
 
chain: passportChain
 
} = certResponse.data;
 
 
const keys =
 
await createOneTimePassportCertificate(
 
makeid() + "-" + passportUUID, null, passportPrivateKey, passportCertificate);
 
 
const { privateKeyPEM: privateKeyOneTime, certificatePEM: certificateOneTime } = keys;
 
 
passportChain.push(passportCertificate);
 
 
const pdfContentType = 'application/pdf';
 
 
if (documentContentType !== pdfContentType) {
 
const convResponse = await executeRestfulFunction(
 
"private", window.viamApi, window.viamApi.documentConvertDocumentByUUID, null, documentUUID, documentContentType, pdfContentType);
 
if (convResponse.code !== "200") {
 
return encodeResponse("400", "", convResponse.status);
 
}
 
}
 
 
const downloadResponse = await executeRestfulFunction(
 
"private", window.viamApi, window.viamApi.documentGetDocumentByUUID, null, documentUUID, pdfContentType);
 
 
if (downloadResponse.code !== "200") {
 
return encodeResponse("400", "", downloadResponse.status);
 
}
 
 
const pdfRaw = base64ToByteArray(downloadResponse.data);
 
 
let signedPdf;
 
try {
 
signedPdf = await signPdf(pdfRaw, certificateOneTime, passportChain, privateKeyOneTime);
 
} catch (err) {
 
console.error(err);
 
return encodeResponse("500", "", err.message);
 
}
 
 
const signedPdfB64 = byteArrayToBase64(signedPdf);
 
 
const uploadResponse = await executeRestfulFunction(
 
"private", window.viamApi, window.viamApi.documentPutDocumentByUUID, null, documentUUID, pdfContentType, signedPdfB64);
 
if (uploadResponse.code !== "200") {
 
return encodeResponse("400", "", uploadResponse.status);
 
}
 
 
const signResponse = await executeRestfulFunction(
 
"private", window.viamApi, window.viamApi.documentSignDocumentByUUID, null, passportUUID, documentUUID, pdfContentType);
 
if (signResponse.code !== "200") {
 
return encodeResponse("400", "", signResponse.status);
 
}
 
 
return encodeResponse("200", "", "Document signed");
 
},
 
documentCreateDocument: async (passportUUID, path, contentType, title) => {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (
if (
!authenticationPublicKey ||
!authenticationPublicKey ||
@@ -943,7 +1049,8 @@ const connection = Penpal.connectToParent({
@@ -943,7 +1049,8 @@ const connection = Penpal.connectToParent({
headers: {
headers: {
path,
path,
passportuuid: passportUUID,
passportuuid: passportUUID,
contenttype
contentType,
 
title
}
}
};
};
const response = await executeRestfulFunction("private", window.viamApi, window.viamApi.documentCreateDocument,
const response = await executeRestfulFunction("private", window.viamApi, window.viamApi.documentCreateDocument,
@@ -951,7 +1058,7 @@ const connection = Penpal.connectToParent({
@@ -951,7 +1058,7 @@ const connection = Penpal.connectToParent({
return encodeResponse("200", response.data, "Document created");
return encodeResponse("200", response.data, "Document created");
},
},
documentPutDocument: async (passportUUID, resourceid, file) => {
documentPutDocument: async (passportUUID, resourceid, contentType, file) => {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (
if (
!authenticationPublicKey ||
!authenticationPublicKey ||
@@ -965,14 +1072,15 @@ const connection = Penpal.connectToParent({
@@ -965,14 +1072,15 @@ const connection = Penpal.connectToParent({
headers: {
headers: {
'Content-Type': 'multipart/form-data',
'Content-Type': 'multipart/form-data',
passportuuid: passportUUID,
passportuuid: passportUUID,
resourceid
resourceid,
 
contentType
}
}
};
};
const response = await executeRestfulFunction(
const response = await executeRestfulFunction(
"private", window.viamApi, window.viamApi.documentPutDocument, config, file);
"private", window.viamApi, window.viamApi.documentPutDocument, config, file);
return encodeResponse("200", response.data, "Document created");
return encodeResponse("200", response.data, "Document stored");
},
},
hasSession() {
hasSession() {
return new Penpal.Promise(result => {
return new Penpal.Promise(result => {
@@ -1052,64 +1160,34 @@ const connection = Penpal.connectToParent({
@@ -1052,64 +1160,34 @@ const connection = Penpal.connectToParent({
);
);
},
},
stringToUtf8ByteArray(str) {
stringToUtf8ByteArray(str) {
if (typeof str !== 'string') {
str = str.toString()
}
let res = Buffer.from(str,'utf-8');
return new Penpal.Promise(result => {
return new Penpal.Promise(result => {
result(res)
result(stringToUtf8ByteArray(str));
})
});
},
},
utf8ByteArrayToString(ba) {
utf8ByteArrayToString(ba) {
if (!Buffer.isBuffer(ba)) {
ba = Buffer.from(ba)
}
let res = ba.toString('utf-8');
return new Penpal.Promise(result => {
return new Penpal.Promise(result => {
result(res)
result(utf8ByteArrayToString(ba));
})
});
},
},
stringToUtf8Base64(str) {
stringToUtf8Base64(str) {
if (!Buffer.isBuffer(str)) {
if (typeof str !== 'string') {
str = str.toString()
}
str = Buffer.from(str, 'utf-8')
}
let res = str.toString('base64');
return new Penpal.Promise(result => {
return new Penpal.Promise(result => {
result(res)
result(stringToUtf8Base64(str));
})
});
},
},
utf8Base64ToString(strBase64) {
utf8Base64ToString(strBase64) {
if (!Buffer.isBuffer(strBase64)) {
if (typeof strBase64 !== 'string') {
strBase64 = strBase64.toString()
}
strBase64 = Buffer.from(strBase64, 'base64')
}
let res = strBase64.toString('utf-8');
return new Penpal.Promise(result => {
return new Penpal.Promise(result => {
result(res)
result(utf8Base64ToString(strBase64));
})
});
},
},
base64ToByteArray(strBase64) {
base64ToByteArray(strBase64) {
if (typeof strBase64 !== 'string') {
strBase64 = strBase64.toString()
}
let res = Buffer.from(strBase64, 'base64');
return new Penpal.Promise(result => {
return new Penpal.Promise(result => {
result(res)
result(base64ToByteArray(strBase64));
})
});
},
},
byteArrayToBase64(ba) {
byteArrayToBase64(ba) {
if (!Buffer.isBuffer(ba)) {
ba = Buffer.from(ba)
}
let res = ba.toString('base64');
return new Penpal.Promise(result => {
return new Penpal.Promise(result => {
result(res)
result(byteArrayToBase64(ba));
})
});
},
},
// Collabora APIs
// Collabora APIs
@@ -1118,7 +1196,7 @@ const connection = Penpal.connectToParent({
@@ -1118,7 +1196,7 @@ const connection = Penpal.connectToParent({
},
},
// WOPI
// WOPI
getPassports: async fileId => {
getPassports: async (resourceID, contentType) => {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (
if (
@@ -1129,10 +1207,36 @@ const connection = Penpal.connectToParent({
@@ -1129,10 +1207,36 @@ const connection = Penpal.connectToParent({
return encodeResponse("400", "", "Identity not authenticated");
return encodeResponse("400", "", "Identity not authenticated");
}
}
const response = await wopiAPI.getPassports(fileId);
const response = await wopiAPI.getPassports(resourceID, contentType);
return response.data;
return response.data;
},
},
 
wopiCreateDocument: async (passportUUID, path, contentType, title) => {
 
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
 
if (
 
!authenticationPublicKey ||
 
!window.loadedIdentities[authenticationPublicKey] ||
 
!extendPinCodeTtl(authenticationPublicKey)
 
) {
 
return encodeResponse("400", "", "Identity not authenticated");
 
}
 
 
const config = {
 
headers: {
 
path,
 
passportuuid: passportUUID,
 
contentType,
 
title
 
}
 
};
 
const executeResult = await executeRestfulFunction("private", window.viamApi, window.viamApi.documentCreateDocument,
 
config);
 
if (executeResult.code !== "200") return executeResult;
 
const resourceID = executeResult.data;
 
const passports = await wopiAPI.getPassports(resourceID, contentType);
 
return passports;
 
},
 
wopiPutFile: async (path, accessToken, file) => {
wopiPutFile: async (path, accessToken, file) => {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
Loading