Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel Sanctum Spa Auth Provider #1742

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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|^8.0",
"illuminate/cache": "^7.0|^8.0",
Expand All @@ -32,6 +31,7 @@
"illuminate/pagination": "^7.0|^8.0",
"laravel/lumen-framework": "^7.0|^8.0",
"mockery/mockery": "~1.0",
"phpdocumentor/reflection-docblock": "3.3.2",
"phpunit/phpunit": "^8.5|^9.0",
"squizlabs/php_codesniffer": "~2.0",
"tymon/jwt-auth": "1.0.*"
Expand Down
3 changes: 2 additions & 1 deletion config/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@
*/

'middleware' => [

// If you are using sanctum spa authentication, please turn off the Comment.
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class
],

/*
Expand Down
49 changes: 49 additions & 0 deletions src/Auth/Provider/SanctumSPA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Dingo\Api\Auth\Provider;

use Dingo\Api\Contract\Auth\Provider;
use Dingo\Api\Routing\Route;
use Illuminate\Auth\AuthManager;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class SanctumSPA implements Provider
{
/**
* Illuminate authentication manager.
*
* @var \Illuminate\Auth\AuthManager
*/
private $auth;

/**
* Create a new basic provider instance.
*
* @param \Illuminate\Auth\AuthManager $auth
*
* @return void
*/
public function __construct(AuthManager $auth)
{
$this->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'
);
}
}
48 changes: 48 additions & 0 deletions tests/Auth/Provider/SanctumSPATest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Dingo\Api\Tests\Auth\Provider;

use Dingo\Api\Auth\Provider\SanctumSPA;
use Dingo\Api\Routing\Route;
use Dingo\Api\Tests\BaseTestCase;
use Illuminate\Http\Request;
use Mockery as m;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class SanctumSPATest extends BaseTestCase
{
protected $auth;
protected $provider;

public function setUp(): void
{
parent::setUp();

$this->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);
}
}