Skip to content

Commit

Permalink
password encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
youennPennarun committed Dec 28, 2015
1 parent 09a4e68 commit 4a7d25e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
#Env variables
.env

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"babel": "^5.8.23",
"babel-eslint": "^4.1.1",
"babelify": "^6.3.0",
"bcrypt": "^0.8.5",
"bcrypt-nodejs": "0.0.3",
"bluebird": "~2.9.34",
"body-parser": "^1.13.3",
Expand Down
23 changes: 15 additions & 8 deletions src/public/src/js/components/RaspberryDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,22 @@ class RaspberriesSettings extends React.Component {
render() {
let {raspberry} = this.state;
return (
<Paper style={styles.container}>
<h2>{raspberry.name}</h2>
{raspberry.modules.map(function(module) {
return (
<ModuleItem module={module}/>
);
<div>
<Paper style={styles.container}>
<h2>{raspberry.name}</h2>
{raspberry.modules.map(function(module) {
return (
<ModuleItem module={module}/>
);
}.bind(this))}

</Paper>

}.bind(this))}
</Paper>
<Paper style={styles.container}>
<h2>Info</h2>
IP address: {raspberry.ip}
</Paper>
</div>
)
}
_getStateView(module) {
Expand Down
19 changes: 15 additions & 4 deletions src/server/middleware/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var userModel = require("../models/mongoose/mongoose-models").User;
var jwt = require("jwt-simple");
var fs = require("fs");
var bcrypt = require('bcrypt');
var config = require("../data/private/config.js");

var login = function(req, res) {
Expand All @@ -27,8 +28,18 @@ var login = function(req, res) {
})
};

var encrypt = function(password, salt) {
if (!salt) {
salt = bcrypt.genSaltSync(12);
}
var hash = bcrypt.hashSync(password, salt);
return hash


}

var checkPassword = function(requestPassword, userInfo) {
return requestPassword === userInfo.password;
return bcrypt.compareSync(requestPassword, userInfo.password);
};

var generateToken = function(userInfo) {
Expand All @@ -52,13 +63,13 @@ var editPassword = function(req, res) {
if (!userInfo) {
return res.json({error: "invalid"});
} else {
if (req.body.oldPassword !== userInfo.password) {
if (!checkPassword(req.body.oldPassword, userInfo)) {
return res.json({error: "invalid password"});
}
if (req.body.newPassword !== req.body.confirmNewPassword) {
return res.json({error: "invalid new password"});
} else {
userInfo.password = req.body.newPassword;
userInfo.password = encrypt(req.body.newPassword);
userInfo.save(function(err) {
if(err) {
res.json({err: err});
Expand Down Expand Up @@ -94,4 +105,4 @@ module.exports = {
isLoggedIn: isLoggedIn,
me: getMe,
editPassword: editPassword
};
};
12 changes: 12 additions & 0 deletions src/server/models/Raspberry.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ Raspberry.add = function(name, modulesNames) {
});
}

Raspberry.emitTo = function(name, event, data) {
return new Promise(function(resolve, reject) {
Raspberry.findOne(name)
.then(function(raspberry) {
if (!raspberry.socketId) {
return reject({code: 404, message: "no raspberry with that name", id: "RASPBERRY_NOT_FOUND"});
}
process.io.sockets.connected[raspberry.socketId].emit(event, data);
}).catch(reject);
});
}

Raspberry.findOne = function(name) {
return new Promise(function(resolve, reject) {
raspberryModel.findOne({name: name}, function(err, raspberry) {
Expand Down
4 changes: 3 additions & 1 deletion src/server/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ IO.init = function(server) {

}
socket.on('ping', function(){
console.log("ping:received")
socket.emit("ping:received");
});
raspberrySocket(socket);

ModuleManager.setUpSocket(socket, IO.io);
socket.on('disconnect', function(){
console.log("client disconnected");
if (socket.decoded_token.isRaspberry) {
if (socket.decoded_token.isRaspberry
&& socket.raspberryInfo) {
Raspberry.stop(socket.raspberryInfo.name)
.then(function(raspberry) {
console.log(JSON.stringify(raspberry))
Expand Down

0 comments on commit 4a7d25e

Please sign in to comment.