Skip to content

Commit

Permalink
Implemented the auth service
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Sep 12, 2024
1 parent 1830827 commit 7268f3e
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 13 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ WSS_ADDRESS=0.0.0.0
WSS_DATA_PORT=9443
WSS_GAME_PORT=8443
WSS_BINARY_PORT=7443
WSS_AUTH_PORT=6443

# cli/ratchet/ws_game.php
# WS_ADDRESS=0.0.0.0
Expand Down
46 changes: 46 additions & 0 deletions cli/workerman/auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace ChessServer\Cli\Workerman;

use ChessServer\Command\Db;
use ChessServer\Command\Parser;
use ChessServer\Command\Auth\Cli;
use ChessServer\Socket\Workerman\ClientStorage;
use ChessServer\Socket\Workerman\DataWebSocket;
use Dotenv\Dotenv;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

require __DIR__ . '/../../vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__.'/../../');
$dotenv->load();

$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(DataWebSocket::STORAGE_FOLDER . '/auth.log', Logger::INFO));

$parser = new Parser(new Cli($db));

$clientStorage = new ClientStorage($logger);

$socketName = "websocket://{$_ENV['WSS_ADDRESS']}:{$_ENV['WSS_AUTH_PORT']}";

$context = [
'ssl' => [
'local_cert' => __DIR__ . '/../../ssl/fullchain.pem',
'local_pk' => __DIR__ . '/../../ssl/privkey.pem',
'verify_peer' => false,
],
];

$webSocket = (new DataWebSocket($socketName, $context, $parser))->init($clientStorage);

$webSocket->getWorker()->runAll();
15 changes: 15 additions & 0 deletions docker-compose.workerman.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ services:
- ./docker/php/php.ini:/usr/local/etc/php/php.ini
working_dir: /usr/share/chess-server
command: ["php", "cli/workerman/binary.php", "start", "--d"]
auth:
build:
context: .
dockerfile: Dockerfile
user: 1000:1000
container_name: chess_server_auth
ports:
- ${WSS_AUTH_PORT}:${WSS_AUTH_PORT}
restart: always
volumes:
- ./:/usr/share/chess-server
- /usr/share/chess-server/vendor
- ./docker/php/php.ini:/usr/local/etc/php/php.ini
working_dir: /usr/share/chess-server
command: ["php", "cli/workerman/auth.php", "start", "--d"]
9 changes: 8 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/license/mit/)
[![Contributors](https://img.shields.io/github/contributors/chesslablab/chess-server)](https://github.com/chesslablab/chess-server/graphs/contributors)

PHP Chess Server is an asynchronous PHP server that provides services of chess data and chess functionality to play online over a WebSocket connection.
PHP Chess Server is an asynchronous PHP server that provides services of data and chess functionality to play online over a WebSocket connection.

| Port | Service Name | Description |
| ---- | ------------ | ----------- |
| 9443 | data | JSON-formatted data |
| 8443 | game | Chess functionality |
| 7443 | binary | Binary data |
| 6443 | auth | Authentication functionality |

## Object-Oriented

Expand Down
9 changes: 3 additions & 6 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ Clone the `chesslablab/chess-server` repo into your projects folder. Then `cd` t
cp .env.example .env
```

Create an empty `data.log` file:
Create empty log files for each service as described next.

```txt
touch storage/data.log
```

Create an empty `game.log` file:

```txt
touch storage/game.log
touch storage/binary.log
touch storage/auth.log
```

Make sure to have installed the `fullchain.pem` and `privkey.pem` files in the `ssl` folder, and run the Docker container in detached mode in the background:
Expand Down
26 changes: 26 additions & 0 deletions src/Command/Auth/Cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ChessServer\Command\Auth;

use ChessServer\Command\AbstractCli;
use ChessServer\Command\Db;

class Cli extends AbstractCli
{
private Db $db;

public function __construct(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));
}

public function getDb(): Db
{
return $this->db;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ChessServer\Command\Data;
namespace ChessServer\Command\Auth;

use ChessServer\Command\AbstractCommand;
use ChessServer\Command\Db;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ChessServer\Command\Data;
namespace ChessServer\Command\Auth;

use ChessServer\Command\AbstractCommand;
use ChessServer\Command\Db;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ChessServer\Command\Data;
namespace ChessServer\Command\Auth;

use ChessServer\Command\AbstractCommand;
use ChessServer\Command\Db;
Expand Down
3 changes: 0 additions & 3 deletions src/Command/Data/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public function __construct(Db $db)
$this->commands->attach(new ResultPlayerCommand($db));
$this->commands->attach(new ResultCommand($db));
$this->commands->attach(new SearchCommand($db));
$this->commands->attach(new TotpRefreshCommand($db));
$this->commands->attach(new TotpSignInCommand($db));
$this->commands->attach(new TotpSignUpCommand($db));
}

public function getDb(): Db
Expand Down

0 comments on commit 7268f3e

Please sign in to comment.