Skip to content

Commit

Permalink
add basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
eucyt committed Dec 8, 2023
1 parent 857efae commit bb02795
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions laravel/.env.local
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

BASIC_AUTH_USERNAME=admin
BASIC_AUTH_PASSWORD=password
1 change: 1 addition & 0 deletions laravel/app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Kernel extends HttpKernel
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\BasicAuthMiddleware::class,
];

/**
Expand Down
33 changes: 33 additions & 0 deletions laravel/app/Http/Middleware/BasicAuthMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class BasicAuthMiddleware
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return Response|RedirectResponse
*/
public function handle(Request $request, Closure $next): Response|RedirectResponse
{
$username = $request->getUser();
$password = $request->getPassword();

if ($username == config('auth.basic.username') && $password == config('auth.basic.password')) {
return $next($request);
}

abort(401, "Enter username and password.", [
header('WWW-Authenticate: Basic realm="Sample Private Page"'),
header('Content-Type: text/plain; charset=utf-8')
]);
}
}
8 changes: 8 additions & 0 deletions laravel/config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,12 @@

'password_timeout' => 10800,


/*
* basic auth
*/
'basic' => [
'username' => env('BASIC_AUTH_USERNAME'),
'password' => env('BASIC_AUTH_PASSWORD'),
],
];

0 comments on commit bb02795

Please sign in to comment.