htpasswd-mgr
Advanced tools
Comparing version 1.1.7 to 2.0.0-beta.0
@@ -1,23 +0,19 @@ | ||
const crypto = require('crypto'), | ||
bcryptLib = require('bcrypt'), | ||
SALT_ROUNDS = 5; | ||
module.exports = { | ||
sha1: sha1, | ||
bcrypt: bcrypt | ||
}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.bcrypt = exports.sha1 = void 0; | ||
const crypto_1 = require("crypto"); | ||
const bcrypt_1 = require("bcrypt"); | ||
const SALT_ROUNDS = 5; | ||
// Return a SHA1 representation of the value | ||
function sha1(value) { | ||
let hash = crypto.createHash('sha1'); | ||
const hash = (0, crypto_1.createHash)('sha1'); | ||
hash.update(value); | ||
return hash.digest('base64'); | ||
} | ||
exports.sha1 = sha1; | ||
function bcrypt(value) { | ||
let salt = bcryptLib.genSaltSync(SALT_ROUNDS); | ||
return bcryptLib.hashSync(value, salt); | ||
const salt = (0, bcrypt_1.genSaltSync)(SALT_ROUNDS); | ||
return (0, bcrypt_1.hashSync)(value, salt); | ||
} | ||
exports.bcrypt = bcrypt; | ||
//# sourceMappingURL=crypto.js.map |
'use strict'; | ||
const { promisify } = require('bluebird'), | ||
fs = require('fs'), | ||
md5 = require('apache-md5'), | ||
crypt = require('apache-crypt'), | ||
writeFileAsync = promisify(fs.writeFile), | ||
crypto = require('./crypto'), | ||
constants = require('./constants'); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HTPasswdAlgorithms = void 0; | ||
const fs_1 = require("fs"); | ||
const apache_md5_1 = __importDefault(require("apache-md5")); | ||
const apache_crypt_1 = __importDefault(require("apache-crypt")); | ||
const crypto_1 = require("./crypto"); | ||
const constants_1 = require("./constants"); | ||
var HTPasswdAlgorithms; | ||
(function (HTPasswdAlgorithms) { | ||
HTPasswdAlgorithms["crypt"] = "crypt"; | ||
HTPasswdAlgorithms["sha"] = "sha"; | ||
HTPasswdAlgorithms["bcrypt"] = "bcrypt"; | ||
HTPasswdAlgorithms["md5"] = "md5"; | ||
})(HTPasswdAlgorithms = exports.HTPasswdAlgorithms || (exports.HTPasswdAlgorithms = {})); | ||
const defaultOptions = { | ||
export: true, | ||
algorithm: HTPasswdAlgorithms.md5, | ||
}; | ||
function initialize(path) { | ||
let htpasswd = {}; | ||
const exports = { | ||
updateState: readFile, | ||
updateFile: exportToFile, | ||
addUser: addUser, | ||
updateUser: updateUser, | ||
upsertUser: addOrUpdateUser, | ||
removeUser: removeUser, | ||
listUsers: listUsers | ||
}; | ||
function readFile() { | ||
if (fs.existsSync(path)) { | ||
let htpasswdContents = fs.readFileSync(path, {encoding: 'utf8'}); | ||
const htpasswd = {}; | ||
async function readFile() { | ||
if ((0, fs_1.existsSync)(path)) { | ||
const htpasswdContents = (0, fs_1.readFileSync)(path, { encoding: 'utf8' }); | ||
parseFile(htpasswdContents); | ||
} | ||
return Promise.resolve(); | ||
return; | ||
} | ||
function parseFile(fileContents) { | ||
let lines = fileContents.trim().split('\n'); | ||
for (let line of lines) { | ||
let [username, password] = line.split(':'); | ||
htpasswd[username] = [password]; | ||
const lines = fileContents.trim().split('\n'); | ||
for (const line of lines) { | ||
const splitLine = line.split(':'); | ||
const [username, password] = splitLine; | ||
htpasswd[username] = password; | ||
} | ||
} | ||
function exportToFile() { | ||
return convertHTPasswdToString() | ||
.then(writeFile); | ||
.then(writeToFile); | ||
} | ||
function writeFile(data) { | ||
return writeFileAsync(path, data, {encoding: 'utf8'}); | ||
function writeToFile(data) { | ||
return new Promise((resolve, reject) => { | ||
(0, fs_1.writeFile)(path, data, { encoding: 'utf8' }, (err) => { | ||
if (err) | ||
reject(err); | ||
else | ||
resolve(true); | ||
}); | ||
}); | ||
} | ||
function convertHTPasswdToString() { | ||
async function convertHTPasswdToString() { | ||
let accumulator = ''; | ||
for (let username in htpasswd) { | ||
let password = htpasswd[username], | ||
userInfo = username + ':' + password + '\n'; | ||
for (const username in htpasswd) { | ||
const password = htpasswd[username], userInfo = username + ':' + password + '\n'; | ||
accumulator += userInfo; | ||
} | ||
return Promise.resolve(accumulator); | ||
return accumulator; | ||
} | ||
function encodePassword(password, algorithm) { | ||
switch (algorithm) { | ||
switch (algorithm?.toLowerCase()) { | ||
case 'crypt': | ||
return crypt(password); | ||
return (0, apache_crypt_1.default)(password); | ||
case 'sha': | ||
return crypto.sha1(password); | ||
return (0, crypto_1.sha1)(password); | ||
case 'bcrypt': | ||
return crypto.bcrypt(password); | ||
return (0, crypto_1.bcrypt)(password); | ||
case 'md5': | ||
default: | ||
return md5(password); | ||
return (0, apache_md5_1.default)(password); | ||
} | ||
} | ||
function optionalExport(exportOption) { | ||
async function optionalExport(exportOption) { | ||
switch (exportOption) { | ||
case false: | ||
return Promise.resolve(); | ||
return; | ||
default: | ||
@@ -84,51 +83,43 @@ return exportToFile(); | ||
} | ||
/** | ||
* Add a user to the htpasswd file and optionally export it to disk | ||
* @param username {String} name of user to be added | ||
* @param password {String} password for the user to be added | ||
* @param options {Object} object with options - example { algorithm: 'crypt', export: false } | ||
*/ | ||
function addUser(username, password, options) { | ||
async function addUser(username, password, options) { | ||
if (htpasswd[username]) { | ||
return Promise.reject(new Error(constants.errors.usernameAlreadyInUse)); | ||
throw new Error(constants_1.errors.usernameAlreadyInUse); | ||
} | ||
return addOrUpdateUser(username, password, options); | ||
} | ||
function updateUser(username, password, options) { | ||
async function updateUser(username, password, options) { | ||
if (!htpasswd[username]) { | ||
return Promise.reject(new Error(constants.errors.noSuchUser)); | ||
throw new Error(constants_1.errors.noSuchUser); | ||
} | ||
return addOrUpdateUser(username, password, options); | ||
} | ||
function addOrUpdateUser(username, password, options) { | ||
const algorithm = options && options.algorithm && options.algorithm.toLowerCase(), | ||
exportOption = options && options.export; | ||
const algorithm = options?.algorithm ?? defaultOptions.algorithm, exportOption = options?.export ?? defaultOptions.export; | ||
htpasswd[username] = encodePassword(password, algorithm); | ||
return optionalExport(exportOption); | ||
} | ||
function removeUser(username, options) { | ||
const exportOption = options && options.export; | ||
const exportOption = options?.export ?? defaultOptions.export; | ||
delete htpasswd[username]; | ||
return optionalExport(exportOption); | ||
} | ||
function listUsers() { | ||
return Promise.resolve(Object.keys(htpasswd)); | ||
async function listUsers() { | ||
return Object.keys(htpasswd); | ||
} | ||
// Update the internal state of the module | ||
readFile(); | ||
return exports; | ||
return { | ||
updateState: readFile, | ||
updateFile: exportToFile, | ||
addUser, | ||
updateUser, | ||
upsertUser: addOrUpdateUser, | ||
removeUser, | ||
listUsers, | ||
}; | ||
} | ||
module.exports = initialize; | ||
exports.default = initialize; | ||
//# sourceMappingURL=manager.js.map |
@@ -5,3 +5,3 @@ { | ||
"description": "HTPasswd Manager for Node.js", | ||
"version": "1.1.7", | ||
"version": "2.0.0-beta.0", | ||
"homepage": "https://mrodrig.github.io/htpasswd-mgr", | ||
@@ -13,7 +13,10 @@ "repository": { | ||
"main": "lib/manager.js", | ||
"types": "./lib/manager.d.ts", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha test/tests.js", | ||
"coverage": "nyc --reporter=lcov --reporter=text-summary --reporter=text _mocha", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls", | ||
"lint": "./node_modules/.bin/eslint lib test" | ||
"build": "npm run lint && npm run test && npm run compile", | ||
"compile": "tsc -p tsconfig.build.json", | ||
"coverage": "nyc npm run test", | ||
"lint": "eslint --ext .js,.ts src test", | ||
"prepublishOnly": "npm run build", | ||
"test": "mocha -r ts-node/register test/index.ts" | ||
}, | ||
@@ -28,4 +31,4 @@ "keywords": [ | ||
"dependencies": { | ||
"apache-crypt": "1.2.5", | ||
"apache-md5": "1.1.7", | ||
"apache-crypt": "1.2.6", | ||
"apache-md5": "1.1.8", | ||
"bcrypt": "5.0.1", | ||
@@ -35,12 +38,20 @@ "bluebird": "3.7.2" | ||
"devDependencies": { | ||
"coveralls": "3.1.1", | ||
"eslint": "8.7.0", | ||
"mocha": "9.2.0", | ||
"@types/bcrypt": "5.0.0", | ||
"@types/mocha": "10.0.1", | ||
"@types/node": "18.15.3", | ||
"@typescript-eslint/eslint-plugin": "5.55.0", | ||
"@typescript-eslint/parser": "5.55.0", | ||
"eslint": "8.36.0", | ||
"eslint-config-google": "0.14.0", | ||
"eslint-plugin-import": "2.27.5", | ||
"mocha": "10.2.0", | ||
"nyc": "15.1.0", | ||
"ts-node": "10.9.1", | ||
"typescript": "5.0.2", | ||
"should": "13.2.3" | ||
}, | ||
"engines": { | ||
"node": ">= 12" | ||
"node": ">= 16" | ||
}, | ||
"license": "MIT" | ||
} |
# htpasswd-mgr - The HTPasswd Manager for Node | ||
[](https://www.npmjs.org/package/htpasswd-mgr) | ||
[](https://www.npmjs.org/package/htpasswd-mgr) | ||
[](https://www.npmjs.org/package/htpasswd-mgr) | ||
[](https://bundlephobia.com/result?p=htpasswd-mgr) | ||
[](https://www.npmjs.org/package/htpasswd-mgr) | ||
[](https://www.npmjs.org/package/htpasswd-mgr) | ||
[](https://bundlephobia.com/result?p=htpasswd-mgr) | ||
[](https://travis-ci.org/mrodrig/htpasswd-mgr) | ||
[](https://coveralls.io/github/mrodrig/htpasswd-mgr?branch=stable) | ||
[](https://codeclimate.com/github/mrodrig/htpasswd-mgr/maintainability) | ||
[](https://github.com/mrodrig/htpasswd-mgr/actions/workflows/automated-tests-workflow.yml) | ||
[](https://coveralls.io/github/mrodrig/htpasswd-mgr?branch=main) | ||
[](https://codeclimate.com/github/mrodrig/htpasswd-mgr/maintainability) | ||
@@ -12,0 +12,0 @@ This module was developed to simplify the management of .htpasswd files from |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
19556
12
176
13
1
1
+ Addedapache-crypt@1.2.6(transitive)
+ Addedapache-md5@1.1.8(transitive)
- Removedapache-crypt@1.2.5(transitive)
- Removedapache-md5@1.1.7(transitive)
Updatedapache-crypt@1.2.6
Updatedapache-md5@1.1.8