Skip to content

Commit 54d7b38

Browse files
crynobonetaylorotwellStyleCIBot
authored andcommitted
[10.x] Fixes Str::password() does not always generate password with numbers (laravel#48681)
* wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * formatting * Apply fixes from StyleCI --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: Taylor Otwell <taylor@laravel.com> Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 31d8346 commit 54d7b38

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/Illuminate/Support/Str.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -841,25 +841,33 @@ public static function pluralStudly($value, $count = 2)
841841
*/
842842
public static function password($length = 32, $letters = true, $numbers = true, $symbols = true, $spaces = false)
843843
{
844-
return (new Collection)
845-
->when($letters, fn ($c) => $c->merge([
846-
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
847-
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
848-
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
849-
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
850-
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
851-
]))
852-
->when($numbers, fn ($c) => $c->merge([
853-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
854-
]))
855-
->when($symbols, fn ($c) => $c->merge([
856-
'~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '-',
857-
'_', '.', ',', '<', '>', '?', '/', '\\', '{', '}', '[',
858-
']', '|', ':', ';',
859-
]))
860-
->when($spaces, fn ($c) => $c->merge([' ']))
861-
->pipe(fn ($c) => Collection::times($length, fn () => $c[random_int(0, $c->count() - 1)]))
862-
->implode('');
844+
$password = new Collection();
845+
846+
$options = (new Collection([
847+
'letters' => $letters === true ? [
848+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
849+
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
850+
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
851+
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
852+
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
853+
] : null,
854+
'numbers' => $numbers === true ? [
855+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
856+
] : null,
857+
'symbols' => $symbols === true ? [
858+
'~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '-',
859+
'_', '.', ',', '<', '>', '?', '/', '\\', '{', '}', '[',
860+
']', '|', ':', ';',
861+
] : null,
862+
'spaces' => $spaces === true ? [' '] : null,
863+
]))->filter()->each(fn ($c) => $password->push($c[random_int(0, count($c) - 1)])
864+
)->flatten();
865+
866+
$length = $length - $password->count();
867+
868+
return $password->merge($options->pipe(
869+
fn ($c) => Collection::times($length, fn () => $c[random_int(0, $c->count() - 1)])
870+
))->shuffle()->implode('');
863871
}
864872

865873
/**

tests/Support/SupportStrTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,13 @@ public function testItCanSpecifyAFallbackForAUlidSequence()
12881288
public function testPasswordCreation()
12891289
{
12901290
$this->assertTrue(strlen(Str::password()) === 32);
1291+
1292+
$this->assertStringNotContainsString(' ', Str::password());
1293+
$this->assertStringContainsString(' ', Str::password(spaces: true));
1294+
1295+
$this->assertTrue(
1296+
Str::of(Str::password())->contains(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
1297+
);
12911298
}
12921299
}
12931300

0 commit comments

Comments
 (0)