From f04026664a762f41a0999aee6dc4fe364ced999c Mon Sep 17 00:00:00 2001 From: Markin Igor <markin.io210@gmail.com> Date: Wed, 20 Feb 2019 15:54:42 +0300 Subject: [PATCH] Refactor signEmail penpal method to handle all requests errors. --- javascript/src/iframe/viamapi-iframe.js | 110 +++++++++--------------- javascript/src/utilities/appUtility.js | 8 ++ 2 files changed, 50 insertions(+), 68 deletions(-) diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js index 981fa97..2c83274 100644 --- a/javascript/src/iframe/viamapi-iframe.js +++ b/javascript/src/iframe/viamapi-iframe.js @@ -1,4 +1,4 @@ -import {createDeviceHash} from '../utilities/appUtility'; +import {createDeviceHash, encodeResponse} from '../utilities/appUtility'; import {LOGIN_MODES} from '../constants'; const libmime = require('libmime'); @@ -2008,80 +2008,54 @@ const connection = Penpal.connectToParent({ }) }); }, - signEmail(passportUUID, emailArg, emailMessage) { - return new Penpal.Promise(result => { - const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - if (authenticationPublicKey === null) { - return {"data" : "", - "code" : "400", - "status" : "Identity not authenticated" - } - } - if (window.loadedIdentities[authenticationPublicKey] === null) { - return {"data" : "", - "code" : "400", - "status" : "Identity not authenticated" - } - } + signEmail: async (passportUUID, emailArg, emailMessage) => { + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - var success = extendPinCodeTtl(authenticationPublicKey); + if ( + !authenticationPublicKey || + !window.loadedIdentities[authenticationPublicKey] || + !extendPinCodeTtl(authenticationPublicKey) + ) { + return encodeResponse("400", "", "Identity not authenticated"); + } - if(success === false) { - result({"data" : "", - "code" : "400", - "status" : "Identity not authenticated" - }) - } + let response = await getCertificateForPassport(passportUUID, true); - getCertificateForPassport(passportUUID, true).then(certificateResult => { - if(certificateResult.code === "200") { - var passportCertificate = certificateResult.data["x509Certificate"]; - var passportPrivateKey = certificateResult.data["privateKey"]; - var passportChain = certificateResult.data["chain"]; + if (response.code !== "200") { + return encodeResponse("400", "", response.status); + } - createOneTimePassportCertificate(makeid() + "-" + passportUUID, emailArg, passportPrivateKey, passportCertificate).then(function(keys){ - var publicKeyOneTime = keys["publicKeyPEM"]; - var privateKeyOneTime = keys["privateKeyPEM"]; - var certificateOneTime = keys["certificatePEM"]; - //download("certificateOneTime.crt", "text/plain", certificateOneTime) + const { + x509Certificate: passportCertificate, + privateKey: passportPrivateKey, + chain: passportChain + } = response.data; - passportChain.push(passportCertificate); + const keys = + await createOneTimePassportCertificate( + makeid() + "-" + passportUUID, emailArg, passportPrivateKey, passportCertificate); - executeRestfulFunction("private", viamApi, viamApi.passportGetEmailWithHeaderByPassport, - passportUUID, emailMessage).then(executeResult2 => { - var emailWithHeader = executeResult2.data; - //download("withheader.eml", "message/rfc822", emailWithHeader) - var signedEmailPromise = signEmail(emailWithHeader, - certificateOneTime, - passportChain, - privateKeyOneTime); - - signedEmailPromise.then(signedEmail => { - executeRestfulFunction("private", viamApi, viamApi.signResignEmail, - passportUUID, signedEmail).then(executeResult => { - result({"data" : executeResult.data, - "code" : "200", - "status" : "Email signed" - }) - }); - /*result({"data" : signedEmail, - "code" : "200", - "status" : "Email signed" - })*/ - }); - }); - // Prints PEM formatted signed certificate - // -----BEGIN CERTIFICATE-----MIID....7Hyg==-----END CERTIFICATE----- + const { privateKeyPEM: privateKeyOneTime, certificatePEM: certificateOneTime } = keys; - }); - } else { - result({"data" : "", - "code" : "400", - "status" : "Can not sign email" - }) - } - }) - }); + passportChain.push(passportCertificate); + + response = await executeRestfulFunction( + "private", window.viamApi, window.viamApi.passportGetEmailWithHeaderByPassport, passportUUID, emailMessage); + + if (response.code !== "200") { + return encodeResponse("400", "", response.status); + } + + const signedEmail = await signEmail(response.data, certificateOneTime, passportChain, privateKeyOneTime); + + response = await executeRestfulFunction( + "private", window.viamApi, window.viamApi.signResignEmail, passportUUID, signedEmail); + + if (response.code !== "200") { + return encodeResponse("400", "", response.status); + } + + return encodeResponse("200", response.data, "Email signed"); }, hasSession() { return new Penpal.Promise(result => { diff --git a/javascript/src/utilities/appUtility.js b/javascript/src/utilities/appUtility.js index 66cfaf8..deeb54f 100644 --- a/javascript/src/utilities/appUtility.js +++ b/javascript/src/utilities/appUtility.js @@ -11,3 +11,11 @@ export const createDeviceHash = async (publicKey) => { console.warn(error); // eslint-disable-line no-console } }; + +export const encodeResponse = (code, data, status) => { + return { + code, + data, + status + }; +}; \ No newline at end of file -- GitLab