mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-04-16 12:48:32 +02:00
Add command to view 2FA recovery codes (#173)
This commit is contained in:
parent
83918b62f3
commit
0762a2441c
24
app/Console/Commands/AsksForUser.php
Normal file
24
app/Console/Commands/AsksForUser.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
trait AsksForUser
|
||||
{
|
||||
/** @var User */
|
||||
protected $user;
|
||||
|
||||
protected function askForUserEmail(): void
|
||||
{
|
||||
do {
|
||||
$email = $this->ask('Please enter the user email address');
|
||||
|
||||
$this->user = User::where('email', $email)->first();
|
||||
|
||||
if (empty($this->user)) {
|
||||
$this->warn('A user with this email address could not be found!');
|
||||
}
|
||||
} while (empty($this->user));
|
||||
}
|
||||
}
|
@ -13,10 +13,9 @@ use Illuminate\Support\Facades\Validator;
|
||||
*/
|
||||
class ResetPasswordCommand extends Command
|
||||
{
|
||||
protected $signature = 'reset-password';
|
||||
use AsksForUser;
|
||||
|
||||
/** @var User */
|
||||
protected $user;
|
||||
protected $signature = 'reset-password';
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
@ -26,19 +25,6 @@ class ResetPasswordCommand extends Command
|
||||
$this->resetUserPassword();
|
||||
}
|
||||
|
||||
protected function askForUserEmail()
|
||||
{
|
||||
do {
|
||||
$email = $this->ask('Please enter the user email address');
|
||||
|
||||
$this->user = User::where('email', $email)->first();
|
||||
|
||||
if (empty($this->user)) {
|
||||
$this->warn('A user with this email address could not be found!');
|
||||
}
|
||||
} while (empty($this->user));
|
||||
}
|
||||
|
||||
protected function resetUserPassword()
|
||||
{
|
||||
do {
|
||||
|
42
app/Console/Commands/ViewRecoveryCodesCommand.php
Normal file
42
app/Console/Commands/ViewRecoveryCodesCommand.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
/**
|
||||
* Class ViewRecoveryCodesCommand
|
||||
*
|
||||
* @package App\Console\Commands
|
||||
*/
|
||||
class ViewRecoveryCodesCommand extends Command
|
||||
{
|
||||
use AsksForUser;
|
||||
|
||||
protected $signature = '2fa:view-recovery-codes';
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$this->line('This tool allows you to view the 2FA recovery codes for any user.');
|
||||
|
||||
$this->askForUserEmail();
|
||||
$this->viewBackupCodes();
|
||||
}
|
||||
|
||||
protected function viewBackupCodes(): void
|
||||
{
|
||||
if (empty($this->user->two_factor_recovery_codes)) {
|
||||
$this->warn('Two Factor Authentication is not enabled for this user.');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->info('Recovery Codes for user ' . $this->user->name .':');
|
||||
|
||||
$recoveryCodes = json_decode(decrypt($this->user->two_factor_recovery_codes), true);
|
||||
foreach ($recoveryCodes as $code) {
|
||||
$this->line($code);
|
||||
}
|
||||
}
|
||||
}
|
40
tests/Commands/ViewRecoveryCodesCommandTest.php
Normal file
40
tests/Commands/ViewRecoveryCodesCommandTest.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ViewRecoveryCodesCommandTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function testCommandWith2FaDisabled(): void
|
||||
{
|
||||
User::factory()->create(['email' => 'test@linkace.org']);
|
||||
|
||||
$this->artisan('2fa:view-recovery-codes')
|
||||
->expectsQuestion('Please enter the user email address', 'wrong@linkace.org')
|
||||
->expectsOutput('A user with this email address could not be found!')
|
||||
->expectsQuestion('Please enter the user email address', 'test@linkace.org')
|
||||
->expectsOutput('Two Factor Authentication is not enabled for this user.')
|
||||
->assertExitCode(0);
|
||||
}
|
||||
|
||||
public function testCommand(): void
|
||||
{
|
||||
$user = User::factory()->create(['email' => 'test@linkace.org']);
|
||||
|
||||
$user->two_factor_recovery_codes = encrypt(json_encode(['test-recovery-code']));
|
||||
$user->save();
|
||||
|
||||
$this->artisan('2fa:view-recovery-codes')
|
||||
->expectsQuestion('Please enter the user email address', 'wrong@linkace.org')
|
||||
->expectsOutput('A user with this email address could not be found!')
|
||||
->expectsQuestion('Please enter the user email address', 'test@linkace.org')
|
||||
->expectsOutput('Recovery Codes for user ' . $user->name . ':')
|
||||
->expectsOutput('test-recovery-code')
|
||||
->assertExitCode(0);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user