Refactor password change and reset

This commit is contained in:
calzoneman 2013-08-11 18:23:20 -04:00
parent d266175d5b
commit c4588fab49
1 changed files with 112 additions and 99 deletions

127
api.js
View File

@ -54,6 +54,11 @@ module.exports = function (Server) {
var app = Server.app;
/* <https://en.wikipedia.org/wiki/Hyper_Text_Coffee_Pot_Control_Protocol> */
app.get("/api/coffee", function (req, res) {
res.send(418); // 418 I'm a teapot
});
/* REGION channels */
/* data about a specific channel */
@ -165,48 +170,62 @@ module.exports = function (Server) {
});
});
var x = {
handlePasswordChange: function (params, req, res) {
var name = params.name || "";
var oldpw = params.oldpw || "";
var newpw = params.newpw || "";
if(oldpw == "" || newpw == "") {
this.sendJSON(res, {
/* password change */
app.get("/api/account/passwordchange", function (req, res) {
res.type("application/jsonp");
var name = req.body.name;
var oldpw = req.body.oldpw;
var newpw = req.body.newpw;
if(!oldpw || !newpw) {
res.jsonp({
success: false,
error: "Old password and new password cannot be empty"
error: "Password cannot be empty"
});
return;
}
var row = Auth.login(name, oldpw);
if(row) {
if(!row) {
res.jsonp({
success: false,
error: "Invalid username/password combination"
});
return;
}
ActionLog.record(getIP(req), name, "password-change");
var success = Auth.setUserPassword(name, newpw);
this.sendJSON(res, {
success: success,
error: success ? "" : "Change password failed",
if(!success) {
res.jsonp({
success: false,
error: "Server error. Please try again or ask an "+
"administrator for assistance."
});
return;
}
res.jsonp({
success: true,
session: row.session_hash
});
}
else {
this.sendJSON(res, {
success: false,
error: "Invalid username/password"
});
}
},
handlePasswordReset: function (params, req, res) {
var name = params.name || "";
var email = params.email || "";
/* password reset */
app.get("/api/account/passwordreset", function (req, res) {
res.type("application/jsonp");
var name = req.body.name;
var email = req.body.email;
var ip = getIP(req);
var hash = false;
try {
hash = Server.db.generatePasswordReset(ip, name, email);
ActionLog.record(ip, name, "password-reset-generate", email);
}
catch(e) {
this.sendJSON(res, {
} catch(e) {
res.jsonp({
success: false,
error: e
});
@ -214,60 +233,54 @@ module.exports = function (Server) {
}
if(!Server.cfg["enable-mail"]) {
this.sendJSON(res, {
res.jsonp({
success: false,
error: "This server does not have email enabled. Contact an administrator"
error: "This server does not have email recovery enabled."+
" Contact an administrator for assistance."
});
return;
}
if(!email) {
this.sendJSON(res, {
res.jsonp({
success: false,
error: "You don't have a recovery email address set. Contact an administrator"
error: "You don't have a recovery email address set. "+
"Contact an administrator for assistance."
});
return;
}
var msg = [
"A password reset request was issued for your account `",
name,
"` on ",
Server.cfg["domain"],
". This request is valid for 24 hours. ",
"If you did not initiate this, there is no need to take action. ",
"To reset your password, copy and paste the following link into ",
"your browser: ",
Server.cfg["domain"],
"/reset.html?",
hash
].join("");
var msg = "A password reset request was issued for your account '"+
name + "' on " + Server.cfg["domain"] + ". This request"+
" is valid for 24 hours. If you did not initiate this, "+
"there is no need to take action. To reset your "+
"password, copy and paste the following link into your "+
"browser: " + Server.cfg["domain"] + "/reset.html?"+hash;
var mail = {
from: "CyTube Services <" + Server.cfg["mail-from"] + ">",
to: email,
to: emial,
subject: "Password reset request",
text: msg
};
var api = this;
Server.cfg["nodemailer"].sendMail(mail, function(err, response) {
Server.cfg["nodemailer"].sendMail(mai, function (err, response) {
if(err) {
Logger.errlog.log("Mail fail: " + err);
api.sendJSON(res, {
Logger.errlog.log("mail fail: " + err);
res.jsonp({
success: false,
error: "Email failed. Contact an admin if this persists."
error: "Email send failed. Contact an administrator "+
"if this persists"
});
}
else {
api.sendJSON(res, {
} else {
res.jsonp({
success: true
});
if(Server.cfg["debug"]) {
Logger.syslog.log(response);
}
}
});
},
});
var x = {
handlePasswordRecover: function (params, req, res) {
var hash = params.hash || "";
var ip = getIP(req);