From f954e1c3e0509dcbf39e9f9b057f9b18c2995742 Mon Sep 17 00:00:00 2001 From: Craig Smith <952595+phpsa@users.noreply.github.com> Date: Thu, 10 Aug 2023 10:30:56 +1200 Subject: [PATCH] fix: imporove password generation --- resources/views/password.blade.php | 24 ++++++++++++++++++------ src/Password.php | 21 --------------------- src/Traits/CanGenerate.php | 3 ++- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/resources/views/password.blade.php b/resources/views/password.blade.php index 841b498..f99575d 100644 --- a/resources/views/password.blade.php +++ b/resources/views/password.blade.php @@ -14,7 +14,6 @@ $suffixIcon = $getSuffixIcon(); $suffixLabel = $getSuffixLabel(); $statePath = $getStatePath(); - $isClearText = ! $extraAlpineAttributes['show']; $stylecode = ''; $x = 0; @@ -26,13 +25,26 @@ $stylecode = 'isRtl ? \'padding-left: '.$x.'rem\' : \'padding-right: '.$x.'rem\''; } - $generatePassword = 'let chars = \''. $getPasswChars().'\'; - let password = \'\'; - for (let i = 0; i < '. $getPasswLength() . '; i++) { - password += chars.charAt(Math.floor(Math.random() * chars.length)); + $generatePassword = ' + let chars = \''. $getPasswChars().'\'; + let minLen = '. $getPasswLength(). '; + + + let password = []; + while(password.length <= minLen){ + password.push(chars.charAt(Math.floor(Math.random() * 26))); + password.push(chars.charAt(Math.floor(Math.random() * 26) +26)); + if(chars.length > 52 && password.length > 4){ + password.push(chars.charAt(Math.floor(Math.random() * 10) +52)); + } + if(chars.length > 62 && password.length > 4){ + password.push(chars.charAt(Math.floor(Math.random() * 10) +62)); + } } + password = password.slice(0, minLen).sort(() => Math.random() - 0.5).join(\'\') + $wire.set(\'' . $getStatePath() . '\', password); - ;'; + '; if($shouldNotifyOnGenerate()){ $generatePassword .= 'new FilamentNotification() .title(\'' . $getGenerateText() . '\') diff --git a/src/Password.php b/src/Password.php index 1f985e8..a0501ae 100644 --- a/src/Password.php +++ b/src/Password.php @@ -13,10 +13,6 @@ class Password extends TextInput use CanCopy; use CanGenerate; - protected array $extraAlpineAttributes = [ - ['show' => false] - ]; - protected string $view = 'filament-password-reveal::password'; protected string $showIcon = 'heroicon-o-eye'; @@ -27,24 +23,7 @@ class Password extends TextInput protected bool|Closure $initiallyHidden = true; - public ?bool $revealPassword = null; - - public function getIsRevealed(): bool - { - if ($this->isRevealable() === false) { - return false; - } - if ($this->revealPassword === null) { - $this->revealPassword = ! $this->isInitiallyHidden(); - } - - return $this->revealPassword; - } - public function toggleReveal(): void - { - $this->revealPassword = ! $this->revealPassword; - } public function revealable(bool|Closure $condition = true): static { diff --git a/src/Traits/CanGenerate.php b/src/Traits/CanGenerate.php index 64fada3..911bb17 100644 --- a/src/Traits/CanGenerate.php +++ b/src/Traits/CanGenerate.php @@ -96,7 +96,8 @@ public function getGenerateText(): string return $this->evaluate($this->generateText ?? __('Password generated')); } - public function shouldNotifyOnGenerate(): bool { + public function shouldNotifyOnGenerate(): bool + { return $this->evaluate($this->notifyOnGenerate); } }