From d985b144cc1ecfe4c90b79f10751ace2fe6c8ccb Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 25 Apr 2017 17:19:13 +0200 Subject: [PATCH] Add a new hash version for the file viewer --- .gitignore | 1 + server.js | 2 ++ www/common/common-hash.js | 26 +++++++++++++++++++------- www/common/cryptpad-common.js | 2 +- www/examples/file/main.js | 33 +++++++++++++++++++-------------- 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 996e55b97..76bc0ea38 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ www/scratch data npm-debug.log pins/ +blob/ diff --git a/server.js b/server.js index f12b90229..d7f5b90fc 100644 --- a/server.js +++ b/server.js @@ -82,6 +82,8 @@ var mainPages = config.mainPages || ['index', 'privacy', 'terms', 'about', 'cont var mainPagePattern = new RegExp('^\/(' + mainPages.join('|') + ').html$'); app.get(mainPagePattern, Express.static(__dirname + '/customize.dist')); +app.use("/blob", Express.static(__dirname + '/blob')); + app.use("/customize", Express.static(__dirname + '/customize')); app.use("/customize", Express.static(__dirname + '/customize.dist')); app.use(/^\/[^\/]*$/, Express.static('customize')); diff --git a/www/common/common-hash.js b/www/common/common-hash.js index 40fe6bc7b..12f23c0a7 100644 --- a/www/common/common-hash.js +++ b/www/common/common-hash.js @@ -23,13 +23,16 @@ define([ return chanKey + keys; } if (!keys.editKeyStr) { return; } - return '/1/edit/' + hexToBase64(chanKey) + '/' + Crypto.b64RemoveSlashes(keys.editKeyStr); + return '/1/edit/' + hexToBase64(chanKey) + '/'+Crypto.b64RemoveSlashes(keys.editKeyStr)+'/'; }; var getViewHashFromKeys = Hash.getViewHashFromKeys = function (chanKey, keys) { if (typeof keys === 'string') { return; } - return '/1/view/' + hexToBase64(chanKey) + '/' + Crypto.b64RemoveSlashes(keys.viewKeyStr); + return '/1/view/' + hexToBase64(chanKey) + '/'+Crypto.b64RemoveSlashes(keys.viewKeyStr)+'/'; + }; + var getFileHashFromKey = Hash.getFileHashFromKey = function (fileKey, cryptKey, type) { + return '/2/' + hexToBase64(fileKey) + '/' + Crypto.b64RemoveSlashes(cryptKey) + '/' + Crypto.base64RemoveSlashes(type); }; var parsePadUrl = Hash.parsePadUrl = function (href) { @@ -119,7 +122,9 @@ define([ } } else if (version === "2") { // version 2 hashes are to be used for encrypted blobs - // TODO + var fileId = secret.file = hashArray[2].replace(/-/g, '/'); + var key = secret.key = hashArray[3].replace(/-/g, '/'); + var type = secret.type = hashArray[4].replace(/-/g, '/'); } } } @@ -150,7 +155,7 @@ define([ var channelId = Util.hexToBase64(createChannelId()); // 18 byte encryption key var key = Crypto.b64RemoveSlashes(Crypto.rand64(18)); - return '/1/edit/' + [channelId, key].join('/'); + return '/1/edit/' + [channelId, key].join('/') + '/'; }; /* @@ -159,8 +164,8 @@ Version 0 Version 1 /code/#/1/edit/3Ujt4F2Sjnjbis6CoYWpoQ/usn4+9CqVja8Q7RZOGTfRgqI Version 2 - /file//#/2// - /file//#/2/ajExFODrFH4lVBwxxsrOKw/pdf + /file/#/2/// + /file/#/2/K6xWU-LT9BJHCQcDCT-DcQ/ajExFODrFH4lVBwxxsrOKw/image-png */ var parseHash = Hash.parseHash = function (hash) { var parsed = {}; @@ -177,7 +182,14 @@ Version 2 parsed.mode = hashArr[2]; parsed.channel = hashArr[3]; parsed.key = hashArr[4]; - parsed.present = hashArr[5] && hashArr[5] === 'present'; + parsed.present = typeof(hashArr[5]) === "string" && hashArr[5] === 'present'; + return parsed; + } + if (hashArr[1] && hashArr[1] === '2') { + parsed.version = 2; + parsed.file = hashArr[2].replace(/-/g, '/'); + parsed.key = hashArr[3].replace(/-/g, '/'); + parsed.type = hashArr[4].replace(/-/g, '/'); return parsed; } return; diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 1d5015497..757b04e15 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -514,7 +514,7 @@ define([ if (p.type !== parsed.type) { return pad; } - var shouldUpdate = p.hash === parsed.hash; + var shouldUpdate = p.hash.replace(/\/$/, '') === parsed.hash.replace(/\/$/, ''); // Version 1 : we have up to 4 differents hash for 1 pad, keep the strongest : // Edit > Edit (present) > View > View (present) diff --git a/www/examples/file/main.js b/www/examples/file/main.js index 9a0d142fc..4db3eb8ba 100644 --- a/www/examples/file/main.js +++ b/www/examples/file/main.js @@ -20,25 +20,30 @@ define([ var andThen = function () { var $bar = $iframe.find('.toolbar-container'); var secret = Cryptpad.getSecrets(); - var readOnly = secret.keys && !secret.keys.editKeyStr; - if (!secret.keys) { - secret.keys = secret.key; - } + + if (secret.keys) { throw new Error("You need a hash"); } // TODO + + var cryptKey = secret.key; + var fileId = secret.file; + var hexFileName = Cryptpad.base64ToHex(fileId); + var type = secret.type; + +// Test hash: +// #/2/K6xWU-LT9BJHCQcDCT-DcQ/TBo77200c0e-FdldQFcnQx4Y/image-png var $mt = $iframe.find('#encryptedFile'); - $mt.attr('src', './assets/image.png-encrypted'); - $mt.attr('data-crypto-key', 'TBo77200c0e/FdldQFcnQx4Y'); - $mt.attr('data-type', 'image/png'); + $mt.attr('src', '/blob/' + hexFileName.slice(0,2) + '/' + hexFileName); + $mt.attr('data-crypto-key', cryptKey); + $mt.attr('data-type', type); require(['/common/media-tag.js'], function (MediaTag) { MediaTag($mt[0]); Cryptpad.removeLoadingScreen(); - var configTb = { - displayed: ['useradmin', 'newpad'], - ifrw: ifrw, - common: Cryptpad - }; - toolbar = Toolbar.create($bar, null, null, null, null, configTb); - + var configTb = { + displayed: ['useradmin', 'newpad'], + ifrw: ifrw, + common: Cryptpad + }; + Toolbar.create($bar, null, null, null, null, configTb); }); };