Skip to content

Commit ba3783d

Browse files
committed
Use ApiSkeletons\Laravel\ApiProblem
1 parent eb544e4 commit ba3783d

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
],
2222
"require": {
2323
"php": "^8.0",
24-
"laravel-doctrine/orm": "^1.6"
24+
"laravel-doctrine/orm": "^1.6",
25+
"api-skeletons/laravel-api-problem": "^1.0.1"
2526
},
2627
"require-dev": {
2728
"doctrine/coding-standard": "^9.0",

src/Http/Middleware/AuthorizeApiKey.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Http\Middleware;
66

7+
use ApiSkeletons\Laravel\ApiProblem\Facades\ApiProblem;
78
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
89
use Closure;
910
use Illuminate\Http\Request;
@@ -26,15 +27,11 @@ public function __construct(ApiKeyService $apiKeyService)
2627
public function handle(Request $request, Closure $next, ?string $scope = null): mixed
2728
{
2829
$header = $request->header('Authorization');
29-
// Remove Bearer from key prefix
3030
if (! $header) {
31-
return response([
32-
'errors' => [
33-
['message' => 'Unauthorized'],
34-
],
35-
], 401);
31+
return ApiProblem::response('Missing the Authorization header', 401);
3632
}
3733

34+
// Remove Bearer from key prefix
3835
$key = substr($header, 7);
3936

4037
$apiKey = $this->apiKeyService->isActive($key);
@@ -55,10 +52,6 @@ public function handle(Request $request, Closure $next, ?string $scope = null):
5552
}
5653
}
5754

58-
return response([
59-
'errors' => [
60-
['message' => 'Unauthorized'],
61-
],
62-
], 401);
55+
return ApiProblem::response('Invalid ApiKey', 401);
6356
}
6457
}

test/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ protected function getPackageProviders($app)
1515
return [
1616
\LaravelDoctrine\ORM\DoctrineServiceProvider::class,
1717
\ApiSkeletons\Laravel\Doctrine\ApiKey\ServiceProvider::class,
18+
\ApiSkeletons\Laravel\ApiProblem\ServiceProvider::class,
1819
];
1920
}
2021

0 commit comments

Comments
 (0)