Skip to content

Commit dcdcecd

Browse files
author
csavelief
committed
user-friendly passwords masking
1 parent b2e9866 commit dcdcecd

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

app/Listeners/EndVulnsScanListener.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function sendEmailReport(YnhTrial $trial): void
5454
return [
5555
'email' => Str::trim(Str::before($line, "\t")),
5656
'website' => Str::trim(Str::between($line, "\t", "\t")),
57-
'password' => $this->maskPassword(Str::trim(Str::afterLast($line, "\t"))),
57+
'password' => self::maskPassword(Str::trim(Str::afterLast($line, "\t"))),
5858
];
5959
})
6060
->map(function (array $credentials) {
@@ -219,12 +219,15 @@ public static function sendEmail(string $from, string $to, string $subject, stri
219219
return [];
220220
}
221221

222-
private static function maskPassword(string $password): string
222+
private static function maskPassword(string $password, int $size = 3): string
223223
{
224224
if (Str::length($password) <= 2) {
225225
return Str::repeat('*', Str::length($password));
226226
}
227-
return Str::substr($password, 0, 1) . Str::repeat('*', Str::length($password) - 2) . Str::substr($password, -1, 1);
227+
if (Str::length($password) <= 2 * $size) {
228+
return self::maskPassword($password, 1);
229+
}
230+
return Str::substr($password, 0, $size) . Str::repeat('*', Str::length($password) - 2 * $size) . Str::substr($password, -$size, $size);
228231
}
229232

230233
public function viaQueue(): string

app/View/Components/Timeline.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,14 @@ private function events(User $user): array
540540
->toArray();
541541
}
542542

543-
private function maskPassword(string $password): string
543+
private function maskPassword(string $password, int $size = 3): string
544544
{
545545
if (Str::length($password) <= 2) {
546546
return Str::repeat('*', Str::length($password));
547547
}
548-
return Str::substr($password, 0, 1) . Str::repeat('*', Str::length($password) - 2) . Str::substr($password, -1, 1);
548+
if (Str::length($password) <= 2 * $size) {
549+
return $this->maskPassword($password, 1);
550+
}
551+
return Str::substr($password, 0, $size) . Str::repeat('*', Str::length($password) - 2 * $size) . Str::substr($password, -$size, $size);
549552
}
550553
}

0 commit comments

Comments
 (0)