From f5f8b1ba7bf6300cda61f1f8e371e21aceab6fc0 Mon Sep 17 00:00:00 2001 From: rokhun Date: Sat, 29 Aug 2020 04:30:24 +0900 Subject: [PATCH 1/2] [add] sanctum spa provider --- composer.json | 2 +- config/api.php | 3 +- src/Auth/Provider/SanctumSPA.php | 49 ++++++++++++++++++++++++++ tests/Auth/Provider/SanctumSPATest.php | 48 +++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 src/Auth/Provider/SanctumSPA.php create mode 100644 tests/Auth/Provider/SanctumSPATest.php diff --git a/composer.json b/composer.json index 39371088c..4915d86e3 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,6 @@ "league/fractal": "^0.19" }, "require-dev": { - "phpdocumentor/reflection-docblock": "3.3.2", "friendsofphp/php-cs-fixer": "~2", "illuminate/auth": "^7.0", "illuminate/cache": "^7.0", @@ -32,6 +31,7 @@ "illuminate/pagination": "^7.0", "laravel/lumen-framework": "^7.0", "mockery/mockery": "~1.0", + "phpdocumentor/reflection-docblock": "3.3.2", "phpunit/phpunit": "^8.5", "squizlabs/php_codesniffer": "~2.0", "tymon/jwt-auth": "1.0.*" diff --git a/config/api.php b/config/api.php index d6e883600..5a1931c36 100644 --- a/config/api.php +++ b/config/api.php @@ -154,7 +154,8 @@ */ 'middleware' => [ - + // If you are using sanctum spa authentication, please turn off the Comment. + // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class ], /* diff --git a/src/Auth/Provider/SanctumSPA.php b/src/Auth/Provider/SanctumSPA.php new file mode 100644 index 000000000..a8051efc0 --- /dev/null +++ b/src/Auth/Provider/SanctumSPA.php @@ -0,0 +1,49 @@ +auth = $auth; + } + + /** + * Authenticate request with Basic. + * + * @param \Illuminate\Http\Request $request + * @param \Dingo\Api\Routing\Route $route + * + * @return mixed + */ + public function authenticate(Request $request, Route $route) + { + if ($user = $this->auth->guard('web')->user()) { + return $user; + } + throw new UnauthorizedHttpException('', + 'Unauthenticated' + ); + } +} diff --git a/tests/Auth/Provider/SanctumSPATest.php b/tests/Auth/Provider/SanctumSPATest.php new file mode 100644 index 000000000..cc302c32b --- /dev/null +++ b/tests/Auth/Provider/SanctumSPATest.php @@ -0,0 +1,48 @@ +auth = m::mock('Illuminate\Auth\AuthManager'); + $this->provider = new SanctumSPA($this->auth); + } + + public function testInvalidSanctumCredentialsThrowsException() + { + $this->expectException(UnauthorizedHttpException::class); + + $request = Request::create('GET', '/'); + + $this->auth->shouldReceive('guard')->andReturn(m::self()); + + $this->auth->shouldReceive('user')->once()->andReturn(null); + + $this->provider->authenticate($request, m::mock(Route::class)); + } + + public function testAuthenticatingSucceedsAndReturnsUserObject() + { + $request = Request::create('GET', '/'); + + $this->auth->shouldReceive('guard')->andReturn(m::self()); + + $this->auth->shouldReceive('user')->once()->andReturn((object) ['id' => 1]); + + $this->assertSame(1, $this->provider->authenticate($request, m::mock(Route::class))->id); + } +} From a58e0315ba3c3e8c97a8746c2f4a0e89767d92c5 Mon Sep 17 00:00:00 2001 From: rokhun Date: Sat, 29 Aug 2020 05:37:29 +0900 Subject: [PATCH 2/2] [fix] style issue --- src/Auth/Provider/SanctumSPA.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Auth/Provider/SanctumSPA.php b/src/Auth/Provider/SanctumSPA.php index a8051efc0..fee15ff6d 100644 --- a/src/Auth/Provider/SanctumSPA.php +++ b/src/Auth/Provider/SanctumSPA.php @@ -1,6 +1,5 @@