Skip to content

Commit

Permalink
Add new jwt support
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostzero committed Mar 10, 2023
1 parent 9182229 commit 7469afa
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Add the `own3d-socket` configuration within the `config/services.php` config fil
'own3d-socket' => [
'username' => env('OWN3D_SOCKET_USERNAME', 'own3d-socket'),
'password' => env('OWN3D_SOCKET_PASSWORD')
'secret' => env('OWN3D_SOCKET_SECRET'),
],
```

Expand Down
41 changes: 39 additions & 2 deletions src/Own3d/Socket/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Own3d\Socket\Support\SocketToken;
use Throwable;

class SocketClient
{
/**
* List of available servers. May not be complete.
*/
public const SERVER_IDS = [
'socket-edge-hel1-1',
'socket-edge-hel1-2',
];

public static string $baseUrl = 'https://socket-hel1-1.own3d.dev';

private Client $client;
Expand Down Expand Up @@ -38,10 +47,10 @@ public static function setBaseUrl(string $baseUrl): void
* - data: array
*
* @param array $parameters
* @param array|null $serverId The server id is the hostname, that should receive the message (optional).
* @param string|null $serverId The server id is the hostname, that should receive the message (optional).
* @return bool
*/
public function emit(array $parameters, array $serverId = null): bool
public function emit(array $parameters, string $serverId = null): bool
{
$options = [
RequestOptions::JSON => $parameters,
Expand All @@ -64,6 +73,34 @@ public function emit(array $parameters, array $serverId = null): bool
return in_array($response->getStatusCode(), [200, 204]);
}

/**
* Sends a message into the own3d websocket system.
*
* @param string $token Pre-signed token for the socket server to emit a message.
* @param string|null $serverId The server id is the hostname, that should receive the message (optional).
* @return bool
*/
public function jwtEmit(string $token, string $serverId = null): bool
{
$options = [
RequestOptions::JSON => [
'token' => $token,
],
];

if ($serverId) {
$options[RequestOptions::HEADERS]['cookie'] = sprintf('serverid=%s', $serverId);
}

try {
$response = $this->getClient()->post('jwt-emit', $options);
} catch (Throwable $exception) {
return false;
}

return in_array($response->getStatusCode(), [200, 204]);
}

public function getClient(): Client
{
return $this->client;
Expand Down
58 changes: 58 additions & 0 deletions src/Own3d/Socket/Support/SocketToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Own3d\Socket\Support;

use Firebase\JWT\JWT;

/**
* Helper class to generate tokens for the socket server.
*/
class SocketToken
{
const ALGORITHM = 'HS256';

/**
* Generates a pre-signed token for the socket server to join a room.
*
* This will generate a token that can be used to authenticate with the socket server to join a specific room.
*/
public static function auth(string $room): string
{
return self::encode(self::payload('auth', [
'type' => 'auth',
'room' => $room,
], 60 * 15));
}

/**
* Generate a pre-signed token for the socket server to emit a message.
*/
public static function emit(string $room, string $event, array $data): string
{
return self::encode(self::payload('emit', [
'room' => $room,
'event' => $event,
'data' => $data,
], 60 * 15));
}

/**
* Helper to generate a JWT token.
*/
public static function encode(array $payload): string
{
return JWT::encode($payload, config('services.own3d-socket.secret'), self::ALGORITHM);
}

/**
* Helper to decode a JWT token.
*/
private static function payload(string $type, array $extra, float $ttl): array
{
return array_merge([
'type' => $type,
'iat' => time(),
'exp' => time() + $ttl,
], $extra);
}
}

0 comments on commit 7469afa

Please sign in to comment.