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 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
18 changes: 18 additions & 0 deletions src/Exceptions/ForbiddenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Horizon\Exceptions;

use Symfony\Component\HttpKernel\Exception\HttpException;

class ForbiddenException extends HttpException
{
/**
* Create a new exception instance.
*
* @return static
*/
public static function make()
{
return new static(403);
}
}
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\ForbiddenException;
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 ForbiddenException::make();
}

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\ForbiddenException;
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(ForbiddenException::class);

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