From 8f5722d242b544675c1904105aca753c42680273 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Mon, 14 Dec 2015 18:06:03 +0000 Subject: [PATCH] Token Caching now works --- lib/Client/InternalClient.js | 18 ++++++++++++++++-- lib/Util/Cache.js | 20 ++++++++++++++++++-- lib/Util/TokenCacher.js | 10 +++++----- src/Client/InternalClient.js | 21 ++++++++++++++++++--- src/Util/Cache.js | 9 +++++---- src/Util/TokenCacher.js | 14 +++++++------- test/msgbot.js | 5 ++--- 7 files changed, 71 insertions(+), 26 deletions(-) diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 494f91e1c..efe8c1f54 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -365,7 +365,6 @@ var InternalClient = (function () { var client = this.client; - console.log(this.tokenCacher.done); if (!this.tokenCacher.done) { return new Promise(function (resolve, reject) { setTimeout(function () { @@ -373,7 +372,21 @@ var InternalClient = (function () { }, 20); }); } else { - console.log("YAA - " + this.tokenCacher.getToken(email, password)); + var tk = this.tokenCacher.getToken(email, password); + if (tk) { + return new Promise(function (resolve, reject) { + _this6.client.emit("debug", "bypassed direct API login, used cached token"); + _this6.state = _ConnectionState2["default"].LOGGED_IN; + _this6.token = tk; + _this6.email = email; + _this6.password = password; + + return _this6.getGateway().then(function (url) { + _this6.createWS(url); + return tk; + }); + }); + } } if (this.state !== _ConnectionState2["default"].DISCONNECTED && this.state !== _ConnectionState2["default"].IDLE) { @@ -386,6 +399,7 @@ var InternalClient = (function () { email: email, password: password }).then(function (res) { + _this6.client.emit("debug", "direct API login, cached token was unavailable"); var token = res.token; _this6.tokenCacher.setToken(email, password, token); _this6.state = _ConnectionState2["default"].LOGGED_IN; diff --git a/lib/Util/Cache.js b/lib/Util/Cache.js index 3f8f01fde..f2eeb711d 100644 --- a/lib/Util/Cache.js +++ b/lib/Util/Cache.js @@ -23,8 +23,24 @@ var Cache = (function (_Array) { Cache.prototype.get = function get(key, value) { if (key === this[discrimS]) return this[discrimCacheS][value] || null; - var l = this.length; - for (var i = 0; i < l; i++) if (this[i][key] == value) return this[i]; + for (var _iterator = this, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { + var _ref; + + if (_isArray) { + if (_i >= _iterator.length) break; + _ref = _iterator[_i++]; + } else { + _i = _iterator.next(); + if (_i.done) break; + _ref = _i.value; + } + + var item = _ref; + + if (item[key] == value) { + return item; + } + } return null; }; diff --git a/lib/Util/TokenCacher.js b/lib/Util/TokenCacher.js index 97a249a70..3ab84f265 100644 --- a/lib/Util/TokenCacher.js +++ b/lib/Util/TokenCacher.js @@ -41,10 +41,9 @@ var TokenCacher = (function (_EventEmitter) { TokenCacher.prototype.setToken = function setToken(email, password, token) { console.log("wanting to cache", token); - token = new Buffer(token).toString("base64"); var cipher = _crypto2["default"].createCipher(algo, password); - var crypted = cipher.update(token, 'utf8', 'base64'); - crypted += cipher.final('base64'); + var crypted = cipher.update("valid" + token, 'utf8', 'hex'); + crypted += cipher.final('hex'); this.data[email] = crypted; this.save(); }; @@ -59,10 +58,11 @@ var TokenCacher = (function (_EventEmitter) { try { var decipher = _crypto2["default"].createDecipher(algo, password); - var dec = decipher.update(this.data[email], "base64", 'utf8'); + var dec = decipher.update(this.data[email], "hex", 'utf8'); dec += decipher.final('utf8'); - return new Buffer(dec, "base64").toString("ascii"); + return dec.indexOf("valid") === 0 ? dec.substr(5) : false; } catch (e) { + console.log(e); return null; } } else { diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 19a4f0535..c5ace69ca 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -265,15 +265,29 @@ export default class InternalClient { login(email, password) { var client = this.client; - console.log(this.tokenCacher.done); if(!this.tokenCacher.done){ return new Promise((resolve, reject) => { setTimeout(() => { this.login(email, password).then(resolve).catch(reject); }, 20); }); - }else{ - console.log("Cached - " + this.tokenCacher.getToken(email, password)); + } else { + var tk = this.tokenCacher.getToken(email, password); + if( tk ){ + return new Promise((resolve, reject) => { + this.client.emit("debug", "bypassed direct API login, used cached token"); + this.state = ConnectionState.LOGGED_IN; + this.token = tk; + this.email = email; + this.password = password; + + return this.getGateway() + .then(url => { + this.createWS(url); + return tk; + }); + }); + } } if(this.state !== ConnectionState.DISCONNECTED && this.state !== ConnectionState.IDLE) { @@ -287,6 +301,7 @@ export default class InternalClient { password }) .then(res => { + this.client.emit("debug", "direct API login, cached token was unavailable"); var token = res.token; this.tokenCacher.setToken(email, password, token); this.state = ConnectionState.LOGGED_IN; diff --git a/src/Util/Cache.js b/src/Util/Cache.js index b6981134b..68187d7e9 100644 --- a/src/Util/Cache.js +++ b/src/Util/Cache.js @@ -14,10 +14,11 @@ export default class Cache extends Array { if (key === this[discrimS]) return this[discrimCacheS][value] || null; - var l = this.length; - for (var i = 0; i < l; i++) - if (this[i][key] == value) - return this[i]; + for(var item of this){ + if(item[key] == value){ + return item; + } + } return null; } diff --git a/src/Util/TokenCacher.js b/src/Util/TokenCacher.js index fe2e48398..508b2476c 100644 --- a/src/Util/TokenCacher.js +++ b/src/Util/TokenCacher.js @@ -25,10 +25,9 @@ export default class TokenCacher extends EventEmitter { setToken(email, password, token) { console.log("wanting to cache", token); - token = new Buffer(token).toString("base64"); - var cipher = crypto.createCipher(algo,password) - var crypted = cipher.update(token,'utf8','base64') - crypted += cipher.final('base64'); + var cipher = crypto.createCipher(algo, password) + var crypted = cipher.update("valid" + token, 'utf8', 'hex') + crypted += cipher.final('hex'); this.data[email] = crypted; this.save(); } @@ -37,16 +36,17 @@ export default class TokenCacher extends EventEmitter { fs.writeJson(this.savePath, this.data); } - getToken(email, password){ + getToken(email, password) { if (this.data[email]) { try { var decipher = crypto.createDecipher(algo, password) - var dec = decipher.update(this.data[email], "base64", 'utf8') + var dec = decipher.update(this.data[email], "hex", 'utf8'); dec += decipher.final('utf8'); - return new Buffer(dec, "base64").toString("ascii"); + return (dec.indexOf("valid") === 0 ? dec.substr(5) : false); } catch (e) { + // not a valid token return null; } diff --git a/test/msgbot.js b/test/msgbot.js index 95b3b4629..39a089a5e 100644 --- a/test/msgbot.js +++ b/test/msgbot.js @@ -7,7 +7,6 @@ var request = require("superagent"); client.on("ready", () => { console.log("ready - " + client.internal.token); - }); client.on("autoRevive", () => { @@ -28,7 +27,7 @@ client.on("message", msg => { } if (msg.content === "$perms") { - + msg.reply(client.channels.get("id", msg.channel.id)); } @@ -49,7 +48,7 @@ client.on("message", msg => { console.log("INIT"); -client.on("debug", console.log); +client.on("debug", msg => console.log("[debug]", msg)); client.login(process.env["ds_email"], process.env["ds_password"]).catch(console.log);