2021-07-02 14:51:30 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Auth;
|
|
|
|
|
2021-07-14 16:27:21 -04:00
|
|
|
use BookStack\Actions\ActivityType;
|
2021-07-02 15:53:33 -04:00
|
|
|
use BookStack\Auth\Access\Mfa\MfaValue;
|
2021-10-14 13:02:16 -04:00
|
|
|
use BookStack\Auth\Role;
|
2021-07-14 15:18:48 -04:00
|
|
|
use BookStack\Auth\User;
|
2021-07-02 14:51:30 -04:00
|
|
|
use PragmaRX\Google2FA\Google2FA;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class MfaConfigurationTest extends TestCase
|
|
|
|
{
|
|
|
|
public function test_totp_setup()
|
|
|
|
{
|
|
|
|
$editor = $this->getEditor();
|
|
|
|
$this->assertDatabaseMissing('mfa_values', ['user_id' => $editor->id]);
|
|
|
|
|
|
|
|
// Setup page state
|
|
|
|
$resp = $this->actingAs($editor)->get('/mfa/setup');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('a[href$="/mfa/totp/generate"]', 'Setup');
|
2021-07-02 14:51:30 -04:00
|
|
|
|
|
|
|
// Generate page access
|
2021-08-02 17:02:25 -04:00
|
|
|
$resp = $this->get('/mfa/totp/generate');
|
2021-07-02 14:51:30 -04:00
|
|
|
$resp->assertSee('Mobile App Setup');
|
|
|
|
$resp->assertSee('Verify Setup');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('form[action$="/mfa/totp/confirm"] button');
|
2021-07-02 14:51:30 -04:00
|
|
|
$this->assertSessionHas('mfa-setup-totp-secret');
|
2022-07-23 10:10:18 -04:00
|
|
|
$svg = $this->withHtml($resp)->getOuterHtml('#main-content .card svg');
|
2021-07-02 14:51:30 -04:00
|
|
|
|
|
|
|
// Validation error, code should remain the same
|
2021-08-02 17:02:25 -04:00
|
|
|
$resp = $this->post('/mfa/totp/confirm', [
|
2021-07-02 14:51:30 -04:00
|
|
|
'code' => 'abc123',
|
|
|
|
]);
|
2021-08-02 17:02:25 -04:00
|
|
|
$resp->assertRedirect('/mfa/totp/generate');
|
2021-07-02 14:51:30 -04:00
|
|
|
$resp = $this->followRedirects($resp);
|
|
|
|
$resp->assertSee('The provided code is not valid or has expired.');
|
2022-07-23 10:10:18 -04:00
|
|
|
$revisitSvg = $this->withHtml($resp)->getOuterHtml('#main-content .card svg');
|
2021-07-02 14:51:30 -04:00
|
|
|
$this->assertTrue($svg === $revisitSvg);
|
2021-09-01 15:58:19 -04:00
|
|
|
$secret = decrypt(session()->get('mfa-setup-totp-secret'));
|
|
|
|
|
2021-10-26 17:04:18 -04:00
|
|
|
$resp->assertSee("?secret={$secret}&issuer=BookStack&algorithm=SHA1&digits=6&period=30");
|
2021-07-02 14:51:30 -04:00
|
|
|
|
|
|
|
// Successful confirmation
|
|
|
|
$google2fa = new Google2FA();
|
2021-07-02 15:53:33 -04:00
|
|
|
$otp = $google2fa->getCurrentOtp($secret);
|
2021-08-02 17:02:25 -04:00
|
|
|
$resp = $this->post('/mfa/totp/confirm', [
|
2021-07-02 14:51:30 -04:00
|
|
|
'code' => $otp,
|
|
|
|
]);
|
|
|
|
$resp->assertRedirect('/mfa/setup');
|
|
|
|
|
|
|
|
// Confirmation of setup
|
|
|
|
$resp = $this->followRedirects($resp);
|
|
|
|
$resp->assertSee('Multi-factor method successfully configured');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('a[href$="/mfa/totp/generate"]', 'Reconfigure');
|
2021-07-02 15:53:33 -04:00
|
|
|
|
|
|
|
$this->assertDatabaseHas('mfa_values', [
|
|
|
|
'user_id' => $editor->id,
|
2021-08-21 10:49:40 -04:00
|
|
|
'method' => 'totp',
|
2021-07-02 15:53:33 -04:00
|
|
|
]);
|
|
|
|
$this->assertFalse(session()->has('mfa-setup-totp-secret'));
|
|
|
|
$value = MfaValue::query()->where('user_id', '=', $editor->id)
|
|
|
|
->where('method', '=', 'totp')->first();
|
|
|
|
$this->assertEquals($secret, decrypt($value->value));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_backup_codes_setup()
|
|
|
|
{
|
|
|
|
$editor = $this->getEditor();
|
|
|
|
$this->assertDatabaseMissing('mfa_values', ['user_id' => $editor->id]);
|
|
|
|
|
|
|
|
// Setup page state
|
|
|
|
$resp = $this->actingAs($editor)->get('/mfa/setup');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('a[href$="/mfa/backup_codes/generate"]', 'Setup');
|
2021-07-02 15:53:33 -04:00
|
|
|
|
|
|
|
// Generate page access
|
2021-08-02 17:02:25 -04:00
|
|
|
$resp = $this->get('/mfa/backup_codes/generate');
|
2021-07-02 15:53:33 -04:00
|
|
|
$resp->assertSee('Backup Codes');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('form[action$="/mfa/backup_codes/confirm"]', 'Confirm and Enable');
|
2021-07-02 15:53:33 -04:00
|
|
|
$this->assertSessionHas('mfa-setup-backup-codes');
|
|
|
|
$codes = decrypt(session()->get('mfa-setup-backup-codes'));
|
|
|
|
// Check code format
|
|
|
|
$this->assertCount(16, $codes);
|
2021-08-21 10:49:40 -04:00
|
|
|
$this->assertEquals(16 * 11, strlen(implode('', $codes)));
|
2021-07-02 15:53:33 -04:00
|
|
|
// Check download link
|
|
|
|
$resp->assertSee(base64_encode(implode("\n\n", $codes)));
|
|
|
|
|
|
|
|
// Confirm submit
|
2021-08-02 17:02:25 -04:00
|
|
|
$resp = $this->post('/mfa/backup_codes/confirm');
|
2021-07-02 15:53:33 -04:00
|
|
|
$resp->assertRedirect('/mfa/setup');
|
|
|
|
|
|
|
|
// Confirmation of setup
|
|
|
|
$resp = $this->followRedirects($resp);
|
|
|
|
$resp->assertSee('Multi-factor method successfully configured');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('a[href$="/mfa/backup_codes/generate"]', 'Reconfigure');
|
2021-07-02 15:53:33 -04:00
|
|
|
|
|
|
|
$this->assertDatabaseHas('mfa_values', [
|
|
|
|
'user_id' => $editor->id,
|
2021-08-21 10:49:40 -04:00
|
|
|
'method' => 'backup_codes',
|
2021-07-02 15:53:33 -04:00
|
|
|
]);
|
|
|
|
$this->assertFalse(session()->has('mfa-setup-backup-codes'));
|
|
|
|
$value = MfaValue::query()->where('user_id', '=', $editor->id)
|
|
|
|
->where('method', '=', 'backup_codes')->first();
|
|
|
|
$this->assertEquals($codes, json_decode(decrypt($value->value)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_backup_codes_cannot_be_confirmed_if_not_previously_generated()
|
|
|
|
{
|
2021-08-02 17:02:25 -04:00
|
|
|
$resp = $this->asEditor()->post('/mfa/backup_codes/confirm');
|
2021-07-02 15:53:33 -04:00
|
|
|
$resp->assertStatus(500);
|
2021-07-02 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 15:06:41 -04:00
|
|
|
public function test_mfa_method_count_is_visible_on_user_edit_page()
|
|
|
|
{
|
2021-07-14 15:18:48 -04:00
|
|
|
$user = $this->getEditor();
|
|
|
|
$resp = $this->actingAs($this->getAdmin())->get($user->getEditUrl());
|
2021-07-14 15:06:41 -04:00
|
|
|
$resp->assertSee('0 methods configured');
|
|
|
|
|
2021-07-14 15:18:48 -04:00
|
|
|
MfaValue::upsertWithValue($user, MfaValue::METHOD_TOTP, 'test');
|
|
|
|
$resp = $this->get($user->getEditUrl());
|
2021-07-14 15:06:41 -04:00
|
|
|
$resp->assertSee('1 method configured');
|
|
|
|
|
2021-07-14 15:18:48 -04:00
|
|
|
MfaValue::upsertWithValue($user, MfaValue::METHOD_BACKUP_CODES, 'test');
|
|
|
|
$resp = $this->get($user->getEditUrl());
|
2021-07-14 15:06:41 -04:00
|
|
|
$resp->assertSee('2 methods configured');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_mfa_setup_link_only_shown_when_viewing_own_user_edit_page()
|
|
|
|
{
|
|
|
|
$admin = $this->getAdmin();
|
|
|
|
$resp = $this->actingAs($admin)->get($admin->getEditUrl());
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('a[href$="/mfa/setup"]');
|
2021-07-14 15:06:41 -04:00
|
|
|
|
|
|
|
$resp = $this->actingAs($admin)->get($this->getEditor()->getEditUrl());
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementNotExists('a[href$="/mfa/setup"]');
|
2021-07-14 15:06:41 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 15:18:48 -04:00
|
|
|
public function test_mfa_indicator_shows_in_user_list()
|
|
|
|
{
|
|
|
|
$admin = $this->getAdmin();
|
|
|
|
User::query()->where('id', '!=', $admin->id)->delete();
|
|
|
|
|
|
|
|
$resp = $this->actingAs($admin)->get('/settings/users');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementNotExists('[title="MFA Configured"] svg');
|
2021-07-14 15:18:48 -04:00
|
|
|
|
|
|
|
MfaValue::upsertWithValue($admin, MfaValue::METHOD_TOTP, 'test');
|
|
|
|
$resp = $this->actingAs($admin)->get('/settings/users');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('[title="MFA Configured"] svg');
|
2021-07-14 15:18:48 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 16:27:21 -04:00
|
|
|
public function test_remove_mfa_method()
|
|
|
|
{
|
|
|
|
$admin = $this->getAdmin();
|
|
|
|
|
|
|
|
MfaValue::upsertWithValue($admin, MfaValue::METHOD_TOTP, 'test');
|
|
|
|
$this->assertEquals(1, $admin->mfaValues()->count());
|
|
|
|
$resp = $this->actingAs($admin)->get('/mfa/setup');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('form[action$="/mfa/totp/remove"]');
|
2021-07-14 16:27:21 -04:00
|
|
|
|
2021-08-21 10:49:40 -04:00
|
|
|
$resp = $this->delete('/mfa/totp/remove');
|
|
|
|
$resp->assertRedirect('/mfa/setup');
|
2021-07-14 16:27:21 -04:00
|
|
|
$resp = $this->followRedirects($resp);
|
|
|
|
$resp->assertSee('Multi-factor method successfully removed');
|
|
|
|
|
|
|
|
$this->assertActivityExists(ActivityType::MFA_REMOVE_METHOD);
|
|
|
|
$this->assertEquals(0, $admin->mfaValues()->count());
|
|
|
|
}
|
2021-10-14 13:02:16 -04:00
|
|
|
|
|
|
|
public function test_totp_setup_url_shows_correct_user_when_setup_forced_upon_login()
|
|
|
|
{
|
|
|
|
$admin = $this->getAdmin();
|
|
|
|
/** @var Role $role */
|
|
|
|
$role = $admin->roles()->first();
|
|
|
|
$role->mfa_enforced = true;
|
|
|
|
$role->save();
|
|
|
|
|
|
|
|
$resp = $this->post('/login', ['email' => $admin->email, 'password' => 'password']);
|
|
|
|
$this->assertFalse(auth()->check());
|
|
|
|
$resp->assertRedirect('/mfa/verify');
|
|
|
|
|
|
|
|
$resp = $this->get('/mfa/totp/generate');
|
|
|
|
$resp->assertSeeText('Mobile App Setup');
|
2021-10-26 17:04:18 -04:00
|
|
|
$resp->assertDontSee('otpauth://totp/BookStack:guest%40example.com', false);
|
|
|
|
$resp->assertSee('otpauth://totp/BookStack:admin%40admin.com', false);
|
2021-10-14 13:02:16 -04:00
|
|
|
}
|
2021-08-21 10:49:40 -04:00
|
|
|
}
|