Skip to content

Commit

Permalink
Moving to babel and mocha.
Browse files Browse the repository at this point in the history
  • Loading branch information
gevorg committed Jun 10, 2016
1 parent f48966b commit 305dca5
Show file tree
Hide file tree
Showing 17 changed files with 310 additions and 158 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
2 changes: 2 additions & 0 deletions .npmignore
Expand Up @@ -4,3 +4,5 @@ node_modules
.idea
htdigest.iml
.settings
src
test
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -28,7 +28,7 @@ $ htdigest [-c] passwordfile realm username

## Running tests

It uses [nodeunit](https://github.com/caolan/nodeunit/), so just run following command in package directory:
It uses [mocha](https://mochajs.org/), so just run following command in package directory:

```bash
$ npm test
Expand All @@ -50,8 +50,9 @@ You can find list of issues using **[this link](http://github.com/http-auth/htdi

## Development dependencies

- **[coffee-script](http://coffeescript.org/)** - CoffeeScript is a little language that compiles into JavaScript.
- **[nodeunit](https://github.com/caolan/nodeunit/)** - Easy unit testing in node.js and the browser, based on the assert module.
- **[babel](https://babeljs.io/)** - compiler for writing next generation JavaScript..
- **[mocha](https://mochajs.org/)** - simple, flexible, fun javascript test framework for node.js & the browser.
- **[chai](http://chaijs.com/)** - BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.

## License

Expand Down
3 changes: 2 additions & 1 deletion bin/htdigest
@@ -1,3 +1,4 @@
#!/usr/bin/env node

require("../gensrc/htdigest");
var settings = require('../package.json');
require("../gensrc/htdigest").default(settings.version, process.argv);
File renamed without changes.
File renamed without changes.
22 changes: 15 additions & 7 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "htdigest",
"description": "Node.js package for HTTP Digest Authentication password file utility.",
"version": "2.1.1",
"version": "2.1.2",
"author": "Gevorg Harutyunyan (http://github.com/gevorg)",
"maintainers": [
{
Expand Down Expand Up @@ -33,16 +33,24 @@
"prompt": "^1.0.0"
},
"devDependencies": {
"coffee-script": "^1.10.0",
"nodeunit": "^0.9.1"
"babel-cli": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"mocha": "^2.5.3",
"chai": "^3.5.0"
},
"engines": {
"node": ">=0.4.1"
},
"scripts": {
"test": "node ./node_modules/nodeunit/bin/nodeunit tests",
"prepublish": "rm -rf gensrc && node ./node_modules/coffee-script/bin/coffee --compile -o gensrc src",
"pretest": "npm run prepublish"
"test": "mocha --compilers js:babel-core/register",
"compile": "babel src -d gensrc",
"prepublish": "npm run compile",
"pretest": "npm run compile"
},
"keywords": ["htdigest", "http", "digest", "authentication"]
"keywords": [
"htdigest",
"http",
"digest",
"authentication"
]
}
7 changes: 0 additions & 7 deletions src/htdigest.coffee

This file was deleted.

30 changes: 30 additions & 0 deletions src/htdigest.js
@@ -0,0 +1,30 @@
'use strict';

// Importing local modules.
import program from 'commander'
import * as processor from './processor'

// Parses and processes command line arguments.
export default function(version, args) {
// Setup.
program
.version(version)
.usage("[options] passwordfile realm username")
.option('-c, --create', "Create a new file.");

// Help option.
program.on('--help', function () {
console.log(`
Examples:
htdigest [-c] passwordfile realm username
`);
});

// Parse options.
program.parse(args);

// Process program output.
processor.exec(program);
}
77 changes: 0 additions & 77 deletions src/processor.coffee

This file was deleted.

101 changes: 101 additions & 0 deletions src/processor.js
@@ -0,0 +1,101 @@
"use strict";

// Utils.
import * as utils from './utils'

// FS.
import fs from 'fs'

// Prompt module.
import prompt from 'prompt'

// Sync file function.
export function syncFile(program) {
// Read params.
let passwordFile = program.args[0];
let realm = program.args[1];
let username = program.args[2];

// Encode file data.
let writeData = utils.encode(program);

// Collectors.
let found = false;
let newLines = [];

// Not creating file.
if (!program.create) {
// Check if file exists.
if (!fs.existsSync(passwordFile)) {
console.error(`Cannot modify file ${passwordFile}; use '-c' to create it.`);
return
}

// Read lines.
let lines = fs.readFileSync(passwordFile, 'UTF-8').split("\n");

// Loop lines.
lines.forEach(line => {
if (line.indexOf(`${username}:${realm}:`) === 0) {
found = true;
newLines.push(writeData);
console.log(`Changing password for user ${username} in realm ${realm}.`);
} else {
newLines.push(line);
}
});
}

// Adding user to existing file.
if (!found) {
newLines.push(writeData);
console.log(`Adding password for user ${username} in realm ${realm}.`);
}

// Write data.
fs.writeFileSync(passwordFile, newLines.join("\n") + "\n", 'UTF-8');
}

// Read password.
export function readPassword(program) {
prompt.message = "";
prompt.delimiter = "";

let passportOption = [{name: 'password', description: 'New password:', hidden: true}];
let rePassportOption = [{name: 'rePassword', description: 'Re-type new password:', hidden: true}];

// Try to read password.
setTimeout(function () {
prompt.get(passportOption, function (err, result) {
if (!err) {
let password = result.password;
setTimeout(function () {
prompt.get(rePassportOption, function (err, result) {
if (!err && password == result.rePassword) {
program.args.push(password);

try {
syncFile(program);
} catch (err) {
console.error(err.message);
}
} else {
console.error("\nPassword verification error.");
}
});
}, 50);
} else {
console.error("\nPassword verification error.");
}
});
}, 50);
}

// Process command.
export function exec(program) {
if (program.args.length === 3) {
readPassword(program);
} else {
program.help();
}
}
23 changes: 0 additions & 23 deletions src/program.coffee

This file was deleted.

20 changes: 0 additions & 20 deletions src/utils.coffee

This file was deleted.

26 changes: 26 additions & 0 deletions src/utils.js
@@ -0,0 +1,26 @@
"use strict";

// Importing crypto module.
import crypto from 'crypto'

// md5 function.
export function md5(str) {
let hash = crypto.createHash('MD5');
hash.update(str);

return hash.digest('hex');
}

// encode function.
export function encode(program) {
// Prepare arguments.
let realm = program.args[1];
let username = program.args[2];
let password = program.args[3];

// Generate hash.
let hash = md5(`${username}:${realm}:${password}`);

// Final result.
return `${username}:${realm}:${hash}`;
}

0 comments on commit 305dca5

Please sign in to comment.