Token Caching now works

This commit is contained in:
Amish Shah
2015-12-14 18:06:03 +00:00
parent a122f0994e
commit 8f5722d242
7 changed files with 71 additions and 26 deletions

View File

@@ -365,7 +365,6 @@ var InternalClient = (function () {
var client = this.client; var client = this.client;
console.log(this.tokenCacher.done);
if (!this.tokenCacher.done) { if (!this.tokenCacher.done) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
setTimeout(function () { setTimeout(function () {
@@ -373,7 +372,21 @@ var InternalClient = (function () {
}, 20); }, 20);
}); });
} else { } 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) { if (this.state !== _ConnectionState2["default"].DISCONNECTED && this.state !== _ConnectionState2["default"].IDLE) {
@@ -386,6 +399,7 @@ var InternalClient = (function () {
email: email, email: email,
password: password password: password
}).then(function (res) { }).then(function (res) {
_this6.client.emit("debug", "direct API login, cached token was unavailable");
var token = res.token; var token = res.token;
_this6.tokenCacher.setToken(email, password, token); _this6.tokenCacher.setToken(email, password, token);
_this6.state = _ConnectionState2["default"].LOGGED_IN; _this6.state = _ConnectionState2["default"].LOGGED_IN;

View File

@@ -23,8 +23,24 @@ var Cache = (function (_Array) {
Cache.prototype.get = function get(key, value) { Cache.prototype.get = function get(key, value) {
if (key === this[discrimS]) return this[discrimCacheS][value] || null; if (key === this[discrimS]) return this[discrimCacheS][value] || null;
var l = this.length; for (var _iterator = this, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
for (var i = 0; i < l; i++) if (this[i][key] == value) return this[i]; 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; return null;
}; };

View File

@@ -41,10 +41,9 @@ var TokenCacher = (function (_EventEmitter) {
TokenCacher.prototype.setToken = function setToken(email, password, token) { TokenCacher.prototype.setToken = function setToken(email, password, token) {
console.log("wanting to cache", token); console.log("wanting to cache", token);
token = new Buffer(token).toString("base64");
var cipher = _crypto2["default"].createCipher(algo, password); var cipher = _crypto2["default"].createCipher(algo, password);
var crypted = cipher.update(token, 'utf8', 'base64'); var crypted = cipher.update("valid" + token, 'utf8', 'hex');
crypted += cipher.final('base64'); crypted += cipher.final('hex');
this.data[email] = crypted; this.data[email] = crypted;
this.save(); this.save();
}; };
@@ -59,10 +58,11 @@ var TokenCacher = (function (_EventEmitter) {
try { try {
var decipher = _crypto2["default"].createDecipher(algo, password); 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'); dec += decipher.final('utf8');
return new Buffer(dec, "base64").toString("ascii"); return dec.indexOf("valid") === 0 ? dec.substr(5) : false;
} catch (e) { } catch (e) {
console.log(e);
return null; return null;
} }
} else { } else {

View File

@@ -265,15 +265,29 @@ export default class InternalClient {
login(email, password) { login(email, password) {
var client = this.client; var client = this.client;
console.log(this.tokenCacher.done);
if(!this.tokenCacher.done){ if(!this.tokenCacher.done){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
this.login(email, password).then(resolve).catch(reject); this.login(email, password).then(resolve).catch(reject);
}, 20); }, 20);
}); });
}else{ } else {
console.log("Cached - " + this.tokenCacher.getToken(email, password)); 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) { if(this.state !== ConnectionState.DISCONNECTED && this.state !== ConnectionState.IDLE) {
@@ -287,6 +301,7 @@ export default class InternalClient {
password password
}) })
.then(res => { .then(res => {
this.client.emit("debug", "direct API login, cached token was unavailable");
var token = res.token; var token = res.token;
this.tokenCacher.setToken(email, password, token); this.tokenCacher.setToken(email, password, token);
this.state = ConnectionState.LOGGED_IN; this.state = ConnectionState.LOGGED_IN;

View File

@@ -14,10 +14,11 @@ export default class Cache extends Array {
if (key === this[discrimS]) if (key === this[discrimS])
return this[discrimCacheS][value] || null; return this[discrimCacheS][value] || null;
var l = this.length; for(var item of this){
for (var i = 0; i < l; i++) if(item[key] == value){
if (this[i][key] == value) return item;
return this[i]; }
}
return null; return null;
} }

View File

@@ -25,10 +25,9 @@ export default class TokenCacher extends EventEmitter {
setToken(email, password, token) { setToken(email, password, token) {
console.log("wanting to cache", token); console.log("wanting to cache", token);
token = new Buffer(token).toString("base64"); var cipher = crypto.createCipher(algo, password)
var cipher = crypto.createCipher(algo,password) var crypted = cipher.update("valid" + token, 'utf8', 'hex')
var crypted = cipher.update(token,'utf8','base64') crypted += cipher.final('hex');
crypted += cipher.final('base64');
this.data[email] = crypted; this.data[email] = crypted;
this.save(); this.save();
} }
@@ -37,16 +36,17 @@ export default class TokenCacher extends EventEmitter {
fs.writeJson(this.savePath, this.data); fs.writeJson(this.savePath, this.data);
} }
getToken(email, password){ getToken(email, password) {
if (this.data[email]) { if (this.data[email]) {
try { try {
var decipher = crypto.createDecipher(algo, password) 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'); dec += decipher.final('utf8');
return new Buffer(dec, "base64").toString("ascii"); return (dec.indexOf("valid") === 0 ? dec.substr(5) : false);
} catch (e) { } catch (e) {
// not a valid token
return null; return null;
} }

View File

@@ -7,7 +7,6 @@ var request = require("superagent");
client.on("ready", () => { client.on("ready", () => {
console.log("ready - " + client.internal.token); console.log("ready - " + client.internal.token);
}); });
client.on("autoRevive", () => { client.on("autoRevive", () => {
@@ -28,7 +27,7 @@ client.on("message", msg => {
} }
if (msg.content === "$perms") { if (msg.content === "$perms") {
msg.reply(client.channels.get("id", msg.channel.id));
} }
@@ -49,7 +48,7 @@ client.on("message", msg => {
console.log("INIT"); 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); client.login(process.env["ds_email"], process.env["ds_password"]).catch(console.log);