Skip to content

Commit 4ed60a8

Browse files
committed
Improve Middleware registration
1 parent 081008a commit 4ed60a8

2 files changed

Lines changed: 19 additions & 47 deletions

File tree

Plugin.php

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

77
use Backend\Helpers\Backend as BackendHelper;
8-
use Illuminate\Routing\Router;
9-
use October\Rain\Foundation\Application;
8+
use Illuminate\Contracts\Http\Kernel;
109
use System\Classes\PluginBase;
1110
use Vdlp\BasicAuthentication\Console\CreateCredentialsCommand;
1211
use Vdlp\BasicAuthentication\Http\Middleware\BasicAuthenticationMiddleware;
@@ -24,29 +23,25 @@ public function pluginDetails(): array
2423
];
2524
}
2625

27-
public function register(): void
28-
{
29-
$this->app->register(BasicAuthenticationServiceProvider::class);
30-
31-
$this->registerConsoleCommand(CreateCredentialsCommand::class, CreateCredentialsCommand::class);
32-
}
33-
3426
public function boot(): void
3527
{
36-
/** @var Application $application */
37-
$application = $this->app;
38-
3928
if (
4029
(bool) config('basicauthentication.enabled', false) === false
41-
|| $application->runningInConsole()
42-
|| $application->runningUnitTests()
30+
|| $this->app->runningInConsole()
31+
|| $this->app->runningUnitTests()
4332
) {
4433
return;
4534
}
4635

47-
/** @var Router $router */
48-
$router = $application->make(Router::class);
49-
$router->pushMiddlewareToGroup('web', BasicAuthenticationMiddleware::class);
36+
$this->app[Kernel::class]
37+
->pushMiddleware(BasicAuthenticationMiddleware::class);
38+
}
39+
40+
public function register(): void
41+
{
42+
$this->app->register(BasicAuthenticationServiceProvider::class);
43+
44+
$this->registerConsoleCommand(CreateCredentialsCommand::class, CreateCredentialsCommand::class);
5045
}
5146

5247
public function registerPermissions(): array

http/middleware/BasicAuthenticationMiddleware.php

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,22 @@
66

77
use Closure;
88
use Illuminate\Contracts\Hashing\Hasher;
9-
use Illuminate\Contracts\Session\Session;
109
use Illuminate\Contracts\Translation\Translator;
11-
use Illuminate\Database\Eloquent\ModelNotFoundException;
1210
use Illuminate\Http\Request;
1311
use Illuminate\Http\Response;
1412
use Illuminate\Support\Str;
15-
use InvalidArgumentException;
16-
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
13+
use Throwable;
1714
use Vdlp\BasicAuthentication\Models\Credential;
1815

1916
final class BasicAuthenticationMiddleware
2017
{
21-
private Session $session;
22-
private Translator $translator;
23-
private Hasher $hasher;
24-
25-
public function __construct(Session $session, Translator $translator, Hasher $hasher)
26-
{
27-
$this->session = $session;
28-
$this->translator = $translator;
29-
$this->hasher = $hasher;
18+
public function __construct(
19+
private Translator $translator,
20+
private Hasher $hasher,
21+
) {
3022
}
3123

32-
/**
33-
* @return mixed
34-
*
35-
* @throws SuspiciousOperationException
36-
* @throws InvalidArgumentException
37-
*/
38-
public function handle(Request $request, Closure $next)
24+
public function handle(Request $request, Closure $next): mixed
3925
{
4026
if ($this->isIpAddressWhitelisted((string) $request->ip())) {
4127
return $next($request);
@@ -47,7 +33,7 @@ public function handle(Request $request, Closure $next)
4733
->where('hostname', $request->getHost())
4834
->where('is_enabled', true)
4935
->firstOrFail(['hostname', 'username', 'password', 'realm', 'whitelist']);
50-
} catch (ModelNotFoundException $exception) {
36+
} catch (Throwable) {
5137
// @ignoreException
5238
return $next($request);
5339
}
@@ -59,13 +45,6 @@ public function handle(Request $request, Closure $next)
5945
return $next($request);
6046
}
6147

62-
$sessionKey = str_slug(str_replace('.', '_', $credential->hostname) . '_basic_authentication');
63-
64-
// Session is authorized.
65-
if ($this->session->has($sessionKey)) {
66-
return $next($request);
67-
}
68-
6948
$needsRehash = $this->hasher->needsRehash($credential->password);
7049

7150
// Validate credentials.
@@ -80,8 +59,6 @@ public function handle(Request $request, Closure $next)
8059
return $this->getUnauthorizedResponse($credential);
8160
}
8261

83-
$this->session->put($sessionKey, $request->getUser());
84-
8562
return $next($request);
8663
}
8764

0 commit comments

Comments
 (0)