-
Notifications
You must be signed in to change notification settings - Fork 703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
frontend password change functionality #57
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,31 @@ $(function() { | |
.show(); | ||
}); | ||
|
||
socket.on("change-password", function(data) { | ||
var passwordForm = $("#change-password"); | ||
if (data.error || data.success) { | ||
var message = data.success ? data.success : data.error; | ||
var feedback = passwordForm.find(".feedback"); | ||
|
||
if (data.success) { | ||
feedback.addClass("success").removeClass("error"); | ||
} else { | ||
feedback.addClass("error").removeClass("success"); | ||
} | ||
|
||
feedback.text(message).show(); | ||
feedback.closest("form").one("submit", function() { | ||
feedback.hide(); | ||
}); | ||
} | ||
passwordForm | ||
.find("input") | ||
.val("") | ||
.end() | ||
.find(".btn") | ||
.prop("disabled", false); | ||
}); | ||
|
||
socket.on("init", function(data) { | ||
if (data.networks.length === 0) { | ||
$("#footer").find(".connect").trigger("click"); | ||
|
@@ -734,7 +759,7 @@ $(function() { | |
}); | ||
|
||
var windows = $("#windows"); | ||
var forms = $("#sign-in, #connect"); | ||
var forms = $("#sign-in, #connect, #change-password"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Off-topic of this PR] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT this is used to wire-up the on-submit event handlers on each of the three elements with those IDs, which happen to be |
||
|
||
windows.on("show", "#sign-in", function() { | ||
var self = $(this); | ||
|
@@ -761,6 +786,8 @@ $(function() { | |
.end(); | ||
if (form.closest(".window").attr("id") === "connect") { | ||
event = "conn"; | ||
} else if (form.closest("div").attr("id") === "change-password") { | ||
event = "change-password"; | ||
} | ||
var values = {}; | ||
$.each(form.serializeArray(), function(i, obj) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
var _ = require("lodash"); | ||
var Chan = require("./models/chan"); | ||
var crypto = require("crypto"); | ||
var fs = require("fs"); | ||
var identd = require("./identd"); | ||
var log = require("./log"); | ||
var net = require("net"); | ||
|
@@ -51,14 +50,15 @@ var inputs = [ | |
"whois" | ||
]; | ||
|
||
function Client(sockets, name, config) { | ||
function Client(manager, name, config) { | ||
_.merge(this, { | ||
activeChannel: -1, | ||
config: config, | ||
id: id++, | ||
name: name, | ||
networks: [], | ||
sockets: sockets | ||
sockets: manager.sockets, | ||
manager: manager | ||
}); | ||
var client = this; | ||
crypto.randomBytes(48, function(err, buf) { | ||
|
@@ -221,6 +221,21 @@ Client.prototype.connect = function(args) { | |
}); | ||
}; | ||
|
||
Client.prototype.setPassword = function(hash) { | ||
var client = this; | ||
client.manager.updateUser(client.name, {password:hash}); | ||
// re-read the hash off disk to ensure we use whatever is saved. this will | ||
// prevent situations where the password failed to save properly and so | ||
// a restart of the server would forget the change and use the old | ||
// password again. | ||
var user = client.manager.readUserConfig(client.name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand that line and comment: if the password was not saved properly (so, not saved), why do we need to re-read the config, since the password will be unchanged? Also, doesn't restarting the server trigger a new config file read? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my thinking was to reload the entire user object from disk rather than just tweak the in-memory so that we are verified to be using the canonical configuration rather than a version which might be different to what's saved to disk. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, I'm good with that for now :-) |
||
if (user.password === hash) { | ||
client.config.password = hash; | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
Client.prototype.input = function(data) { | ||
var client = this; | ||
var text = data.text.trim(); | ||
|
@@ -353,9 +368,6 @@ Client.prototype.save = function(force) { | |
return; | ||
} | ||
|
||
var name = this.name; | ||
var path = Helper.HOME + "/users/" + name + ".json"; | ||
|
||
var networks = _.map( | ||
this.networks, | ||
function(n) { | ||
|
@@ -364,29 +376,6 @@ Client.prototype.save = function(force) { | |
); | ||
|
||
var json = {}; | ||
fs.readFile(path, "utf-8", function(err, data) { | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
|
||
try { | ||
json = JSON.parse(data); | ||
json.networks = networks; | ||
} catch (e) { | ||
console.log(e); | ||
return; | ||
} | ||
|
||
fs.writeFile( | ||
path, | ||
JSON.stringify(json, null, " "), | ||
{mode: "0777"}, | ||
function(err) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
} | ||
); | ||
}); | ||
json.networks = networks; | ||
client.manager.updateUser(client.name, json); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,18 +29,14 @@ ClientManager.prototype.loadUsers = function() { | |
|
||
ClientManager.prototype.loadUser = function(name) { | ||
try { | ||
var json = fs.readFileSync( | ||
Helper.HOME + "/users/" + name + ".json", | ||
"utf-8" | ||
); | ||
json = JSON.parse(json); | ||
var json = this.readUserConfig(name); | ||
} catch (e) { | ||
console.log(e); | ||
return; | ||
} | ||
if (!this.findClient(name)) { | ||
this.clients.push(new Client( | ||
this.sockets, | ||
this, | ||
name, | ||
json | ||
)); | ||
|
@@ -93,6 +89,50 @@ ClientManager.prototype.addUser = function(name, password) { | |
return true; | ||
}; | ||
|
||
ClientManager.prototype.updateUser = function(name, opts) { | ||
var users = this.getUsers(); | ||
if (users.indexOf(name) === -1) { | ||
return false; | ||
} | ||
if (typeof opts === "undefined") { | ||
return false; | ||
} | ||
var path = Helper.HOME + "/users/" + name + ".json"; | ||
var user = {}; | ||
|
||
try { | ||
user = this.readUserConfig(name); | ||
_.merge(user, opts); | ||
} catch (e) { | ||
console.log(e); | ||
return; | ||
} | ||
|
||
fs.writeFileSync( | ||
path, | ||
JSON.stringify(user, null, " "), | ||
{mode: "0777"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Off-topic of this PR] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ouch, that's bad... |
||
function(err) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
} | ||
); | ||
return true; | ||
}; | ||
|
||
ClientManager.prototype.readUserConfig = function(name) { | ||
var users = this.getUsers(); | ||
if (users.indexOf(name) === -1) { | ||
return false; | ||
} | ||
var path = Helper.HOME + "/users/" + name + ".json"; | ||
var user = {}; | ||
var data = fs.readFileSync(path, "utf-8"); | ||
user = JSON.parse(data); | ||
return user; | ||
}; | ||
|
||
ClientManager.prototype.removeUser = function(name) { | ||
var users = this.getUsers(); | ||
if (users.indexOf(name) === -1) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
margin-bottom
for the error but not the success message? It doesn't seem necessary for none I think, but I can be wrong..error.success
? :-)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to the first point, why a margin-bottom on .error but not .error.success: the second rule will inherit from the first so there is no reason to override the margin-bottom with an identical value when it is already applied by inheritance.
.error.success: that's a semantic issue I guess, but we can rename .error if need be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it seems very messy to have elements that are both
.error
and.success
. You should use one or the other, and if you do need a common class, you could call it.feedback
. If it's just for common CSS properties though,.error, .success { /* ... */ }
will do just fine.