cloakx
Version:
Cloakx is a secure, lightweight CLI tool to manage your development secrets locally — no cloud, no hassle. Store, retrieve, and manage secrets across projects with encryption and ease. 🔐 Perfect for solo devs, indie hackers, and teams who value speed, si
65 lines (64 loc) • 2.71 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSession = createSession;
exports.getSessionPassword = getSessionPassword;
exports.getSession = getSession;
exports.clearSession = clearSession;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const paths_1 = require("../config/paths");
function createSession(password, username = 'user') {
const sessionDir = path_1.default.dirname(paths_1.sessionPath);
// Create directory if it doesn't exist
if (!fs_1.default.existsSync(sessionDir)) {
fs_1.default.mkdirSync(sessionDir, { recursive: true });
}
const session = {
// 🔐 Encode password as base64 (same as your older version)
token: Buffer.from(password).toString('base64'),
username,
createdAt: new Date().toISOString(), // human-readable timestamp
createdAtTimestamp: Date.now() // numeric timestamp for expiry checks
};
fs_1.default.writeFileSync(paths_1.sessionPath, JSON.stringify(session, null, 2));
}
function getSessionPassword() {
// if (!fs.existsSync(sessionPath)) throw new Error('No active session. Please login.');
const session = JSON.parse(fs_1.default.readFileSync(paths_1.sessionPath, 'utf-8'));
const now = Date.now();
// if (!session.createdAtTimestamp || now - session.createdAtTimestamp > 30 * 60 * 1000) {
// fs.unlinkSync(sessionPath);
// throw new Error('Session expired. Please login again.');
// }
// Decode base64 back to original password
return Buffer.from(session.token, 'base64').toString('utf-8');
}
function getSession() {
if (!fs_1.default.existsSync(paths_1.sessionPath))
return null;
try {
const session = JSON.parse(fs_1.default.readFileSync(paths_1.sessionPath, 'utf-8'));
const now = Date.now();
const isExpired = !session.createdAtTimestamp || now - session.createdAtTimestamp > 30 * 60 * 1000;
// if (isExpired) {
// fs.unlinkSync(sessionPath);
// return null;
// }
return {
username: session.username || 'user', // default, or fetch from vault if available
token: session.token,
createdAt: session.createdAt,
expiresAt: new Date(session.createdAtTimestamp + 30 * 60 * 1000).toISOString()
};
}
catch (err) {
return null;
}
}
function clearSession() {
if (fs_1.default.existsSync(paths_1.sessionPath))
fs_1.default.unlinkSync(paths_1.sessionPath);
}