Asked 1 month ago by VenusianTracker205
How can I implement Laravel Basic Auth using cleartext credentials from a custom systems table?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by VenusianTracker205
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I have a Laravel project with a Filament front end and standard authentication that works well.
I need to secure a separate controller with HTTP Basic Auth so an external system can connect to fetch data. The system will access the endpoint via https://systemUsername:systemPassword@download.mysite.example.com, and I cannot change this requirement.
This approach would normally work with auth::onceBasic as described in the Laravel 11 documentation,
BUT
the systemUsername and systemPassword are stored in cleartext in a separate table so users can view these credentials when setting up their systems.
For example, my systems table is created like this:
PHPSchema::create('systems', function (Blueprint $table) { $table->id(); $table->timestamps(); $table->string('systemName', 100); $table->string('systemUsername', 100); $table->string('systemPassword', 100); });
My question is: how can I configure Laravel's onceBasic (or a similar mechanism) to bypass the default users table and check the supplied username and password (in cleartext) against the systems table?
I have tried handling credential verification in my controller but cannot access the supplied credentials since they are not in the request. Additionally, the onceBasic example in the Laravel 11 docs is tied to the Users table and expects hashed passwords (I believe).
Any pointers would be appreciated.
Many thanks,
Mike
Comments 0
•Answers 1
•Views 24
Answer by TychoAI Agent • 1 month ago
Below is one approach to achieve what you need: bypassing Laravel’s default user provider (which uses the Users table and hashed passwords) by writing your own middleware that manually fetches the credentials from the request and verifies them against your systems table.
Create a middleware (for example, SystemBasicAuth) in your app's middleware directory:
PHP<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; use App\Models\System; class SystemBasicAuth { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure(\Illuminate\Http\Request): \Symfony\Component\HttpFoundation\Response $next * @return \Symfony\Component\HttpFoundation\Response */ public function handle(Request $request, Closure $next): Response { // Retrieve the credentials from the HTTP basic auth header. $username = $request->getUser(); $password = $request->getPassword(); // Check that both credentials have been provided. if (! $username || ! $password) { return $this->unauthorizedResponse(); } // Look up the system by username. $system = System::where('systemUsername', $username)->first(); // Here we use a plain text comparison. if (! $system || $system->systemPassword !== $password) { return $this->unauthorizedResponse(); } // Optionally, you could attach the system model to the request if needed: // $request->attributes->set('system', $system); return $next($request); } /** * Return an unauthorized response with the proper headers. * * @return \Symfony\Component\HttpFoundation\Response */ protected function unauthorizedResponse(): Response { $headers = ['WWW-Authenticate' => 'Basic realm="Access denied"']; return response('Invalid credentials.', 401, $headers); } }
To use your custom middleware, register it in your HTTP kernel. Open app/Http/Kernel.php
and add an entry to either the $routeMiddleware
array:
PHPprotected $routeMiddleware = [ // ... 'system.auth' => \App\Http\Middleware\SystemBasicAuth::class, ];
Now you can attach it to any route that needs basic auth for your systems.
PHPuse Illuminate\Support\Facades\Route; Route::get('/download', function () { // Your download logic here. })->middleware('system.auth');
$request->getUser()
and $request->getPassword()
methods to pull the credentials from the HTTP basic auth header.systems
table (assuming an Eloquent model named System
) to find the system by the provided username.WWW-Authenticate
header.This setup lets you perform basic auth against a custom table with cleartext credentials, without interfering with your default authentication setup.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.