Skip to content

Commit d587ccb

Browse files
committed
Add hash-pw option
1 parent 023a69e commit d587ccb

File tree

4 files changed

+125
-10
lines changed

4 files changed

+125
-10
lines changed

lib/commands/hash.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2015 IBM Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
var when = require("when");
18+
var prompt = require("../prompt");
19+
try { bcrypt = require('bcrypt'); }
20+
catch(e) { bcrypt = require('bcryptjs'); }
21+
22+
function command(argv,result) {
23+
return when.promise(function(resolve) {
24+
prompt.read({prompt:"Password:".bold,silent: true},function(err, password) {
25+
if (password) {
26+
result.log(bcrypt.hashSync(password, 8));
27+
}
28+
resolve();
29+
});
30+
});
31+
}
32+
command.name = "hash-pwd";
33+
command.usage = command.name;
34+
command.description = "Creates a password hash suitable for use with adminAuth or httpNodeAuth";
35+
36+
module.exports = command;

node-red-admin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ var commands = {
3232
"search": require("./lib/commands/search"),
3333
"install": require("./lib/commands/install"),
3434
"remove": require("./lib/commands/remove"),
35-
"login": require("./lib/commands/login")
35+
"login": require("./lib/commands/login"),
36+
"hash-pw": require("./lib/commands/hash")
3637
};
3738

3839

@@ -51,7 +52,9 @@ function help() {
5152
" disable\n" +
5253
" search\n" +
5354
" install\n" +
54-
" remove\n";
55+
" remove\n" +
56+
" hash-pw\n"
57+
;
5558
console.log(helpText);
5659
}
5760

@@ -67,4 +70,3 @@ if (command) {
6770
} else {
6871
help();
6972
}
70-

package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-admin",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "The Node-RED admin command line interface",
55
"homepage": "http://nodered.org",
66
"license": "Apache-2.0",
@@ -17,21 +17,25 @@
1717
],
1818
"preferGlobal": "true",
1919
"dependencies": {
20+
"bcryptjs": "2.3.0",
21+
"cli-table": "0.3.1",
2022
"colors": "1.1.2",
2123
"minimist": "1.2.0",
22-
"request": "2.64.0",
23-
"when": "3.7.3",
24-
"cli-table": "0.3.1",
25-
"read": "1.0.7"
24+
"read": "1.0.7",
25+
"request": "2.67.0",
26+
"when": "3.7.5"
2627
},
2728
"devDependencies": {
2829
"grunt": "0.4.5",
2930
"grunt-cli": "0.1.13",
3031
"grunt-contrib-jshint": "0.11.3",
3132
"grunt-simple-mocha": "^0.4.0",
32-
"mocha": "2.3.3",
33+
"mocha": "2.3.4",
3334
"should": "6.0.3",
34-
"sinon": "1.17.1"
35+
"sinon": "1.17.2"
36+
},
37+
"optionalDependencies": {
38+
"bcrypt":"0.8.5"
3539
},
3640
"bin": {
3741
"node-red-admin": "node-red-admin.js"

test/lib/commands/hash_spec.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright 2015 IBM Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
var command = require("../../../lib/commands/hash");
18+
19+
var prompt = require("../../../lib/prompt");
20+
21+
var should = require("should");
22+
var sinon = require("sinon");
23+
var when = require("when");
24+
25+
26+
var request = require("../../../lib/request");
27+
try { bcrypt = require('bcrypt'); }
28+
catch(e) { bcrypt = require('bcryptjs'); }
29+
30+
var result = require("./result_helper");
31+
32+
describe("commands/hash-pw", function() {
33+
afterEach(function() {
34+
result.reset();
35+
prompt.read.restore();
36+
});
37+
it('generates a bcrypt hash of provided password',function(done) {
38+
sinon.stub(prompt,"read",function(opts,callback) {
39+
callback(null,"a-test-password");
40+
});
41+
42+
command({},result).then(function() {
43+
result.log.calledOnce.should.be.true;
44+
var hash = result.log.firstCall.args[0];
45+
bcrypt.compare("a-test-password",hash,function(err,match) {
46+
match.should.be.true
47+
done();
48+
});
49+
});
50+
});
51+
it('ignores blank password',function(done) {
52+
sinon.stub(prompt,"read",function(opts,callback) {
53+
callback(null,"");
54+
});
55+
56+
command({},result).then(function() {
57+
result.log.called.should.be.false;
58+
done();
59+
});
60+
});
61+
it('ignores null password',function(done) {
62+
sinon.stub(prompt,"read",function(opts,callback) {
63+
callback(null,null);
64+
});
65+
66+
command({},result).then(function() {
67+
result.log.called.should.be.false;
68+
done();
69+
});
70+
});
71+
72+
73+
});

0 commit comments

Comments
 (0)