Skip to content

Commit

Permalink
Basic Auth For Production rap2hpoutre#294
Browse files Browse the repository at this point in the history
  • Loading branch information
ashokdevatwal committed Jan 3, 2024
1 parent d09e6b5 commit e911ac4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Illuminate\Support\ServiceProvider;

use Illuminate\Routing\Router;

use Rap2hpoutre\LaravelLogViewer\LogViewerBasicAuthMiddleware;

class LaravelLogViewerServiceProvider extends ServiceProvider
{
/**
Expand All @@ -29,6 +33,10 @@ public function boot()
__DIR__.'/../../config/logviewer.php' => $this->config_path('logviewer.php'),
]);
}

$router = $this->app->make(Router::class);
$router->aliasMiddleware('auth.logviewer', LogViewerBasicAuthMiddleware::class);

}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/config/logviewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@
'max_file_size' => 52428800, // size in Byte
'pattern' => env('LOGVIEWER_PATTERN', '*.log'),
'storage_path' => env('LOGVIEWER_STORAGE_PATH', storage_path('logs')),

/*
|--------------------------------------------------------------------------
| Basic Auth
|--------------------------------------------------------------------------
|
*/

'username' => env('LOGVIEWER_USERNAME', 'username'),
'password' => env('LOGVIEWER_PASSWORD', 'password'),
];
32 changes: 32 additions & 0 deletions src/middleware/LogViewerBasicAuthMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rap2hpoutre\LaravelLogViewer;

use Closure;

use Config;

class LogViewerBasicAuthMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$username = Config::get('logviewer.username');
$password = Config::get('logviewer.password');

$givenUsername = $request->getUser();
$givenPassword = $request->getPassword();

if ($givenUsername !== $username || $givenPassword !== $password) {
return response('Unauthorized.', 401, ['WWW-Authenticate' => 'Basic']);
}

return $next($request);
}
}

0 comments on commit e911ac4

Please sign in to comment.