doclyft
Version:
CLI for DocLyft - Interactive documentation generator with hosted documentation support
161 lines (160 loc) • 5.21 kB
JavaScript
;
/**
* Command protection utility for applying authentication guards
*/
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PUBLIC_COMMANDS = exports.PROTECTED_COMMANDS = void 0;
exports.protectCommand = protectCommand;
exports.protectCommands = protectCommands;
exports.shouldProtectCommand = shouldProtectCommand;
exports.createProtectedCommand = createProtectedCommand;
exports.getRequiredAuthInfo = getRequiredAuthInfo;
exports.getOptionalAuthInfo = getOptionalAuthInfo;
const commander_1 = require("commander");
const auth_1 = __importStar(require("../middleware/auth"));
const chalk_1 = __importDefault(require("chalk"));
/**
* Commands that require authentication
*/
exports.PROTECTED_COMMANDS = [
'analyze',
'generate',
'push',
'github-token',
'history',
'status',
'test',
'config'
];
/**
* Commands that don't require authentication (public utilities)
*/
exports.PUBLIC_COMMANDS = [
'login',
'logout',
'help',
'version',
'--help',
'--version'
];
/**
* Apply authentication guard to a command
*/
function protectCommand(command) {
return command.hook('preAction', async (thisCommand) => {
try {
await auth_1.default.requireAuth();
}
catch (error) {
if (error instanceof auth_1.AuthError) {
console.log(auth_1.default.formatAuthError(error, thisCommand.name()));
console.log(''); // Empty line for better readability
auth_1.default.showAuthGuidance();
process.exit(1);
}
else {
console.log(chalk_1.default.red('❌ Authentication check failed'));
console.log(chalk_1.default.yellow('Please try running `doclyft login` first'));
process.exit(1);
}
}
});
}
/**
* Apply authentication guards to multiple commands
*/
function protectCommands(commands) {
commands.forEach(command => {
if (shouldProtectCommand(command.name())) {
protectCommand(command);
}
});
}
/**
* Check if a command should be protected based on its name
*/
function shouldProtectCommand(commandName) {
// Extract main command name (handle subcommands)
const mainCommand = commandName.split(' ')[0];
return exports.PROTECTED_COMMANDS.includes(mainCommand) ||
!exports.PUBLIC_COMMANDS.includes(mainCommand);
}
/**
* Create an authentication-aware command
* Automatically applies auth guard to non-public commands
*/
function createProtectedCommand(name, description) {
const command = new commander_1.Command(name).description(description);
if (shouldProtectCommand(name)) {
protectCommand(command);
}
return command;
}
/**
* Get authentication info for use in commands
* Assumes command has already passed auth guard
*/
function getRequiredAuthInfo() {
const authInfo = auth_1.default.getAuthInfo();
if (!authInfo.user_id || !authInfo.user_email || !authInfo.token) {
throw new Error('Authentication info not available. This should not happen after auth guard.');
}
return {
user_id: authInfo.user_id,
user_email: authInfo.user_email,
token: authInfo.token
};
}
/**
* Safe auth info getter that won't throw
* Returns undefined if not authenticated
*/
function getOptionalAuthInfo() {
return auth_1.default.getAuthInfo();
}
exports.default = {
protectCommand,
protectCommands,
shouldProtectCommand,
createProtectedCommand,
getRequiredAuthInfo,
getOptionalAuthInfo,
PROTECTED_COMMANDS: exports.PROTECTED_COMMANDS,
PUBLIC_COMMANDS: exports.PUBLIC_COMMANDS
};