summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChad Weider <cweider@oofn.net>2012-01-28 17:38:23 -0800
committerChad Weider <cweider@oofn.net>2012-02-10 14:52:34 -0800
commit363e1685618b1bb05802a221021549eb0ee7a3bd (patch)
tree2f23e58b4ac2b9459e6f1658792940f5e9fa89ac
parent34edba3adfb2c15543273d3de4ddc74abf896e8c (diff)
downloadetherpad-lite-363e1685618b1bb05802a221021549eb0ee7a3bd.zip
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.
-rw-r--r--bin/convert.js2
-rw-r--r--node/db/AuthorManager.js4
-rw-r--r--node/db/GroupManager.js3
-rw-r--r--node/db/Pad.js11
-rw-r--r--node/db/ReadOnlyManager.js4
-rw-r--r--node/db/SecurityManager.js4
-rw-r--r--node/db/SessionManager.js3
-rw-r--r--node/handler/APIHandler.js3
-rw-r--r--node/utils/randomstring.js16
-rw-r--r--static/js/pad.js16
-rw-r--r--static/js/pad_utils.js18
-rw-r--r--static/js/timeslider.js14
12 files changed, 37 insertions, 61 deletions
diff --git a/bin/convert.js b/bin/convert.js
index f8ea77e8..c5dc535c 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 7c054a56..9baf6347 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 7e3b7d6d..bd294ba6 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 f29f7173..99a53143 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 1e5079c5..e5dab99b 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 4b86d868..33ab37d4 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 084d4a69..c5af33c6 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 ca45e1f8..a7f66151 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 4c1bba24..00000000
--- 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 0b292174..e9663129 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 ddfe4664..3ecc34bf 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 11eeddd7..143ef328 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<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;
var socket, token, padId, export_links;
@@ -59,7 +49,7 @@ function init() {
token = readCookie("token");
if(token == null)
{
- token = randomString();
+ token = "t." + randomString();
createCookie("token", token, 60);
}