Skip to content

Commit

Permalink
Implemented TotpSignUpAsyncTask
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Oct 4, 2024
1 parent a2d24e3 commit 3eac7b8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 deletions.
5 changes: 4 additions & 1 deletion cli/ratchet/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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);

Expand Down
5 changes: 4 additions & 1 deletion cli/workerman/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/Command/Auth/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions src/Command/Auth/TotpSignUpAsyncTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace ChessServer\Command\Auth;

use ChessServer\Db;
use OTPHP\InternalClock;
use OTPHP\TOTP;
use Spatie\Async\Task;

class TotpSignUpAsyncTask extends Task
{
private array $conf;

private array $totp;

private Db $db;

public function __construct(array $conf, array $totp)
{
$this->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]'
)
];
}
}
39 changes: 16 additions & 23 deletions src/Command/Auth/TotpSignUpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
}
Expand All @@ -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,
]);
});
}
}

0 comments on commit 3eac7b8

Please sign in to comment.