mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: totp 2fa; httpcontext extension for jwtuser; asp.net data protection; prevent banned users from certain actions; account deletion requires 2fa if enabled; making users admins requires 2fa if enabled; httpcontext extension to get ip; enhance user store
refactor: organize dtos by domain; verification providers use verification context; move ip parsing into http context extensions chore: clean code
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
import Navbar from '$lib/components/Navbar.svelte';
|
||||
import { user } from '$lib/stores/user';
|
||||
import { startTokenRefreshInterval, getToken, refreshTokenIfNeeded } from '$lib/utils/auth';
|
||||
import { parseTotpEnabled } from '$lib/utils/totp';
|
||||
import { page } from '$app/state';
|
||||
import type { LayoutData } from './$types';
|
||||
|
||||
@@ -27,7 +28,8 @@
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
user.set(await res.json());
|
||||
const me = await res.json();
|
||||
user.set({ ...me, totpEnabled: parseTotpEnabled(getToken()) });
|
||||
} else {
|
||||
user.set(null);
|
||||
}
|
||||
|
||||
@@ -82,7 +82,11 @@
|
||||
}
|
||||
window.location.href = `/${result.id}`;
|
||||
} else {
|
||||
error = 'Failed to create paste. Please try again.';
|
||||
if (response.status === 403) {
|
||||
error = 'You are banned from creating new pastes.';
|
||||
} else {
|
||||
error = 'Failed to create paste. Please try again.';
|
||||
}
|
||||
setInterval(() => {
|
||||
error = '';
|
||||
}, 5000);
|
||||
@@ -152,7 +156,8 @@
|
||||
{/if}
|
||||
<button
|
||||
type="submit"
|
||||
class="w-full cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900"
|
||||
disabled={$user?.roles && $user.roles.includes(403)}
|
||||
class="w-full cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900 disabled:cursor-not-allowed disabled:bg-neutral-950/50 disabled:text-neutral-400"
|
||||
>Create Paste</button
|
||||
>
|
||||
{#if error}
|
||||
@@ -163,6 +168,14 @@
|
||||
{error}
|
||||
</div>
|
||||
{/if}
|
||||
{#if $user?.roles && $user.roles.includes(403)}
|
||||
<div
|
||||
transition:fly={{ y: 40, duration: 300 }}
|
||||
class="mt-2 rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200"
|
||||
>
|
||||
You are banned from creating new pastes.
|
||||
</div>
|
||||
{/if}
|
||||
</form>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
let username = '';
|
||||
let password = '';
|
||||
let error = '';
|
||||
let totpEnabled = false;
|
||||
let totpCode = '';
|
||||
export let data: PageData;
|
||||
|
||||
onMount(() => {
|
||||
@@ -31,7 +33,7 @@
|
||||
});
|
||||
|
||||
async function login() {
|
||||
const body: { username: string; password: string; token?: string } = {
|
||||
const body: { username: string; password: string; token?: string; totpcode?: string } = {
|
||||
username,
|
||||
password
|
||||
};
|
||||
@@ -43,6 +45,10 @@
|
||||
}
|
||||
}
|
||||
|
||||
if (totpEnabled && totpCode) {
|
||||
body.totpcode = totpCode;
|
||||
}
|
||||
|
||||
const res = await fetch('/api/auth/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -67,6 +73,14 @@
|
||||
const messages = Object.values(resData.errors).flat();
|
||||
error = messages.join('\n');
|
||||
} else {
|
||||
if (resData.message === 'TOTP code is required.') {
|
||||
totpEnabled = true;
|
||||
}
|
||||
if (resData.message === 'Verification failed.') {
|
||||
if (window.turnstile) {
|
||||
window.turnstile.reset();
|
||||
}
|
||||
}
|
||||
error = resData.message || 'Login failed. Please try again.';
|
||||
}
|
||||
}
|
||||
@@ -95,6 +109,24 @@
|
||||
autocomplete="current-password"
|
||||
class="focus:bg-neutral-750 w-full rounded border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
|
||||
/>
|
||||
{#if totpEnabled}
|
||||
<input
|
||||
type="text"
|
||||
placeholder="TOTP Code"
|
||||
bind:value={totpCode}
|
||||
maxlength="6"
|
||||
inputmode="numeric"
|
||||
pattern="[0-9]*"
|
||||
required
|
||||
on:input={(e) => {
|
||||
if (e.target instanceof HTMLInputElement) {
|
||||
e.target.value = e.target.value.replace(/\D/g, '').slice(0, 6);
|
||||
totpCode = e.target.value;
|
||||
}
|
||||
}}
|
||||
class="focus:bg-neutral-750 w-full rounded border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
|
||||
/>
|
||||
{/if}
|
||||
{#if data.options?.cf_turnstile_site_key}
|
||||
<div class="flex w-full justify-center">
|
||||
<div class="cf-turnstile"></div>
|
||||
|
||||
@@ -100,6 +100,11 @@
|
||||
const messages = Object.values(resData.errors).flat();
|
||||
error = messages.join('\n');
|
||||
} else {
|
||||
if (resData.message === 'Verification failed.') {
|
||||
if (window.turnstile) {
|
||||
window.turnstile.reset();
|
||||
}
|
||||
}
|
||||
error = resData.message || 'Registration failed. Please try again.';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
import { extractDateFromUUIDv7, tooltip, dateToRelativeString } from '$lib/utils/misc';
|
||||
import { roles } from '$lib/consts';
|
||||
import { getToken } from '$lib/utils/auth';
|
||||
import { needsAdminTotp } from '$lib/utils/totp';
|
||||
import Modal from '$lib/components/Modal.svelte';
|
||||
import { user } from '$lib/stores/user';
|
||||
import { onMount } from 'svelte';
|
||||
@@ -36,11 +37,19 @@
|
||||
let showEditModal = false;
|
||||
let showDeleteModal = false;
|
||||
let showRoleModal = false;
|
||||
let showTotpModal = false;
|
||||
let showRoleTotpModal = false;
|
||||
let showDeleteTotpModal = false;
|
||||
let modalError = '';
|
||||
let totpEnabled = false;
|
||||
let pendingRoles: number[] | null = null;
|
||||
let roleTotpCode = '';
|
||||
let deleteTotpCode = '';
|
||||
let now = new Date();
|
||||
let interval: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
$: isOwner = data.user && $user ? data.user?.uuid === $user.uuid : false;
|
||||
$: totpEnabled = Boolean($user?.totpEnabled);
|
||||
|
||||
const roleOptions = [
|
||||
{ label: 'Member', value: 0 },
|
||||
@@ -69,7 +78,16 @@
|
||||
|
||||
async function handleUserUpdate(displayName?: string, selectedRoles?: number[]) {
|
||||
loading = true;
|
||||
modalError = '';
|
||||
const token = getToken();
|
||||
const currentRoles = data.user?.roles ?? [];
|
||||
const nextRoles = selectedRoles ?? currentRoles;
|
||||
if (needsAdminTotp(totpEnabled, currentRoles, nextRoles) && !roleTotpCode) {
|
||||
pendingRoles = nextRoles;
|
||||
showRoleTotpModal = true;
|
||||
loading = false;
|
||||
return;
|
||||
}
|
||||
const res = await fetch(`/api/user/uuid/${data.user?.uuid}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
@@ -78,7 +96,8 @@
|
||||
},
|
||||
body: JSON.stringify({
|
||||
...(displayName !== undefined ? { displayName: displayName.trim() } : {}),
|
||||
...(selectedRoles !== undefined ? { roles: selectedRoles } : {})
|
||||
...(selectedRoles !== undefined ? { roles: selectedRoles } : {}),
|
||||
...(roleTotpCode ? { totpcode: roleTotpCode } : {})
|
||||
})
|
||||
});
|
||||
loading = false;
|
||||
@@ -93,17 +112,36 @@
|
||||
if (selectedRoles !== undefined) {
|
||||
data.user!.roles = selectedRoles;
|
||||
}
|
||||
roleTotpCode = '';
|
||||
showRoleTotpModal = false;
|
||||
pendingRoles = null;
|
||||
modalError = '';
|
||||
}
|
||||
|
||||
function handleRoleTotpConfirm(value: unknown) {
|
||||
if (typeof value === 'string') {
|
||||
roleTotpCode = value.trim();
|
||||
}
|
||||
showRoleTotpModal = false;
|
||||
if (!roleTotpCode) return;
|
||||
handleUserUpdate(undefined, pendingRoles ?? data.user?.roles ?? []);
|
||||
}
|
||||
|
||||
async function handleDeleteAccount() {
|
||||
loading = true;
|
||||
modalError = '';
|
||||
const token = getToken();
|
||||
if (totpEnabled && !deleteTotpCode) {
|
||||
showDeleteTotpModal = true;
|
||||
loading = false;
|
||||
return;
|
||||
}
|
||||
const res = await fetch(`/api/user/uuid/${data.user?.uuid}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
}
|
||||
},
|
||||
body: totpEnabled && deleteTotpCode ? JSON.stringify({ totpcode: deleteTotpCode }) : undefined
|
||||
});
|
||||
loading = false;
|
||||
if (!res.ok) {
|
||||
@@ -112,9 +150,29 @@
|
||||
return;
|
||||
}
|
||||
showDeleteModal = false;
|
||||
showDeleteTotpModal = false;
|
||||
deleteTotpCode = '';
|
||||
window.location.href = '/';
|
||||
}
|
||||
|
||||
function handleTotpConfirm(value: unknown) {
|
||||
if (value && typeof value === 'object' && 'enabled' in value) {
|
||||
totpEnabled = Boolean((value as { enabled?: boolean }).enabled);
|
||||
}
|
||||
user.update((u) => (u ? { ...u, totpEnabled } : u));
|
||||
showTotpModal = false;
|
||||
modalError = '';
|
||||
}
|
||||
|
||||
function handleDeleteTotpConfirm(value: unknown) {
|
||||
if (typeof value === 'string') {
|
||||
deleteTotpCode = value.trim();
|
||||
}
|
||||
showDeleteTotpModal = false;
|
||||
if (!deleteTotpCode) return;
|
||||
handleDeleteAccount();
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
interval = setInterval(() => {
|
||||
now = new Date();
|
||||
@@ -201,6 +259,10 @@
|
||||
{data.user?.uid}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex shrink-0 items-center">
|
||||
<ShieldUser class="mr-2 h-6 w-6 text-neutral-400" />
|
||||
<span class="text-neutral-400">2FA {totpEnabled ? 'enabled' : 'disabled'}</span>
|
||||
</div>
|
||||
{#if isOwner && data.user?.lastLogin}
|
||||
<div class="flex shrink-0 items-center">
|
||||
<Clock class="mr-2 h-6 w-6 text-neutral-400" />
|
||||
@@ -225,6 +287,18 @@
|
||||
<Pencil class="h-4 w-4" />
|
||||
Edit Display Name
|
||||
</button>
|
||||
{#if isOwner}
|
||||
<button
|
||||
on:click={() => {
|
||||
showTotpModal = true;
|
||||
modalError = '';
|
||||
}}
|
||||
class="flex items-center gap-2 rounded bg-neutral-700 px-4 py-2 text-sm font-medium transition-colors hover:bg-neutral-600"
|
||||
>
|
||||
<ShieldUser class="h-4 w-4" />
|
||||
{totpEnabled ? 'Manage 2FA' : 'Enable 2FA'}
|
||||
</button>
|
||||
{/if}
|
||||
{#if $user?.roles.some((r) => r === 255)}
|
||||
<button
|
||||
on:click={() => {
|
||||
@@ -323,6 +397,22 @@
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
show={showDeleteTotpModal}
|
||||
mode="totp"
|
||||
title="Enter TOTP to delete"
|
||||
placeholder="Enter 6-digit code"
|
||||
inputType="text"
|
||||
error={modalError}
|
||||
confirmButtonText="Delete Account"
|
||||
onConfirm={(value) => handleDeleteTotpConfirm(value)}
|
||||
onCancel={() => {
|
||||
showDeleteTotpModal = false;
|
||||
deleteTotpCode = '';
|
||||
modalError = '';
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
show={showRoleModal}
|
||||
mode="multiselect"
|
||||
@@ -337,6 +427,38 @@
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
show={showTotpModal}
|
||||
mode="totpSetup"
|
||||
title="Two-Factor Authentication"
|
||||
message={totpEnabled
|
||||
? 'Two-factor authentication is currently enabled.'
|
||||
: 'Enable two-factor authentication with an authenticator app.'}
|
||||
error={modalError}
|
||||
totpActive={totpEnabled}
|
||||
onConfirm={(value) => handleTotpConfirm(value)}
|
||||
onCancel={() => {
|
||||
showTotpModal = false;
|
||||
modalError = '';
|
||||
}}
|
||||
/>
|
||||
<Modal
|
||||
show={showRoleTotpModal}
|
||||
mode="totp"
|
||||
title="Admin role requires TOTP"
|
||||
placeholder="Enter 6-digit code"
|
||||
inputType="text"
|
||||
error={modalError}
|
||||
confirmButtonText="Continue"
|
||||
onConfirm={(value) => handleRoleTotpConfirm(value)}
|
||||
onCancel={() => {
|
||||
showRoleTotpModal = false;
|
||||
roleTotpCode = '';
|
||||
pendingRoles = null;
|
||||
modalError = '';
|
||||
}}
|
||||
/>
|
||||
|
||||
<style>
|
||||
button {
|
||||
cursor: pointer;
|
||||
|
||||
Reference in New Issue
Block a user