Repo rebuild
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\InternalApi\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Modules\InternalApi\app\Http\Middleware\VerifyInternalApiSignature;
|
||||
|
||||
class InternalApiServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap module services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
Route::middleware('api')->prefix('api/internal')->group(__DIR__ . '/../routes/api.php');
|
||||
app('router')->aliasMiddleware('internal.signed', VerifyInternalApiSignature::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\InternalApi\app\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Modules\Company\Models\Company;
|
||||
|
||||
class CompanySummaryController extends Controller
|
||||
{
|
||||
/**
|
||||
* Return tenant summary for authenticated internal callers.
|
||||
*/
|
||||
public function __invoke()
|
||||
{
|
||||
return response()->json([
|
||||
'companies' => Company::withCount(['users', 'warehouses', 'licenseKeys'])->orderBy('name')->get(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\InternalApi\app\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class VerifyInternalApiSignature
|
||||
{
|
||||
/**
|
||||
* Verify the HMAC signature used between separated WMS applications.
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$secret = (string) config('platform.internal_api_key');
|
||||
$timestamp = (string) $request->header('X-WMS-Timestamp');
|
||||
$signature = (string) $request->header('X-WMS-Signature');
|
||||
|
||||
abort_if($secret === '' || $timestamp === '' || $signature === '', 401);
|
||||
abort_if(strtotime($timestamp) === false, 401, 'Invalid internal API timestamp.');
|
||||
abort_if(abs(now()->timestamp - strtotime($timestamp)) > 300, 401, 'Expired internal API signature.');
|
||||
|
||||
$body = $request->getContent();
|
||||
$payloads = array_unique([
|
||||
$timestamp . '.' . $body,
|
||||
$timestamp . '.',
|
||||
$timestamp . '.[]',
|
||||
]);
|
||||
|
||||
foreach ($payloads as $payload) {
|
||||
if (hash_equals(hash_hmac('sha256', $payload, $secret), $signature)) {
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
abort(401, 'Invalid internal API signature.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"name":"InternalApi","alias":"internal-api","description":"Authenticated internal API between separated WMS applications.","keywords":["api","hmac","integration"],"priority":2,"providers":["Modules\\InternalApi\\Providers\\InternalApiServiceProvider"],"files":[]}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Modules\InternalApi\app\Http\Controllers\CompanySummaryController;
|
||||
|
||||
Route::middleware('internal.signed')->group(function (): void {
|
||||
Route::get('/companies', CompanySummaryController::class)->name('internal.companies.index');
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\InternalApi\tests\Feature;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InternalApiSignatureTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_internal_api_rejects_missing_signature(): void
|
||||
{
|
||||
$this->getJson('/api/internal/companies')->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function test_internal_api_accepts_valid_signature(): void
|
||||
{
|
||||
config(['platform.internal_api_key' => 'testing-secret']);
|
||||
$timestamp = now()->toIso8601String();
|
||||
$body = '';
|
||||
$signature = hash_hmac('sha256', $timestamp . '.' . $body, 'testing-secret');
|
||||
|
||||
$this->withHeaders([
|
||||
'X-WMS-Timestamp' => $timestamp,
|
||||
'X-WMS-Signature' => $signature,
|
||||
])->getJson('/api/internal/companies')->assertOk();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user