| Total Complexity | 88 |
| Total Lines | 598 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SQL2 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SQL2, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class SQL2 extends UserPassBase |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * List of one or more databases that are used by auth and attribute queries. |
||
| 27 | * Each database must have a unique name, and the name is used to refer to |
||
| 28 | * the database in auth and attribute queries. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private array $databases = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * List of one or more authentication queries. The first query that returns a result |
||
| 36 | * is considered to have authenticated the user (and termed "winning"). |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private array $authQueries = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * List of zero or more attribute queries, which can optionally be limited to run only |
||
| 44 | * for certain "winning" authentication queries. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private array $attributesQueries = []; |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor for this authentication source. |
||
| 53 | * |
||
| 54 | * @param array $info Information about this authentication source. |
||
| 55 | * @param array $config Configuration. |
||
| 56 | */ |
||
| 57 | public function __construct(array $info, array $config) |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * Create a database connection. |
||
| 276 | * |
||
| 277 | * @return \PDO The database connection. |
||
| 278 | */ |
||
| 279 | protected function connect(string $dbname): PDO |
||
| 280 | { |
||
| 281 | if (!array_key_exists($dbname, $this->databases)) { |
||
| 282 | throw new Exception('sqlauth:' . $this->authId . ': Attempt to connect to unknown database \'' . |
||
| 283 | $dbname . '\''); |
||
| 284 | } |
||
| 285 | if ($this->databases[$dbname]['_pdo'] !== null) { |
||
| 286 | // Already connected |
||
| 287 | return $this->databases[$dbname]['_pdo']; |
||
| 288 | } |
||
| 289 | |||
| 290 | try { |
||
| 291 | $db = new PDO( |
||
| 292 | $this->databases[$dbname]['dsn'], |
||
| 293 | $this->databases[$dbname]['username'], |
||
| 294 | $this->databases[$dbname]['password'], |
||
| 295 | $this->databases[$dbname]['options'], |
||
| 296 | ); |
||
| 297 | } catch (PDOException $e) { |
||
| 298 | // Obfuscate the password if it's part of the dsn |
||
| 299 | $obfuscated_dsn = |
||
| 300 | preg_replace('/(user|password)=(.*?([;]|$))/', '${1}=***', $this->databases[$dbname]['dsn']); |
||
| 301 | |||
| 302 | throw new Exception('sqlauth:' . $this->authId . ': - Failed to connect to \'' . |
||
| 303 | $obfuscated_dsn . '\': ' . $e->getMessage()); |
||
| 304 | } |
||
| 305 | |||
| 306 | $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 307 | |||
| 308 | $driver = explode(':', $this->databases[$dbname]['dsn'], 2); |
||
| 309 | $driver = strtolower($driver[0]); |
||
| 310 | |||
| 311 | // Driver specific initialization |
||
| 312 | switch ($driver) { |
||
| 313 | case 'mysql': |
||
| 314 | // Use UTF-8 |
||
| 315 | $db->exec("SET NAMES 'utf8mb4'"); |
||
| 316 | break; |
||
| 317 | case 'pgsql': |
||
| 318 | // Use UTF-8 |
||
| 319 | $db->exec("SET NAMES 'UTF8'"); |
||
| 320 | break; |
||
| 321 | } |
||
| 322 | |||
| 323 | Logger::debug('sqlauth:' . $this->authId . ': Connected to database ' . $dbname); |
||
| 324 | $this->databases[$dbname]['_pdo'] = $db; |
||
| 325 | return $db; |
||
| 326 | } |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Extract SQL columns into SAML attribute array |
||
| 331 | * |
||
| 332 | * @param array $attributes output place to store extracted attributes |
||
| 333 | * @param array $data Associative array from database in the format of PDO fetchAll |
||
| 334 | * @param array $forbiddenAttributes An array of attributes to never return |
||
| 335 | * @return array &$attributes |
||
| 336 | */ |
||
| 337 | protected function extractAttributes(array &$attributes, array $data, array $forbiddenAttributes = []): array |
||
| 338 | { |
||
| 339 | foreach ($data as $row) { |
||
| 340 | foreach ($row as $name => $value) { |
||
| 341 | if ($value === null) { |
||
| 342 | continue; |
||
| 343 | } |
||
| 344 | if (in_array($name, $forbiddenAttributes)) { |
||
| 345 | continue; |
||
| 346 | } |
||
| 347 | |||
| 348 | $value = (string) $value; |
||
| 349 | |||
| 350 | if (!array_key_exists($name, $attributes)) { |
||
| 351 | $attributes[$name] = []; |
||
| 352 | } |
||
| 353 | |||
| 354 | if (in_array($value, $attributes[$name], true)) { |
||
| 355 | // Value already exists in attribute |
||
| 356 | continue; |
||
| 357 | } |
||
| 358 | |||
| 359 | $attributes[$name][] = $value; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | return $attributes; |
||
| 363 | } |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * Execute the query with given parameters and return the tuples that result. |
||
| 368 | * |
||
| 369 | * @param string $query SQL to execute |
||
| 370 | * @param array $params parameters to the SQL query |
||
| 371 | * @return array tuples that result |
||
| 372 | */ |
||
| 373 | protected function executeQuery(PDO $db, string $query, array $params): array |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | |||
| 399 | /** |
||
| 400 | * Authenticate using the optional password_verify() support against a hash retrieved from the database. |
||
| 401 | * |
||
| 402 | * @param string $queryname Name of the auth query being processed |
||
| 403 | * @param array $queryConfig Configuration from authsources.php for this auth query |
||
| 404 | * @param array $data Result data from the database query |
||
| 405 | * @param string $password Password to verify with password_verify() |
||
| 406 | * @return bool True if password_verify() password verification succeeded, false otherwise |
||
| 407 | */ |
||
| 408 | protected function authenticatePasswordVerifyHash( |
||
| 409 | string $queryname, |
||
| 410 | array $queryConfig, |
||
| 411 | array $data, |
||
| 412 | string $password, |
||
| 413 | ): bool { |
||
| 414 | // If password_verify_hash_column is not set, we are not using password_verify() |
||
| 415 | if (!array_key_exists('password_verify_hash_column', $queryConfig)) { |
||
| 416 | Logger::error(sprintf( |
||
| 417 | 'sqlauth:%s: authenticatePasswordVerifyHash() called but configuration for ' . |
||
| 418 | '"password_verify_hash_column" not found in query config for query %s.', |
||
| 419 | $this->authId, |
||
| 420 | $queryname, |
||
| 421 | )); |
||
| 422 | throw new Error\Error('WRONGUSERPASS'); |
||
| 423 | } elseif (count($data) < 1) { |
||
| 424 | // No rows returned, password_verify() cannot succeed |
||
| 425 | return false; |
||
| 426 | } |
||
| 427 | |||
| 428 | /* This is where we need to run password_verify() if we are using password_verify() to |
||
| 429 | * authenticate hashed passwords that are only stored in the database. */ |
||
| 430 | $hashColumn = $queryConfig['password_verify_hash_column']; |
||
| 431 | if (!array_key_exists($hashColumn, $data[0])) { |
||
| 432 | Logger::error('sqlauth:' . $this->authId . ': Auth query ' . $queryname . |
||
| 433 | ' did not return expected hash column \'' . $hashColumn . '\''); |
||
| 434 | throw new Error\Error('WRONGUSERPASS'); |
||
| 435 | } |
||
| 436 | |||
| 437 | $validPasswordHashFound = false; |
||
| 438 | $passwordHash = null; |
||
| 439 | foreach ($data as $row) { |
||
| 440 | if ((!array_key_exists($hashColumn, $row)) || is_null($row[$hashColumn])) { |
||
| 441 | Logger::error(sprintf( |
||
| 442 | 'sqlauth:%s: column `%s` must be in every result tuple.', |
||
| 443 | $this->authId, |
||
| 444 | $hashColumn, |
||
| 445 | )); |
||
| 446 | throw new Error\Error('WRONGUSERPASS'); |
||
| 447 | } |
||
| 448 | if (($passwordHash === null) && (strlen($row[$hashColumn]) > 0)) { |
||
| 449 | $passwordHash = $row[$hashColumn]; |
||
| 450 | $validPasswordHashFound = true; |
||
| 451 | } elseif ($passwordHash != $row[$hashColumn]) { |
||
| 452 | Logger::error(sprintf( |
||
| 453 | 'sqlauth:%s: column %s must be THE SAME in every result tuple.', |
||
| 454 | $this->authId, |
||
| 455 | $hashColumn, |
||
| 456 | )); |
||
| 457 | throw new Error\Error('WRONGUSERPASS'); |
||
| 458 | } elseif (strlen($row[$hashColumn]) === 0) { |
||
| 459 | Logger::error(sprintf( |
||
| 460 | 'sqlauth:%s: column `%s` must contain a valid password hash.', |
||
| 461 | $this->authId, |
||
| 462 | $hashColumn, |
||
| 463 | )); |
||
| 464 | throw new Error\Error('WRONGUSERPASS'); |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | if ((!$validPasswordHashFound) || (!password_verify($password, $passwordHash))) { |
||
| 469 | Logger::error('sqlauth:' . $this->authId . ': Auth query ' . $queryname . |
||
| 470 | ' password verification failed'); |
||
| 471 | /* Authentication with verify_password() failed, however that only means that |
||
| 472 | * this auth query did not succeed. We should try the next auth query if any. */ |
||
| 473 | return false; |
||
| 474 | } |
||
| 475 | |||
| 476 | Logger::debug('sqlauth:' . $this->authId . ': Auth query ' . $queryname . |
||
| 477 | ' password verification using password_verify() succeeded'); |
||
| 478 | return true; |
||
| 479 | } |
||
| 480 | |||
| 481 | |||
| 482 | /** |
||
| 483 | * Attempt to log in using the given username and password. |
||
| 484 | * |
||
| 485 | * On a successful login, this function should return the users attributes. On failure, |
||
| 486 | * it should throw an exception. If the error was caused by the user entering the wrong |
||
| 487 | * username or password, a \SimpleSAML\Error\Error('WRONGUSERPASS') should be thrown. |
||
| 488 | * |
||
| 489 | * Note that both the username and the password are UTF-8 encoded. |
||
| 490 | * |
||
| 491 | * @param string $username The username the user wrote. |
||
| 492 | * @param string $password The password the user wrote. |
||
| 493 | * @return array Associative array with the users attributes. |
||
| 494 | */ |
||
| 495 | protected function login( |
||
| 621 | } |
||
| 622 | } |
||
| 623 |