Skip to content

Commit 602a8da

Browse files
korridorOnatcer
authored andcommitted
Suppress expected OAuth access denial reports
1 parent 38b448a commit 602a8da

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

app/Exceptions/Handler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
88
use Illuminate\Http\RedirectResponse;
99
use Illuminate\Http\Request;
10+
use League\OAuth2\Server\Exception\OAuthServerException;
1011
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
1112
use Symfony\Component\HttpFoundation\Response;
1213
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -34,6 +35,10 @@ public function register(): void
3435
//
3536
});
3637

38+
$this->dontReportWhen(fn (Throwable $e): bool => $e instanceof OAuthServerException
39+
&& $e->getErrorType() === 'access_denied'
40+
&& $e->getHttpStatusCode() === 401);
41+
3742
// A request on an untrusted host (see App\Http\Middleware\TrustHosts)
3843
// otherwise renders as a bare "Bad request." 400. Show a message that
3944
// says how to fix it instead. The framework has already converted the

tests/Unit/Exceptions/Api/ApiExceptionTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ public static function expectedApiExceptionProvider(): iterable
6363
#[DataProvider('expectedApiExceptionProvider')]
6464
public function test_expected_api_exceptions_are_not_reported(ApiException $exception): void
6565
{
66-
$this->assertTrue($exception->report());
66+
// Arrange: exception supplied by the data provider
67+
68+
// Act
69+
$reportingHandled = $exception->report();
70+
71+
// Assert
72+
$this->assertTrue($reportingHandled);
6773
}
6874

6975
/**
@@ -79,6 +85,12 @@ public static function operationalApiExceptionProvider(): iterable
7985
#[DataProvider('operationalApiExceptionProvider')]
8086
public function test_operational_api_exceptions_are_reported(ApiException $exception): void
8187
{
82-
$this->assertFalse($exception->report());
88+
// Arrange: exception supplied by the data provider
89+
90+
// Act
91+
$reportingHandled = $exception->report();
92+
93+
// Assert
94+
$this->assertFalse($reportingHandled);
8395
}
8496
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit\Exceptions;
6+
7+
use App\Exceptions\Handler;
8+
use League\OAuth2\Server\Exception\OAuthServerException;
9+
use RuntimeException;
10+
use Tests\TestCase;
11+
12+
class HandlerTest extends TestCase
13+
{
14+
public function test_oauth_access_denied_exceptions_are_not_reported(): void
15+
{
16+
// Arrange
17+
$exception = OAuthServerException::accessDenied(
18+
'Access token could not be verified',
19+
previous: new RuntimeException('The token is expired')
20+
);
21+
22+
// Act
23+
$shouldReport = app(Handler::class)->shouldReport($exception);
24+
25+
// Assert
26+
$this->assertFalse($shouldReport);
27+
}
28+
29+
public function test_operational_oauth_exceptions_are_reported(): void
30+
{
31+
// Arrange
32+
$exception = OAuthServerException::serverError('Signing key could not be read');
33+
34+
// Act
35+
$shouldReport = app(Handler::class)->shouldReport($exception);
36+
37+
// Assert
38+
$this->assertTrue($shouldReport);
39+
}
40+
}

0 commit comments

Comments
 (0)