Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 53eda06

Browse files
committed
add Basic Auth without API Gateway support
1 parent 8c313fa commit 53eda06

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

app/Http/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Kernel extends HttpKernel
5454
protected $routeMiddleware = [
5555
'auth' => \App\Http\Middleware\Authenticate::class,
5656
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
57+
'basic' => \App\Http\Middleware\BasicAuthMiddleware::class,
5758
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
5859
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
5960
'can' => \Illuminate\Auth\Middleware\Authorize::class,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
7+
class BasicAuthMiddleware
8+
{
9+
/**
10+
* Handle an incoming request.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
14+
* @return mixed
15+
*/
16+
public function handle($request, Closure $next, ...$args)
17+
{
18+
$correct_user = $args[0];
19+
$correct_password = $args[1];
20+
switch (true) {
21+
case !isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']):
22+
case $_SERVER['PHP_AUTH_USER'] !== $correct_user:
23+
case $_SERVER['PHP_AUTH_PW'] !== $correct_password:
24+
header('WWW-Authenticate: Basic realm="Access denied"');
25+
header('Content-Type: text/plain; charset=utf-8');
26+
die('Not authorized');
27+
}
28+
return $next($request);
29+
}
30+
}

routes/web.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@
1616
Route::get('/', function () {
1717
return view('welcome');
1818
});
19+
20+
Route::group(['middleware' => ['basic:hoge,fuga']], function () {
21+
Route::get('/basic', function () {
22+
return "Authorized";
23+
});
24+
});

0 commit comments

Comments
 (0)