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.x] Throws Laravel\Horizon\Exceptions\ForbiddenException on unauthorized access #1308

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 10 additions & 0 deletions src/Exceptions/UnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Laravel\Horizon\Exceptions;

use Symfony\Component\HttpKernel\Exception\HttpException;

class UnauthorizedException extends HttpException
{
//
}
7 changes: 6 additions & 1 deletion src/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Horizon\Http\Middleware;

use Laravel\Horizon\Exceptions\UnauthorizedException;
use Laravel\Horizon\Horizon;

class Authenticate
Expand All @@ -15,6 +16,10 @@ class Authenticate
*/
public function handle($request, $next)
{
return Horizon::check($request) ? $next($request) : abort(403);
if (! Horizon::check($request)) {
throw new UnauthorizedException(401);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use 403 same as before?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably pasted the incorrect exception on my last review. Sorry

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the 403 status code !== unauthorized. We can use 403, but we should probably change the name of the exception to use "Forbidden" instead :)

}

return $next($request);
}
}
6 changes: 3 additions & 3 deletions tests/Feature/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Laravel\Horizon\Tests\Feature;

use Laravel\Horizon\Exceptions\UnauthorizedException;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\Http\Middleware\Authenticate;
use Laravel\Horizon\Tests\IntegrationTest;
use Symfony\Component\HttpKernel\Exception\HttpException;

class AuthTest extends IntegrationTest
{
Expand Down Expand Up @@ -41,9 +41,9 @@ function ($value) {
$this->assertSame('response', $response);
}

public function test_authentication_middleware_responds_with_403_on_failure()
public function test_authentication_middleware_throws_on_failure()
{
$this->expectException(HttpException::class);
$this->expectException(UnauthorizedException::class);

Horizon::auth(function () {
return false;
Expand Down