Skip to content

Commit

Permalink
[5.x] Throws Laravel\Horizon\Exceptions\ForbiddenException on unaut…
Browse files Browse the repository at this point in the history
…horized access (#1308)

* Allow overriding the status code for unauthorized requests via config

* Update tests

* Use custom unauthorized exception instead of config value

* Remove unused imports

* Use custom exception

* Use original error code and rename exception

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* formatting

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Joel Butcher <>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Aug 30, 2023
1 parent 0c3d961 commit 2fc2ba7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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

0 comments on commit 2fc2ba7

Please sign in to comment.