Skip to content

Commit b6a76c5

Browse files
Merge pull request #162 from gigya/GMSP-63---GConnector-Password-Generation-for-Social-Login
Fixing issue with password generation
2 parents c098c79 + a9d7daa commit b6a76c5

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

Helper/GigyaMageHelper.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Gigya\GigyaIM\Model\Config;
1919
use Gigya\GigyaIM\Model\SettingsFactory;
2020
use Gigya\GigyaIM\Encryption\Encryptor;
21+
use InvalidArgumentException;
2122
use Magento\Customer\Api\Data\CustomerInterface;
2223
use Magento\Framework\App\ObjectManager;
2324
use Magento\Store\Model\ScopeInterface;
@@ -459,31 +460,45 @@ public function verifyGigyaRequiredFields($gigya_user_account): array
459460
return $message;
460461
}
461462

463+
/**
464+
* @throws Exception
465+
*/
462466
public function generatePassword($len = 8): string
463467
{
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);
469469
return 'Gigya_' . $str;
470470
}
471471

472472
/**
473-
* Taken from magento 1 helper core
474473
* @param $len
475-
* @param $chars
476474
* @return mixed
475+
* @throws Exception
477476
*/
478-
private function getRandomString($len, $chars): mixed
477+
private function getRandomString($len): mixed
479478
{
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)];
482494
}
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)];
485499
}
486-
return $str;
500+
501+
return str_shuffle($randomString);
487502
}
488503

489504
/**

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "gigya/magento2-im",
33
"description": "Gigya Identity Management for Magento 2",
44
"type": "magento2-module",
5-
"version": "6.3.0",
5+
"version": "6.3.1",
66
"license": [
77
"OSL-3.0",
88
"AFL-3.0"

0 commit comments

Comments
 (0)