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

[5.3] Add given guards to AuthenticationException #15745

Merged
merged 2 commits into from
Oct 4, 2016
Merged
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
30 changes: 30 additions & 0 deletions src/Illuminate/Auth/AuthenticationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

class AuthenticationException extends Exception
{
/**
* Array of the given guards.
*
* @var array
*/
protected $guards;

/**
* Create a new authentication exception.
*
Expand All @@ -15,4 +22,27 @@ public function __construct($message = 'Unauthenticated.')
{
parent::__construct($message);
}

/**
* Set the given guards.
*
* @param array $guards
* @return $this
*/
public function setGuards(array $guards)
{
$this->guards = $guards;

return $this;
}

/**
* Get the given guards.
*
* @return array
*/
public function getGuards()
{
return $this->guards;
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Auth/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ protected function authenticate(array $guards)
}
}

throw new AuthenticationException;
throw (new AuthenticationException)->setGuards($guards);
}
}
34 changes: 34 additions & 0 deletions tests/Auth/AuthenticateMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public function testDefaultUnauthenticatedThrows()
$this->authenticate();
}

public function testDefaultUnauthenticatedThrowsWithGuards()
{
try {
$this->registerAuthDriver('default', false);

$this->authenticate('default');
} catch (AuthenticationException $e) {
$this->assertContains('default', $e->getGuards());

return;
}

return $this->fail();
}

public function testDefaultAuthenticatedKeepsDefaultDriver()
{
$driver = $this->registerAuthDriver('default', true);
Expand Down Expand Up @@ -69,6 +84,25 @@ public function testMultipleDriversUnauthenticatedThrows()
$this->authenticate('default', 'secondary');
}

public function testMultipleDriversUnauthenticatedThrowsWithGuards()
{
$expectedGuards = ['default', 'secondary'];

try {
$this->registerAuthDriver('default', false);

$this->registerAuthDriver('secondary', false);

$this->authenticate(...$expectedGuards);
} catch (AuthenticationException $e) {
$this->assertEquals($expectedGuards, $e->getGuards());

return;
}

return $this->fail();
}

public function testMultipleDriversAuthenticatedUdatesDefault()
{
$this->registerAuthDriver('default', false);
Expand Down