Skip to content

Commit 73b11b7

Browse files
author
HungNA - Technical Manager
committed
Refactor BasicAuth Lib
1 parent 04e8818 commit 73b11b7

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
return [
4-
'enabled' => env('ENABLED_BASIC_AUTH', true), // Default is true
4+
'enabled' => env('ENABLED_BASIC_AUTH', false), // Default is true
55
'in_production' => env('ENABLED_BASIC_AUTH_IN_PRODUCTION', false), // Default is false
66
'username' => env('BASIC_AUTH_USERNAME', 'hungna'),
77
'password' => env('BASIC_AUTH_PASSWORD', 'HungNA@Password@2024'),

src/LaravelBasicAuthServiceProvider.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ class LaravelBasicAuthServiceProvider extends ServiceProvider
1313
*/
1414
public function boot()
1515
{
16-
$this->publishes([
17-
__DIR__ . '/../config/config.php' => config_path('laravel-basic-auth.php'),
18-
]);
19-
2016
$this->mergeConfigFrom(
21-
__DIR__ . '/../config/config.php', 'laravel-basic-auth'
17+
__DIR__ . '/../config/config.php',
18+
'laravel-basic-auth'
2219
);
2320
}
2421

src/Middleware/BasicAuth.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
class BasicAuth
1212
{
13-
protected array $defaultWhitelist = [
13+
protected $forceEnabled = false;
14+
protected $defaultWhitelist = [
1415
'127.0.0.1',
1516
];
16-
protected array $defaultEnvUseBasicAuth = [
17+
protected $defaultEnvUseBasicAuth = [
1718
'beta',
1819
'staging',
1920
'test',
@@ -28,7 +29,7 @@ class BasicAuth
2829
*/
2930
protected function promptForBasicAuthCredentials(Request $request)
3031
{
31-
Log::info('BasicAuthCredentials::Failed', [
32+
Log::info('Laravel::BasicAuthCredentials::Failed', [
3233
'request_ip' => $request->ip() ?? null,
3334
'request_method' => $request->method() ?? '',
3435
'request_full_url' => $request->fullUrl() ?? '',
@@ -54,7 +55,9 @@ public function handle(Request $request, Closure $next)
5455
$envUseBasicAuth = $this->defaultEnvUseBasicAuth;
5556
$appENV = config('app.env');
5657
$isEnabled = config('laravel-basic-auth.enabled');
57-
58+
if ($this->forceEnabled === true) {
59+
$isEnabled = $this->forceEnabled;
60+
}
5861
// Enable basic authentication for Production environments
5962
$isEnabledInProduction = config('laravel-basic-auth.in_production');
6063
if ($isEnabled === true && $isEnabledInProduction === true) {

0 commit comments

Comments
 (0)