-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create middleware for resolving API key
- Loading branch information
1 parent
bd0e731
commit 924b305
Showing
6 changed files
with
95 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use App\Models\ApiKey; | ||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class ResolveApiKey | ||
{ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
$apiKey = ApiKey::query() | ||
->where('token', $request->header('api-key')) | ||
->first(); | ||
|
||
if (!$apiKey) { | ||
abort(401); | ||
} | ||
|
||
$request->attributes->add(['apiKey' => $apiKey]); | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Support\Str; | ||
|
||
class ApiKeyFactory extends Factory | ||
{ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'token' => Str::random(32), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\ApiKey; | ||
use App\Models\Space; | ||
use App\Models\User; | ||
use Tests\TestCase; | ||
|
||
class ResolveApiKeyMiddlewareTest extends TestCase | ||
{ | ||
public function testWithoutApiKey(): void | ||
{ | ||
$response = $this->get('/api/transactions'); | ||
|
||
$response->assertStatus(401); | ||
} | ||
|
||
public function testWithWrongApikey(): void | ||
{ | ||
$response = $this->get('/api/transactions', ['api_key' => 'WRONG_API_KEY']); | ||
|
||
$response->assertStatus(401); | ||
} | ||
|
||
public function testWithCorrectApiKey(): void | ||
{ | ||
$user = User::factory() | ||
->create(); | ||
|
||
$space = Space::factory() | ||
->create(); | ||
|
||
$user->spaces()->attach($space->id); | ||
|
||
$apiKey = ApiKey::factory() | ||
->create(['user_id' => $user->id]); | ||
|
||
$response = $this->get('/api/transactions', ['api_key' => $apiKey->token]); | ||
|
||
$response->assertStatus(200); | ||
} | ||
} |