55namespace Vdlp \BasicAuthentication ;
66
77use 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 ;
1110use System \Classes \PluginBase ;
12- use Vdlp \BasicAuthentication \Classes \AuthorizationHelper ;
1311use Vdlp \BasicAuthentication \Console \CreateCredentialsCommand ;
14- use Vdlp \BasicAuthentication \Models \Credential ;
12+ use Vdlp \BasicAuthentication \Http \Middleware \BasicAuthenticationMiddleware ;
13+ use Vdlp \BasicAuthentication \ServiceProviders \BasicAuthenticationServiceProvider ;
1514
1615final 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
0 commit comments