Skip to content

Commit a5dd1b6

Browse files
Merge pull request #17901 from MauricioFauth/auth-plugin
Refactor authentication plugin creation
2 parents 561d9ca + a2a3f6d commit a5dd1b6

12 files changed

Lines changed: 207 additions & 78 deletions

libraries/classes/Common.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
use PhpMyAdmin\ConfigStorage\Relation;
99
use PhpMyAdmin\Dbal\DatabaseName;
1010
use PhpMyAdmin\Dbal\TableName;
11+
use PhpMyAdmin\Exceptions\AuthenticationPluginException;
1112
use PhpMyAdmin\Exceptions\ConfigException;
1213
use PhpMyAdmin\Exceptions\MissingExtensionException;
1314
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
1415
use PhpMyAdmin\Http\ServerRequest;
1516
use PhpMyAdmin\Plugins\AuthenticationPlugin;
17+
use PhpMyAdmin\Plugins\AuthenticationPluginFactory;
1618
use PhpMyAdmin\SqlParser\Lexer;
1719
use RuntimeException;
1820
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -86,7 +88,6 @@ final class Common
8688
public static function run(bool $isSetupPage = false): void
8789
{
8890
$GLOBALS['lang'] = $GLOBALS['lang'] ?? null;
89-
$GLOBALS['auth_plugin'] = $GLOBALS['auth_plugin'] ?? null;
9091
$GLOBALS['theme'] = $GLOBALS['theme'] ?? null;
9192
$GLOBALS['urlParams'] = $GLOBALS['urlParams'] ?? null;
9293
$GLOBALS['token_mismatch'] = $GLOBALS['token_mismatch'] ?? null;
@@ -228,8 +229,17 @@ public static function run(bool $isSetupPage = false): void
228229
if (! empty($GLOBALS['cfg']['Server'])) {
229230
$config->getLoginCookieValidityFromCache($GLOBALS['server']);
230231

231-
$GLOBALS['auth_plugin'] = Plugins::getAuthPlugin();
232-
$GLOBALS['auth_plugin']->authenticate();
232+
/** @var AuthenticationPluginFactory $authPluginFactory */
233+
$authPluginFactory = $container->get(AuthenticationPluginFactory::class);
234+
try {
235+
$authPlugin = $authPluginFactory->create();
236+
} catch (AuthenticationPluginException $exception) {
237+
echo self::getGenericError($exception->getMessage());
238+
239+
return;
240+
}
241+
242+
$authPlugin->authenticate();
233243

234244
/* Enable LOAD DATA LOCAL INFILE for LDI plugin */
235245
if ($route === '/import' && ($_POST['format'] ?? '') === 'ldi') {
@@ -239,11 +249,9 @@ public static function run(bool $isSetupPage = false): void
239249
// phpcs:enable
240250
}
241251

242-
self::connectToDatabaseServer($GLOBALS['dbi'], $GLOBALS['auth_plugin']);
243-
244-
$GLOBALS['auth_plugin']->rememberCredentials();
245-
246-
$GLOBALS['auth_plugin']->checkTwoFactor();
252+
self::connectToDatabaseServer($GLOBALS['dbi'], $authPlugin);
253+
$authPlugin->rememberCredentials();
254+
$authPlugin->checkTwoFactor();
247255

248256
/* Log success */
249257
Logging::logUser($GLOBALS['cfg']['Server']['user']);

libraries/classes/Controllers/LogoutController.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@
66

77
use PhpMyAdmin\Core;
88
use PhpMyAdmin\Http\ServerRequest;
9+
use PhpMyAdmin\Plugins\AuthenticationPluginFactory;
910

1011
class LogoutController
1112
{
12-
public function __invoke(ServerRequest $request): void
13+
/** @var AuthenticationPluginFactory */
14+
private $authPluginFactory;
15+
16+
public function __construct(AuthenticationPluginFactory $authPluginFactory)
1317
{
14-
$GLOBALS['auth_plugin'] = $GLOBALS['auth_plugin'] ?? null;
15-
$GLOBALS['token_mismatch'] = $GLOBALS['token_mismatch'] ?? null;
18+
$this->authPluginFactory = $authPluginFactory;
19+
}
1620

17-
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST' || $GLOBALS['token_mismatch']) {
21+
public function __invoke(ServerRequest $request): void
22+
{
23+
if (! $request->isPost() || $GLOBALS['token_mismatch']) {
1824
Core::sendHeaderLocation('./index.php?route=/');
1925

2026
return;
2127
}
2228

23-
$GLOBALS['auth_plugin']->logOut();
29+
$authPlugin = $this->authPluginFactory->create();
30+
$authPlugin->logOut();
2431
}
2532
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Exceptions;
6+
7+
use Exception;
8+
9+
class AuthenticationPluginException extends Exception
10+
{
11+
}

libraries/classes/Plugins.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use FilesystemIterator;
88
use PhpMyAdmin\Html\MySQLDocumentation;
9-
use PhpMyAdmin\Plugins\AuthenticationPlugin;
109
use PhpMyAdmin\Plugins\ExportPlugin;
1110
use PhpMyAdmin\Plugins\ImportPlugin;
1211
use PhpMyAdmin\Plugins\Plugin;
@@ -27,7 +26,6 @@
2726
use function __;
2827
use function class_exists;
2928
use function count;
30-
use function defined;
3129
use function get_class;
3230
use function htmlspecialchars;
3331
use function is_subclass_of;
@@ -42,8 +40,6 @@
4240
use function str_starts_with;
4341
use function strcasecmp;
4442
use function strcmp;
45-
use function strtolower;
46-
use function ucfirst;
4743
use function usort;
4844

4945
class Plugins
@@ -621,29 +617,4 @@ public static function getOptions($section, array $list)
621617

622618
return $ret;
623619
}
624-
625-
public static function getAuthPlugin(): AuthenticationPlugin
626-
{
627-
/** @psalm-var class-string $class */
628-
$class = 'PhpMyAdmin\\Plugins\\Auth\\Authentication'
629-
. ucfirst(strtolower($GLOBALS['cfg']['Server']['auth_type']));
630-
631-
if (! class_exists($class)) {
632-
echo (new Template())->render('error/generic', [
633-
'lang' => $GLOBALS['lang'] ?? 'en',
634-
'dir' => $GLOBALS['text_dir'] ?? 'ltr',
635-
'error_message' => __('Invalid authentication method set in configuration:')
636-
. ' ' . $GLOBALS['cfg']['Server']['auth_type'],
637-
]);
638-
639-
if (! defined('TESTSUITE')) {
640-
exit;
641-
}
642-
}
643-
644-
/** @var AuthenticationPlugin $plugin */
645-
$plugin = new $class();
646-
647-
return $plugin;
648-
}
649620
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Plugins;
6+
7+
use PhpMyAdmin\Exceptions\AuthenticationPluginException;
8+
9+
use function __;
10+
use function class_exists;
11+
use function is_subclass_of;
12+
use function strtolower;
13+
use function ucfirst;
14+
15+
class AuthenticationPluginFactory
16+
{
17+
/** @var AuthenticationPlugin|null */
18+
private $plugin = null;
19+
20+
/**
21+
* @throws AuthenticationPluginException
22+
*/
23+
public function create(): AuthenticationPlugin
24+
{
25+
if ($this->plugin instanceof AuthenticationPlugin) {
26+
return $this->plugin;
27+
}
28+
29+
$authType = $GLOBALS['cfg']['Server']['auth_type'];
30+
$class = 'PhpMyAdmin\\Plugins\\Auth\\Authentication' . ucfirst(strtolower($authType));
31+
if (! class_exists($class) || ! is_subclass_of($class, AuthenticationPlugin::class)) {
32+
throw new AuthenticationPluginException(
33+
__('Invalid authentication method set in configuration:') . ' ' . $authType
34+
);
35+
}
36+
37+
$this->plugin = new $class();
38+
39+
return $this->plugin;
40+
}
41+
}

libraries/classes/UserPassword.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin;
66

77
use PhpMyAdmin\Html\Generator;
8+
use PhpMyAdmin\Plugins\AuthenticationPluginFactory;
89
use PhpMyAdmin\Query\Compatibility;
910
use PhpMyAdmin\Server\Privileges;
1011

@@ -19,12 +20,13 @@ class UserPassword
1920
/** @var Privileges */
2021
private $serverPrivileges;
2122

22-
/**
23-
* @param Privileges $serverPrivileges Privileges object
24-
*/
25-
public function __construct(Privileges $serverPrivileges)
23+
/** @var AuthenticationPluginFactory */
24+
private $authPluginFactory;
25+
26+
public function __construct(Privileges $serverPrivileges, AuthenticationPluginFactory $authPluginFactory)
2627
{
2728
$this->serverPrivileges = $serverPrivileges;
29+
$this->authPluginFactory = $authPluginFactory;
2830
}
2931

3032
/**
@@ -65,8 +67,6 @@ public function setChangePasswordMsg(string $pmaPw, string $pmaPw2, bool $skipPa
6567
*/
6668
public function changePassword($password, ?string $authenticationPlugin): string
6769
{
68-
$GLOBALS['auth_plugin'] = $GLOBALS['auth_plugin'] ?? null;
69-
7070
$hashing_function = $this->changePassHashingFunction($authenticationPlugin);
7171

7272
[$username, $hostname] = $GLOBALS['dbi']->getCurrentUserAndHost();
@@ -114,7 +114,8 @@ public function changePassword($password, ?string $authenticationPlugin): string
114114
$orig_auth_plugin
115115
);
116116

117-
$GLOBALS['auth_plugin']->handlePasswordChange($password);
117+
$authPlugin = $this->authPluginFactory->create();
118+
$authPlugin->handlePasswordChange($password);
118119

119120
return $sql_query;
120121
}

libraries/services.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpMyAdmin\ConfigStorage\Relation;
66
use PhpMyAdmin\ConfigStorage\RelationCleanup;
7+
use PhpMyAdmin\Plugins\AuthenticationPluginFactory;
78

89
return [
910
'services' => [
@@ -132,6 +133,9 @@
132133
'class' => PhpMyAdmin\Partitioning\Maintenance::class,
133134
'arguments' => ['$dbi' => '@dbi'],
134135
],
136+
AuthenticationPluginFactory::class => [
137+
'class' => AuthenticationPluginFactory::class,
138+
],
135139
'relation' => [
136140
'class' => Relation::class,
137141
'arguments' => ['$dbi' => '@dbi'],
@@ -251,7 +255,7 @@
251255
],
252256
'user_password' => [
253257
'class' => PhpMyAdmin\UserPassword::class,
254-
'arguments' => ['@server_privileges'],
258+
'arguments' => ['@server_privileges', '@' . AuthenticationPluginFactory::class],
255259
],
256260
'user_preferences' => [
257261
'class' => PhpMyAdmin\UserPreferences::class,

libraries/services_controllers.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use PhpMyAdmin\Controllers\UserPasswordController;
3838
use PhpMyAdmin\Controllers\VersionCheckController;
3939
use PhpMyAdmin\Controllers\View;
40+
use PhpMyAdmin\Plugins\AuthenticationPluginFactory;
4041

4142
return [
4243
'services' => [
@@ -585,6 +586,7 @@
585586
],
586587
LogoutController::class => [
587588
'class' => LogoutController::class,
589+
'arguments' => ['@' . AuthenticationPluginFactory::class],
588590
],
589591
NavigationController::class => [
590592
'class' => NavigationController::class,

psalm-baseline.xml

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@
222222
<code>$GLOBALS['cfg']['MysqlMinVersion']['internal']</code>
223223
<code>$GLOBALS['cfg']['Server']['user']</code>
224224
</MixedArrayAccess>
225-
<MixedAssignment occurrences="6">
226-
<code>$GLOBALS['auth_plugin']</code>
225+
<MixedAssignment occurrences="5">
227226
<code>$GLOBALS['back']</code>
228227
<code>$GLOBALS['theme']</code>
229228
<code>$controlLink</code>
@@ -2312,17 +2311,6 @@
23122311
<code>$options</code>
23132312
</MixedAssignment>
23142313
</file>
2315-
<file src="libraries/classes/Controllers/LogoutController.php">
2316-
<MixedAssignment occurrences="1">
2317-
<code>$GLOBALS['auth_plugin']</code>
2318-
</MixedAssignment>
2319-
<MixedMethodCall occurrences="1">
2320-
<code>logOut</code>
2321-
</MixedMethodCall>
2322-
<PossiblyNullReference occurrences="1">
2323-
<code>logOut</code>
2324-
</PossiblyNullReference>
2325-
</file>
23262314
<file src="libraries/classes/Controllers/NavigationController.php">
23272315
<PossiblyInvalidArgument occurrences="9">
23282316
<code>! empty($_POST['tableName']) ? $_POST['tableName'] : null</code>
@@ -9256,10 +9244,9 @@
92569244
<code>$val</code>
92579245
<code>$val</code>
92589246
</MixedAssignment>
9259-
<MixedMethodCall occurrences="3">
9247+
<MixedMethodCall occurrences="2">
92609248
<code>getProperties</code>
92619249
<code>new $class()</code>
9262-
<code>new $class()</code>
92639250
</MixedMethodCall>
92649251
<PossiblyInvalidArgument occurrences="4">
92659252
<code>$_GET[$opt]</code>
@@ -9300,9 +9287,6 @@
93009287
<RedundantConditionGivenDocblockType occurrences="1">
93019288
<code>$properties != null</code>
93029289
</RedundantConditionGivenDocblockType>
9303-
<RedundantFunctionCall occurrences="1">
9304-
<code>strtolower</code>
9305-
</RedundantFunctionCall>
93069290
<UnnecessaryVarAnnotation occurrences="3">
93079291
<code>RadioPropertyItem</code>
93089292
<code>SelectPropertyItem</code>
@@ -9439,6 +9423,14 @@
94399423
<code>issetCookie</code>
94409424
</PossiblyNullReference>
94419425
</file>
9426+
<file src="libraries/classes/Plugins/AuthenticationPluginFactory.php">
9427+
<RedundantFunctionCall occurrences="1">
9428+
<code>strtolower</code>
9429+
</RedundantFunctionCall>
9430+
<UnsafeInstantiation occurrences="1">
9431+
<code>new $class()</code>
9432+
</UnsafeInstantiation>
9433+
</file>
94429434
<file src="libraries/classes/Plugins/Export/ExportCodegen.php">
94439435
<PossiblyNullArgument occurrences="4">
94449436
<code>$row[0]</code>
@@ -14196,21 +14188,12 @@
1419614188
<code>$username</code>
1419714189
<code>$username</code>
1419814190
</MixedArgument>
14199-
<MixedAssignment occurrences="1">
14200-
<code>$GLOBALS['auth_plugin']</code>
14201-
</MixedAssignment>
14202-
<MixedMethodCall occurrences="1">
14203-
<code>handlePasswordChange</code>
14204-
</MixedMethodCall>
1420514191
<PossiblyInvalidArgument occurrences="1">
1420614192
<code>$_POST['pma_pw']</code>
1420714193
</PossiblyInvalidArgument>
1420814194
<PossiblyInvalidCast occurrences="1">
1420914195
<code>$_POST['pma_pw']</code>
1421014196
</PossiblyInvalidCast>
14211-
<PossiblyNullReference occurrences="1">
14212-
<code>handlePasswordChange</code>
14213-
</PossiblyNullReference>
1421414197
</file>
1421514198
<file src="libraries/classes/UserPreferences.php">
1421614199
<DeprecatedMethod occurrences="7">

0 commit comments

Comments
 (0)