|
18 | 18 | use Gigya\GigyaIM\Model\Config;
|
19 | 19 | use Gigya\GigyaIM\Model\SettingsFactory;
|
20 | 20 | use Gigya\GigyaIM\Encryption\Encryptor;
|
| 21 | +use InvalidArgumentException; |
21 | 22 | use Magento\Customer\Api\Data\CustomerInterface;
|
22 | 23 | use Magento\Framework\App\ObjectManager;
|
23 | 24 | use Magento\Store\Model\ScopeInterface;
|
@@ -459,31 +460,45 @@ public function verifyGigyaRequiredFields($gigya_user_account): array
|
459 | 460 | return $message;
|
460 | 461 | }
|
461 | 462 |
|
| 463 | + /** |
| 464 | + * @throws Exception |
| 465 | + */ |
462 | 466 | public function generatePassword($len = 8): string
|
463 | 467 | {
|
464 |
| - $chars = self::CHARS_PASSWORD_LOWERS |
465 |
| - . self::CHARS_PASSWORD_UPPERS |
466 |
| - . self::CHARS_PASSWORD_DIGITS |
467 |
| - . self::CHARS_PASSWORD_SPECIALS; |
468 |
| - $str = $this->getRandomString($len, $chars); |
| 468 | + $str = $this->getRandomString($len); |
469 | 469 | return 'Gigya_' . $str;
|
470 | 470 | }
|
471 | 471 |
|
472 | 472 | /**
|
473 |
| - * Taken from magento 1 helper core |
474 | 473 | * @param $len
|
475 |
| - * @param $chars |
476 | 474 | * @return mixed
|
| 475 | + * @throws Exception |
477 | 476 | */
|
478 |
| - private function getRandomString($len, $chars): mixed |
| 477 | + private function getRandomString($len): mixed |
479 | 478 | {
|
480 |
| - if (empty($chars)) { |
481 |
| - $chars = self::CHARS_PASSWORD_LOWERS . self::CHARS_PASSWORD_UPPERS . self::CHARS_PASSWORD_DIGITS; |
| 479 | + if ($len < 4) { |
| 480 | + throw new InvalidArgumentException('Length must be at least 4 to include all character types.'); |
| 481 | + } |
| 482 | + |
| 483 | + $charSets = [ |
| 484 | + self::CHARS_PASSWORD_LOWERS, |
| 485 | + self::CHARS_PASSWORD_UPPERS, |
| 486 | + self::CHARS_PASSWORD_DIGITS, |
| 487 | + self::CHARS_PASSWORD_SPECIALS |
| 488 | + ]; |
| 489 | + |
| 490 | + $randomString = ''; |
| 491 | + // Ensure each character type is included at least once |
| 492 | + foreach ($charSets as $set) { |
| 493 | + $randomString .= $set[random_int(0, strlen($set) - 1)]; |
482 | 494 | }
|
483 |
| - for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) { |
484 |
| - $str .= $chars[mt_rand(0, $lc)]; |
| 495 | + |
| 496 | + $allCharacters = implode('', $charSets); |
| 497 | + for ($i = count($charSets); $i < $len; $i++) { |
| 498 | + $randomString .= $allCharacters[random_int(0, strlen($allCharacters) - 1)]; |
485 | 499 | }
|
486 |
| - return $str; |
| 500 | + |
| 501 | + return str_shuffle($randomString); |
487 | 502 | }
|
488 | 503 |
|
489 | 504 | /**
|
|
0 commit comments