Skip to content

Feature/api create license 2 #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ ANYSTACK_FOREVER_POLICY_ID=
ANYSTACK_TRIAL_POLICY_ID=

FILAMENT_USERS=

BIFROST_API_KEY=your-secure-api-key-here
111 changes: 0 additions & 111 deletions app/Filament/Resources/PersonalAccessTokenResource.php

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Kernel extends HttpKernel
*/
protected $middlewareAliases = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.api_key' => \App\Http\Middleware\AuthenticateApiKey::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
Expand Down
38 changes: 38 additions & 0 deletions app/Http/Middleware/AuthenticateApiKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class AuthenticateApiKey
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$apiKey = config('services.bifrost.api_key');

if (! $apiKey) {
return response()->json(['message' => 'API key not configured'], 500);
}

$authHeader = $request->header('Authorization');

if (! $authHeader || ! str_starts_with($authHeader, 'Bearer ')) {
return response()->json(['message' => 'Unauthorized'], 401);
}

$providedKey = substr($authHeader, 7); // Remove 'Bearer ' prefix

if (! hash_equals($apiKey, $providedKey)) {
return response()->json(['message' => 'Unauthorized'], 401);
}

return $next($request);
}
}
4 changes: 4 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@
'anystack' => [
'key' => env('ANYSTACK_API_KEY'),
],

'bifrost' => [
'api_key' => env('BIFROST_API_KEY'),
],
];
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
<env name="ANYSTACK_MINI_POLICY_ID" value="pol_mini"/>
<env name="ANYSTACK_PRO_POLICY_ID" value="pol_pro"/>
<env name="ANYSTACK_MAX_POLICY_ID" value="pol_max"/>
<env name="BIFROST_API_KEY" value="fake_key"/>
</php>
</phpunit>
5 changes: 4 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
|
*/

Route::middleware('auth.api_key')->group(function () {
Route::post('/licenses', [LicenseController::class, 'store']);
});

Route::middleware('auth:sanctum')->group(function () {
Route::get('/user', fn (Request $request) => $request->user());
Route::post('/licenses', [LicenseController::class, 'store']);
});
18 changes: 6 additions & 12 deletions tests/Feature/Api/CreateLicenseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public function test_requires_authentication()

public function test_validates_required_fields()
{
$user = User::factory()->create();
$token = $user->createToken('test-token')->plainTextToken;
$token = config('services.bifrost.api_key');

$response = $this->withHeaders([
'Authorization' => 'Bearer '.$token,
Expand All @@ -57,8 +56,7 @@ public function test_validates_required_fields()

public function test_validates_subscription_enum()
{
$user = User::factory()->create();
$token = $user->createToken('test-token')->plainTextToken;
$token = config('services.bifrost.api_key');

$response = $this->withHeaders([
'Authorization' => 'Bearer '.$token,
Expand All @@ -74,8 +72,7 @@ public function test_validates_subscription_enum()

public function test_creates_new_user_when_email_not_exists()
{
$user = User::factory()->create();
$token = $user->createToken('test-token')->plainTextToken;
$token = config('services.bifrost.api_key');

$this->withHeaders([
'Authorization' => 'Bearer '.$token,
Expand All @@ -101,8 +98,7 @@ public function test_finds_existing_user_when_email_exists()
'name' => 'Original Name',
]);

$user = User::factory()->create();
$token = $user->createToken('test-token')->plainTextToken;
$token = config('services.bifrost.api_key');

$this->withHeaders([
'Authorization' => 'Bearer '.$token,
Expand All @@ -121,8 +117,7 @@ public function test_finds_existing_user_when_email_exists()

public function test_creates_license_with_bifrost_source()
{
$user = User::factory()->create();
$token = $user->createToken('test-token')->plainTextToken;
$token = config('services.bifrost.api_key');

$response = $this->withHeaders([
'Authorization' => 'Bearer '.$token,
Expand Down Expand Up @@ -164,8 +159,7 @@ public function test_creates_license_for_existing_user()
'name' => 'Existing User',
]);

$authUser = User::factory()->create();
$token = $authUser->createToken('test-token')->plainTextToken;
$token = config('services.bifrost.api_key');

$response = $this->withHeaders([
'Authorization' => 'Bearer '.$token,
Expand Down