Skip to content

Commit 96594af

Browse files
committed
Merge branch 'release/2.0.0'
2 parents f08120d + d7fca3a commit 96594af

File tree

17 files changed

+230
-245
lines changed

17 files changed

+230
-245
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: true
1616
matrix:
17-
php: [ 8.0, 7.4, 7.3, 7.2, 7.1 ]
17+
php: [ 8.0, 7.4 ]
1818
stability: [ prefer-lowest, prefer-stable ]
1919

2020
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

Plugin.php

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,16 @@
55
namespace Vdlp\BasicAuthentication;
66

77
use Backend\Helpers\Backend as BackendHelper;
8-
use Illuminate\Contracts\Config\Repository;
9-
use Illuminate\Contracts\Session\Session;
108
use Illuminate\Database\Eloquent\ModelNotFoundException;
119
use Illuminate\Http\Request;
12-
use October\Rain\Translation\Translator;
1310
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
1411
use System\Classes\PluginBase;
15-
use Vdlp\BasicAuthentication\Classes\Helper\AuthorizationHelper;
12+
use Vdlp\BasicAuthentication\Classes\AuthorizationHelper;
1613
use Vdlp\BasicAuthentication\Console\CreateCredentialsCommand;
1714
use Vdlp\BasicAuthentication\Models\Credential;
18-
use Vdlp\BasicAuthentication\ServiceProviders\BasicAuthenticationServiceProvider;
19-
20-
/**
21-
* Class Plugin
22-
*
23-
* @package Vdlp\BasicAuthentication
24-
*/
25-
class Plugin extends PluginBase
15+
16+
final class Plugin extends PluginBase
2617
{
27-
/**
28-
* {@inheritdoc}
29-
*/
3018
public function pluginDetails(): array
3119
{
3220
return [
@@ -37,45 +25,36 @@ public function pluginDetails(): array
3725
];
3826
}
3927

40-
/**
41-
* {@inheritdoc}
42-
*/
4328
public function register(): void
4429
{
45-
$this->app->register(BasicAuthenticationServiceProvider::class);
30+
$this->app->register(ServiceProvider::class);
4631

4732
$this->registerConsoleCommand(CreateCredentialsCommand::class, CreateCredentialsCommand::class);
4833
}
4934

5035
/**
5136
* {@inheritdoc}
37+
*
5238
* @throws SuspiciousOperationException
5339
*/
54-
public function boot()
40+
public function boot(): void
5541
{
56-
/** @var Repository $config */
57-
$config = resolve(Repository::class);
58-
59-
if (!$config->get('basicauthentication.enabled')
42+
if (
43+
!config('basicauthentication.enabled')
6044
|| app()->runningInConsole()
6145
|| app()->runningUnitTests()
6246
|| app()->runningInBackend()
6347
) {
6448
return;
6549
}
6650

51+
/** @var AuthorizationHelper $authorizationHelper */
52+
$authorizationHelper = resolve(AuthorizationHelper::class);
53+
6754
/** @var Request $request */
6855
$request = resolve(Request::class);
6956

70-
/** @var Session $session */
71-
$session = resolve(Session::class);
72-
73-
/** @var Translator $translator */
74-
$translator = resolve('translator');
75-
76-
/** @var AuthorizationHelper $authorizationHelper */
77-
$authorizationHelper = resolve(AuthorizationHelper::class);
78-
if ($authorizationHelper->isIpAddressWhitelisted($request->ip())) {
57+
if ($authorizationHelper->isIpAddressWhitelisted((string) $request->ip())) {
7958
return;
8059
}
8160

@@ -94,27 +73,27 @@ public function boot()
9473
}
9574

9675
$sessionKey = str_slug(str_replace('.', '_', $credential->getAttribute('hostname')) . '_basic_authentication');
97-
if ($session->has($sessionKey)) {
76+
77+
if (session()->has($sessionKey)) {
9878
return;
9979
}
10080

101-
if ($request->getUser() === $credential->getAttribute('username')
81+
if (
82+
$request->getUser() === $credential->getAttribute('username')
10283
&& $request->getPassword() === $credential->getAttribute('password')
10384
) {
104-
$session->put($sessionKey, $request->getUser());
85+
session()->put($sessionKey, $request->getUser());
86+
10587
return;
10688
}
10789

10890
header('WWW-Authenticate: Basic realm="' . $credential->getAttribute('realm') . '"');
10991
header('HTTP/1.0 401 Unauthorized');
11092

111-
echo $translator->get('vdlp.basicauthentication::lang.output.unauthorized');
93+
echo (string) trans('vdlp.basicauthentication::lang.output.unauthorized');
11294
exit(0);
11395
}
11496

115-
/**
116-
* {@inheritdoc}
117-
*/
11897
public function registerPermissions(): array
11998
{
12099
return [
@@ -125,9 +104,6 @@ public function registerPermissions(): array
125104
];
126105
}
127106

128-
/**
129-
* {@inheritdoc}
130-
*/
131107
public function registerSettings(): array
132108
{
133109
/** @var BackendHelper $backendHelper */

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,39 @@ Allows users to manage Basic Authentication credentials for multiple hostnames a
44

55
## Requirements
66

7-
* PHP 7.1 or higher
7+
* PHP 7.4 or higher
88

99
## Installation
1010

1111
```
1212
composer require vdlp/oc-basicauthentication-plugin
1313
```
1414

15-
Or:
16-
17-
```
18-
php artisan plugin:install Vdlp.BasicAuthentication
19-
```
20-
2115
## Configuration
2216

2317
To configure this plugin execute the following command:
2418

2519
```
26-
php artisan vendor:publish --provider="Vdlp\BasicAuthentication\ServiceProviders\BasicAuthenticationServiceProvider" --tag="config"
20+
php artisan vendor:publish --provider="Vdlp\BasicAuthentication\ServiceProvider" --tag="config"
2721
```
2822

29-
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.
23+
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.
3024

3125
## Enable / disable plugin
3226

33-
By default basic authentication is disabled.
27+
> By default basic authentication is disabled.
3428
3529
To enable basic authentication, you have to set the env variable to `BASIC_AUTHENTICATION_ENABLED` to `true` in your `.env` file or edit the published config file.
3630

31+
## A Note On FastCGI
32+
33+
If you are using PHP FastCGI, HTTP Basic authentication may not work correctly out of the box. The following lines should be added to your `.htaccess` file:
34+
35+
```
36+
RewriteCond %{HTTP:Authorization} ^(.+)$
37+
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
38+
```
39+
3740
## Questions? Need help?
3841

39-
If you have any question about how to use this plugin, please don't hesitate to contact us at octobercms@vdlp.nl. We're happy to help you. You can also visit the support forum and drop your questions/issues there.
42+
If you have any question about how to use this plugin, please don't hesitate to contact us at octobercms@vdlp.nl. We're happy to help you.

ServiceProvider.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vdlp\BasicAuthentication;
6+
7+
use October\Rain\Support\ServiceProvider as ServiceProviderBase;
8+
9+
final class ServiceProvider extends ServiceProviderBase
10+
{
11+
public function boot(): void
12+
{
13+
$this->publishes([
14+
__DIR__ . '/config.php' => config_path('basicauthentication.php'),
15+
], 'config');
16+
17+
$this->mergeConfigFrom(__DIR__ . '/config.php', 'basicauthentication');
18+
}
19+
}

classes/AuthorizationHelper.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vdlp\BasicAuthentication\Classes;
6+
7+
use Illuminate\Http\Request;
8+
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
9+
use Vdlp\BasicAuthentication\Models\ExcludedUrl;
10+
11+
final class AuthorizationHelper
12+
{
13+
private Request $request;
14+
15+
public function __construct(Request $request)
16+
{
17+
$this->request = $request;
18+
}
19+
20+
/**
21+
* @throws SuspiciousOperationException
22+
*/
23+
public function isUrlExcluded(string $currentUrl): bool
24+
{
25+
/** @var array|mixed $parsedCurrentUrl */
26+
$parsedCurrentUrl = parse_url($currentUrl);
27+
28+
if (!is_array($parsedCurrentUrl)) {
29+
return false;
30+
}
31+
32+
/** @var ExcludedUrl[] $excludedUrls */
33+
$excludedUrls = ExcludedUrl::all();
34+
35+
foreach ($excludedUrls as $excludedUrl) {
36+
/** @var array|mixed $parsedExcludedUrl */
37+
$parsedExcludedUrl = parse_url($excludedUrl->getAttribute('url'));
38+
39+
if (!is_array($parsedExcludedUrl)) {
40+
continue;
41+
}
42+
43+
$host = $parsedCurrentUrl['host'] ?? '';
44+
45+
if (
46+
array_key_exists('host', $parsedExcludedUrl)
47+
&& $host !== $this->request->getHost()
48+
) {
49+
continue;
50+
}
51+
52+
if (
53+
array_key_exists('path', $parsedExcludedUrl)
54+
&& array_key_exists('path', $parsedCurrentUrl)
55+
&& $parsedExcludedUrl['path'] === $parsedCurrentUrl['path']
56+
) {
57+
return true;
58+
}
59+
}
60+
61+
return false;
62+
}
63+
64+
public function isIpAddressWhitelisted(string $ipAddress): bool
65+
{
66+
return in_array($ipAddress, explode(',', config('basicauthentication.whitelisted_ips')), true);
67+
}
68+
}

classes/helper/AuthorizationHelper.php

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

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vdlp/oc-basicauthentication-plugin",
3-
"description": "Allows you to manage Basic Authentication credentials in October CMS powered websites.",
3+
"description": "Protect your website with Basic Authentication.",
44
"type": "october-plugin",
55
"license": "GPL-2.0",
66
"authors": [
@@ -13,7 +13,7 @@
1313
"email": "octobercms@vdlp.nl"
1414
},
1515
"require": {
16-
"php": "^7.1||^8.0",
16+
"php": "^7.4 || ^8.0",
1717
"composer/installers": "^1.0"
1818
}
1919
}

config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33
declare(strict_types=1);
44

55
return [
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Basic Authentication enabled
10+
|--------------------------------------------------------------------------
11+
|
12+
| Enable the Basic Authentication plugin by adding the
13+
| BASIC_AUTHENTICATION_ENABLED to your .env file.
14+
|
15+
*/
616
'enabled' => (bool) env('BASIC_AUTHENTICATION_ENABLED', false),
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| White Listed IP addresses
21+
|--------------------------------------------------------------------------
22+
|
23+
| Provide a comma separated list of IP addresses to whitelist.
24+
|
25+
*/
726
'whitelisted_ips' => env('BASIC_AUTHENTICATION_WHITELISTED_IPS', ''),
27+
828
];

0 commit comments

Comments
 (0)