Skip to content

Commit 05d5b42

Browse files
committed
Merge branch 'release/2.1.0'
2 parents 96594af + 587fba2 commit 05d5b42

19 files changed

+240
-159
lines changed

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# Exclude files from exporting
3+
#
4+
5+
/.gitattributes export-ignore
6+
/.github export-ignore
7+
/.gitignore export-ignore

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [2.1.0] - 2022-02-15
8+
9+
- Maintenance update:
10+
- Added `.gitattributes` files
11+
- Added composer/installers:^2.0 constraint to composer.json
12+
- Code (readability) improvements
13+
- Moved `Vdlp\BasicAuthentication\ServiceProvider` to `Vdlp\BasicAuthentication\ServiceProviders\BasicAuthenticationServiceProvider`
14+
- Moved boot logic from `Plugin` to its own middleware class `Vdlp\BasicAuthentication\Http\Middleware\BasicAuthenticationMiddleware`
15+
- Moved logic from AuthorizationHelper to `Vdlp\BasicAuthentication\Http\Middleware\BasicAuthenticationMiddleware`
16+
17+
## [2.0.0] - 2021-07-13
18+
19+
- Add support for PHP 7.4 and higher
20+
21+
## [1.2.0] - 2021-06-09
22+
23+
- Add console command for adding credentials
24+
25+
## [1.1.0] - 2021-05-28
26+
27+
- Add notification to settings view when basic authentication is disabled

Plugin.php

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
namespace Vdlp\BasicAuthentication;
66

77
use Backend\Helpers\Backend as BackendHelper;
8-
use Illuminate\Database\Eloquent\ModelNotFoundException;
9-
use Illuminate\Http\Request;
10-
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
8+
use October\Rain\Foundation\Application;
9+
use October\Rain\Foundation\Http\Kernel;
1110
use System\Classes\PluginBase;
12-
use Vdlp\BasicAuthentication\Classes\AuthorizationHelper;
1311
use Vdlp\BasicAuthentication\Console\CreateCredentialsCommand;
14-
use Vdlp\BasicAuthentication\Models\Credential;
12+
use Vdlp\BasicAuthentication\Http\Middleware\BasicAuthenticationMiddleware;
13+
use Vdlp\BasicAuthentication\ServiceProviders\BasicAuthenticationServiceProvider;
1514

1615
final class Plugin extends PluginBase
1716
{
@@ -27,71 +26,28 @@ public function pluginDetails(): array
2726

2827
public function register(): void
2928
{
30-
$this->app->register(ServiceProvider::class);
29+
$this->app->register(BasicAuthenticationServiceProvider::class);
3130

3231
$this->registerConsoleCommand(CreateCredentialsCommand::class, CreateCredentialsCommand::class);
3332
}
3433

35-
/**
36-
* {@inheritdoc}
37-
*
38-
* @throws SuspiciousOperationException
39-
*/
4034
public function boot(): void
4135
{
42-
if (
43-
!config('basicauthentication.enabled')
44-
|| app()->runningInConsole()
45-
|| app()->runningUnitTests()
46-
|| app()->runningInBackend()
47-
) {
48-
return;
49-
}
50-
51-
/** @var AuthorizationHelper $authorizationHelper */
52-
$authorizationHelper = resolve(AuthorizationHelper::class);
53-
54-
/** @var Request $request */
55-
$request = resolve(Request::class);
56-
57-
if ($authorizationHelper->isIpAddressWhitelisted((string) $request->ip())) {
58-
return;
59-
}
60-
61-
try {
62-
/** @var Credential $credential */
63-
$credential = Credential::query()
64-
->where('hostname', '=', $request->getHost())
65-
->where('is_enabled', '=', true)
66-
->firstOrFail();
67-
} catch (ModelNotFoundException $e) {
68-
return;
69-
}
70-
71-
if ($authorizationHelper->isUrlExcluded($request->getUri())) {
72-
return;
73-
}
74-
75-
$sessionKey = str_slug(str_replace('.', '_', $credential->getAttribute('hostname')) . '_basic_authentication');
76-
77-
if (session()->has($sessionKey)) {
78-
return;
79-
}
36+
/** @var Application $application */
37+
$application = $this->app;
8038

8139
if (
82-
$request->getUser() === $credential->getAttribute('username')
83-
&& $request->getPassword() === $credential->getAttribute('password')
40+
(bool) config('basicauthentication.enabled', false) === false
41+
|| $application->runningInConsole()
42+
|| $application->runningUnitTests()
43+
|| $application->runningInBackend()
8444
) {
85-
session()->put($sessionKey, $request->getUser());
86-
8745
return;
8846
}
8947

90-
header('WWW-Authenticate: Basic realm="' . $credential->getAttribute('realm') . '"');
91-
header('HTTP/1.0 401 Unauthorized');
92-
93-
echo (string) trans('vdlp.basicauthentication::lang.output.unauthorized');
94-
exit(0);
48+
/** @var Kernel $kernel */
49+
$kernel = $application['Illuminate\Contracts\Http\Kernel'];
50+
$kernel->prependMiddleware(BasicAuthenticationMiddleware::class);
9551
}
9652

9753
public function registerPermissions(): array

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ composer require vdlp/oc-basicauthentication-plugin
1717
To configure this plugin execute the following command:
1818

1919
```
20-
php artisan vendor:publish --provider="Vdlp\BasicAuthentication\ServiceProvider" --tag="config"
20+
php artisan vendor:publish --provider="Vdlp\BasicAuthentication\ServiceProviders\BasicAuthenticationServiceProvider" --tag="config"
2121
```
2222

2323
This will create a `config/basicauthentication.php` file in your app where you can modify the configuration if you don't want to use `.env` variables.

ServiceProvider.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

classes/AuthorizationHelper.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "vdlp/oc-basicauthentication-plugin",
33
"description": "Protect your website with Basic Authentication.",
4-
"type": "october-plugin",
54
"license": "GPL-2.0",
5+
"type": "october-plugin",
66
"authors": [
77
{
88
"name": "Van der Let & Partners",
@@ -14,6 +14,12 @@
1414
},
1515
"require": {
1616
"php": "^7.4 || ^8.0",
17-
"composer/installers": "^1.0"
17+
"composer/installers": "^1.0 || ^2.0"
18+
},
19+
"archive": {
20+
"exclude": [
21+
".gitignore",
22+
".github"
23+
]
1824
}
1925
}

config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| BASIC_AUTHENTICATION_ENABLED to your .env file.
1414
|
1515
*/
16+
1617
'enabled' => (bool) env('BASIC_AUTHENTICATION_ENABLED', false),
1718

1819
/*
@@ -23,6 +24,7 @@
2324
| Provide a comma separated list of IP addresses to whitelist.
2425
|
2526
*/
27+
2628
'whitelisted_ips' => env('BASIC_AUTHENTICATION_WHITELISTED_IPS', ''),
2729

2830
];

console/CreateCredentialsCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function handle(): void
3434
]);
3535

3636
$this->info('Basic Authentication credentials have been added to the database.');
37-
} catch (Throwable $e) {
38-
$this->error('Could not create Basic Authentication credentials: ' . $e->getMessage());
37+
} catch (Throwable $throwable) {
38+
$this->error('Could not create Basic Authentication credentials: ' . $throwable->getMessage());
3939
}
4040
}
4141
}

controllers/Credentials.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use Backend\Behaviors\FormController;
88
use Backend\Behaviors\ListController;
9-
use Backend\Classes\NavigationManager;
109
use Backend\Classes\Controller;
10+
use Backend\Classes\NavigationManager;
1111
use Illuminate\Contracts\Config\Repository;
1212
use System\Classes\SettingsManager;
1313

@@ -34,6 +34,6 @@ public function __construct(Repository $config)
3434
NavigationManager::instance()->setContext('October.System', 'system', 'settings');
3535
SettingsManager::setContext('Vdlp.BasicAuthentication', 'credentials');
3636

37-
$this->enabled = $config->get('basicauthentication.enabled');
37+
$this->enabled = (bool) $config->get('basicauthentication.enabled', false);
3838
}
3939
}

controllers/ExcludedUrls.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use Backend\Behaviors\FormController;
88
use Backend\Behaviors\ListController;
9-
use Backend\Classes\NavigationManager;
109
use Backend\Classes\Controller;
10+
use Backend\Classes\NavigationManager;
1111
use Illuminate\Contracts\Config\Repository;
1212
use System\Classes\SettingsManager;
1313

@@ -34,6 +34,6 @@ public function __construct(Repository $config)
3434
NavigationManager::instance()->setContext('October.System', 'system', 'settings');
3535
SettingsManager::setContext('Vdlp.BasicAuthentication', 'excludedurls');
3636

37-
$this->enabled = $config->get('basicauthentication.enabled');
37+
$this->enabled = (bool) $config->get('basicauthentication.enabled', false);
3838
}
3939
}

0 commit comments

Comments
 (0)