Skip to content

Commit

Permalink
wip: ConnectionFactory for dependency injection into console commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pavarnos committed Aug 12, 2018
1 parent 3ffa952 commit f327e6c
Show file tree
Hide file tree
Showing 22 changed files with 393 additions and 224 deletions.
5 changes: 4 additions & 1 deletion bin/threema-gateway
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Symfony\Component\Console\Application;
use Threema\MsgApi\ConnectionFactory;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

$autoloaderInWorkingDirectory = getcwd() . '/vendor/autoload.php';
Expand All @@ -21,6 +22,8 @@ if (!class_exists(AbstractEncryptor::class, true)) {
require_once $composerAutoloadFile;
}

$connectionFactory = new ConnectionFactory();

// https://gist.github.com/sroze/3e8d45d0cdc301debfd2
$application = new Application('Threema Gateway');

Expand All @@ -38,7 +41,7 @@ foreach (new DirectoryIterator(dirname($reflection->getFileName())) as $fileInfo
continue;
}
$className = '\\' . $reflection->getNamespaceName() . '\\' . $fileInfo->getBasename('.php');
$application->add(new $className);
$application->add(new $className($connectionFactory));
}

$application->run();
21 changes: 19 additions & 2 deletions src/Threema/Console/Symfony/AbstractLocalCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\ConnectionFactory;
use Threema\MsgApi\Constants;
use Threema\MsgApi\Exceptions\InvalidArgumentException;
use Threema\MsgApi\Encryptor\AbstractEncryptor;
use Threema\MsgApi\Exceptions\InvalidArgumentException;

abstract class AbstractLocalCommand extends Command
{
Expand All @@ -24,6 +25,15 @@ abstract class AbstractLocalCommand extends Command
/** @var array default option and argument values stored in self::KEY_FILE */
private $defaults = [];

/** @var \Threema\MsgApi\ConnectionFactory */
protected $connectionFactory;

public function __construct(ConnectionFactory $connectionFactory)
{
parent::__construct();
$this->connectionFactory = $connectionFactory;
}

protected function configure()
{
parent::configure();
Expand Down Expand Up @@ -57,6 +67,7 @@ protected function optionalMessageOrStdIn()

protected function getPrivateKey(InputInterface $input, OutputInterface $output): string
{
// returns hex version of key
if (!empty($input->getOption('private-key'))) {
$output->writeln('<error>Private keys on the command line may be insecure. Use --key-file file instead.</error>',
OutputInterface::VERBOSITY_VERBOSE);
Expand All @@ -66,6 +77,7 @@ protected function getPrivateKey(InputInterface $input, OutputInterface $output)

protected function getPublicKey(InputInterface $input): string
{
// returns hex version of key
return $this->getKey($input->getArgument('public-key'), 'public', Constants::PUBLIC_KEY_PREFIX);
}

Expand Down Expand Up @@ -102,6 +114,11 @@ protected function getDefault(string $argumentName): string
return $this->defaults[$argumentName] ?? '';
}

protected function getEncryptor(): AbstractEncryptor
{
return $this->connectionFactory->getEncryptor();
}

private function readStdInput(): string
{
// read console standard input stream. Strip empty / blank lines
Expand All @@ -118,6 +135,6 @@ private function getKey(string $key, string $optionName, string $keyPrefix): str
if (empty($key)) {
throw new InvalidArgumentException(ucfirst($optionName) . ' key invalid or missing');
}
return AbstractEncryptor::getInstance()->hex2bin(str_replace($keyPrefix, '', $key));
return str_replace($keyPrefix, '', $key);
}
}
3 changes: 1 addition & 2 deletions src/Threema/Console/Symfony/AbstractNetworkedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Threema\MsgApi\Connection;
use Threema\MsgApi\Constants;
use Threema\MsgApi\Exceptions\InvalidArgumentException;
use Threema\MsgApi\HttpDriver\CurlHttpDriver;

abstract class AbstractNetworkedCommand extends AbstractLocalCommand
{
Expand All @@ -33,7 +32,7 @@ protected function configure()
protected function getConnection(InputInterface $input, OutputInterface $output): Connection
{
$this->loadDefaults($input, $output);
return new Connection(new CurlHttpDriver($this->getSenderID($input), $this->getSecret($input, $output)));
return $this->connectionFactory->getConnection($this->getSenderID($input), $this->getSecret($input, $output));
}

protected function assertSuccess(Result $result)
Expand Down
10 changes: 6 additions & 4 deletions src/Threema/Console/Symfony/DecryptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class DecryptCommand extends AbstractLocalCommand
{
Expand All @@ -30,9 +29,12 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadDefaults($input, $output);
$encryptor = AbstractEncryptor::getInstance();
$message = $encryptor->decryptMessage($encryptor->hex2bin($this->getMessage($input)),
$this->getPrivateKey($input, $output), $this->getPublicKey($input), $encryptor->hex2bin($input->getArgument('nonce')));
$encryptor = $this->getEncryptor();
$message = $encryptor->decryptMessage(
$encryptor->hex2bin($this->getMessage($input)),
$encryptor->hex2bin($this->getPrivateKey($input, $output)),
$encryptor->hex2bin($this->getPublicKey($input)),
$encryptor->hex2bin($input->getArgument('nonce')));
$output->writeln($message->__toString());
return 0;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Threema/Console/Symfony/DerivePublicKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class DerivePublicKeyCommand extends AbstractLocalCommand
{
Expand All @@ -26,8 +25,8 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadDefaults($input, $output);
$encryptor = AbstractEncryptor::getInstance();
$publicKey = $encryptor->derivePublicKey($this->getPrivateKey($input, $output));
$encryptor = $this->getEncryptor();
$publicKey = $encryptor->derivePublicKey($encryptor->hex2bin($this->getPrivateKey($input, $output)));
$output->writeln($encryptor->bin2hex($publicKey));
return 0;
}
Expand Down
10 changes: 6 additions & 4 deletions src/Threema/Console/Symfony/EncryptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class EncryptCommand extends AbstractLocalCommand
{
Expand All @@ -28,10 +27,13 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadDefaults($input, $output);
$encryptor = AbstractEncryptor::getInstance();
$encryptor = $this->getEncryptor();
$nonce = $encryptor->randomNonce();
$text = $encryptor->encryptMessageText($this->getMessage($input), $this->getPrivateKey($input, $output),
$this->getPublicKey($input), $nonce);
$text = $encryptor->encryptMessageText(
$this->getMessage($input),
$encryptor->hex2bin($this->getPrivateKey($input, $output)),
$encryptor->hex2bin($this->getPublicKey($input)),
$nonce);

$output->writeln('Nonce:', OutputInterface::VERBOSITY_VERBOSE);
$output->writeln($encryptor->bin2hex($nonce));
Expand Down
2 changes: 1 addition & 1 deletion src/Threema/Console/Symfony/GenerateKeyPairCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$encryptor = AbstractEncryptor::getInstance();
$encryptor = $this->getEncryptor();
$keyPair = $encryptor->generateKeyPair();
$this->writeKey($input->getArgument('private-key-file'),
KeyPrefix::addPrivate($encryptor->bin2hex($keyPair->getPrivateKey())), $output);
Expand Down
4 changes: 1 addition & 3 deletions src/Threema/Console/Symfony/HashEmailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class HashEmailCommand extends AbstractLocalCommand
{
Expand All @@ -26,8 +25,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$encryptor = AbstractEncryptor::getInstance();
$output->writeln($encryptor->hashEmail($input->getArgument('email')));
$output->writeln($this->getEncryptor()->hashEmail($input->getArgument('email')));
return 0;
}
}
4 changes: 1 addition & 3 deletions src/Threema/Console/Symfony/HashPhoneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class HashPhoneCommand extends AbstractLocalCommand
{
Expand All @@ -26,8 +25,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$encryptor = AbstractEncryptor::getInstance();
$output->writeln($encryptor->hashPhoneNo($input->getArgument('phone')));
$output->writeln($this->getEncryptor()->hashPhoneNo($input->getArgument('phone')));
return 0;
}
}
14 changes: 6 additions & 8 deletions src/Threema/Console/Symfony/MessageReceive.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Exceptions\BadMessageException;
use Threema\MsgApi\Helpers\E2EHelper;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class MessageReceive extends AbstractNetworkedCommand
{
Expand All @@ -32,13 +30,13 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$encryptor = AbstractEncryptor::getInstance();
$helper = new E2EHelper($this->getPrivateKey($input, $output), $this->getConnection($input, $output));
$result = $helper->receiveMessage(
$encryptor->hex2bin($input->getArgument('sender-key')),
$connection = $this->getConnection($input, $output);
$result = $connection->receiveMessage(
$this->getPrivateKey($input, $output),
$input->getArgument('sender-key'),
$input->getArgument('message-id'),
$encryptor->hex2bin($this->getMessage($input)),
$encryptor->hex2bin($input->getArgument('nonce')),
$this->getMessage($input),
$input->getArgument('nonce'),
$input->getArgument('folder')
);

Expand Down
11 changes: 7 additions & 4 deletions src/Threema/Console/Symfony/MessageSendFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Helpers\E2EHelper;

class MessageSendFileCommand extends AbstractNetworkedCommand
{
Expand All @@ -24,6 +23,7 @@ protected function configure()
->setAliases(['file'])
->requireRecipientID()
->requirePublicKey()
->requirePrivateKey()
->addArgument('file', InputArgument::REQUIRED, 'Path and file name to encrypt and send')
->addArgument('thumbnail', InputArgument::OPTIONAL,
'Path to optional thumbnail image to display in the message');
Expand All @@ -32,9 +32,12 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$connection = $this->getConnection($input, $output);
$helper = new E2EHelper($this->getPrivateKey($input, $output), $connection);
$result = $helper->sendFileMessage($this->getRecipientID($input), $this->getPublicKey($input),
$input->getArgument('file'), $input->getArgument('thumbnail'));
$result = $connection->sendFileMessage(
$this->getPrivateKey($input, $output),
$this->getRecipientID($input),
$this->getPublicKey($input),
$input->getArgument('file'),
$input->getArgument('thumbnail'));
$this->assertSuccess($result);
$output->writeln($result->getMessageId());
return 0;
Expand Down
8 changes: 5 additions & 3 deletions src/Threema/Console/Symfony/MessageSendImageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Helpers\E2EHelper;

class MessageSendImageCommand extends AbstractNetworkedCommand
{
Expand All @@ -24,14 +23,17 @@ protected function configure()
->setAliases(['image'])
->requireRecipientID()
->requirePublicKey()
->requirePrivateKey()
->addArgument('file', InputArgument::REQUIRED, 'Path and file name to encrypt and send');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$connection = $this->getConnection($input, $output);
$helper = new E2EHelper($this->getPrivateKey($input, $output), $connection);
$result = $helper->sendImageMessage($this->getRecipientID($input), $this->getPublicKey($input),
$result = $connection->sendImageMessage(
$this->getPrivateKey($input, $output),
$this->getRecipientID($input),
$this->getPublicKey($input),
$input->getArgument('file'));
$this->assertSuccess($result);
$output->writeln($result->getMessageId());
Expand Down
7 changes: 4 additions & 3 deletions src/Threema/Console/Symfony/MessageSendTextCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Helpers\E2EHelper;

class MessageSendTextCommand extends AbstractNetworkedCommand
{
Expand All @@ -30,8 +29,10 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$connection = $this->getConnection($input, $output);
$helper = new E2EHelper($this->getPrivateKey($input, $output), $connection);
$result = $helper->sendTextMessage($this->getRecipientID($input), $this->getPublicKey($input),
$result = $connection->sendTextMessage(
$this->getPrivateKey($input, $output),
$this->getRecipientID($input),
$this->getPublicKey($input),
$this->getMessage($input));
$this->assertSuccess($result);
$output->writeln($result->getMessageId());
Expand Down
8 changes: 3 additions & 5 deletions src/Threema/Console/Symfony/VersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@

namespace Threema\Console\Symfony;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Constants;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class VersionCommand extends Command
class VersionCommand extends AbstractLocalCommand
{
protected function configure()
{
Expand All @@ -28,10 +26,10 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$left = function (int $level) {
$left = function (int $level) {
return $level == Constants::MSGAPI_SDK_FEATURE_LEVEL ? '===>' : '';
};
$encryptor = AbstractEncryptor::getInstance();
$encryptor = $this->getEncryptor();
$output->writeln('Threema PHP MsgApi Tool');
$output->writeln('Gateway API Version: ' . Constants::MSGAPI_SDK_VERSION);
$output->writeln('Encryptor: ' . $encryptor->getName() . ' (' . $encryptor->getDescription() . ')');
Expand Down
2 changes: 1 addition & 1 deletion src/Threema/MsgApi/Commands/Results/SendE2EResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class SendE2EResult extends Result
{
/**
* @var string
* @var string hex
*/
private $messageId;

Expand Down
Loading

0 comments on commit f327e6c

Please sign in to comment.