From 363e1685618b1bb05802a221021549eb0ee7a3bd Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Sat, 28 Jan 2012 17:38:23 -0800 Subject: [PATCH] Share `randomString` method. This simply shares a common implementation and makes no judgements on the validity of its use. The string created is not a secure random number, so some uses of it may not be applicable. --- bin/convert.js | 2 +- node/db/AuthorManager.js | 4 ++-- node/db/GroupManager.js | 3 ++- node/db/Pad.js | 11 ++--------- node/db/ReadOnlyManager.js | 4 ++-- node/db/SecurityManager.js | 4 ++-- node/db/SessionManager.js | 3 ++- node/handler/APIHandler.js | 3 ++- node/utils/randomstring.js | 16 ---------------- static/js/pad.js | 16 ++-------------- static/js/pad_utils.js | 18 ++++++++++++++++++ static/js/timeslider.js | 14 ++------------ 12 files changed, 37 insertions(+), 61 deletions(-) delete mode 100644 node/utils/randomstring.js diff --git a/bin/convert.js b/bin/convert.js index f8ea77e8f..c5dc535cd 100644 --- a/bin/convert.js +++ b/bin/convert.js @@ -5,7 +5,7 @@ var ueberDB = require("ueberDB"); var mysql = require("mysql"); var async = require("async"); var Changeset = CommonCode.require("/Changeset"); -var randomString = require("../node/utils/randomstring"); +var randomString = CommonCode.require('/pad_utils').randomString; var AttributePoolFactory = CommonCode.require("/AttributePoolFactory"); var settingsFile = process.argv[2]; diff --git a/node/db/AuthorManager.js b/node/db/AuthorManager.js index 7c054a56f..9baf63475 100644 --- a/node/db/AuthorManager.js +++ b/node/db/AuthorManager.js @@ -18,11 +18,11 @@ * limitations under the License. */ +var CommonCode = require('../utils/common_code'); var ERR = require("async-stacktrace"); var db = require("./DB").db; var async = require("async"); - -var randomString = require("../utils/randomstring"); +var randomString = CommonCode.require('/pad_utils').randomString; /** * Checks if the author exists diff --git a/node/db/GroupManager.js b/node/db/GroupManager.js index 7e3b7d6d1..bd294ba62 100644 --- a/node/db/GroupManager.js +++ b/node/db/GroupManager.js @@ -18,9 +18,10 @@ * limitations under the License. */ +var CommonCode = require('../utils/common_code'); var ERR = require("async-stacktrace"); var customError = require("../utils/customError"); -var randomString = require("../utils/randomstring"); +var randomString = CommonCode.require('/pad_utils').randomString; var db = require("./DB").db; var async = require("async"); var padManager = require("./PadManager"); diff --git a/node/db/Pad.js b/node/db/Pad.js index f29f7173f..99a53143c 100644 --- a/node/db/Pad.js +++ b/node/db/Pad.js @@ -6,6 +6,7 @@ var CommonCode = require('../utils/common_code'); var ERR = require("async-stacktrace"); var Changeset = CommonCode.require("/Changeset"); var AttributePoolFactory = CommonCode.require("/AttributePoolFactory"); +var randomString = CommonCode.require('/pad_utils').randomString; var db = require("./DB").db; var async = require("async"); var settings = require('../utils/Settings'); @@ -478,15 +479,7 @@ function hash(password, salt) function generateSalt() { - var len = 86; - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"; - var randomstring = ''; - for (var i = 0; i < len; i++) - { - var rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return randomstring; + return randomstring(86); } function compare(hashStr, password) diff --git a/node/db/ReadOnlyManager.js b/node/db/ReadOnlyManager.js index 1e5079c52..e5dab99b4 100644 --- a/node/db/ReadOnlyManager.js +++ b/node/db/ReadOnlyManager.js @@ -18,11 +18,11 @@ * limitations under the License. */ +var CommonCode = require('../utils/common_code'); var ERR = require("async-stacktrace"); var db = require("./DB").db; var async = require("async"); - -var randomString = require("../utils/randomstring"); +var randomString = CommonCode.require('/pad_utils').randomString; /** * returns a read only id for a pad diff --git a/node/db/SecurityManager.js b/node/db/SecurityManager.js index 4b86d868a..33ab37d44 100644 --- a/node/db/SecurityManager.js +++ b/node/db/SecurityManager.js @@ -18,6 +18,7 @@ * limitations under the License. */ +var CommonCode = require('../utils/common_code'); var ERR = require("async-stacktrace"); var db = require("./DB").db; var async = require("async"); @@ -25,8 +26,7 @@ var authorManager = require("./AuthorManager"); var padManager = require("./PadManager"); var sessionManager = require("./SessionManager"); var settings = require("../utils/Settings") - -var randomString = require("../utils/randomstring"); +var randomString = CommonCode.require('/pad_utils').randomString; /** * This function controlls the access to a pad, it checks if the user can access a pad. diff --git a/node/db/SessionManager.js b/node/db/SessionManager.js index 084d4a695..c5af33c68 100644 --- a/node/db/SessionManager.js +++ b/node/db/SessionManager.js @@ -18,9 +18,10 @@ * limitations under the License. */ +var CommonCode = require('../utils/common_code'); var ERR = require("async-stacktrace"); var customError = require("../utils/customError"); -var randomString = require("../utils/randomstring"); +var randomString = CommonCode.require('/pad_utils').randomString; var db = require("./DB").db; var async = require("async"); var groupMangager = require("./GroupManager"); diff --git a/node/handler/APIHandler.js b/node/handler/APIHandler.js index ca45e1f8f..a7f66151c 100644 --- a/node/handler/APIHandler.js +++ b/node/handler/APIHandler.js @@ -18,11 +18,12 @@ * limitations under the License. */ +var CommonCode = require('../utils/common_code'); var ERR = require("async-stacktrace"); var fs = require("fs"); var api = require("../db/API"); var padManager = require("../db/PadManager"); -var randomString = require("../utils/randomstring"); +var randomString = CommonCode.require('/pad_utils').randomString; //ensure we have an apikey var apikey = null; diff --git a/node/utils/randomstring.js b/node/utils/randomstring.js deleted file mode 100644 index 4c1bba244..000000000 --- a/node/utils/randomstring.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * Generates a random String with the given length. Is needed to generate the Author, Group, readonly, session Ids - */ -var randomString = function randomString(len) -{ - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var randomstring = ''; - for (var i = 0; i < len; i++) - { - var rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return randomstring; -}; - -module.exports = randomString; diff --git a/static/js/pad.js b/static/js/pad.js index 0b2921748..e96631298 100644 --- a/static/js/pad.js +++ b/static/js/pad.js @@ -48,19 +48,7 @@ var padutils = require('/pad_utils').padutils; var createCookie = require('/pad_utils').createCookie; var readCookie = require('/pad_utils').readCookie; - -function randomString() -{ - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var string_length = 20; - var randomstring = ''; - for (var i = 0; i < string_length; i++) - { - var rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return "t." + randomstring; -} +var randomString = require('/pad_utils').randomString; function getParams() { @@ -184,7 +172,7 @@ function handshake() var token = readCookie("token"); if (token == null) { - token = randomString(); + token = "t." + randomString(); createCookie("token", token, 60); } diff --git a/static/js/pad_utils.js b/static/js/pad_utils.js index ddfe4664b..3ecc34bf8 100644 --- a/static/js/pad_utils.js +++ b/static/js/pad_utils.js @@ -20,6 +20,23 @@ * limitations under the License. */ +/** + * Generates a random String with the given length. Is needed to generate the Author, Group, readonly, session Ids + */ + +function randomString(len) +{ + var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + var randomstring = ''; + len = len || 20 + for (var i = 0; i < len; i++) + { + var rnum = Math.floor(Math.random() * chars.length); + randomstring += chars.substring(rnum, rnum + 1); + } + return randomstring; +} + function createCookie(name, value, days, path) { if (days) @@ -510,6 +527,7 @@ padutils.setupGlobalExceptionHandler = setupGlobalExceptionHandler; padutils.binarySearch = require('/ace2_common').binarySearch; +exports.randomString = randomString; exports.createCookie = createCookie; exports.readCookie = readCookie; exports.padutils = padutils; diff --git a/static/js/timeslider.js b/static/js/timeslider.js index 11eeddd7b..143ef3281 100644 --- a/static/js/timeslider.js +++ b/static/js/timeslider.js @@ -28,17 +28,7 @@ require('/undo-xpopup'); var createCookie = require('/pad_utils').createCookie; var readCookie = require('/pad_utils').readCookie; - -function randomString() { - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var string_length = 20; - var randomstring = ''; - for (var i=0; i