Fix user.js for new auth

This commit is contained in:
calzoneman 2013-08-17 15:54:23 -05:00
parent ebd55b4846
commit 65d7a8a455
1 changed files with 97 additions and 144 deletions

241
user.js
View File

@ -533,15 +533,16 @@ User.prototype.initCallbacks = function() {
var lastguestlogin = {}; var lastguestlogin = {};
// Attempt to login // Attempt to login
User.prototype.login = function(name, pw, session) { User.prototype.login = function(name, pw, session) {
var self = this;
// No password => try guest login // No password => try guest login
if(pw == "" && session == "") { if(pw == "" && session == "") {
if(this.ip in lastguestlogin) { if(self.ip in lastguestlogin) {
var diff = (Date.now() - lastguestlogin[this.ip])/1000; var diff = (Date.now() - lastguestlogin[self.ip])/1000;
if(diff < this.server.cfg["guest-login-delay"]) { if(diff < self.server.cfg["guest-login-delay"]) {
this.socket.emit("login", { self.socket.emit("login", {
success: false, success: false,
error: ["Guest logins are restricted to one per ", error: ["Guest logins are restricted to one per ",
this.server.cfg["guest-login-delay"] self.server.cfg["guest-login-delay"]
+ " seconds per IP. ", + " seconds per IP. ",
"This restriction does not apply to registered users." "This restriction does not apply to registered users."
].join("") ].join("")
@ -549,149 +550,101 @@ User.prototype.login = function(name, pw, session) {
return false; return false;
} }
} }
try { if(!$util.isValidUserName(name)) {
// Sorry bud, can't take that name self.socket.emit("login", {
if(Auth.isRegistered(name)) {
this.socket.emit("login", {
success: false,
error: "That username is already taken"
});
return false;
}
// YOUR ARGUMENT IS INVALID
else if(!Auth.validateName(name)) {
this.socket.emit("login", {
success: false,
error: "Invalid username. Usernames must be 1-20 characters long and consist only of alphanumeric characters and underscores"
});
}
else {
if(this.channel != null) {
for(var i = 0; i < this.channel.users.length; i++) {
if(this.channel.users[i].name == name) {
this.socket.emit("login", {
success: false,
error: "That name is already taken on this channel"
});
return;
}
}
}
lastguestlogin[this.ip] = Date.now();
this.rank = Rank.Guest;
Logger.syslog.log(this.ip + " signed in as " + name);
this.server.db.recordVisit(this.ip, name);
this.name = name;
this.loggedIn = false;
this.socket.emit("login", {
success: true,
name: name
});
this.socket.emit("rank", this.rank);
if(this.channel != null) {
this.channel.logger.log(this.ip + " signed in as " + name);
this.channel.broadcastNewUser(this);
}
}
}
catch(e) {
this.socket.emit("login", {
success: false, success: false,
error: e error: "Invalid username. Usernames must be 1-20 characters long and consist only of alphanumeric characters and underscores"
}); });
return;
} }
}
else {
try {
var row;
if((row = Auth.login(name, pw, session))) {
if(this.channel != null) {
for(var i = 0; i < this.channel.users.length; i++) {
if(this.channel.users[i].name == name) {
this.channel.kick(this.channel.users[i], "Duplicate login");
}
}
}
if(this.global_rank >= 255)
ActionLog.record(this.ip, name, "login-success");
this.loggedIn = true;
this.socket.emit("login", {
success: true,
session: row.session_hash,
name: name
});
Logger.syslog.log(this.ip + " logged in as " + name);
this.server.db.recordVisit(this.ip, name);
this.profile = {
image: row.profile_image,
text: row.profile_text
};
var chanrank = (this.channel != null) ? this.channel.getRank(name)
: Rank.Guest;
var rank = (chanrank > row.global_rank) ? chanrank
: row.global_rank;
this.rank = (this.rank > rank) ? this.rank : rank;
this.global_rank = row.global_rank;
this.socket.emit("rank", this.rank);
this.name = name;
if(this.channel != null) {
this.channel.logger.log(this.ip + " logged in as " + name);
this.channel.broadcastNewUser(this);
}
}
// Wrong password
else {
ActionLog.record(this.ip, this.name, "login-failure");
this.socket.emit("login", {
success: false,
error: "Invalid session"
});
return false;
}
}
catch(e) {
this.socket.emit("login", {
success: false,
error: e
});
}
}
}
// Attempt to register a user account self.server.db.isUsernameTaken(name, function (err, taken) {
User.prototype.register = function(name, pw) { if(err) {
if(pw == "") { self.socket.emit("login", {
// Sorry bud, password required success: false,
this.socket.emit("register", { error: "Internal error: " + err
success: false, });
error: "You must provide a password" return;
}); }
return false;
} if(taken) {
else if(Auth.isRegistered(name)) { self.socket.emit("login", {
this.socket.emit("register", { success: false,
success: false, error: "That username is taken"
error: "That username is already taken" });
}); return;
return false; }
}
else if(!Auth.validateName(name)) { if(self.channel != null) {
this.socket.emit("register", { for(var i = 0; i < self.channel.users.length; i++) {
success: false, if(self.channel.users[i].name == name) {
error: "Invalid username. Usernames must be 1-20 characters long and consist only of alphanumeric characters and underscores" self.socket.emit("login", {
}); success: false,
} error: "That name is already taken on self channel"
else if(Auth.register(name, pw)) { });
console.log(this.ip + " registered " + name); return;
this.socket.emit("register", { }
success: true }
}); }
this.login(name, pw); lastguestlogin[self.ip] = Date.now();
} self.rank = Rank.Guest;
else { Logger.syslog.log(self.ip + " signed in as " + name);
this.socket.emit("register", { self.server.db.recordVisit(self.ip, name);
success: false, self.name = name;
error: "[](/ppshrug) Registration Failed." self.loggedIn = false;
self.socket.emit("login", {
success: true,
name: name
});
self.socket.emit("rank", self.rank);
if(self.channel != null) {
self.channel.logger.log(self.ip + " signed in as " + name);
self.channel.broadcastNewUser(self);
}
}
} else {
self.server.db.userLogin(name, pw, session, function (err, row) {
if(err) {
ActionLog.record(self.ip, self.name, "login-failure");
self.socket.emit("login", {
success: false,
error: err
});
return;
}
if(self.channel != null) {
for(var i = 0; i < self.channel.users.length; i++) {
if(self.channel.users[i].name == name) {
self.channel.kick(self.channel.users[i], "Duplicate login");
}
}
}
if(self.global_rank >= 255)
ActionLog.record(self.ip, name, "login-success");
self.loggedIn = true;
self.socket.emit("login", {
success: true,
session: row.session_hash,
name: name
});
Logger.syslog.log(self.ip + " logged in as " + name);
self.server.db.recordVisit(self.ip, name);
self.profile = {
image: row.profile_image,
text: row.profile_text
};
var chanrank = (self.channel != null) ? self.channel.getRank(name)
: Rank.Guest;
var rank = (chanrank > row.global_rank) ? chanrank
: row.global_rank;
self.rank = (self.rank > rank) ? self.rank : rank;
self.global_rank = row.global_rank;
self.socket.emit("rank", self.rank);
self.name = name;
if(self.channel != null) {
self.channel.logger.log(self.ip + " logged in as " + name);
self.channel.broadcastNewUser(self);
}
}); });
} }
} }