124 lines
5.1 KiB
JavaScript
124 lines
5.1 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.PasswordService = void 0;
|
|
const bcrypt = __importStar(require("bcrypt"));
|
|
const Logger_1 = require("./Logger");
|
|
class PasswordService {
|
|
/**
|
|
* Hashes a plain text password using bcrypt
|
|
* @param password - The plain text password to hash
|
|
* @returns Promise<string> - The hashed password
|
|
*/
|
|
static async hashPassword(password) {
|
|
try {
|
|
if (!password || typeof password !== 'string') {
|
|
throw new Error('Password must be a non-empty string');
|
|
}
|
|
return await bcrypt.hash(password, this.SALT_ROUNDS);
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('PasswordService.hashPassword error', error instanceof Error ? error : new Error(String(error)));
|
|
if (error instanceof Error && error.message === 'Password must be a non-empty string') {
|
|
throw error; // Re-throw validation errors as-is
|
|
}
|
|
throw new Error('Failed to hash password');
|
|
}
|
|
}
|
|
/**
|
|
* Verifies a plain text password against a hashed password
|
|
* @param password - The plain text password to verify
|
|
* @param hashedPassword - The hashed password to compare against
|
|
* @returns Promise<boolean> - True if password matches, false otherwise
|
|
*/
|
|
static async verifyPassword(password, hashedPassword) {
|
|
try {
|
|
if (!password || typeof password !== 'string') {
|
|
return false; // Invalid input should return false, not throw
|
|
}
|
|
if (!hashedPassword || typeof hashedPassword !== 'string') {
|
|
return false; // Invalid input should return false, not throw
|
|
}
|
|
return await bcrypt.compare(password, hashedPassword);
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('PasswordService.verifyPassword error', error instanceof Error ? error : new Error(String(error)));
|
|
return false; // Return false on error instead of throwing
|
|
}
|
|
}
|
|
/**
|
|
* Validates password strength requirements
|
|
* @param password - The password to validate
|
|
* @returns object - Object containing isValid boolean and error messages
|
|
*/
|
|
static validatePasswordStrength(password) {
|
|
try {
|
|
const errors = [];
|
|
if (!password || typeof password !== 'string') {
|
|
errors.push('Password must be provided as a string');
|
|
return { isValid: false, errors };
|
|
}
|
|
if (password.length < 8) {
|
|
errors.push('Password must be at least 8 characters long');
|
|
}
|
|
if (!/[A-Z]/.test(password)) {
|
|
errors.push('Password must contain at least one uppercase letter');
|
|
}
|
|
if (!/[a-z]/.test(password)) {
|
|
errors.push('Password must contain at least one lowercase letter');
|
|
}
|
|
if (!/\d/.test(password)) {
|
|
errors.push('Password must contain at least one number');
|
|
}
|
|
if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
|
|
errors.push('Password must contain at least one special character');
|
|
}
|
|
return {
|
|
isValid: errors.length === 0,
|
|
errors
|
|
};
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('PasswordService.validatePasswordStrength error', error instanceof Error ? error : new Error(String(error)));
|
|
return {
|
|
isValid: false,
|
|
errors: ['Password validation failed due to internal error']
|
|
};
|
|
}
|
|
}
|
|
}
|
|
exports.PasswordService = PasswordService;
|
|
PasswordService.SALT_ROUNDS = 12;
|
|
//# sourceMappingURL=PasswordService.js.map
|