/** * Runs the command. * * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $config = OTPGen::LoadConfig(); $keyArgument = $input->getArgument('name'); if (!key_exists($keyArgument, $config) && !is_numeric($keyArgument)) { $output->writeln('The entry ' . $keyArgument . ' does not exist in your library.'); return 0; } if (is_numeric($keyArgument)) { if (intval($keyArgument) > count($config) - 1) { $output->writeln('There is no entry in your library with the id: ' . $keyArgument . '.'); return 0; } $count = 0; foreach ($config as $key => $WhyBother) { if ($count == intval($keyArgument)) { $keyArgument = $key; continue; } $count++; } } unset($config[$key]); $otpFile = fopen(OTPGEN_WORKING_DIR . '.otp', 'w') or die('Wasn\'t able to open library are you sure I have enough permissions?'); fwrite($otpFile, OTPGen::toYAML($config)); fclose($otpFile); $output->writeln('Removed ' . $keyArgument . ' from your library.'); return 0; }
/** * Runs the command. * * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $config = OTPGen::LoadConfig(); $keyArgument = $input->getArgument('name'); if (!key_exists($keyArgument, $config) && !is_numeric($keyArgument)) { $output->writeln('The entry ' . $keyArgument . ' does not exist in your library. Add it by using the add command.'); return 0; } if (is_numeric($keyArgument)) { if (intval($keyArgument) > count($config) - 1) { $output->writeln('There is no entry in your library with the id: ' . $keyArgument . '.'); return 0; } $count = 0; foreach ($config as $key => $WhyBother) { if ($count == intval($keyArgument)) { $keyArgument = $key; continue; } $count++; } } $totp = new TOTP(); $totp->setDigits($config[$keyArgument]['size'])->setDigest($config[$keyArgument]['digest'])->setInterval($config[$keyArgument]['interval'])->setSecret($config[$keyArgument]['token']); $output->writeln('Your token is: <options=bold>' . $totp->now() . '</>'); return 0; }
/** * Initializes the command just after the input has been validated. * * This is mainly useful when a lot of commands extends one main command * where some things need to be initialized based on the input arguments and options. * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance */ public function initialize(InputInterface $input, OutputInterface $output) { $this->startTime = microtime(true); $this->input = $input; $this->output = $output; if ($this->loadConfig) { $this->config = OTPGen::loadConfig(); } }
/** * Runs the command. * * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $config = OTPGen::LoadConfig(); $count = 0; foreach ($config as $key => $whyBother) { $output->writeln('(' . $count . ') ' . $key); $count++; } return 0; }
/** * @throws \RuntimeException */ public static function loadConfig() { if (!file_exists(OTPGEN_WORKING_DIR . '.otp')) { throw new \RuntimeException('Directory is not a OTPGen directory: ' . OTPGEN_WORKING_DIR . ' or add an OTP token.'); } $config = OTPGen::fromYAML(file_get_contents(OTPGEN_WORKING_DIR . '.otp')); if (!$config || !is_array($config)) { throw new \RuntimeException('Error: ' . OTPGEN_WORKING_DIR . '.otp file is not valid YAML, or is empty.'); } return $config; }
/** * Runs the command. * * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); //Ask te user for a name which will be used as key $question = new Question('Enter a name: '); $question->setValidator(function ($answer) { if (empty($answer)) { throw new \RuntimeException('Please enter a name.'); } if (strrpos($answer, " ") === true) { throw new \RuntimeException('No spaces allowed in your name.'); } return $answer; }); $name = $helper->ask($input, $output, $question); $question = new Question('Enter your token: '); $question->setValidator(function ($answer) { if (empty($answer)) { throw new \RuntimeException('Please enter your token.'); } }); $token = $helper->ask($input, $output, $question); $question = new Question('Enter the digest algorithm (default sha1): ', 'sha1'); $digest = $helper->ask($input, $output, $question); $question = new Question('Enter the lenght of the generated token (default 6): ', 6); $size = $helper->ask($input, $output, $question); $question = new Question('Enter the interval of the token (default 30): ', 30); $interval = $helper->ask($input, $output, $question); $data = array($name => array('token' => $token, 'digest' => $digest, 'size' => $size, 'interval' => $interval)); if (file_exists(OTPGEN_WORKING_DIR . '.otp')) { $config = OTPGen::loadConfig(); $data = array_merge($config, $data); } $otpFile = fopen(OTPGEN_WORKING_DIR . '.otp', 'w') or die('Wasn\'t able to open or create library are you sure I have enough permissions?'); fwrite($otpFile, OTPGen::toYAML($data)); fclose($otpFile); $output->writeln('Added ' . $name . ' to your library.'); return 0; }
* Specify the working directory, if it hasn't been set yet. */ if (!defined('OTPGEN_WORKING_DIR')) { if (!getenv("OTPGEN_DIR")) { $cwd = dirname(__FILE__) . DIRECTORY_SEPARATOR; $cwd = str_replace('\\', '/', $cwd); } else { $cwd = rtrim(getenv("OTPGEN_DIR"), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; } define('OTPGEN_WORKING_DIR', $cwd); } /** * Load all the commands and create the instance */ use beagon\OTPGen\Command\CodeCommand; use beagon\OTPGen\Command\AddCommand; use beagon\OTPGen\Command\LibraryCommand; use beagon\OTPGen\Command\DeleteCommand; use beagon\OTPGen\OTPGen; $composerData = file_get_contents(__DIR__ . '/composer.json'); $composerData = json_decode($composerData, true); $version = $composerData['version']; $application = new OTPGen('OTPGen', $version); $application->add(new CodeCommand()); $application->add(new AddCommand()); $application->add(new LibraryCommand()); $application->add(new DeleteCommand()); /** * We return it so the CLI controller in /OTPGen can run it. */ return $application;