5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\Mfa\MfaValue;
7 use BookStack\Auth\User;
8 use PragmaRX\Google2FA\Google2FA;
11 class MfaConfigurationTest extends TestCase
13 public function test_totp_setup()
15 $editor = $this->getEditor();
16 $this->assertDatabaseMissing('mfa_values', ['user_id' => $editor->id]);
19 $resp = $this->actingAs($editor)->get('/mfa/setup');
20 $resp->assertElementContains('a[href$="/mfa/totp/generate"]', 'Setup');
22 // Generate page access
23 $resp = $this->get('/mfa/totp/generate');
24 $resp->assertSee('Mobile App Setup');
25 $resp->assertSee('Verify Setup');
26 $resp->assertElementExists('form[action$="/mfa/totp/confirm"] button');
27 $this->assertSessionHas('mfa-setup-totp-secret');
28 $svg = $resp->getElementHtml('#main-content .card svg');
30 // Validation error, code should remain the same
31 $resp = $this->post('/mfa/totp/confirm', [
34 $resp->assertRedirect('/mfa/totp/generate');
35 $resp = $this->followRedirects($resp);
36 $resp->assertSee('The provided code is not valid or has expired.');
37 $revisitSvg = $resp->getElementHtml('#main-content .card svg');
38 $this->assertTrue($svg === $revisitSvg);
39 $secret = decrypt(session()->get('mfa-setup-totp-secret'));
41 $resp->assertSee(htmlentities("?secret={$secret}&issuer=BookStack&algorithm=SHA1&digits=6&period=30"));
43 // Successful confirmation
44 $google2fa = new Google2FA();
45 $otp = $google2fa->getCurrentOtp($secret);
46 $resp = $this->post('/mfa/totp/confirm', [
49 $resp->assertRedirect('/mfa/setup');
51 // Confirmation of setup
52 $resp = $this->followRedirects($resp);
53 $resp->assertSee('Multi-factor method successfully configured');
54 $resp->assertElementContains('a[href$="/mfa/totp/generate"]', 'Reconfigure');
56 $this->assertDatabaseHas('mfa_values', [
57 'user_id' => $editor->id,
60 $this->assertFalse(session()->has('mfa-setup-totp-secret'));
61 $value = MfaValue::query()->where('user_id', '=', $editor->id)
62 ->where('method', '=', 'totp')->first();
63 $this->assertEquals($secret, decrypt($value->value));
66 public function test_backup_codes_setup()
68 $editor = $this->getEditor();
69 $this->assertDatabaseMissing('mfa_values', ['user_id' => $editor->id]);
72 $resp = $this->actingAs($editor)->get('/mfa/setup');
73 $resp->assertElementContains('a[href$="/mfa/backup_codes/generate"]', 'Setup');
75 // Generate page access
76 $resp = $this->get('/mfa/backup_codes/generate');
77 $resp->assertSee('Backup Codes');
78 $resp->assertElementContains('form[action$="/mfa/backup_codes/confirm"]', 'Confirm and Enable');
79 $this->assertSessionHas('mfa-setup-backup-codes');
80 $codes = decrypt(session()->get('mfa-setup-backup-codes'));
82 $this->assertCount(16, $codes);
83 $this->assertEquals(16 * 11, strlen(implode('', $codes)));
84 // Check download link
85 $resp->assertSee(base64_encode(implode("\n\n", $codes)));
88 $resp = $this->post('/mfa/backup_codes/confirm');
89 $resp->assertRedirect('/mfa/setup');
91 // Confirmation of setup
92 $resp = $this->followRedirects($resp);
93 $resp->assertSee('Multi-factor method successfully configured');
94 $resp->assertElementContains('a[href$="/mfa/backup_codes/generate"]', 'Reconfigure');
96 $this->assertDatabaseHas('mfa_values', [
97 'user_id' => $editor->id,
98 'method' => 'backup_codes',
100 $this->assertFalse(session()->has('mfa-setup-backup-codes'));
101 $value = MfaValue::query()->where('user_id', '=', $editor->id)
102 ->where('method', '=', 'backup_codes')->first();
103 $this->assertEquals($codes, json_decode(decrypt($value->value)));
106 public function test_backup_codes_cannot_be_confirmed_if_not_previously_generated()
108 $resp = $this->asEditor()->post('/mfa/backup_codes/confirm');
109 $resp->assertStatus(500);
112 public function test_mfa_method_count_is_visible_on_user_edit_page()
114 $user = $this->getEditor();
115 $resp = $this->actingAs($this->getAdmin())->get($user->getEditUrl());
116 $resp->assertSee('0 methods configured');
118 MfaValue::upsertWithValue($user, MfaValue::METHOD_TOTP, 'test');
119 $resp = $this->get($user->getEditUrl());
120 $resp->assertSee('1 method configured');
122 MfaValue::upsertWithValue($user, MfaValue::METHOD_BACKUP_CODES, 'test');
123 $resp = $this->get($user->getEditUrl());
124 $resp->assertSee('2 methods configured');
127 public function test_mfa_setup_link_only_shown_when_viewing_own_user_edit_page()
129 $admin = $this->getAdmin();
130 $resp = $this->actingAs($admin)->get($admin->getEditUrl());
131 $resp->assertElementExists('a[href$="/mfa/setup"]');
133 $resp = $this->actingAs($admin)->get($this->getEditor()->getEditUrl());
134 $resp->assertElementNotExists('a[href$="/mfa/setup"]');
137 public function test_mfa_indicator_shows_in_user_list()
139 $admin = $this->getAdmin();
140 User::query()->where('id', '!=', $admin->id)->delete();
142 $resp = $this->actingAs($admin)->get('/settings/users');
143 $resp->assertElementNotExists('[title="MFA Configured"] svg');
145 MfaValue::upsertWithValue($admin, MfaValue::METHOD_TOTP, 'test');
146 $resp = $this->actingAs($admin)->get('/settings/users');
147 $resp->assertElementExists('[title="MFA Configured"] svg');
150 public function test_remove_mfa_method()
152 $admin = $this->getAdmin();
154 MfaValue::upsertWithValue($admin, MfaValue::METHOD_TOTP, 'test');
155 $this->assertEquals(1, $admin->mfaValues()->count());
156 $resp = $this->actingAs($admin)->get('/mfa/setup');
157 $resp->assertElementExists('form[action$="/mfa/totp/remove"]');
159 $resp = $this->delete('/mfa/totp/remove');
160 $resp->assertRedirect('/mfa/setup');
161 $resp = $this->followRedirects($resp);
162 $resp->assertSee('Multi-factor method successfully removed');
164 $this->assertActivityExists(ActivityType::MFA_REMOVE_METHOD);
165 $this->assertEquals(0, $admin->mfaValues()->count());