From 389606fb2b6720813f2b79e37076b273f7517674 Mon Sep 17 00:00:00 2001 From: Yorda Date: Thu, 8 Sep 2022 17:24:43 -0600 Subject: [PATCH] Pusher Authorization Endpoint (#16) * added pusher auth endpoint. * Fix styling --- composer.json | 3 +- composer.lock | 208 +++++++++++++++++++++++++- routes/BroadcastAuthRoute.php | 12 ++ src/QueryWatcherServiceProvider.php | 15 +- src/Services/BroadcastAuthService.php | 31 ++++ 5 files changed, 263 insertions(+), 6 deletions(-) create mode 100644 routes/BroadcastAuthRoute.php create mode 100644 src/Services/BroadcastAuthService.php diff --git a/composer.json b/composer.json index 278ba11..d9946b2 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "require": { "php": "^8.0", "illuminate/contracts": "^8.0|^9.0", - "guzzlehttp/guzzle": "^7.4.5" + "guzzlehttp/guzzle": "^7.4.5", + "pusher/pusher-php-server": "^7.1@beta" }, "require-dev": { "ext-pdo_sqlite": "*", diff --git a/composer.lock b/composer.lock index 90a3b78..dd5fe11 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": "6fc37d080bd41d36dfb7f1de1923bbae", + "content-hash": "11512756378bb9d251a7430cfddef31d", "packages": [ { "name": "brick/math", @@ -1921,6 +1921,147 @@ ], "time": "2022-08-01T11:03:24+00:00" }, + { + "name": "paragonie/random_compat", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/paragonie/random_compat.git", + "reference": "64f3265002e9e181436942aac2fbd482e186dc55" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/random_compat/zipball/64f3265002e9e181436942aac2fbd482e186dc55", + "reference": "64f3265002e9e181436942aac2fbd482e186dc55", + "shasum": "" + }, + "require": { + "php": ">=5.2.0" + }, + "require-dev": { + "phpunit/phpunit": "*" + }, + "suggest": { + "ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes." + }, + "default-branch": true, + "type": "library", + "autoload": { + "files": [ + "lib/random.php" + ] + }, + "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": "2022-06-19T18:08:39+00:00" + }, + { + "name": "paragonie/sodium_compat", + "version": "v1.17.1", + "source": { + "type": "git", + "url": "https://github.com/paragonie/sodium_compat.git", + "reference": "ac994053faac18d386328c91c7900f930acadf1e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/sodium_compat/zipball/ac994053faac18d386328c91c7900f930acadf1e", + "reference": "ac994053faac18d386328c91c7900f930acadf1e", + "shasum": "" + }, + "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.17.1" + }, + "time": "2022-03-23T19:32:04+00:00" + }, { "name": "phpoption/phpoption", "version": "dev-master", @@ -2370,6 +2511,67 @@ }, "time": "2022-04-08T16:41:45+00:00" }, + { + "name": "pusher/pusher-php-server", + "version": "7.1.0-beta", + "source": { + "type": "git", + "url": "https://github.com/pusher/pusher-http-php.git", + "reference": "9b3fa26629ac7e5f993b09511c4b8875b3088510" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pusher/pusher-http-php/zipball/9b3fa26629ac7e5f993b09511c4b8875b3088510", + "reference": "9b3fa26629ac7e5f993b09511c4b8875b3088510", + "shasum": "" + }, + "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.1.0-beta" + }, + "time": "2022-06-02T09:26:15+00:00" + }, { "name": "ralouphie/getallheaders", "version": "3.0.3", @@ -7884,7 +8086,9 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": [], + "stability-flags": { + "pusher/pusher-php-server": 10 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/routes/BroadcastAuthRoute.php b/routes/BroadcastAuthRoute.php new file mode 100644 index 0000000..4d97e37 --- /dev/null +++ b/routes/BroadcastAuthRoute.php @@ -0,0 +1,12 @@ + BroadcastAuthService::pusher($request), + default => response([], 401), + }; +})->name('broadcasting.pusher.auth'); diff --git a/src/QueryWatcherServiceProvider.php b/src/QueryWatcherServiceProvider.php index f0633c0..0a833f7 100644 --- a/src/QueryWatcherServiceProvider.php +++ b/src/QueryWatcherServiceProvider.php @@ -2,9 +2,10 @@ namespace YorCreative\QueryWatcher; -use Illuminate\Support\Facades\Broadcast; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider; +use Pusher\Pusher; use YorCreative\QueryWatcher\Events\QueryEvent; use YorCreative\QueryWatcher\Listeners\QueryListener; @@ -20,6 +21,8 @@ public function register() { $this->mergeConfigFrom(dirname(__DIR__, 1).'/config/querywatcher.php', 'querywatcher'); + $this->loadRoutesFrom(dirname(__DIR__, 1).'/routes/BroadcastAuthRoute.php'); + $this->publishes([ dirname(__DIR__, 1).'/config' => base_path('config'), ]); @@ -49,8 +52,14 @@ public function getEventListeners(): array public function boot() { - Broadcast::routes(); - require dirname(__DIR__, 1).'/routes/QueryChannel.php'; + + $this->app->singleton(Pusher::class, function () { + return new Pusher( + Config::get('broadcasting.connections.pusher.key'), + Config::get('broadcasting.connections.pusher.secret'), + Config::get('broadcasting.connections.pusher.app_id') + ); + }); } } diff --git a/src/Services/BroadcastAuthService.php b/src/Services/BroadcastAuthService.php new file mode 100644 index 0000000..0173e98 --- /dev/null +++ b/src/Services/BroadcastAuthService.php @@ -0,0 +1,31 @@ +authorizeChannel( + $request->input('channel_name'), + $request->input('socket_id') + ); + + return response($auth, 200); + } +}