Skip to content

Commit

Permalink
respectively custom session guard
Browse files Browse the repository at this point in the history
  • Loading branch information
taxusorg committed Nov 1, 2024
1 parent 5716afc commit 4d2fa2d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class Guard
*/
protected $auth;

/**
* guards will be checked
*
* @var string|string[]|null
*/
protected $guards;

/**
* The number of minutes tokens should be allowed to remain valid.
*
Expand All @@ -36,13 +43,15 @@ class Guard
* @param \Illuminate\Contracts\Auth\Factory $auth
* @param int $expiration
* @param string $provider
* @param string|string[]|null $guards
* @return void
*/
public function __construct(AuthFactory $auth, $expiration = null, $provider = null)
public function __construct(AuthFactory $auth, $expiration = null, $provider = null, $guards = null)
{
$this->auth = $auth;
$this->expiration = $expiration;
$this->provider = $provider;
$this->guards = $guards;
}

/**
Expand All @@ -53,7 +62,7 @@ public function __construct(AuthFactory $auth, $expiration = null, $provider = n
*/
public function __invoke(Request $request)
{
foreach (Arr::wrap(config('sanctum.guard', 'web')) as $guard) {
foreach (Arr::wrap($this->guards ?? config('sanctum.guard', 'web')) as $guard) {
if ($user = $this->auth->guard($guard)->user()) {
return $this->supportsTokens($user)
? $user->withAccessToken(new TransientToken)
Expand Down
2 changes: 1 addition & 1 deletion src/SanctumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function configureGuard()
protected function createGuard($auth, $config)
{
return new RequestGuard(
new Guard($auth, config('sanctum.expiration'), $config['provider']),
new Guard($auth, config('sanctum.expiration'), $config['provider'], $config['with_guard'] ?? null),
request(),
$auth->createUserProvider($config['provider'] ?? null)
);
Expand Down
38 changes: 38 additions & 0 deletions tests/Feature/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,44 @@ public function test_authentication_is_attempted_with_web_middleware()
$this->assertTrue($user->tokenCan('foo'));
}

public function test_authentication_is_attempted_with_given_guard()
{
$factory = Mockery::mock(AuthFactory::class);

$guard = new Guard($factory, null, 'users', ['other']);

$webGuard = Mockery::mock(stdClass::class);
$otherGuard = Mockery::mock(stdClass::class);

$factory->shouldReceive('guard')
->with('web')
->andReturn($webGuard);
$factory->shouldReceive('guard')
->with('other')
->andReturn($otherGuard);

$webGuard->shouldReceive('user')->never();
$otherGuard->shouldReceive('user')->once()->andReturn($fakeUser = new User);

$user = $guard->__invoke(Request::create('/', 'GET'));

$this->assertSame($user, $fakeUser);
$this->assertTrue($user->tokenCan('foo'));
}

public function test_authentication_ignore_all_guard()
{
$factory = Mockery::mock(AuthFactory::class);

$guard = new Guard($factory, null, 'users', []);

$factory->shouldReceive('guard')->never();

$user = $guard->__invoke(Request::create('/', 'GET'));

$this->assertNull($user);
}

public function test_authentication_is_attempted_with_token_if_no_session_present()
{
$factory = Mockery::mock(AuthFactory::class);
Expand Down

0 comments on commit 4d2fa2d

Please sign in to comment.