intervention/httpauth
Intervention Httpauth is a lightweight Laravel/PHP package for adding HTTP authentication to your app. Protect routes with Basic or Digest auth, integrate easily with middleware, and configure credentials and realms for quick, standards-based access control.
Installation
composer require intervention/httpauth
Add to composer.json if not auto-loaded:
"autoload": {
"psr-4": {
"Intervention\\HttpAuth\\": "vendor/intervention/httpauth/src/"
}
}
Run composer dump-autoload.
First Use Case: Basic Auth Middleware
Create a middleware (app/Http/Middleware/AuthBasic.php):
use Intervention\HttpAuth\BasicAuth;
use Closure;
class AuthBasic extends Middleware
{
public function handle($request, Closure $next)
{
$auth = new BasicAuth();
$auth->setRealm('Admin Area');
if (!$auth->check($request->user(), $request->password())) {
return response('Unauthorized', 401, [
'WWW-Authenticate' => $auth->getHeader()
]);
}
return $next($request);
}
}
Register in app/Http/Kernel.php:
protected $routeMiddleware = [
'auth.basic' => \App\Http\Middleware\AuthBasic::class,
];
Apply to routes:
Route::get('/admin', function () {
return 'Admin Dashboard';
})->middleware('auth.basic');
First Use Case: Digest Auth
Replace BasicAuth with DigestAuth in the middleware and ensure the client sends credentials in the Authorization header.
$auth->setRealm('User: ' . $user->name);
Route::group(['middleware' => ['auth.basic', 'auth:sanctum']], function () {
// Requires both Basic Auth and Sanctum token
});
$auth = new BasicAuth();
if ($auth->check($request->header('PHP_AUTH_USER'), $request->header('PHP_AUTH_PW'))) {
// Proceed
}
return response()->json(['data'], 200, [
'WWW-Authenticate' => $auth->getHeader()
]);
$response = $this->withHeaders([
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'pass'
])->get('/admin');
401 responses with auth headers:
$response->assertStatus(401)
->assertHeader('WWW-Authenticate', 'Basic realm="Admin Area"');
Intervention\HttpAuth\Auth for custom logic:
class ApiKeyAuth extends Auth
{
public function check($key) {
return Hash::check($key, config('app.api_key'));
}
}
Route::middleware(['throttle:60,1', 'auth.basic'])->group(function () {
// Rate-limited auth-protected routes
});
Header Case Sensitivity:
$request->header() is case-insensitive, but PHP_AUTH_USER/PHP_AUTH_PW are case-sensitive in the Authorization header.$request->header('authorization') and parse manually if needed:
$authHeader = $request->header('authorization');
if (preg_match('/Basic (.*)/', $authHeader, $matches)) {
list($user, $pass) = base64_decode($matches[1]);
}
Digest Auth Complexity:
MD5 is widely supported but weak).DigestAuth::setNonce() with a timestamp:
$auth->setNonce(time() . '-' . Str::random(10));
Middleware Caching:
Route::middleware('auth.basic')->group(function () {
Route::get('/admin/{any}', function () { /* ... */ })->where('any', '.*');
});
Password Hashing:
Hash::check():
if (Hash::check($request->password, $user->password)) {
// Valid
}
\Log::debug('Auth Headers', [
'PHP_AUTH_USER' => $request->header('PHP_AUTH_USER'),
'Authorization' => $request->header('authorization'),
]);
curl:
curl -u user:pass http://localhost/admin
For Digest Auth:
curl --digest -u user:pass http://localhost/admin
Auth::check() to add logic (e.g., IP whitelisting):
public function check($user, $pass) {
return parent::check($user, $pass) && $this->isIpAllowed($request->ip());
}
if ($auth->check($user, $pass)) {
event(new Authenticated($user));
} else {
event(new AuthenticationFailed($user));
}
Route::middleware(['auth.basic', 'auth:sanctum'])->get('/secure');
session() helper:
$auth->setNonce(session()->get('digest_nonce', Str::random(32)));
How can I help you explore Laravel packages today?