From f8c6da512f157e33a59a610ce78498bd76971e95 Mon Sep 17 00:00:00 2001 From: programarivm Date: Tue, 8 Oct 2024 23:14:23 +0200 Subject: [PATCH] Refactored ChessServer\Command\Auth --- src/Command/AbstractAsyncTask.php | 7 +++++++ src/Command/Auth/AbstractAuthAsyncTask.php | 16 ++++++++++++++++ src/Command/Auth/TotpRefreshAsyncTask.php | 21 +-------------------- src/Command/Auth/TotpRefreshCommand.php | 16 +--------------- src/Command/Auth/TotpSignInAsyncTask.php | 21 +-------------------- src/Command/Auth/TotpSignInCommand.php | 19 +------------------ src/Command/Auth/TotpSignUpAsyncTask.php | 18 +----------------- src/Command/Auth/TotpSignUpCommand.php | 15 +-------------- 8 files changed, 29 insertions(+), 104 deletions(-) create mode 100644 src/Command/Auth/AbstractAuthAsyncTask.php diff --git a/src/Command/AbstractAsyncTask.php b/src/Command/AbstractAsyncTask.php index d79baafa..14109f47 100644 --- a/src/Command/AbstractAsyncTask.php +++ b/src/Command/AbstractAsyncTask.php @@ -20,6 +20,13 @@ public function __construct(array $params = []) 'username' => $_ENV['DB_USERNAME'], 'password' => $_ENV['DB_PASSWORD'], ], + 'totp' => [ + 'secret' => $_ENV['TOTP_SECRET'], + ], + 'jwt' => [ + 'iss' => $_ENV['JWT_ISS'], + 'secret' => $_ENV['JWT_SECRET'], + ], ]; $this->params = $params; diff --git a/src/Command/Auth/AbstractAuthAsyncTask.php b/src/Command/Auth/AbstractAuthAsyncTask.php new file mode 100644 index 00000000..c4bff5ea --- /dev/null +++ b/src/Command/Auth/AbstractAuthAsyncTask.php @@ -0,0 +1,16 @@ +db = new Db($this->env['db']); + } +} diff --git a/src/Command/Auth/TotpRefreshAsyncTask.php b/src/Command/Auth/TotpRefreshAsyncTask.php index aa426bdc..55b84feb 100644 --- a/src/Command/Auth/TotpRefreshAsyncTask.php +++ b/src/Command/Auth/TotpRefreshAsyncTask.php @@ -2,30 +2,11 @@ namespace ChessServer\Command\Auth; -use ChessServer\Db; use Firebase\JWT\JWT; use Firebase\JWT\Key; -use Spatie\Async\Task; -class TotpRefreshAsyncTask extends Task +class TotpRefreshAsyncTask extends AbstractAuthAsyncTask { - private array $params; - - private array $env; - - private Db $db; - - public function __construct(array $params, array $env) - { - $this->params = $params; - $this->env = $env; - } - - public function configure() - { - $this->db = new Db($this->env['db']); - } - public function run() { if (isset($this->params['access_token'])) { diff --git a/src/Command/Auth/TotpRefreshCommand.php b/src/Command/Auth/TotpRefreshCommand.php index d70c1a6c..f556b4f0 100644 --- a/src/Command/Auth/TotpRefreshCommand.php +++ b/src/Command/Auth/TotpRefreshCommand.php @@ -25,21 +25,7 @@ public function run(AbstractSocket $socket, array $argv, int $id) { $params = json_decode(stripslashes($argv[1]), true); - $env = [ - 'db' => [ - 'driver' => $_ENV['DB_DRIVER'], - 'host' => $_ENV['DB_HOST'], - 'database' => $_ENV['DB_DATABASE'], - 'username' => $_ENV['DB_USERNAME'], - 'password' => $_ENV['DB_PASSWORD'], - ], - 'jwt' => [ - 'iss' => $_ENV['JWT_ISS'], - 'secret' => $_ENV['JWT_SECRET'], - ], - ]; - - $this->pool->add(new TotpRefreshAsyncTask($params, $env)) + $this->pool->add(new TotpRefreshAsyncTask($params)) ->then(function ($result) use ($socket, $id) { return $socket->getClientStorage()->send([$id], [ $this->name => $result, diff --git a/src/Command/Auth/TotpSignInAsyncTask.php b/src/Command/Auth/TotpSignInAsyncTask.php index 342786e0..c9fb97b2 100644 --- a/src/Command/Auth/TotpSignInAsyncTask.php +++ b/src/Command/Auth/TotpSignInAsyncTask.php @@ -2,31 +2,12 @@ namespace ChessServer\Command\Auth; -use ChessServer\Db; use Firebase\JWT\JWT; use OTPHP\InternalClock; use OTPHP\TOTP; -use Spatie\Async\Task; -class TotpSignInAsyncTask extends Task +class TotpSignInAsyncTask extends AbstractAuthAsyncTask { - private array $params; - - private array $env; - - private Db $db; - - public function __construct(array $params, array $env) - { - $this->params = $params; - $this->env = $env; - } - - public function configure() - { - $this->db = new Db($this->env['db']); - } - public function run() { $otp = TOTP::createFromSecret($this->env['totp']['secret'], new InternalClock()); diff --git a/src/Command/Auth/TotpSignInCommand.php b/src/Command/Auth/TotpSignInCommand.php index 971d79c7..95c3174f 100644 --- a/src/Command/Auth/TotpSignInCommand.php +++ b/src/Command/Auth/TotpSignInCommand.php @@ -25,24 +25,7 @@ public function run(AbstractSocket $socket, array $argv, int $id) { $params = json_decode(stripslashes($argv[1]), true); - $env = [ - 'db' => [ - 'driver' => $_ENV['DB_DRIVER'], - 'host' => $_ENV['DB_HOST'], - 'database' => $_ENV['DB_DATABASE'], - 'username' => $_ENV['DB_USERNAME'], - 'password' => $_ENV['DB_PASSWORD'], - ], - 'totp' => [ - 'secret' => $_ENV['TOTP_SECRET'], - ], - 'jwt' => [ - 'iss' => $_ENV['JWT_ISS'], - 'secret' => $_ENV['JWT_SECRET'], - ], - ]; - - $this->pool->add(new TotpSignInAsyncTask($params, $env)) + $this->pool->add(new TotpSignInAsyncTask($params)) ->then(function ($result) use ($socket, $id) { return $socket->getClientStorage()->send([$id], [ $this->name => $result, diff --git a/src/Command/Auth/TotpSignUpAsyncTask.php b/src/Command/Auth/TotpSignUpAsyncTask.php index 5eebde9b..ccbe39b1 100644 --- a/src/Command/Auth/TotpSignUpAsyncTask.php +++ b/src/Command/Auth/TotpSignUpAsyncTask.php @@ -2,27 +2,11 @@ namespace ChessServer\Command\Auth; -use ChessServer\Db; use OTPHP\InternalClock; use OTPHP\TOTP; -use Spatie\Async\Task; -class TotpSignUpAsyncTask extends Task +class TotpSignUpAsyncTask extends AbstractAuthAsyncTask { - private array $env; - - private Db $db; - - public function __construct(array $env) - { - $this->env = $env; - } - - public function configure() - { - $this->db = new Db($this->env['db']); - } - public function run() { $sql = "SELECT username FROM users WHERE lastLoginAt IS NULL ORDER BY RAND() LIMIT 1"; diff --git a/src/Command/Auth/TotpSignUpCommand.php b/src/Command/Auth/TotpSignUpCommand.php index aae37f59..638d9730 100644 --- a/src/Command/Auth/TotpSignUpCommand.php +++ b/src/Command/Auth/TotpSignUpCommand.php @@ -20,20 +20,7 @@ public function validate(array $argv) public function run(AbstractSocket $socket, array $argv, int $id) { - $env = [ - 'db' => [ - 'driver' => $_ENV['DB_DRIVER'], - 'host' => $_ENV['DB_HOST'], - 'database' => $_ENV['DB_DATABASE'], - 'username' => $_ENV['DB_USERNAME'], - 'password' => $_ENV['DB_PASSWORD'], - ], - 'totp' => [ - 'secret' => $_ENV['TOTP_SECRET'], - ], - ]; - - $this->pool->add(new TotpSignUpAsyncTask($env)) + $this->pool->add(new TotpSignUpAsyncTask()) ->then(function ($result) use ($socket, $id) { return $socket->getClientStorage()->send([$id], [ $this->name => $result,