emotionctl
Version:
A secure terminal-based journaling system designed as a safe space for developers going through heartbreak, breakups, or betrayal
149 lines • 4.94 kB
JavaScript
;
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;
};
})();
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthManager = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const os_1 = require("os");
const CryptoService_1 = require("./CryptoService");
class AuthManager {
constructor() {
this.passwordHash = null;
this.ensureConfigDir();
}
/**
* Ensures the configuration directory exists
*/
async ensureConfigDir() {
try {
await fs.ensureDir(_a.CONFIG_DIR);
}
catch (error) {
throw new Error(`Failed to create config directory: ${error}`);
}
}
/**
* Checks if the journal is already initialized
*/
async isInitialized() {
try {
return await fs.pathExists(_a.AUTH_FILE);
}
catch (error) {
return false;
}
}
/**
* Initializes the journal with a password
*/
async initialize(password) {
if (await this.isInitialized()) {
throw new Error('Journal is already initialized');
}
try {
this.passwordHash = CryptoService_1.CryptoService.hashPassword(password);
const authData = {
passwordHash: this.passwordHash,
created: new Date().toISOString(),
version: '1.0.0'
};
await fs.writeJson(_a.AUTH_FILE, authData, { spaces: 2 });
}
catch (error) {
throw new Error(`Failed to initialize authentication: ${error}`);
}
}
/**
* Authenticates with the provided password
*/
async authenticate(password) {
try {
if (!await this.isInitialized()) {
throw new Error('Journal is not initialized. Run "emotionctl init" first.');
}
const authData = await fs.readJson(_a.AUTH_FILE);
const isValid = CryptoService_1.CryptoService.verifyPassword(password, authData.passwordHash);
if (isValid) {
this.passwordHash = authData.passwordHash;
}
return isValid;
}
catch (error) {
throw new Error(`Authentication failed: ${error}`);
}
}
/**
* Changes the password
*/
async changePassword(currentPassword, newPassword) {
if (!await this.authenticate(currentPassword)) {
throw new Error('Current password is incorrect');
}
try {
const authData = await fs.readJson(_a.AUTH_FILE);
authData.passwordHash = CryptoService_1.CryptoService.hashPassword(newPassword);
authData.lastModified = new Date().toISOString();
await fs.writeJson(_a.AUTH_FILE, authData, { spaces: 2 });
this.passwordHash = authData.passwordHash;
}
catch (error) {
throw new Error(`Failed to change password: ${error}`);
}
}
/**
* Gets the configuration directory path
*/
getConfigDir() {
return _a.CONFIG_DIR;
}
/**
* Checks if user is currently authenticated
*/
isAuthenticated() {
return this.passwordHash !== null;
}
/**
* Clears authentication state
*/
logout() {
this.passwordHash = null;
}
}
exports.AuthManager = AuthManager;
_a = AuthManager;
AuthManager.CONFIG_DIR = path.join((0, os_1.homedir)(), '.emotionctl');
AuthManager.AUTH_FILE = path.join(_a.CONFIG_DIR, 'auth.json');
//# sourceMappingURL=AuthManager.js.map