|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Vdlp\BasicAuthentication; |
| 6 | + |
| 7 | +use Backend\Helpers\Backend as BackendHelper; |
| 8 | +use Illuminate\Contracts\Config\Repository; |
| 9 | +use Illuminate\Contracts\Session\Session; |
| 10 | +use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 11 | +use Illuminate\Http\Request; |
| 12 | +use October\Rain\Translation\Translator; |
| 13 | +use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException; |
| 14 | +use System\Classes\PluginBase; |
| 15 | +use Vdlp\BasicAuthentication\Classes\Helper\AuthorizationHelper; |
| 16 | +use Vdlp\BasicAuthentication\Models\Credential; |
| 17 | +use Vdlp\BasicAuthentication\ServiceProviders\BasicAuthenticationServiceProvider; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class Plugin |
| 21 | + * |
| 22 | + * @package Vdlp\BasicAuthentication |
| 23 | + */ |
| 24 | +class Plugin extends PluginBase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + public function pluginDetails(): array |
| 30 | + { |
| 31 | + return [ |
| 32 | + 'name' => 'vdlp.basicauthentication::lang.plugin.name', |
| 33 | + 'description' => 'vdlp.basicauthentication::lang.plugin.description', |
| 34 | + 'author' => 'Van der Let & Partners', |
| 35 | + 'icon' => 'icon-lock', |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * {@inheritdoc} |
| 41 | + */ |
| 42 | + public function register(): void |
| 43 | + { |
| 44 | + $this->app->register(BasicAuthenticationServiceProvider::class); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + * @throws SuspiciousOperationException |
| 50 | + */ |
| 51 | + public function boot() |
| 52 | + { |
| 53 | + /** @var Repository $config */ |
| 54 | + $config = resolve(Repository::class); |
| 55 | + |
| 56 | + if (!$config->get('basicauthentication.enabled') |
| 57 | + || app()->runningInConsole() |
| 58 | + || app()->runningUnitTests() |
| 59 | + || app()->runningInBackend() |
| 60 | + ) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + /** @var Request $request */ |
| 65 | + $request = resolve(Request::class); |
| 66 | + |
| 67 | + /** @var Session $session */ |
| 68 | + $session = resolve(Session::class); |
| 69 | + |
| 70 | + /** @var Translator $translator */ |
| 71 | + $translator = resolve('translator'); |
| 72 | + |
| 73 | + /** @var AuthorizationHelper $authorizationHelper */ |
| 74 | + $authorizationHelper = resolve(AuthorizationHelper::class); |
| 75 | + if ($authorizationHelper->isIpAddressWhitelisted($request->ip())) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + /** @var Credential $credential */ |
| 81 | + $credential = Credential::query() |
| 82 | + ->where('hostname', '=', $request->getHost()) |
| 83 | + ->where('is_enabled', '=', true) |
| 84 | + ->firstOrFail(); |
| 85 | + } catch (ModelNotFoundException $e) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if ($authorizationHelper->isUrlExcluded($request->getUri())) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + $sessionKey = str_slug(str_replace('.', '_', $credential->getAttribute('hostname')) . '_basic_authentication'); |
| 94 | + if ($session->has($sessionKey)) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if ($request->getUser() === $credential->getAttribute('username') |
| 99 | + && $request->getPassword() === $credential->getAttribute('password') |
| 100 | + ) { |
| 101 | + $session->put($sessionKey, $request->getUser()); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + header('WWW-Authenticate: Basic realm="' . $credential->getAttribute('realm') . '"'); |
| 106 | + header('HTTP/1.0 401 Unauthorized'); |
| 107 | + |
| 108 | + echo $translator->get('vdlp.basicauthentication::lang.output.unauthorized'); |
| 109 | + exit; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * {@inheritdoc} |
| 114 | + */ |
| 115 | + public function registerPermissions(): array |
| 116 | + { |
| 117 | + return [ |
| 118 | + 'vdlp.basicauthentication.access_settings' => [ |
| 119 | + 'label' => 'vdlp.basicauthentication::lang.permissions.access_settings.label', |
| 120 | + 'tab' => 'vdlp.basicauthentication::lang.permissions.access_settings.tab', |
| 121 | + ], |
| 122 | + ]; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * {@inheritdoc} |
| 127 | + */ |
| 128 | + public function registerSettings(): array |
| 129 | + { |
| 130 | + /** @var BackendHelper $backendHelper */ |
| 131 | + $backendHelper = resolve(BackendHelper::class); |
| 132 | + |
| 133 | + return [ |
| 134 | + 'credentials' => [ |
| 135 | + 'label' => 'vdlp.basicauthentication::lang.settings.label', |
| 136 | + 'description' => 'vdlp.basicauthentication::lang.settings.description', |
| 137 | + 'url' => $backendHelper->url('vdlp/basicauthentication/credentials'), |
| 138 | + 'category' => 'Basic Authentication', |
| 139 | + 'icon' => 'icon-lock', |
| 140 | + 'permissions' => ['vdlp.basicauthentication.*'], |
| 141 | + 'keywords' => 'basic authentication security', |
| 142 | + 'order' => 500, |
| 143 | + ], |
| 144 | + 'excludedurls' => [ |
| 145 | + 'label' => 'vdlp.basicauthentication::lang.excludedurls.label', |
| 146 | + 'description' => 'vdlp.basicauthentication::lang.excludedurls.description', |
| 147 | + 'url' => $backendHelper->url('vdlp/basicauthentication/excludedurls'), |
| 148 | + 'category' => 'Basic Authentication', |
| 149 | + 'icon' => 'icon-link', |
| 150 | + 'permissions' => ['vdlp.basicauthentication.*'], |
| 151 | + 'keywords' => 'basic authentication security', |
| 152 | + 'order' => 501, |
| 153 | + ], |
| 154 | + ]; |
| 155 | + } |
| 156 | +} |
0 commit comments