38 lines
1.3 KiB
JavaScript
38 lines
1.3 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');
|
|
|
|
// Get username from environment variable or use default
|
|
const USERNAME = process.env.AUTH_USERNAME || 'user';
|
|
|
|
// Get password hash from environment variable
|
|
const passwordHash = process.env.PASSWORD_HASH || '$2a$10$EgxHKjDDFcZKtQY9hl/N4.QvEQHCXVnQXw9dzFYlUDVKOcLMGp9eq';
|
|
|
|
// For Nginx basic auth, we need to use the format: username:{PLAIN}password
|
|
// This is simpler and more reliable than trying to use bcrypt hashes with Nginx
|
|
// Extract the original password from environment variable if available
|
|
const plainPassword = process.env.AUTH_PASSWORD || 'password';
|
|
|
|
// Format for .htpasswd with plaintext password
|
|
const htpasswdContent = `${USERNAME}:{PLAIN}${plainPassword}`;
|
|
|
|
// 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');
|