From a7e6921fc1fe815fad67799540a29ac982fc5743 Mon Sep 17 00:00:00 2001 From: Ben Sutter Date: Thu, 2 Jul 2020 14:27:12 +1000 Subject: [PATCH 1/3] Update to allow custom User classpath and also correctly implement Passport's 'auth:api' guard --- Plugin.php | 37 +++++++++++++++++++ README.md | 10 ++++- classes/Authenticate.php | 74 +++++++++++++++++++++++++++++++++++++ config/config.php | 2 +- models/Settings.php | 16 ++++++++ models/settings/fields.yaml | 7 ++++ 6 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 classes/Authenticate.php create mode 100644 models/Settings.php create mode 100644 models/settings/fields.yaml diff --git a/Plugin.php b/Plugin.php index f8e985d..6190293 100644 --- a/Plugin.php +++ b/Plugin.php @@ -3,6 +3,7 @@ use App; use Config; use Laravel\Passport\Passport; +use LukeTowers\Passport\Classes\Authenticate; use System\Classes\PluginBase; use Illuminate\Foundation\AliasLoader; @@ -11,6 +12,10 @@ */ class Plugin extends PluginBase { + public $middlewareAliases = [ + 'auth' => Authenticate::class + ]; + /** * Returns information about this plugin. * @@ -27,11 +32,29 @@ public function pluginDetails() ]; } + public function registerSettings() + { + return [ + 'settings' => [ + 'label' => 'Passport Settings', + 'description' => 'Manage passport based settings.', + 'category' => 'Passport', + 'icon' => 'icon-cog', + 'class' => 'LukeTowers\Passport\Models\Settings', + 'order' => 500, + 'keywords' => 'user auth passport' + ] + ]; + } + /** * Runs right before the request route */ public function boot() { + // Boot middleware aliases + $this->aliasMiddleware(); + // Disable the Laravel migrations from Passport, handled via plugin updates Passport::ignoreMigrations(); @@ -81,4 +104,18 @@ public function bootPackages() } } } + + /** + * Registers provided middleware aliases with the router + */ + protected function aliasMiddleware() + { + $router = $this->app['router']; + + $method = method_exists($router, 'aliasMiddleware') ? 'aliasMiddleware' : 'middleware'; + + foreach ($this->middlewareAliases as $alias => $middleware) { + $router->$method($alias, $middleware); + } + } } diff --git a/README.md b/README.md index e626d3b..238a052 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ WORK-IN-PROGRESS Laravel Passport integration for OctoberCMS # Installation -Add the plugin to your application and then run `php artisan october:up` to run the plugin's migrations. Following https://laravel.com/docs/5.5/passport#installation would have you run `php artisan passport:install`. Read https://laravel.com/docs/5.5/passport for more information on using Passport. +Add the plugin to your application and then run `php artisan october:up` to run the plugin's migrations. +Following https://laravel.com/docs/5.5/passport#installation would have you run `php artisan passport:install`. +Read https://laravel.com/docs/5.5/passport for more information on using Passport. -**NOTE:** The default backend user model is extended to work with Passport via the `LukeTowers\Passport\Models\BackendUser` model, use that instead of `Backend\Models\User` when working with Passport. +**NOTE:** + +The default backend user model is extended to work with Passport via the `LukeTowers\Passport\Models\BackendUser` model, use that instead of `Backend\Models\User` when working with Passport. + +To use a different use model simply update the class path via settings. Noting that the given class must use `HasApiTokens` as per Passport documentation. diff --git a/classes/Authenticate.php b/classes/Authenticate.php new file mode 100644 index 0000000..5bb98f4 --- /dev/null +++ b/classes/Authenticate.php @@ -0,0 +1,74 @@ +auth = app()['auth']; + } + + /** + * Handle an incoming request. + * + * @param Request $request + * @param Closure $next + * @param string[] ...$guards + * @return mixed + * + */ + public function handle($request, Closure $next, ...$guards) + { + try { + $this->authenticate($guards); + } catch (AuthenticationException $e) { + return response()->json(['error' => 'Unauthenticated!'], 401); + } + + return $next($request); + } + + /** + * Determine if the user is logged in to any of the given guards. + * + * @param array $guards + * @return void + * + * @throws AuthenticationException + */ + protected function authenticate(array $guards) + { + if (empty($guards)) { + return $this->auth->authenticate(); + } + + foreach ($guards as $guard) { + if ($this->auth->guard($guard)->check()) { + return $this->auth->shouldUse($guard); + } + } + + throw new AuthenticationException('Unauthenticated.', $guards); + } +} diff --git a/config/config.php b/config/config.php index ba80239..a135a5d 100644 --- a/config/config.php +++ b/config/config.php @@ -74,7 +74,7 @@ 'providers' => [ 'users' => [ 'driver' => 'eloquent', - 'model' => \LukeTowers\Passport\Models\BackendUser::class, + 'model' => \LukeTowers\Passport\Models\Settings::get('user_class', 'LukeTowers\Passport\Models\BackendUser'), ], // 'users' => [ diff --git a/models/Settings.php b/models/Settings.php new file mode 100644 index 0000000..5184f31 --- /dev/null +++ b/models/Settings.php @@ -0,0 +1,16 @@ + Date: Thu, 2 Jul 2020 14:38:46 +1000 Subject: [PATCH 2/3] Remove unnecessary trait as it is now covered. --- models/BackendUser.php | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/models/BackendUser.php b/models/BackendUser.php index 1da5897..3030c99 100644 --- a/models/BackendUser.php +++ b/models/BackendUser.php @@ -2,38 +2,12 @@ use Backend\Models\User as BackendUserModel; -// Authenticates the user for logging in and manages the remember token -// Initial compatibility with Illumninate.Auth -use Illuminate\Auth\Authenticatable; -use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; - // Initial compatibility with Laravel.Passport use Laravel\Passport\HasApiTokens; use Illuminate\Notifications\Notifiable; -// Manages the user's access to the application using Laravel's concept -// of 'Gates' & 'Policies'. See https://laravel.com/docs/5.5/authorization -// A parallel could in theory be constructed to use October's permission -// system -// use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; -// use Illuminate\Foundation\Auth\Access\Authorizable; - -// Triggers sending a password reset notification for the user -// use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; -// use Illuminate\Auth\Passwords\CanResetPassword; - -class BackendUser extends BackendUserModel implements AuthenticatableContract +class BackendUser extends BackendUserModel { - use Authenticatable, - HasApiTokens, + use HasApiTokens, Notifiable; - - /** - * The column name of the "remember me" token. - * - * @var string - */ - protected $rememberTokenName = 'persist_code'; - - } From 67ff0579f5cbf4d4cd55784c95d85ad35a1a4477 Mon Sep 17 00:00:00 2001 From: Ben Sutter Date: Wed, 24 Mar 2021 10:54:50 +1100 Subject: [PATCH 3/3] Update to work on Laravel 6.x --- README.md | 4 ++-- composer.json | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 238a052..8c4f198 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ WORK-IN-PROGRESS Laravel Passport integration for OctoberCMS # Installation Add the plugin to your application and then run `php artisan october:up` to run the plugin's migrations. -Following https://laravel.com/docs/5.5/passport#installation would have you run `php artisan passport:install`. -Read https://laravel.com/docs/5.5/passport for more information on using Passport. +Following https://laravel.com/docs/6.x/passport#installation would have you run `php artisan passport:install`. +Read https://laravel.com/docs/6.x/passport for more information on using Passport. **NOTE:** diff --git a/composer.json b/composer.json index 2d46eb9..c2cb0d3 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ ], "require": { "composer/installers": "~1.0", - "laravel/passport": "~4.0" + "laravel/passport": "~8.5", + "lcobucci/jwt": "3.3.3" } }