Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions resources/js/components/two-factor/Setup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ onMounted(() => getSetupCode());
function getSetupCode() {
loading.value = true;

axios.post(props.enableUrl).then((response) => {
qrCode.value = response.data.qr;
secretKey.value = response.data.secret_key;
confirmUrl.value = response.data.confirm_url;
loading.value = false;
});
axios
.post(props.enableUrl)
.then((response) => {
qrCode.value = response.data.qr;
secretKey.value = response.data.secret_key;
confirmUrl.value = response.data.confirm_url;
})
.catch((error) => {
setupModalOpen.value = false;
Statamic.$toast.error(error.response?.data?.message ?? error.message);
})
.finally(() => {
loading.value = false;
});
}

function confirm() {
Expand Down
9 changes: 8 additions & 1 deletion resources/js/pages/auth/two-factor/Setup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Head from '@/pages/layout/Head.vue';
import Outside from '@/pages/layout/Outside.vue';
import TwoFactorSetup from '@/components/two-factor/Setup.vue';
import { requireElevatedSession } from '@/components/elevated-sessions';
import { AuthCard, Button } from '@ui';
import { ref } from 'vue';

Expand All @@ -11,6 +12,12 @@ const props = defineProps(['routes', 'redirect']);

const setupModalOpen = ref(false);

function openSetupModal() {
requireElevatedSession()
.then(() => (setupModalOpen.value = true))
.catch(() => Statamic.$toast.error(__('statamic::messages.elevated_session_required')));
}

function setupComplete() {
window.location.href = props.redirect;
}
Expand All @@ -24,7 +31,7 @@ function setupComplete() {
:title="__('Set up Two Factor Authentication')"
:description="__('statamic::messages.two_factor_account_requirement')"
>
<Button variant="primary" @click="setupModalOpen = true" :text="__('Set up')" class="w-full" />
<Button variant="primary" @click="openSetupModal" :text="__('Set up')" class="w-full" />

<TwoFactorSetup
v-if="setupModalOpen"
Expand Down
10 changes: 9 additions & 1 deletion resources/js/pages/layout/Outside.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ onMounted(() => {
</div>
</div>
<slot />
<PortalTargets />
<PortalTargets />

<Component
v-for="component in $root.appendedComponents"
:key="component.id"
:is="component.name"
v-bind="component.props"
v-on="component.events"
/>
</div>
</template>
12 changes: 7 additions & 5 deletions routes/cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,13 @@
Route::get('session-timeout', SessionTimeoutController::class)->name('session.timeout');

if (config('statamic.users.elevated_sessions_enabled')) {
Route::get('auth/confirm-password', [ElevatedSessionController::class, 'showForm'])->name('confirm-password');
Route::get('elevated-session', [ElevatedSessionController::class, 'status'])->name('elevated-session.status');
Route::get('elevated-session/passkey-options', [ElevatedSessionController::class, 'options'])->name('elevated-session.passkey-options')->middleware('throttle:statamic.cp.passkeys');
Route::post('elevated-session', [ElevatedSessionController::class, 'confirm'])->name('elevated-session.confirm')->middleware('throttle:statamic.cp.auth');
Route::get('elevated-session/resend-code', [ElevatedSessionController::class, 'resendCode'])->name('elevated-session.resend-code')->middleware('throttle:send-elevated-session-code');
Route::withoutMiddleware(RedirectIfTwoFactorSetupIncomplete::class)->group(function () {
Route::get('auth/confirm-password', [ElevatedSessionController::class, 'showForm'])->name('confirm-password');
Route::get('elevated-session', [ElevatedSessionController::class, 'status'])->name('elevated-session.status');
Route::get('elevated-session/passkey-options', [ElevatedSessionController::class, 'options'])->name('elevated-session.passkey-options')->middleware('throttle:statamic.cp.passkeys');
Route::post('elevated-session', [ElevatedSessionController::class, 'confirm'])->name('elevated-session.confirm')->middleware('throttle:statamic.cp.auth');
Route::get('elevated-session/resend-code', [ElevatedSessionController::class, 'resendCode'])->name('elevated-session.resend-code')->middleware('throttle:send-elevated-session-code');
});
}

Route::get('playground', PlaygroundController::class)->name('playground');
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/DisableTwoFactorAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function buttonText()

public function visibleTo($item)
{
return $item instanceof User && $item->hasEnabledTwoFactorAuthentication();
return $item instanceof User && ! is_null($item->two_factor_secret);
}

public function authorize($user, $item)
Expand Down
8 changes: 8 additions & 0 deletions tests/Actions/DisableTwoFactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function its_only_visible_for_users_with_two_factor_enabled()
$this->assertTrue((new Action)->visibleTo($userWith2fa));
}

#[Test]
public function its_visible_for_users_with_incomplete_two_factor_setup()
{
$userWithIncompleteSetup = User::make()->set('two_factor_secret', 'secret');

$this->assertTrue((new Action)->visibleTo($userWithIncompleteSetup));
}

#[Test]
public function it_does_not_disable_two_factor_if_current_user_doesnt_have_permission()
{
Expand Down
27 changes: 27 additions & 0 deletions tests/Auth/ElevatedSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,33 @@ public function starting_elevated_session_clears_stored_verification_code()
->assertSessionMissing('statamic_elevated_session_verification_code');
}

#[Test]
public function it_can_get_elevated_session_status_when_two_factor_setup_is_incomplete()
{
config(['statamic.users.two_factor_enforced_roles' => ['*']]);

$this
->actingAs($this->user)
->getJson('/cp/elevated-session')
->assertOk()
->assertJson([
'elevated' => false,
'method' => 'password_confirmation',
]);
}

#[Test]
public function it_can_start_an_elevated_session_when_two_factor_setup_is_incomplete()
{
config(['statamic.users.two_factor_enforced_roles' => ['*']]);

$this
->actingAs($this->user)
->postJson('/cp/elevated-session', ['password' => 'secret'])
->assertOk()
->assertSessionHas('statamic_elevated_session', now()->timestamp);
}

#[Test]
public function it_cannot_start_elevated_session_with_incorrect_password()
{
Expand Down
Loading