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

add a user with node add_user #29

Merged
merged 8 commits into from
May 4, 2015
Merged
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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ they will be visible to forever, via sudo only. [\[1\]][1]

## Add a users.json file

Add a `users.json` to the root of forever-webui
``` [{
"username" : "here the name of your user",
"password" : "here the password for your user"
}]
```
node add_user
```
this will create a users.json file if it doesn't exist yet and adds a new user via prompt.

## Run Tests

Expand Down
71 changes: 71 additions & 0 deletions add_user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var colors = require('colors/safe');
var fs = require('fs');
var readline = require('readline');
var CryptoJS = require("crypto-js");

var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});

var ticker = 0;
var user = {};
var users;
var usersFile;

if (fs.existsSync('users.json')) {
try {
var usersFile = fs.readFileSync('users.json', 'utf8');
users = JSON.parse(usersFile);
console.info(colors.green('Found users.json file'));
console.info(colors.green('Contains '+ users.length + ' users'));
} catch (e) {
console.error(colors.red('users.json is not a valid json'));
}
} else {
var fd = fs.openSync('users.json', 'w');
console.info(colors.green('users.json file created'));
users = [];
}


console.log(colors.yellow.underline('Create a user:'));
console.log(colors.yellow('Enter the username:'));

// found on http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
function generateUUID(){
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
}


rl.on('line', function(line){
switch (ticker) {
case 0:
user.username = line;
console.log(colors.yellow('Enter the password:'));

break;
case 1:
user.password = CryptoJS.SHA256(line).toString(CryptoJS.enc.Hex);
user.id = generateUUID();
users.push(user);
fs.writeFile("users.json", JSON.stringify(users), function(err) {
if(err) {
return console.log(err);
}

console.log("The file was saved!");
});
rl.close();
break;
}
ticker++;
});

18 changes: 13 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@
session = require('express-session');
errorhandler = require('errorhandler');
router = express.Router();
CryptoJS = require("crypto-js");

var users;
try {
var usersFile = fs.readFileSync('users.json', 'utf8');
users = JSON.parse(usersFile);
if (users.length === 0) {
throw new Error('no users in users.json');
}
logger.info('Loaded users');
} catch (e) {
logger.warn('Make a users.json file in the root of the project');
logger.warn('Run: node add_user.js to add a user.');
logger.error(e);
process.exit(0);
}

process.on("uncaughtException", function(err) {
Expand Down Expand Up @@ -125,9 +130,9 @@
};

function findById(id, fn) {
var idx = id - 1;
if (users[idx]) {
fn(null, users[idx]);
var user = _.findWhere(users,{id:id});
if (user) {
fn(null, user);
} else {
fn(new Error('User ' + id + ' does not exist'));
}
Expand Down Expand Up @@ -183,13 +188,16 @@

passport.use(new LocalStrategy(
function(username, password, done) {
if (!users) {
return done(null, false, { message: 'The system has no users'});
}
process.nextTick(function () {
findByUsername(username, function(err, user) {
if (err) {
return done(err);
}
if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
if (user.password != password) { return done(null, false, { message: 'Invalid password' }); }
if (user.password != CryptoJS.SHA256(password).toString(CryptoJS.enc.Hex)) { return done(null, false, { message: 'Invalid password' }); }
return done(null, user);
});
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"coffee-script": "1.5.0",
"colors": "^1.0.3",
"cookie-parser": "^1.3.4",
"crypto-js": "^3.1.4",
"ejs": "0.8.3",
"errorhandler": "^1.3.5",
"express": "4.12.2",
Expand Down
2 changes: 1 addition & 1 deletion public/dist/foreverui.min.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! forever-webui - v0.2.1 - 2015-04-28
/*! forever-webui - v0.2.1 - 2015-05-04
* Copyright (c) 2015 ; */
html,body{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,cite,code,del,dfn,em,img,q,s,samp,small,strike,strong,sub,sup,tt,var,dd,dl,dt,li,ol,ul,fieldset,form,label,legend,button,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;font-weight:normal;font-style:normal;font-size:100%;line-height:1;font-family:inherit;}
Expand Down
2 changes: 1 addition & 1 deletion public/dist/foreverui.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.