mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Token Caching now works
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user