From ba9c0cb8608736173e00de0b759c277e37925e62 Mon Sep 17 00:00:00 2001 From: Rahul Dey Date: Tue, 2 Jun 2020 02:18:38 +0530 Subject: [PATCH 1/7] added multi auth support --- .idea/.gitignore | 8 +++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 +++ .idea/php-test-framework.xml | 14 +++++ .idea/php.xml | 95 ++++++++++++++++++++++++++++++++++ .idea/phpunit.xml | 10 ++++ .idea/sanctum.iml | 94 +++++++++++++++++++++++++++++++++ .idea/vcs.xml | 6 +++ src/Guard.php | 27 +++++++++- src/SanctumServiceProvider.php | 14 ++++- 10 files changed, 279 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/php-test-framework.xml create mode 100644 .idea/php.xml create mode 100644 .idea/phpunit.xml create mode 100644 .idea/sanctum.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..73f69e09 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..28a804d8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..542d8b3f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php-test-framework.xml b/.idea/php-test-framework.xml new file mode 100644 index 00000000..abc0c211 --- /dev/null +++ b/.idea/php-test-framework.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 00000000..d8067f1e --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml new file mode 100644 index 00000000..4f8104cf --- /dev/null +++ b/.idea/phpunit.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/sanctum.iml b/.idea/sanctum.iml new file mode 100644 index 00000000..798a828c --- /dev/null +++ b/.idea/sanctum.iml @@ -0,0 +1,94 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Guard.php b/src/Guard.php index 60a7658a..bb1ce526 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -14,6 +14,13 @@ class Guard */ protected $auth; + /** + * The provider name + * + * @var string + */ + protected $provider; + /** * The number of minutes tokens should be allowed to remain valid. * @@ -25,13 +32,15 @@ class Guard * Create a new guard instance. * * @param \Illuminate\Contracts\Auth\Factory $auth + * @param string $provider * @param int $expiration * @return void */ - public function __construct(AuthFactory $auth, $expiration = null) + public function __construct(AuthFactory $auth, $provider, $expiration = null) { $this->auth = $auth; $this->expiration = $expiration; + $this->provider = $provider; } /** @@ -55,7 +64,8 @@ public function __invoke(Request $request) if (! $accessToken || ($this->expiration && - $accessToken->created_at->lte(now()->subMinutes($this->expiration)))) { + $accessToken->created_at->lte(now()->subMinutes($this->expiration))) || + ! $this->hasValidProvider($accessToken->tokenable)) { return; } @@ -77,4 +87,17 @@ protected function supportsTokens($tokenable = null) get_class($tokenable) )); } + + /** + * Determine if the Token Provider Correct + * + * @param \Illuminate\Database\Eloquent\Model $tokenable + * @return bool + */ + protected function hasValidProvider($tokenable) + { + $model = config("auth.providers.{$this->provider}.model"); + + return $tokenable instanceof $model; + } } diff --git a/src/SanctumServiceProvider.php b/src/SanctumServiceProvider.php index ecee3fc6..96261ad2 100644 --- a/src/SanctumServiceProvider.php +++ b/src/SanctumServiceProvider.php @@ -2,6 +2,7 @@ namespace Laravel\Sanctum; +use Illuminate\Auth\RequestGuard; use Illuminate\Contracts\Http\Kernel; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Route; @@ -92,10 +93,21 @@ protected function defineRoutes() protected function configureGuard() { Auth::resolved(function ($auth) { - $auth->viaRequest('sanctum', new Guard($auth, config('sanctum.expiration'))); + $auth->extend('sanctum', function ($app, $name, array $config) use ($auth) { + return tap($this->registerGuard($auth, $config), function ($guard) { + $this->app->refresh('request', $guard, 'setRequest'); + }); + }); }); } + protected function registerGuard($auth, $config) { + return new RequestGuard( + new Guard($auth, $config['provider'], config('sanctum.expiration')), + $this->app['request'], + $auth->createUserProvider()); + } + /** * Configure the Sanctum middleware and priority. * From f5d47af7b61983e36dadc1043825cdd5f9cba57f Mon Sep 17 00:00:00 2001 From: Rahul Dey Date: Wed, 3 Jun 2020 07:08:22 +0530 Subject: [PATCH 2/7] fixed some issues --- src/Guard.php | 5 +++++ src/SanctumServiceProvider.php | 1 + tests/GuardTest.php | 8 ++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Guard.php b/src/Guard.php index bb1ce526..09b4c043 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -96,6 +96,11 @@ protected function supportsTokens($tokenable = null) */ protected function hasValidProvider($tokenable) { + // If provider is null this method will return TRUE + if($this->provider === null) { + return true; + } + $model = config("auth.providers.{$this->provider}.model"); return $tokenable instanceof $model; diff --git a/src/SanctumServiceProvider.php b/src/SanctumServiceProvider.php index 96261ad2..d109e5d7 100644 --- a/src/SanctumServiceProvider.php +++ b/src/SanctumServiceProvider.php @@ -22,6 +22,7 @@ public function register() config([ 'auth.guards.sanctum' => array_merge([ 'driver' => 'sanctum', + 'provider' => null ], config('auth.guards.sanctum', [])), ]); diff --git a/tests/GuardTest.php b/tests/GuardTest.php index bef095ed..136474b3 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -39,7 +39,7 @@ public function test_authentication_is_attempted_with_web_middleware() { $factory = Mockery::mock(AuthFactory::class); - $guard = new Guard($factory); + $guard = new Guard($factory, 'users'); $webGuard = Mockery::mock(stdClass::class); @@ -61,7 +61,7 @@ public function test_authentication_is_attempted_with_token_if_no_session_presen $factory = Mockery::mock(AuthFactory::class); - $guard = new Guard($factory); + $guard = new Guard($factory, 'users'); $webGuard = Mockery::mock(stdClass::class); @@ -86,7 +86,7 @@ public function test_authentication_with_token_fails_if_expired() $factory = Mockery::mock(AuthFactory::class); - $guard = new Guard($factory, 1); + $guard = new Guard($factory, 'users', 1); $webGuard = Mockery::mock(stdClass::class); @@ -126,7 +126,7 @@ public function test_authentication_is_successful_with_token_if_no_session_prese $factory = Mockery::mock(AuthFactory::class); - $guard = new Guard($factory); + $guard = new Guard($factory, null); $webGuard = Mockery::mock(stdClass::class); From 8adaa91622c7e6d48c9bc7271bb36bb61a0f4096 Mon Sep 17 00:00:00 2001 From: Rahul Dey Date: Wed, 3 Jun 2020 07:12:01 +0530 Subject: [PATCH 3/7] phpstrom files deleted --- .gitignore | 1 + .idea/.gitignore | 8 --- .idea/misc.xml | 6 --- .idea/modules.xml | 8 --- .idea/php-test-framework.xml | 14 ------ .idea/php.xml | 95 ------------------------------------ .idea/phpunit.xml | 10 ---- .idea/sanctum.iml | 94 ----------------------------------- .idea/vcs.xml | 6 --- 9 files changed, 1 insertion(+), 241 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/php-test-framework.xml delete mode 100644 .idea/php.xml delete mode 100644 .idea/phpunit.xml delete mode 100644 .idea/sanctum.iml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 660fc15e..cc31cac1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ composer.lock /phpunit.xml .phpunit.result.cache +.idea diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 73f69e09..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 28a804d8..00000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 542d8b3f..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/php-test-framework.xml b/.idea/php-test-framework.xml deleted file mode 100644 index abc0c211..00000000 --- a/.idea/php-test-framework.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml deleted file mode 100644 index d8067f1e..00000000 --- a/.idea/php.xml +++ /dev/null @@ -1,95 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml deleted file mode 100644 index 4f8104cf..00000000 --- a/.idea/phpunit.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/sanctum.iml b/.idea/sanctum.iml deleted file mode 100644 index 798a828c..00000000 --- a/.idea/sanctum.iml +++ /dev/null @@ -1,94 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 9b44c3c4727869172f390c056b96e9c25a843daa Mon Sep 17 00:00:00 2001 From: Rahul Dey Date: Wed, 3 Jun 2020 07:15:45 +0530 Subject: [PATCH 4/7] added doc comment --- src/SanctumServiceProvider.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/SanctumServiceProvider.php b/src/SanctumServiceProvider.php index d109e5d7..e33aa7ce 100644 --- a/src/SanctumServiceProvider.php +++ b/src/SanctumServiceProvider.php @@ -102,6 +102,13 @@ protected function configureGuard() }); } + /** + * Register the guard + * + * @param \Illuminate\Contracts\Auth\Factory $auth + * @param array $config + * @return RequestGuard + */ protected function registerGuard($auth, $config) { return new RequestGuard( new Guard($auth, $config['provider'], config('sanctum.expiration')), From 93ee6f34e620ba09c5c8b144ac1d88e834c80329 Mon Sep 17 00:00:00 2001 From: Rahul Dey Date: Wed, 3 Jun 2020 07:26:07 +0530 Subject: [PATCH 5/7] fixed styles --- src/Guard.php | 6 +++--- src/SanctumServiceProvider.php | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Guard.php b/src/Guard.php index 09b4c043..015fb616 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -15,7 +15,7 @@ class Guard protected $auth; /** - * The provider name + * The provider name. * * @var string */ @@ -89,7 +89,7 @@ protected function supportsTokens($tokenable = null) } /** - * Determine if the Token Provider Correct + * Determine if the Token Provider Correct. * * @param \Illuminate\Database\Eloquent\Model $tokenable * @return bool @@ -97,7 +97,7 @@ protected function supportsTokens($tokenable = null) protected function hasValidProvider($tokenable) { // If provider is null this method will return TRUE - if($this->provider === null) { + if ($this->provider === null) { return true; } diff --git a/src/SanctumServiceProvider.php b/src/SanctumServiceProvider.php index e33aa7ce..1ae220c8 100644 --- a/src/SanctumServiceProvider.php +++ b/src/SanctumServiceProvider.php @@ -22,7 +22,7 @@ public function register() config([ 'auth.guards.sanctum' => array_merge([ 'driver' => 'sanctum', - 'provider' => null + 'provider' => null, ], config('auth.guards.sanctum', [])), ]); @@ -96,20 +96,21 @@ protected function configureGuard() Auth::resolved(function ($auth) { $auth->extend('sanctum', function ($app, $name, array $config) use ($auth) { return tap($this->registerGuard($auth, $config), function ($guard) { - $this->app->refresh('request', $guard, 'setRequest'); + $this->app->refresh('request', $guard, 'setRequest'); }); }); }); } /** - * Register the guard + * Register the guard. * * @param \Illuminate\Contracts\Auth\Factory $auth * @param array $config * @return RequestGuard */ - protected function registerGuard($auth, $config) { + protected function registerGuard($auth, $config) + { return new RequestGuard( new Guard($auth, $config['provider'], config('sanctum.expiration')), $this->app['request'], From 19dcda563f515dc61c23d36f38582bd9b4983ec6 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Wed, 3 Jun 2020 11:41:18 +0200 Subject: [PATCH 6/7] Update .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index cc31cac1..660fc15e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ composer.lock /phpunit.xml .phpunit.result.cache -.idea From 822fd55f4e7ee7d3cc49cdd4dde8cddc7f1cc899 Mon Sep 17 00:00:00 2001 From: Rahul Dey Date: Wed, 3 Jun 2020 17:55:56 +0530 Subject: [PATCH 7/7] fixed backward compatibility --- src/Guard.php | 6 +++--- src/SanctumServiceProvider.php | 2 +- tests/GuardTest.php | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Guard.php b/src/Guard.php index 015fb616..993aeedb 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -32,11 +32,11 @@ class Guard * Create a new guard instance. * * @param \Illuminate\Contracts\Auth\Factory $auth - * @param string $provider * @param int $expiration + * @param string $provider * @return void */ - public function __construct(AuthFactory $auth, $provider, $expiration = null) + public function __construct(AuthFactory $auth, $expiration = null, $provider = null) { $this->auth = $auth; $this->expiration = $expiration; @@ -91,7 +91,7 @@ protected function supportsTokens($tokenable = null) /** * Determine if the Token Provider Correct. * - * @param \Illuminate\Database\Eloquent\Model $tokenable + * @param \Illuminate\Database\Eloquent\Model $tokenable * @return bool */ protected function hasValidProvider($tokenable) diff --git a/src/SanctumServiceProvider.php b/src/SanctumServiceProvider.php index 1ae220c8..706ebda0 100644 --- a/src/SanctumServiceProvider.php +++ b/src/SanctumServiceProvider.php @@ -112,7 +112,7 @@ protected function configureGuard() protected function registerGuard($auth, $config) { return new RequestGuard( - new Guard($auth, $config['provider'], config('sanctum.expiration')), + new Guard($auth, config('sanctum.expiration'), $config['provider']), $this->app['request'], $auth->createUserProvider()); } diff --git a/tests/GuardTest.php b/tests/GuardTest.php index 136474b3..2d8ec85d 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -39,7 +39,7 @@ public function test_authentication_is_attempted_with_web_middleware() { $factory = Mockery::mock(AuthFactory::class); - $guard = new Guard($factory, 'users'); + $guard = new Guard($factory, null, 'users'); $webGuard = Mockery::mock(stdClass::class); @@ -61,7 +61,7 @@ public function test_authentication_is_attempted_with_token_if_no_session_presen $factory = Mockery::mock(AuthFactory::class); - $guard = new Guard($factory, 'users'); + $guard = new Guard($factory, null, 'users'); $webGuard = Mockery::mock(stdClass::class); @@ -86,7 +86,7 @@ public function test_authentication_with_token_fails_if_expired() $factory = Mockery::mock(AuthFactory::class); - $guard = new Guard($factory, 'users', 1); + $guard = new Guard($factory, 1, 'users'); $webGuard = Mockery::mock(stdClass::class);