Skip to content

Commit

Permalink
Merge pull request #415 from chesslablab/issue/412-Parallelize-the-to…
Browse files Browse the repository at this point in the history
…tp_signin-command

Implemented TotpSignInAsyncTask
  • Loading branch information
programarivm authored Oct 5, 2024
2 parents 5bb18ba + 6f06f85 commit 4cabe4f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/Command/Auth/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(Pool $pool, Db $db)

$this->db = $db;
$this->commands->attach(new TotpRefreshCommand($db));
$this->commands->attach(new TotpSignInCommand($db));
$this->commands->attach((new TotpSignInCommand())->setPool($pool));
$this->commands->attach((new TotpSignUpCommand())->setPool($pool));
}

Expand Down
73 changes: 73 additions & 0 deletions src/Command/Auth/TotpSignInAsyncTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

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
{
private array $params;

private array $conf;

private array $totp;

private array $jwt;

private Db $db;

public function __construct(array $params, array $conf, array $totp, array $jwt)
{
$this->params = $params;
$this->conf = $conf;
$this->totp = $totp;
$this->jwt = $jwt;
}

public function configure()
{
$this->db = new Db($this->conf);
}

public function run()
{
$otp = TOTP::createFromSecret($this->totp['secret'], new InternalClock());
$otp->setDigits(9);

if ($otp->verify($this->params['password'], null, 5)) {
$sql = "SELECT * FROM users WHERE username = :username";
$values[] = [
'param' => ":username",
'value' => $this->params['username'],
'type' => \PDO::PARAM_STR,
];
$arr = $this->db->query($sql, $values)->fetch(\PDO::FETCH_ASSOC);

$sql = "UPDATE users SET lastLoginAt = now() WHERE username = :username";
$values[] = [
'param' => ":username",
'value' => $this->params['username'],
'type' => \PDO::PARAM_STR,
];
$this->db->query($sql, $values);

$payload = [
'iss' => $this->jwt['iss'],
'iat' => time(),
'exp' => time() + 3600, // one hour by default
'username' => $arr['username'],
'elo' => $arr['elo'],
];

return [
'access_token' => JWT::encode($payload, $this->jwt['secret'], 'HS256'),
];
}

return null;
}
}
63 changes: 21 additions & 42 deletions src/Command/Auth/TotpSignInCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

namespace ChessServer\Command\Auth;

use ChessServer\Db;
use ChessServer\Command\AbstractCommand;
use ChessServer\Socket\AbstractSocket;
use Firebase\JWT\JWT;
use OTPHP\InternalClock;
use OTPHP\TOTP;

class TotpSignInCommand extends AbstractCommand
{
public function __construct(Db $db)
public function __construct()
{
parent::__construct($db);

$this->name = '/totp_signin';
$this->description = 'TOTP sign in.';
$this->params = [
Expand All @@ -31,43 +25,28 @@ public function run(AbstractSocket $socket, array $argv, int $id)
{
$params = json_decode(stripslashes($argv[1]), true);

$otp = TOTP::createFromSecret($_ENV['TOTP_SECRET'], new InternalClock());
$otp->setDigits(9);

if ($otp->verify($params['password'], null, 5)) {
$sql = "SELECT * FROM users WHERE username = :username";
$values[] = [
'param' => ":username",
'value' => $params['username'],
'type' => \PDO::PARAM_STR,
];
$arr = $this->db->query($sql, $values)->fetch(\PDO::FETCH_ASSOC);

$sql = "UPDATE users SET lastLoginAt = now() WHERE username = :username";
$values[] = [
'param' => ":username",
'value' => $params['username'],
'type' => \PDO::PARAM_STR,
];
$this->db->query($sql, $values);
$conf = [
'driver' => $_ENV['DB_DRIVER'],
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_DATABASE'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'],
];

$payload = [
'iss' => $_ENV['JWT_ISS'],
'iat' => time(),
'exp' => time() + 3600, // one hour by default
'username' => $arr['username'],
'elo' => $arr['elo'],
];
$totp = [
'secret' => $_ENV['TOTP_SECRET'],
];

return $socket->getClientStorage()->send([$id], [
$this->name => [
'access_token' => JWT::encode($payload, $_ENV['JWT_SECRET'], 'HS256'),
],
]);
}
$jwt = [
'iss' => $_ENV['JWT_ISS'],
'secret' => $_ENV['JWT_SECRET'],
];

return $socket->getClientStorage()->send([$id], [
$this->name => null,
]);
$this->pool->add(new TotpSignInAsyncTask($params, $conf, $totp, $jwt))
->then(function ($result) use ($socket, $id) {
return $socket->getClientStorage()->send([$id], [
$this->name => $result,
]);
});
}
}

0 comments on commit 4cabe4f

Please sign in to comment.