The external application's option to "add an HTTP user and an HTTP password" refers to HTTP Basic Authentication, not OAuth. This is a simple authentication scheme built into the HTTP protocol, where the client (the external app) sends a username and password with each request.
To protect your Laravel webhook route using HTTP Basic Auth, you can use Laravel's built-in middleware.
Step 1: Add the middleware to your route
Suppose your webhook route looks like this:
Route::post('/webhook-handler', [WebhookController::class, 'handle']);
You can protect it with HTTP Basic Auth like so:
Route::post('/webhook-handler', [WebhookController::class, 'handle'])
->middleware('auth.basic');
Step 2: Configure the credentials
Laravel's auth.basic middleware uses your users table by default. But for webhooks, you might want to use a dedicated username and password, not tied to your users table.
You can create a custom middleware for this. Here’s how:
Create a middleware:
php artisan make:middleware WebhookAuth
Edit app/Http/Middleware/WebhookAuth.php:
namespace App\Http\Middleware;
use Closure;
class WebhookAuth
{
public function handle($request, Closure $next)
{
$USERNAME = env('WEBHOOK_USER', 'your_username');
$PASSWORD = env('WEBHOOK_PASSWORD', 'your_password');
if (
$request->getUser() !== $USERNAME ||
$request->getPassword() !== $PASSWORD
) {
return response('Unauthorized', 401, ['WWW-Authenticate' => 'Basic']);
}
return $next($request);
}
}
Register the middleware in app/Http/Kernel.php:
protected $routeMiddleware = [
// ...
'webhook.auth' => \App\Http\Middleware\WebhookAuth::class,
];
Protect your route:
Route::post('/webhook-handler', [WebhookController::class, 'handle'])
->middleware('webhook.auth');
Step 3: Set the credentials in your .env file:
WEBHOOK_USER=your_username
WEBHOOK_PASSWORD=your_password
Summary:
- This is HTTP Basic Auth, not OAuth.
- Use Laravel's built-in
auth.basicor create a custom middleware for static credentials. - Set the same username and password in both your Laravel app and the external application.
Let me know if you need help with any step!