Skip to content

Commit 5415c0b

Browse files
authored
Refactor password generator (#73)
1 parent 53175cc commit 5415c0b

3 files changed

Lines changed: 48 additions & 20 deletions

File tree

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
use Illuminate\Console\Command;
66
use Symfony\Component\Console\Attribute\AsCommand;
77

8-
#[AsCommand('very-basic-auth:password-generate')]
9-
class PasswordGenerateCommand extends Command
8+
#[AsCommand('very-basic-auth:generate-password')]
9+
class GeneratePassword extends Command
1010
{
1111
/**
1212
* The name and signature of the console command.
1313
*
1414
* @var string
1515
*/
16-
protected $signature = 'very-basic-auth:password-generate';
16+
protected $signature = 'very-basic-auth:generate-password';
1717

1818
/**
1919
* The console command description.
@@ -29,31 +29,59 @@ class PasswordGenerateCommand extends Command
2929
*/
3030
public function handle(): int
3131
{
32-
$approved = false;
32+
$password = $this->promptForValidPassword();
3333

34-
do {
35-
$password = $this->secret('Please enter a password for the very basic auth');
34+
if ($this->writeNewEnvironmentFileWith($this->hashPassword($password))) {
35+
$this->info('The password has been set successfully.');
36+
}
37+
38+
return static::SUCCESS;
39+
}
40+
41+
/**
42+
* Keep asking for a password until a valid one is provided.
43+
*/
44+
protected function promptForValidPassword(): string
45+
{
46+
while (true) {
47+
$password = (string) $this->secret('Please enter a password for the very basic auth');
3648

37-
if (empty($password) || strlen($password) < 8) {
49+
if (!$this->isPasswordAccepted($password)) {
3850
$this->error('The password must be at least 8 characters.');
3951
continue;
4052
}
4153

42-
$confirmation = $this->secret('Please confirm your password');
43-
44-
if ($password !== $confirmation) {
54+
if (!$this->passwordsMatch($password)) {
4555
$this->error('The passwords do not match. Please try again.');
46-
$approved = false;
47-
} else {
48-
$approved = true;
56+
continue;
4957
}
50-
} while (!$approved);
5158

52-
if ($this->writeNewEnvironmentFileWith(app()->make('hash')->make($password))) {
53-
$this->info('The password has been set successfully.');
59+
return $password;
5460
}
61+
}
5562

56-
return static::SUCCESS;
63+
/**
64+
* Determine if the provided password fulfills minimum requirements.
65+
*/
66+
protected function isPasswordAccepted(?string $password): bool
67+
{
68+
return !empty($password) && strlen($password) >= 8;
69+
}
70+
71+
/**
72+
* Ask for confirmation and ensure it matches.
73+
*/
74+
protected function passwordsMatch(string $password): bool
75+
{
76+
return $password === (string) $this->secret('Please confirm your password');
77+
}
78+
79+
/**
80+
* Hash the password using Laravel's configured hasher.
81+
*/
82+
protected function hashPassword(string $password): string
83+
{
84+
return app()->make('hash')->make($password);
5785
}
5886

5987
/**

src/VeryBasicAuthServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Olssonm\VeryBasicAuth;
44

55
use Illuminate\Support\ServiceProvider;
6-
use Olssonm\VeryBasicAuth\Console\PasswordGenerateCommand;
6+
use Olssonm\VeryBasicAuth\Console\GeneratePassword;
77
use Olssonm\VeryBasicAuth\Handlers\DefaultResponseHandler;
88
use Olssonm\VeryBasicAuth\Handlers\ResponseHandler;
99

@@ -57,7 +57,7 @@ public function boot(\Illuminate\Routing\Router $router)
5757
// Register commands
5858
if ($this->app->runningInConsole()) {
5959
$this->commands([
60-
PasswordGenerateCommand::class,
60+
GeneratePassword::class,
6161
]);
6262
}
6363
}

tests/VeryBasicAuthTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
$password = 'password' . uniqid();
196196

197197
// Simulate user input for the console command
198-
$this->artisan('very-basic-auth:password-generate')
198+
$this->artisan('very-basic-auth:generate-password')
199199
->expectsQuestion('Please enter a password for the very basic auth', $password)
200200
->expectsQuestion('Please confirm your password', $password)
201201
->assertExitCode(0);

0 commit comments

Comments
 (0)