1
0
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:
Kovah 2021-01-10 18:18:15 +01:00
parent 83918b62f3
commit 0762a2441c
No known key found for this signature in database
GPG Key ID: AAAA031BA9830D7B
4 changed files with 108 additions and 16 deletions

View 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));
}
}

View File

@ -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 {

View 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);
}
}
}

View 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);
}
}