Skip to content
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

implemented user password change #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 74 additions & 41 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,39 @@ app.get('/users', function(req, res) {
res.send(JSON.stringify(users));
});
});
app.put('/users/:id', function(req, res) {
var token = req.header('Authorization');
if (!token || token.length == 0) {
return res.status(401).end();
}
if (!('oldPassword' in req.body)) {
return res.status(400).end('old password not set');
}
var oldPassword = req.body.oldPassword;
if (!('newPassword' in req.body)) {
return res.status(400).end('new password not set');
}
var newPassword = req.body.newPassword;

jwt.verify(token, SECRET_KEY, function(err, decoded) {
if (err || decoded.id != req.params.id) {
return res.status(401).end('token invalid');
}
usersDB.findOne({'_id': req.params.id}).then(function(user, err) {
if (user && bcrypt.compareSync(oldPassword, user.password)) {
user.password = bcrypt.hashSync(newPassword, saltRounds);
usersDB.update({'_id': req.params.id}, user).then(function(updatedUser, err) {
if (err) {
return res.status(500).end();
}
return res.status(200).end();
});
} else {
return res.status(401).end('user not found or invalid password');
}
});
});
});
app.post('/tokens', function(req, res) {
var userReq = req.body;
try {
Expand Down Expand Up @@ -120,50 +153,50 @@ app.post('/tokens', function(req, res) {
app.post('/users', function(req, res) {
var user = req.body;
try {
// name validation
if (!('name' in user)) {
throw 'name not set';
}
if (typeof user.name !== 'string') {
throw 'name is not a string';
}
if (user.name.length < 3 || user.name.length > 64) {
throw 'name length < 3 or > 64';
}
// password validation
if (!('password' in user)) {
throw 'password not set';
}
if (typeof user.password !== 'string') {
throw 'password is not a string';
}
if (user.password.length < 8) {
throw 'password min length 8';
}
user.name = user.name.trim();
// name validation
if (!('name' in user)) {
throw 'name not set';
}
if (typeof user.name !== 'string') {
throw 'name is not a string';
}
if (user.name.length < 3 || user.name.length > 64) {
throw 'name length < 3 or > 64';
}

// password validation
if (!('password' in user)) {
throw 'password not set';
}
if (typeof user.password !== 'string') {
throw 'password is not a string';
}
if (user.password.length < 8) {
throw 'password min length 8';
}
user.name = user.name.trim();

usersDB.findOne({"name": user.name}).then(function(existingUser) {
if (existingUser) {
res.status(400).send('name already exists');
return;
usersDB.findOne({"name": user.name}).then(function(existingUser) {
if (existingUser) {
res.status(400).send('name already exists');
return;
}
usersDB.insert({
'name': user.name,
'password': bcrypt.hashSync(user.password, saltRounds)
}).then(function(r) {
usersById[r._id] = {
'id': r._id,
'name': user.name
}
usersDB.insert({
'name': user.name,
'password': bcrypt.hashSync(user.password, saltRounds)
}).then(function(r) {
usersById[r._id] = {
'id': r._id,
'name': user.name
}
groupIdsByUserId[r._id] = [];
res.send(jwt.sign(usersById[r._id], SECRET_KEY, { expiresIn: '24h' }));
});
groupIdsByUserId[r._id] = [];
res.send(jwt.sign(usersById[r._id], SECRET_KEY, { expiresIn: '24h' }));
});
} catch (e) {
console.error(e);
res.status(500).send(e);
}
});
} catch (e) {
console.error(e);
res.status(500).send(e);
}
});

io.use((socket, next) => {
Expand Down
18 changes: 18 additions & 0 deletions src/main/adoc/rest/_users.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ Response
[source]
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImQ2M2MyODBhLTNiYjUtNGZlOC1iY2RhLWNlYWJlMmFiYzIyMiIsIm5hbWUiOiJteSBuYW1lIiwiaWF0IjoxNTAxMjY0Mzk2LCJleHAiOjE1MDEzNTA3OTZ9.50sT-mmp0OtdsHqsxrjOhwQnoyhFMAXdapcShnET8lsasd


=== PUT
==== Change Password
[source]
/users/:id

Request Header
[source]
Content-type: application/json
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImQ2M2MyODBhLTNiYjUtNGZlOC1iY2RhLWNlYWJlMmFiYzIyMiIsIm5hbWUiOiJteSBuYW1lIiwiaWF0IjoxNTAxMjY0Mzk2LCJleHAiOjE1MDEzNTA3OTZ9.50sT-mmp0OtdsHqsxrjOhwQnoyhFMAXdapcShnET8lsasd

Request Body
[source]
{
"oldPassword": "secret",
"newPassword": "s3cr3t"
}

=== GET
List of all users on and offline

Expand Down