From af9d7646f78488fee935a7d57ddb85d6edda311b Mon Sep 17 00:00:00 2001 From: zuzanna-maria Date: Mon, 11 Nov 2024 23:10:51 +0100 Subject: [PATCH] Moved FORM_FETCH_ANSWERS to sframe-common-outer.js --- www/common/make-backup.js | 21 +++++++++++--- www/common/sframe-common-outer.js | 47 +++++++++++++++++++++++-------- www/form/inner.js | 5 +++- www/form/main.js | 3 ++ 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/www/common/make-backup.js b/www/common/make-backup.js index 405427020..fe9ac110f 100644 --- a/www/common/make-backup.js +++ b/www/common/make-backup.js @@ -239,10 +239,23 @@ define([ transform(ctx, parsed.type, val, function (res) { if (ctx.stop) { return; } if (!res.data) { return void error('EEMPTY'); } - ctx.sframeChan.query("Q_FORM_FETCH_ANSWERS", JSON.parse(val)["answers"], function (err, obj) { - var answers = obj && obj.results; - console.log("ANSWERS", answers) - }); + var data = JSON.parse(val) + var _answers = data["answers"] + if (data.form) { + _answers['href'] = parsed.hash + _answers['password'] = fData.password + _answers['drive'] = true + var answers + ctx.sframeChan.query("Q_FORM_FETCH_ANSWERS", _answers, function (err, obj) { + answers = obj && obj.results; + console.log("ANSWERS", answers) + }); + // var opts = { + // binary: true, + // }; + // zip.file(fileName, answers, opts); + } + var fileName = getUnique(sanitize(rawName), res.ext, existingNames); existingNames.push(fileName.toLowerCase()); zip.file(fileName, res.data, opts); diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 3f6d9aeae..54ad6b92c 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -2433,6 +2433,7 @@ define([ }); sframeChan.on('Q_FORM_FETCH_ANSWERS', function (data, _cb) { + var formHref = data.href var cb = Utils.Util.once(_cb); var myKeys = {}; var myFormKeys; @@ -2489,14 +2490,18 @@ define([ if (myFormKeys.formSeed) { myFormKeys = getAnonymousKeys(myFormKeys.formSeed, data.channel); } - - var keys = Utils.secret && Utils.secret.keys; - - var formData = Utils.Hash.getFormData(Utils.secret); - console.log("SECRET", Utils.secret) - console.log("FORMDATA", formData) - privateKey = formData.form_private; - publicKey = formData.form_public; + var keys; + var privateKey, publicKey; + if (data.drive) { + var secret = Utils.Hash.getSecrets('form', formHref, data.password); + keys = secret && secret.keys; + var formData = Utils.Hash.getFormData(secret); + privateKey = formData.form_private; + publicKey = formData.form_public; + + } else { + keys = Utils.secret && Utils.secret.keys; + } var curvePrivate = privateKey || data.privateKey; if (!curvePrivate) { return void cb({error: 'EFORBIDDEN'}); } @@ -2506,10 +2511,6 @@ define([ validateKey: data.validateKey }); - console.log("PRIVATE", curvePrivate) - console.log("PUBLIC", publicKey, data.publicKey) - console.log("VALIDATE", data.validateKey) - var config = { network: network, channel: data.channel, @@ -2563,6 +2564,28 @@ define([ // If we have a "non-anonymous" answer, it may be the edition of a // previous anonymous answer. Check if a previous anonymous answer exists // with the same uid and delete it. + var u8_slice = function (A, start, end) { + return new Uint8Array(Array.prototype.slice.call(A, start, end)); + }; + var checkAnonProof = function (proofObj, channel, curvePrivate) { + var pub = proofObj.key; + var proofTxt = proofObj.proof; + try { + var u8_bundle = Nacl.util.decodeBase64(proofTxt); + var u8_nonce = u8_slice(u8_bundle, 0, Nacl.box.nonceLength); + var u8_cipher = u8_slice(u8_bundle, Nacl.box.nonceLength); + var u8_plain = Nacl.box.open( + u8_cipher, + u8_nonce, + Nacl.util.decodeBase64(pub), + Nacl.util.decodeBase64(curvePrivate) + ); + return channel === Nacl.util.encodeUTF8(u8_plain); + } catch (e) { + console.error(e); + return false; + } + }; if (parsed._proof) { var check = checkAnonProof(parsed._proof, data.channel, curvePrivate); var theirAnonKey = parsed._proof.key; diff --git a/www/form/inner.js b/www/form/inner.js index 3428d98b6..b14c932a3 100644 --- a/www/form/inner.js +++ b/www/form/inner.js @@ -50,6 +50,7 @@ define([ 'css!/lib/datepicker/flatpickr.min.css', 'css!/components/components-font-awesome/css/font-awesome.min.css', 'less!/form/app-form.less', + '/common/sframe-common-outer.js' ], function ( $, Sortify, @@ -78,7 +79,8 @@ define([ DatePicker, Share, Access, Properties, Flatpickr, - Sortable + Sortable, + SFCommonO ) { @@ -5375,6 +5377,7 @@ define([ } var getMyAnswers = APP.getMyAnswers = function () { + var sframeChan = framework._.sfCommon.getSframeChannel(); sframeChan.query("Q_FETCH_MY_ANSWERS", { channel: content.answers.channel, validateKey: content.answers.validateKey, diff --git a/www/form/main.js b/www/form/main.js index 5efc81aab..6d8a02bf7 100644 --- a/www/form/main.js +++ b/www/form/main.js @@ -129,6 +129,9 @@ define([ return false; } }; + var beep = function () { + + } var deleteLines = false; // "false" to support old forms sframeChan.on('Q_FORM_FETCH_ANSWERS', function (data, _cb) {