diff --git a/cli/ratchet/auth.php b/cli/ratchet/auth.php index 17e918e..12d2d55 100644 --- a/cli/ratchet/auth.php +++ b/cli/ratchet/auth.php @@ -2,7 +2,6 @@ namespace ChessServer\Cli\Ratchet; -use ChessServer\Db; use ChessServer\Command\Parser; use ChessServer\Command\Auth\Cli; use ChessServer\Socket\Ratchet\ClientStorage; @@ -13,7 +12,6 @@ use Ratchet\Http\HttpServer; use Ratchet\Server\IoServer; use Ratchet\WebSocket\WsServer; -use React\EventLoop\Factory; use React\Socket\LimitingServer; use React\Socket\Server; use React\Socket\SecureServer; @@ -26,18 +24,10 @@ $pool = Pool::create(); -$db = new Db([ - 'driver' => $_ENV['DB_DRIVER'], - 'host' => $_ENV['DB_HOST'], - 'database' => $_ENV['DB_DATABASE'], - 'username' => $_ENV['DB_USERNAME'], - 'password' => $_ENV['DB_PASSWORD'], -]); - $logger = new Logger('auth'); $logger->pushHandler(new StreamHandler(__DIR__.'/../../storage' . '/auth.log', Logger::INFO)); -$parser = new Parser(new Cli($pool, $db)); +$parser = new Parser(new Cli($pool)); $clientStorage = new ClientStorage($logger); diff --git a/cli/workerman/auth.php b/cli/workerman/auth.php index 8b973d6..e219f19 100644 --- a/cli/workerman/auth.php +++ b/cli/workerman/auth.php @@ -2,7 +2,6 @@ namespace ChessServer\Cli\Workerman; -use ChessServer\Db; use ChessServer\Command\Parser; use ChessServer\Command\Auth\Cli; use ChessServer\Socket\Workerman\ClientStorage; @@ -19,18 +18,10 @@ $pool = Pool::create(); -$db = new Db([ - 'driver' => $_ENV['DB_DRIVER'], - 'host' => $_ENV['DB_HOST'], - 'database' => $_ENV['DB_DATABASE'], - 'username' => $_ENV['DB_USERNAME'], - 'password' => $_ENV['DB_PASSWORD'], -]); - $logger = new Logger('auth'); $logger->pushHandler(new StreamHandler(AuthWebSocket::STORAGE_FOLDER . '/auth.log', Logger::INFO)); -$parser = new Parser(new Cli($pool, $db)); +$parser = new Parser(new Cli($pool)); $clientStorage = new ClientStorage($logger); diff --git a/src/Command/Auth/Cli.php b/src/Command/Auth/Cli.php index 9dc373d..be7d898 100644 --- a/src/Command/Auth/Cli.php +++ b/src/Command/Auth/Cli.php @@ -2,20 +2,16 @@ namespace ChessServer\Command\Auth; -use ChessServer\Db; use ChessServer\Command\AbstractCli; use Spatie\Async\Pool; class Cli extends AbstractCli { - private Db $db; - - public function __construct(Pool $pool, Db $db) + public function __construct(Pool $pool) { parent::__construct(); - $this->db = $db; - $this->commands->attach(new TotpRefreshCommand($db)); + $this->commands->attach((new TotpRefreshCommand())->setPool($pool)); $this->commands->attach((new TotpSignInCommand())->setPool($pool)); $this->commands->attach((new TotpSignUpCommand())->setPool($pool)); } diff --git a/src/Command/Auth/TotpRefreshAsyncTask.php b/src/Command/Auth/TotpRefreshAsyncTask.php new file mode 100644 index 0000000..aa426bd --- /dev/null +++ b/src/Command/Auth/TotpRefreshAsyncTask.php @@ -0,0 +1,54 @@ +params = $params; + $this->env = $env; + } + + public function configure() + { + $this->db = new Db($this->env['db']); + } + + public function run() + { + if (isset($this->params['access_token'])) { + $decoded = JWT::decode($this->params['access_token'], new Key($this->env['jwt']['secret'], 'HS256')); + $sql = "SELECT * FROM users WHERE username = :username"; + $values[] = [ + 'param' => ":username", + 'value' => $decoded->username, + 'type' => \PDO::PARAM_STR, + ]; + $arr = $this->db->query($sql, $values)->fetch(\PDO::FETCH_ASSOC); + $payload = [ + 'iss' => $this->env['jwt']['iss'], + 'iat' => time(), + 'exp' => time() + 3600, // one hour by default + 'username' => $arr['username'], + 'elo' => $arr['elo'], + ]; + return [ + 'access_token' => JWT::encode($payload, $this->env['jwt']['secret'], 'HS256'), + ]; + } + + return null; + } +} diff --git a/src/Command/Auth/TotpRefreshCommand.php b/src/Command/Auth/TotpRefreshCommand.php index 846a29b..d70c1a6 100644 --- a/src/Command/Auth/TotpRefreshCommand.php +++ b/src/Command/Auth/TotpRefreshCommand.php @@ -2,18 +2,13 @@ namespace ChessServer\Command\Auth; -use ChessServer\Db; use ChessServer\Command\AbstractCommand; use ChessServer\Socket\AbstractSocket; -use Firebase\JWT\JWT; -use Firebase\JWT\Key; class TotpRefreshCommand extends AbstractCommand { - public function __construct(Db $db) + public function __construct() { - parent::__construct($db); - $this->name = '/totp_refresh'; $this->description = 'Refresh the TOTP access token.'; $this->params = [ @@ -30,31 +25,25 @@ public function run(AbstractSocket $socket, array $argv, int $id) { $params = json_decode(stripslashes($argv[1]), true); - if (isset($params['access_token'])) { - $decoded = JWT::decode($params['access_token'], new Key($_ENV['JWT_SECRET'], 'HS256')); - $sql = "SELECT * FROM users WHERE username = :username"; - $values[] = [ - 'param' => ":username", - 'value' => $decoded->username, - 'type' => \PDO::PARAM_STR, - ]; - $arr = $this->db->query($sql, $values)->fetch(\PDO::FETCH_ASSOC); - $payload = [ - 'iss' => $_ENV['JWT_ISS'], - 'iat' => time(), - 'exp' => time() + 3600, // one hour by default - 'username' => $arr['username'], - 'elo' => $arr['elo'], - ]; - return $socket->getClientStorage()->send([$id], [ - $this->name => [ - 'access_token' => JWT::encode($payload, $_ENV['JWT_SECRET'], 'HS256'), - ], - ]); - } + $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'], + ], + ]; - return $socket->getClientStorage()->send([$id], [ - $this->name => null, - ]); + $this->pool->add(new TotpRefreshAsyncTask($params, $env)) + ->then(function ($result) use ($socket, $id) { + return $socket->getClientStorage()->send([$id], [ + $this->name => $result, + ]); + }); } } diff --git a/src/Command/Data/Cli.php b/src/Command/Data/Cli.php index 2102766..79eaf35 100644 --- a/src/Command/Data/Cli.php +++ b/src/Command/Data/Cli.php @@ -2,7 +2,6 @@ namespace ChessServer\Command\Data; -use ChessServer\Db; use ChessServer\Command\AbstractCli; use Spatie\Async\Pool; diff --git a/src/Socket/Ratchet/AuthWebSocket.php b/src/Socket/Ratchet/AuthWebSocket.php index 30c1c1e..8aeddf7 100644 --- a/src/Socket/Ratchet/AuthWebSocket.php +++ b/src/Socket/Ratchet/AuthWebSocket.php @@ -3,22 +3,10 @@ namespace ChessServer\Socket\Ratchet; use ChessServer\Command\Parser; -use ChessServer\Socket\DbReconnectTrait; use Ratchet\ConnectionInterface; class AuthWebSocket extends AbstractWebSocket { - use DbReconnectTrait; - - public function __construct(Parser $parser) - { - parent::__construct($parser); - - $this->loop->addPeriodicTimer($this->timeInterval, function() { - $this->reconnect(); - }); - } - public function onClose(ConnectionInterface $conn) { $this->clientStorage->detachById($conn->resourceId); diff --git a/src/Socket/Workerman/AuthWebSocket.php b/src/Socket/Workerman/AuthWebSocket.php index 06cf407..eadcf88 100644 --- a/src/Socket/Workerman/AuthWebSocket.php +++ b/src/Socket/Workerman/AuthWebSocket.php @@ -3,23 +3,13 @@ namespace ChessServer\Socket\Workerman; use ChessServer\Command\Parser; -use ChessServer\Socket\DbReconnectTrait; -use Workerman\Timer; class AuthWebSocket extends AbstractWebSocket { - use DbReconnectTrait; - public function __construct(string $socketName, array $context, Parser $parser) { parent::__construct($socketName, $context, $parser); - $this->worker->onWorkerStart = function() { - Timer::add($this->timeInterval, function() { - $this->reconnect(); - }); - }; - $this->connect()->message()->error()->close(); }