Skip to content

Commit

Permalink
Let failedBasicResponse throw an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmichot committed Aug 21, 2017
1 parent 6b692b3 commit 37fb414
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use RuntimeException;
use Illuminate\Support\Str;
use Illuminate\Http\Response;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Session\Session;
use Illuminate\Contracts\Auth\UserProvider;
Expand All @@ -13,6 +12,7 @@
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Contracts\Auth\SupportsBasicAuth;
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class SessionGuard implements StatefulGuard, SupportsBasicAuth
Expand Down Expand Up @@ -331,11 +331,12 @@ protected function basicCredentials(Request $request, $field)
/**
* Get the response for basic authentication.
*
* @return \Symfony\Component\HttpFoundation\Response
* @return void
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
protected function failedBasicResponse()
{
return new Response('Invalid credentials.', 401, ['WWW-Authenticate' => 'Basic']);
throw new UnauthorizedHttpException('Basic');
}

/**
Expand Down
9 changes: 5 additions & 4 deletions tests/Auth/AuthGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function testBasicReturnsNullWhenAlreadyLoggedIn()
$guard->basic('email');
}

/**
* @expectedException \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
* @expectedExceptionMessage
*/
public function testBasicReturnsResponseOnFailure()
{
list($session, $provider, $request, $cookie) = $this->getMocks();
Expand All @@ -48,10 +52,7 @@ public function testBasicReturnsResponseOnFailure()
$guard->shouldReceive('attempt')->once()->with(['email' => '[email protected]', 'password' => 'secret'])->andReturn(false);
$request = \Symfony\Component\HttpFoundation\Request::create('/', 'GET', [], [], [], ['PHP_AUTH_USER' => '[email protected]', 'PHP_AUTH_PW' => 'secret']);
$guard->setRequest($request);
$response = $guard->basic('email');

$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$this->assertEquals(401, $response->getStatusCode());
$guard->basic('email');
}

public function testBasicWithExtraConditions()
Expand Down

0 comments on commit 37fb414

Please sign in to comment.