diff --git a/app/Events/ExampleEvent.php b/app/Events/ExampleEvent.php deleted file mode 100644 index 4bd1268..0000000 --- a/app/Events/ExampleEvent.php +++ /dev/null @@ -1,16 +0,0 @@ -user = $user; + } + + public function broadcastOn() + { + return new PrivateChannel('users.' . $this->user->id); + } + + public function broadcastAs() + { + return 'user'; + } +} diff --git a/app/Http/Controllers/BroadcastController.php b/app/Http/Controllers/BroadcastController.php new file mode 100644 index 0000000..875ef8e --- /dev/null +++ b/app/Http/Controllers/BroadcastController.php @@ -0,0 +1,16 @@ + $request->getMethod(), + 'path' => $request->path(), + 'data' => $request->all(), + ]); + // Pre-Middleware Action + + $response = $next($request); + + // Post-Middleware Action + // Log::debug('response', $response->content); + + + return $response; + } +} diff --git a/app/Listeners/ExampleListener.php b/app/Listeners/ExampleListener.php deleted file mode 100644 index a4fe5f3..0000000 --- a/app/Listeners/ExampleListener.php +++ /dev/null @@ -1,31 +0,0 @@ -configure('alipay'); $app->configure('drops'); $app->configure('forum'); +$app->configure('broadcasting'); /* |-------------------------------------------------------------------------- @@ -91,6 +92,7 @@ $app->middleware([ // App\Http\Middleware\ExampleMiddleware::class \App\Http\Middleware\AllowCors::class, + // \App\Http\Middleware\LogAllRequest::class, ]); $app->routeMiddleware([ @@ -117,10 +119,13 @@ $app->register(Illuminate\Redis\RedisServiceProvider::class); $app->register(Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class); $app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class); -// $app->register(App\Providers\EventServiceProvider::class); +$app->register(App\Providers\EventServiceProvider::class); $app->register(\Anik\Form\FormRequestServiceProvider::class); $app->register(App\Providers\RouteBindingServiceProvider::class); +// broadcast +$app->register(App\Providers\BroadcastServiceProvider::class); + /* |-------------------------------------------------------------------------- @@ -155,5 +160,4 @@ require __DIR__ . '/../routes/remote.php'; }); - return $app; diff --git a/composer.json b/composer.json index a7b294a..b04b5ab 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "league/flysystem": "^3.2", "mmghv/lumen-route-binding": "^1.0", "mongodb/mongodb": "^1.13", - "predis/predis": "^2.0" + "predis/predis": "^2.0", + "pusher/pusher-php-server": "^7.0" }, "require-dev": { "fakerphp/faker": "^1.9.1", diff --git a/composer.lock b/composer.lock index f656bc5..8899543 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "22a158b343410ac17bf6d2ee94827c2c", + "content-hash": "8e55271fe6968a10880e3c206658b974", "packages": [ { "name": "adbario/php-dot-notation", @@ -4527,6 +4527,154 @@ ], "time": "2022-08-01T11:03:24+00:00" }, + { + "name": "paragonie/random_compat", + "version": "v9.99.100", + "source": { + "type": "git", + "url": "https://github.com/paragonie/random_compat.git", + "reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/random_compat/zipball/996434e5492cb4c3edcb9168db6fbb1359ef965a", + "reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": ">= 7" + }, + "require-dev": { + "phpunit/phpunit": "4.*|5.*", + "vimeo/psalm": "^1" + }, + "suggest": { + "ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes." + }, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paragon Initiative Enterprises", + "email": "security@paragonie.com", + "homepage": "https://paragonie.com" + } + ], + "description": "PHP 5.x polyfill for random_bytes() and random_int() from PHP 7", + "keywords": [ + "csprng", + "polyfill", + "pseudorandom", + "random" + ], + "support": { + "email": "info@paragonie.com", + "issues": "https://github.com/paragonie/random_compat/issues", + "source": "https://github.com/paragonie/random_compat" + }, + "time": "2020-10-15T08:29:30+00:00" + }, + { + "name": "paragonie/sodium_compat", + "version": "v1.18.0", + "source": { + "type": "git", + "url": "https://github.com/paragonie/sodium_compat.git", + "reference": "906e0b925895d3a5941eda25f371fbafb3cbc22f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/sodium_compat/zipball/906e0b925895d3a5941eda25f371fbafb3cbc22f", + "reference": "906e0b925895d3a5941eda25f371fbafb3cbc22f", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "paragonie/random_compat": ">=1", + "php": "^5.2.4|^5.3|^5.4|^5.5|^5.6|^7|^8" + }, + "require-dev": { + "phpunit/phpunit": "^3|^4|^5|^6|^7|^8|^9" + }, + "suggest": { + "ext-libsodium": "PHP < 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security.", + "ext-sodium": "PHP >= 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security." + }, + "type": "library", + "autoload": { + "files": [ + "autoload.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "ISC" + ], + "authors": [ + { + "name": "Paragon Initiative Enterprises", + "email": "security@paragonie.com" + }, + { + "name": "Frank Denis", + "email": "jedisct1@pureftpd.org" + } + ], + "description": "Pure PHP implementation of libsodium; uses the PHP extension if it exists", + "keywords": [ + "Authentication", + "BLAKE2b", + "ChaCha20", + "ChaCha20-Poly1305", + "Chapoly", + "Curve25519", + "Ed25519", + "EdDSA", + "Edwards-curve Digital Signature Algorithm", + "Elliptic Curve Diffie-Hellman", + "Poly1305", + "Pure-PHP cryptography", + "RFC 7748", + "RFC 8032", + "Salpoly", + "Salsa20", + "X25519", + "XChaCha20-Poly1305", + "XSalsa20-Poly1305", + "Xchacha20", + "Xsalsa20", + "aead", + "cryptography", + "ecdh", + "elliptic curve", + "elliptic curve cryptography", + "encryption", + "libsodium", + "php", + "public-key cryptography", + "secret-key cryptography", + "side-channel resistant" + ], + "support": { + "issues": "https://github.com/paragonie/sodium_compat/issues", + "source": "https://github.com/paragonie/sodium_compat/tree/v1.18.0" + }, + "time": "2022-09-13T20:54:27+00:00" + }, { "name": "phpoption/phpoption", "version": "1.8.1", @@ -5224,6 +5372,73 @@ }, "time": "2022-07-28T14:25:11+00:00" }, + { + "name": "pusher/pusher-php-server", + "version": "7.0.2", + "source": { + "type": "git", + "url": "https://github.com/pusher/pusher-http-php.git", + "reference": "af3eeaefc0c7959f5b3852f0a4dd5547245d33df" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pusher/pusher-http-php/zipball/af3eeaefc0c7959f5b3852f0a4dd5547245d33df", + "reference": "af3eeaefc0c7959f5b3852f0a4dd5547245d33df", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "ext-curl": "*", + "ext-json": "*", + "guzzlehttp/guzzle": "^7.2", + "paragonie/sodium_compat": "^1.6", + "php": "^7.3|^8.0", + "psr/log": "^1.0|^2.0|^3.0" + }, + "require-dev": { + "overtrue/phplint": "^2.3", + "phpunit/phpunit": "^8.5|^9.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.0-dev" + } + }, + "autoload": { + "psr-4": { + "Pusher\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Library for interacting with the Pusher REST API", + "keywords": [ + "events", + "messaging", + "php-pusher-server", + "publish", + "push", + "pusher", + "real time", + "real-time", + "realtime", + "rest", + "trigger" + ], + "support": { + "issues": "https://github.com/pusher/pusher-http-php/issues", + "source": "https://github.com/pusher/pusher-http-php/tree/7.0.2" + }, + "time": "2021-12-07T13:09:00+00:00" + }, { "name": "ralouphie/getallheaders", "version": "3.0.3", diff --git a/config/broadcasting.php b/config/broadcasting.php new file mode 100644 index 0000000..2733d4e --- /dev/null +++ b/config/broadcasting.php @@ -0,0 +1,62 @@ + env('BROADCAST_DRIVER', 'null'), + + /* + |-------------------------------------------------------------------------- + | Broadcast Connections + |-------------------------------------------------------------------------- + | + | Here you may define all of the broadcast connections that will be used + | to broadcast events to other systems or over websockets. Samples of + | each available type of connection are provided inside this array. + | + */ + + 'connections' => [ + + 'pusher' => [ + 'driver' => 'pusher', + 'key' => env('PUSHER_APP_KEY', 'app-key'), + 'secret' => env('PUSHER_APP_SECRET', 'app-secret'), + 'app_id' => env('PUSHER_APP_ID', 'app-id'), + 'options' => [ + 'host' => env('PUSHER_HOST', '127.0.0.1'), + 'port' => env('PUSHER_PORT', 6001), + 'scheme' => env('PUSHER_SCHEME', 'http'), + 'encrypted' => true, + 'useTLS' => env('PUSHER_SCHEME') === 'https', + ], + ], + + 'redis' => [ + 'driver' => 'redis', + 'connection' => env('BROADCAST_REDIS_CONNECTION', 'default'), + ], + + 'log' => [ + 'driver' => 'log', + ], + + 'null' => [ + 'driver' => 'null', + ], + + ], + +]; diff --git a/routes/api.php b/routes/api.php index 88b3581..9743b38 100644 --- a/routes/api.php +++ b/routes/api.php @@ -2,6 +2,8 @@ /** @var \Laravel\Lumen\Routing\Router $router */ +use Illuminate\Support\Facades\Broadcast; + $router->get('/users', [ 'uses' => 'UserController@index' ]); @@ -103,3 +105,7 @@ $router->patch('/{route:.*}/', $controller); $router->delete('/{route:.*}/', $controller); }); + + +$router->get('broadcasting/auth', ['uses' => 'BroadcastController@authenticate']); +$router->post('broadcasting/auth', ['uses' => 'BroadcastController@authenticate']); diff --git a/routes/channels.php b/routes/channels.php new file mode 100644 index 0000000..3a035ce --- /dev/null +++ b/routes/channels.php @@ -0,0 +1,7 @@ +id === (int) $userId; +});