From 3eac7b8d0bad7be1cc3620f303d14bc6eb75822a Mon Sep 17 00:00:00 2001 From: programarivm Date: Fri, 4 Oct 2024 18:46:50 +0200 Subject: [PATCH] Implemented TotpSignUpAsyncTask --- cli/ratchet/auth.php | 5 ++- cli/workerman/auth.php | 5 ++- src/Command/Auth/Cli.php | 5 ++- src/Command/Auth/TotpSignUpAsyncTask.php | 48 ++++++++++++++++++++++++ src/Command/Auth/TotpSignUpCommand.php | 39 ++++++++----------- 5 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 src/Command/Auth/TotpSignUpAsyncTask.php diff --git a/cli/ratchet/auth.php b/cli/ratchet/auth.php index caf602a9..17e918e6 100644 --- a/cli/ratchet/auth.php +++ b/cli/ratchet/auth.php @@ -17,12 +17,15 @@ use React\Socket\LimitingServer; use React\Socket\Server; use React\Socket\SecureServer; +use Spatie\Async\Pool; require __DIR__ . '/../../vendor/autoload.php'; $dotenv = Dotenv::createImmutable(__DIR__.'/../../'); $dotenv->load(); +$pool = Pool::create(); + $db = new Db([ 'driver' => $_ENV['DB_DRIVER'], 'host' => $_ENV['DB_HOST'], @@ -34,7 +37,7 @@ $logger = new Logger('auth'); $logger->pushHandler(new StreamHandler(__DIR__.'/../../storage' . '/auth.log', Logger::INFO)); -$parser = new Parser(new Cli($db)); +$parser = new Parser(new Cli($pool, $db)); $clientStorage = new ClientStorage($logger); diff --git a/cli/workerman/auth.php b/cli/workerman/auth.php index c0289345..8b973d6b 100644 --- a/cli/workerman/auth.php +++ b/cli/workerman/auth.php @@ -10,12 +10,15 @@ use Dotenv\Dotenv; use Monolog\Logger; use Monolog\Handler\StreamHandler; +use Spatie\Async\Pool; require __DIR__ . '/../../vendor/autoload.php'; $dotenv = Dotenv::createImmutable(__DIR__.'/../../'); $dotenv->load(); +$pool = Pool::create(); + $db = new Db([ 'driver' => $_ENV['DB_DRIVER'], 'host' => $_ENV['DB_HOST'], @@ -27,7 +30,7 @@ $logger = new Logger('auth'); $logger->pushHandler(new StreamHandler(AuthWebSocket::STORAGE_FOLDER . '/auth.log', Logger::INFO)); -$parser = new Parser(new Cli($db)); +$parser = new Parser(new Cli($pool, $db)); $clientStorage = new ClientStorage($logger); diff --git a/src/Command/Auth/Cli.php b/src/Command/Auth/Cli.php index 23c6c188..04e95aab 100644 --- a/src/Command/Auth/Cli.php +++ b/src/Command/Auth/Cli.php @@ -4,19 +4,20 @@ use ChessServer\Db; use ChessServer\Command\AbstractCli; +use Spatie\Async\Pool; class Cli extends AbstractCli { private Db $db; - public function __construct(Db $db) + public function __construct(Pool $pool, Db $db) { parent::__construct(); $this->db = $db; $this->commands->attach(new TotpRefreshCommand($db)); $this->commands->attach(new TotpSignInCommand($db)); - $this->commands->attach(new TotpSignUpCommand($db)); + $this->commands->attach((new TotpSignUpCommand())->setPool($pool)); } public function getDb(): Db diff --git a/src/Command/Auth/TotpSignUpAsyncTask.php b/src/Command/Auth/TotpSignUpAsyncTask.php new file mode 100644 index 00000000..1ab16980 --- /dev/null +++ b/src/Command/Auth/TotpSignUpAsyncTask.php @@ -0,0 +1,48 @@ +conf = $conf; + $this->totp = $totp; + } + + public function configure() + { + $this->db = new Db($this->conf); + } + + public function run() + { + $sql = "SELECT username FROM users WHERE lastLoginAt IS NULL ORDER BY RAND() LIMIT 1"; + + $username = $this->db->query($sql)->fetchColumn(); + + $otp = TOTP::createFromSecret($this->totp['secret'], new InternalClock()); + $otp->setDigits(9); + $otp->setLabel($username); + $otp->setIssuer('ChesslaBlab'); + $otp->setParameter('image', 'https://chesslablab.org/logo.png'); + + return [ + 'uri' => $otp->getQrCodeUri( + 'https://api.qrserver.com/v1/create-qr-code/?data=[DATA]&size=300x300&ecc=M', + '[DATA]' + ) + ]; + } +} diff --git a/src/Command/Auth/TotpSignUpCommand.php b/src/Command/Auth/TotpSignUpCommand.php index a250d5f6..0d3d45fe 100644 --- a/src/Command/Auth/TotpSignUpCommand.php +++ b/src/Command/Auth/TotpSignUpCommand.php @@ -2,18 +2,13 @@ namespace ChessServer\Command\Auth; -use ChessServer\Db; use ChessServer\Command\AbstractCommand; use ChessServer\Socket\AbstractSocket; -use OTPHP\InternalClock; -use OTPHP\TOTP; class TotpSignUpCommand extends AbstractCommand { - public function __construct(Db $db) + public function __construct() { - parent::__construct($db); - $this->name = '/totp_signup'; $this->description = 'TOTP sign up URL.'; } @@ -25,25 +20,23 @@ public function validate(array $argv) public function run(AbstractSocket $socket, array $argv, int $id) { - $sql = "SELECT username FROM users WHERE lastLoginAt IS NULL ORDER BY RAND() LIMIT 1"; - - $username = $this->db->query($sql)->fetchColumn(); - - $otp = TOTP::createFromSecret($_ENV['TOTP_SECRET'], new InternalClock()); - $otp->setDigits(9); - $otp->setLabel($username); - $otp->setIssuer('ChesslaBlab'); - $otp->setParameter('image', 'https://chesslablab.org/logo.png'); + $conf = [ + 'driver' => $_ENV['DB_DRIVER'], + 'host' => $_ENV['DB_HOST'], + 'database' => $_ENV['DB_DATABASE'], + 'username' => $_ENV['DB_USERNAME'], + 'password' => $_ENV['DB_PASSWORD'], + ]; - $arr = [ - 'uri' => $otp->getQrCodeUri( - 'https://api.qrserver.com/v1/create-qr-code/?data=[DATA]&size=300x300&ecc=M', - '[DATA]' - ) + $totp = [ + 'secret' => $_ENV['TOTP_SECRET'], ]; - return $socket->getClientStorage()->send([$id], [ - $this->name => $arr, - ]); + $this->pool->add(new TotpSignUpAsyncTask($conf, $totp)) + ->then(function ($result) use ($socket, $id) { + return $socket->getClientStorage()->send([$id], [ + $this->name => $result, + ]); + }); } }