Skip to content

Commit c5079b4

Browse files
authored
Merge pull request #13 from TomHAnderson/feature/api-problem
Use ApiSkeletons\Laravel\ApiProblem
2 parents eb544e4 + fdc69d2 commit c5079b4

File tree

3 files changed

+7
-13
lines changed

3 files changed

+7
-13
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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
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;
1011

11-
use function response;
1212
use function substr;
1313

1414
class AuthorizeApiKey
@@ -26,15 +26,11 @@ public function __construct(ApiKeyService $apiKeyService)
2626
public function handle(Request $request, Closure $next, ?string $scope = null): mixed
2727
{
2828
$header = $request->header('Authorization');
29-
// Remove Bearer from key prefix
3029
if (! $header) {
31-
return response([
32-
'errors' => [
33-
['message' => 'Unauthorized'],
34-
],
35-
], 401);
30+
return ApiProblem::response('Missing the Authorization header', 401);
3631
}
3732

33+
// Remove Bearer from key prefix
3834
$key = substr($header, 7);
3935

4036
$apiKey = $this->apiKeyService->isActive($key);
@@ -55,10 +51,6 @@ public function handle(Request $request, Closure $next, ?string $scope = null):
5551
}
5652
}
5753

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

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)