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.5] Let failedBasicResponse throw an exception. #20673

Merged
merged 1 commit into from
Aug 21, 2017
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
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