WeightTracker/generate-htpasswd.js

34 lines
1.0 KiB
JavaScript

/**
* Generate .htpasswd file for Nginx basic authentication
*
* This script creates a .htpasswd file from the bcrypt hash provided in the
* PASSWORD_HASH environment variable.
*/
const fs = require('fs');
const path = require('path');
// Default username
const USERNAME = 'user';
// Get password hash from environment variable
const passwordHash = process.env.PASSWORD_HASH || '$2a$10$EgxHKjDDFcZKtQY9hl/N4.QvEQHCXVnQXw9dzFYlUDVKOcLMGp9eq';
// Format for .htpasswd: username:$2y$...hash...
// Note: Nginx requires $2y$ format instead of bcrypt's $2a$ format
const htpasswdContent = `${USERNAME}:${passwordHash.replace('$2a$', '$2y$')}`;
// Path to the .htpasswd file
const htpasswdPath = '/etc/nginx/.htpasswd';
// Write the .htpasswd file
try {
fs.writeFileSync(htpasswdPath, htpasswdContent);
console.log(`Generated .htpasswd file at ${htpasswdPath}`);
} catch (error) {
console.error(`Error generating .htpasswd file: ${error.message}`);
process.exit(1);
}
console.log('Basic authentication setup complete');