Commit 0f3f9fa1 authored by Sven Franck's avatar Sven Franck

jslint pass cryptstorage.js

parent 59d01abf
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, sjcl: true, $: true, setTimeout: true */
jIO.addStorageType('crypt', function (spec, my) { jIO.addStorageType('crypt', function (spec, my) {
spec = spec || {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {}; var that = my.basicStorage(spec, my),
priv = {},
var is_valid_storage = (spec.storage?true:false); is_valid_storage = (spec.storage ? true : false),
super_serialized = that.serialized;
priv.username = spec.username || ''; priv.username = spec.username || '';
priv.password = spec.password || ''; priv.password = spec.password || '';
priv.sub_storage_spec = spec.storage || {type:'base'}; priv.sub_storage_spec = spec.storage || {
priv.sub_storage_string = JSON.stringify (priv.sub_storage_string); type: 'base'
};
priv.sub_storage_string = JSON.stringify(priv.sub_storage_string);
var super_serialized = that.serialized;
that.serialized = function () { that.serialized = function () {
var o = super_serialized(); var o = super_serialized();
o.username = priv.username; o.username = priv.username;
...@@ -28,76 +32,78 @@ jIO.addStorageType('crypt', function (spec, my) { ...@@ -28,76 +32,78 @@ jIO.addStorageType('crypt', function (spec, my) {
// TODO : IT IS NOT SECURE AT ALL! // TODO : IT IS NOT SECURE AT ALL!
// WE MUST REWORK CRYPTED STORAGE! // WE MUST REWORK CRYPTED STORAGE!
priv.encrypt_param_object = { priv.encrypt_param_object = {
"iv":"kaprWwY/Ucr7pumXoTHbpA", "iv": "kaprWwY/Ucr7pumXoTHbpA",
"v":1, "v": 1,
"iter":1000, "iter": 1000,
"ks":256, "ks": 256,
"ts":128, "ts": 128,
"mode":"ccm", "mode": "ccm",
"adata":"", "adata": "",
"cipher":"aes", "cipher": "aes",
"salt":"K4bmZG9d704" "salt": "K4bmZG9d704"
}; };
priv.decrypt_param_object = { priv.decrypt_param_object = {
"iv":"kaprWwY/Ucr7pumXoTHbpA", "iv": "kaprWwY/Ucr7pumXoTHbpA",
"ks":256, "ks": 256,
"ts":128, "ts": 128,
"salt":"K4bmZG9d704" "salt": "K4bmZG9d704"
}; };
priv.encrypt = function (data,callback) { priv.encrypt = function (data, callback) {
// end with a callback in order to improve encrypt to an // end with a callback in order to improve encrypt to an
// asynchronous encryption. // asynchronous encryption.
var tmp = sjcl.encrypt (priv.username+':'+ var tmp = sjcl.encrypt(priv.username + ':' + priv.password, data,
priv.password, data,
priv.encrypt_param_object); priv.encrypt_param_object);
callback(JSON.parse(tmp).ct); callback(JSON.parse(tmp).ct);
}; };
priv.decrypt = function (data,callback) { priv.decrypt = function (data, callback) {
var tmp, param = $.extend(true,{},priv.decrypt_param_object); var tmp, param = $.extend(true, {}, priv.decrypt_param_object);
param.ct = data || ''; param.ct = data || '';
param = JSON.stringify (param); param = JSON.stringify(param);
try { try {
tmp = sjcl.decrypt (priv.username+':'+ tmp = sjcl.decrypt(priv.username + ':' + priv.password, param);
priv.password,
param);
} catch (e) { } catch (e) {
callback({status:403,statusText:'Forbidden',error:'forbidden', callback({
message:'Unable to decrypt.',reason:'unable to decrypt'}); status: 403,
statusText: 'Forbidden',
error: 'forbidden',
message: 'Unable to decrypt.',
reason: 'unable to decrypt'
});
return; return;
} }
callback(undefined,tmp); callback(undefined, tmp);
}; };
priv.newAsyncModule = function () { priv.newAsyncModule = function () {
var async = {}; var async = {};
async.call = function (obj,function_name,arglist) { async.call = function (obj, function_name, arglist) {
obj._wait = obj._wait || {}; obj._wait = obj._wait || {};
if (obj._wait[function_name]) { if (obj._wait[function_name]) {
obj._wait[function_name]--; obj._wait[function_name] -= 1;
return function () {}; return function () {};
} }
// ok if undef or 0 // ok if undef or 0
arglist = arglist || []; arglist = arglist || [];
setTimeout(function (){ setTimeout(function () {
obj[function_name].apply(obj[function_name],arglist); obj[function_name].apply(obj[function_name], arglist);
}); });
}; };
async.neverCall = function (obj,function_name) { async.neverCall = function (obj, function_name) {
obj._wait = obj._wait || {}; obj._wait = obj._wait || {};
obj._wait[function_name] = -1; obj._wait[function_name] = -1;
}; };
async.wait = function (obj,function_name,times) { async.wait = function (obj, function_name, times) {
obj._wait = obj._wait || {}; obj._wait = obj._wait || {};
obj._wait[function_name] = times; obj._wait[function_name] = times;
}; };
async.end = function () { async.end = function () {
async.call = function(){}; async.call = function () {};
}; };
return async; return async;
}; };
that.post = function (command) { that.post = function (command) {
that.put (command); that.put(command);
}; };
/** /**
...@@ -105,36 +111,38 @@ jIO.addStorageType('crypt', function (spec, my) { ...@@ -105,36 +111,38 @@ jIO.addStorageType('crypt', function (spec, my) {
* @method put * @method put
*/ */
that.put = function (command) { that.put = function (command) {
var new_file_name, new_file_content, am = priv.newAsyncModule(), o = {}; var new_file_name, new_file_content, am = priv.newAsyncModule(),
o = {};
o.encryptFilePath = function () { o.encryptFilePath = function () {
priv.encrypt(command.getDocId(),function(res) { priv.encrypt(command.getDocId(), function (res) {
new_file_name = res; new_file_name = res;
am.call(o,'save'); am.call(o, 'save');
}); });
}; };
o.encryptFileContent = function () { o.encryptFileContent = function () {
priv.encrypt(command.getDocContent(),function(res) { priv.encrypt(command.getDocContent(), function (res) {
new_file_content = res; new_file_content = res;
am.call(o,'save'); am.call(o, 'save');
}); });
}; };
o.save = function () { o.save = function () {
var success = function (val) { var success = function (val) {
val.id = command.getDocId(); val.id = command.getDocId();
that.success (val); that.success(val);
}, },
error = function (err) { error = function (err) {
that.error (err); that.error(err);
}, },
cloned_doc = command.cloneDoc(); cloned_doc = command.cloneDoc();
cloned_doc._id = new_file_name; cloned_doc._id = new_file_name;
cloned_doc.content = new_file_content; cloned_doc.content = new_file_content;
that.addJob ('put',priv.sub_storage_spec,cloned_doc, that.addJob('put', priv.sub_storage_spec, cloned_doc,
command.cloneOption(),success,error); command.cloneOption(), success, error);
}; };
am.wait(o,'save',1); am.wait(o, 'save', 1);
am.call(o,'encryptFilePath'); am.call(o, 'encryptFilePath');
am.call(o,'encryptFileContent'); am.call(o, 'encryptFileContent');
}; // end put }; // end put
/** /**
...@@ -142,28 +150,29 @@ jIO.addStorageType('crypt', function (spec, my) { ...@@ -142,28 +150,29 @@ jIO.addStorageType('crypt', function (spec, my) {
* @method get * @method get
*/ */
that.get = function (command) { that.get = function (command) {
var new_file_name, option, am = priv.newAsyncModule(), o = {}; var new_file_name, am = priv.newAsyncModule(),
o = {};
o.encryptFilePath = function () { o.encryptFilePath = function () {
priv.encrypt(command.getDocId(),function(res) { priv.encrypt(command.getDocId(), function (res) {
new_file_name = res; new_file_name = res;
am.call(o,'get'); am.call(o, 'get');
}); });
}; };
o.get = function () { o.get = function () {
that.addJob('get',priv.sub_storage_spec,new_file_name, that.addJob('get', priv.sub_storage_spec, new_file_name,
command.cloneOption(),o.success,o.error); command.cloneOption(), o.success, o.error);
}; };
o.success = function (val) { o.success = function (val) {
val._id = command.getDocId(); val._id = command.getDocId();
if (command.getOption('metadata_only')) { if (command.getOption('metadata_only')) {
that.success (val); that.success(val);
} else { } else {
priv.decrypt (val.content, function(err,res){ priv.decrypt(val.content, function (err, res) {
if (err) { if (err) {
that.error(err); that.error(err);
} else { } else {
val.content = res; val.content = res;
that.success (val); that.success(val);
} }
}); });
} }
...@@ -171,7 +180,7 @@ jIO.addStorageType('crypt', function (spec, my) { ...@@ -171,7 +180,7 @@ jIO.addStorageType('crypt', function (spec, my) {
o.error = function (error) { o.error = function (error) {
that.error(error); that.error(error);
}; };
am.call(o,'encryptFilePath'); am.call(o, 'encryptFilePath');
}; // end get }; // end get
/** /**
...@@ -179,57 +188,64 @@ jIO.addStorageType('crypt', function (spec, my) { ...@@ -179,57 +188,64 @@ jIO.addStorageType('crypt', function (spec, my) {
* @method allDocs * @method allDocs
*/ */
that.allDocs = function (command) { that.allDocs = function (command) {
var result_array = [], am = priv.newAsyncModule(), o = {}; var result_array = [],
am = priv.newAsyncModule(),
o = {};
o.allDocs = function () { o.allDocs = function () {
that.addJob ('allDocs', priv.sub_storage_spec, null, that.addJob('allDocs', priv.sub_storage_spec, null,
command.cloneOption(), o.onSuccess, o.error); command.cloneOption(), o.onSuccess, o.error);
}; };
o.onSuccess = function (val) { o.onSuccess = function (val) {
if (val.total_rows === 0) { if (val.total_rows === 0) {
return am.call(o,'success'); return am.call(o, 'success');
} }
result_array = val.rows; result_array = val.rows;
var i, decrypt = function (c) { var i, decrypt = function (c) {
priv.decrypt (result_array[c].id,function (err,res) { priv.decrypt(result_array[c].id, function (err, res) {
if (err) { if (err) {
am.call(o,'error',[err]); am.call(o, 'error', [err]);
} else { } else {
result_array[c].id = res; result_array[c].id = res;
result_array[c].key = res; result_array[c].key = res;
am.call(o,'success'); am.call(o, 'success');
} }
}); });
if (!command.getOption('metadata_only')) { if (!command.getOption('metadata_only')) {
priv.decrypt ( priv.decrypt(
result_array[c].value.content, result_array[c].value.content,
function (err,res) {
function (err, res) {
if (err) { if (err) {
am.call(o,'error',[err]); am.call(o, 'error', [err]);
} else { } else {
result_array[c].value.content = res; result_array[c].value.content = res;
am.call(o,'success'); am.call(o, 'success');
} }
}); }
);
} }
}; };
if (command.getOption('metadata_only')) { if (command.getOption('metadata_only')) {
am.wait(o,'success',val.total_rows*1-1); am.wait(o, 'success', val.total_rows - 1);
} else { } else {
am.wait(o,'success',val.total_rows*2-1); am.wait(o, 'success', val.total_rows * 2 - 1);
} }
for (i = 0; i < result_array.length; i+= 1) { for (i = 0; i < result_array.length; i += 1) {
decrypt(i); decrypt(i);
} }
}; };
o.error = function (error) { o.error = function (error) {
am.end(); am.end();
that.error (error); that.error(error);
}; };
o.success = function () { o.success = function () {
am.end(); am.end();
that.success ({total_rows:result_array.length,rows:result_array}); that.success({
total_rows: result_array.length,
rows: result_array
});
}; };
am.call(o,'allDocs'); am.call(o, 'allDocs');
}; // end allDocs }; // end allDocs
/** /**
...@@ -239,7 +255,7 @@ jIO.addStorageType('crypt', function (spec, my) { ...@@ -239,7 +255,7 @@ jIO.addStorageType('crypt', function (spec, my) {
that.remove = function (command) { that.remove = function (command) {
var new_file_name, o = {}; var new_file_name, o = {};
o.encryptDocId = function () { o.encryptDocId = function () {
priv.encrypt(command.getDocId(),function(res) { priv.encrypt(command.getDocId(), function (res) {
new_file_name = res; new_file_name = res;
o.removeDocument(); o.removeDocument();
}); });
...@@ -247,15 +263,15 @@ jIO.addStorageType('crypt', function (spec, my) { ...@@ -247,15 +263,15 @@ jIO.addStorageType('crypt', function (spec, my) {
o.removeDocument = function () { o.removeDocument = function () {
var cloned_doc = command.cloneDoc(); var cloned_doc = command.cloneDoc();
cloned_doc._id = new_file_name; cloned_doc._id = new_file_name;
that.addJob ('remove', priv.sub_storage_spec, cloned_doc, that.addJob('remove', priv.sub_storage_spec, cloned_doc,
command.cloneOption(), o.success, that.error); command.cloneOption(), o.success, that.error);
}; };
o.success = function (val) { o.success = function (val) {
val.id = command.getDocId(); val.id = command.getDocId();
that.success (val); that.success(val);
}; };
o.encryptDocId(); o.encryptDocId();
}; // end remove }; // end remove
return that; return that;
}; });
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment