From d71b60bd7e6f6abf08e6e9854cf21e5601a2eb43 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Tue, 30 Dec 2025 11:23:55 -0500 Subject: [PATCH] chore: npm run format + lint --- SharpbinV3.Client/src/lib/consts.ts | 2 +- SharpbinV3.Client/src/lib/types/pagination.ts | 10 +- SharpbinV3.Client/src/lib/utils/auth.ts | 33 +++- SharpbinV3.Client/src/lib/utils/encryption.ts | 166 +++++++++--------- SharpbinV3.Client/src/routes/+page.svelte | 16 +- SharpbinV3.Client/src/routes/layout.css | 1 - .../routes/user/[username]/+page.server.ts | 22 +-- 7 files changed, 134 insertions(+), 116 deletions(-) diff --git a/SharpbinV3.Client/src/lib/consts.ts b/SharpbinV3.Client/src/lib/consts.ts index c6a1648..4099f20 100644 --- a/SharpbinV3.Client/src/lib/consts.ts +++ b/SharpbinV3.Client/src/lib/consts.ts @@ -78,4 +78,4 @@ export const roles = { 1: 'Moderator', 255: 'Administrator', 403: 'Banned' -} \ No newline at end of file +}; diff --git a/SharpbinV3.Client/src/lib/types/pagination.ts b/SharpbinV3.Client/src/lib/types/pagination.ts index 1d576af..fcd20b4 100644 --- a/SharpbinV3.Client/src/lib/types/pagination.ts +++ b/SharpbinV3.Client/src/lib/types/pagination.ts @@ -1,6 +1,6 @@ export interface Pagination { - page: number; - pageSize: number; - totalCount: number; - totalPages: number; -} \ No newline at end of file + page: number; + pageSize: number; + totalCount: number; + totalPages: number; +} diff --git a/SharpbinV3.Client/src/lib/utils/auth.ts b/SharpbinV3.Client/src/lib/utils/auth.ts index 1b6c2c2..d618f97 100644 --- a/SharpbinV3.Client/src/lib/utils/auth.ts +++ b/SharpbinV3.Client/src/lib/utils/auth.ts @@ -6,12 +6,22 @@ const REFRESH_TOKEN_KEY = 'refreshToken'; export function getToken() { if (!browser) return null; - return document.cookie.split('; ').find(c => c.startsWith(`${TOKEN_KEY}=`))?.split('=')[1] || null; + return ( + document.cookie + .split('; ') + .find((c) => c.startsWith(`${TOKEN_KEY}=`)) + ?.split('=')[1] || null + ); } export function getRefreshToken() { if (!browser) return null; - return document.cookie.split('; ').find(c => c.startsWith(`${REFRESH_TOKEN_KEY}=`))?.split('=')[1] || null; + return ( + document.cookie + .split('; ') + .find((c) => c.startsWith(`${REFRESH_TOKEN_KEY}=`)) + ?.split('=')[1] || null + ); } export function setTokens(token: string, refreshToken: string) { @@ -67,19 +77,24 @@ export function startTokenRefreshInterval() { } export function getServerToken(cookies: Cookies) { - return cookies.get('token'); + return cookies.get('token'); } export function getServerRefreshToken(cookies: Cookies) { - return cookies.get('refreshToken'); + return cookies.get('refreshToken'); } export function setServerTokens(cookies: Cookies, token: string, refreshToken: string) { - cookies.set('token', token, { path: '/', httpOnly: true, sameSite: 'strict', secure: true }); - cookies.set('refreshToken', refreshToken, { path: '/', httpOnly: true, sameSite: 'strict', secure: true }); + cookies.set('token', token, { path: '/', httpOnly: true, sameSite: 'strict', secure: true }); + cookies.set('refreshToken', refreshToken, { + path: '/', + httpOnly: true, + sameSite: 'strict', + secure: true + }); } export function clearServerTokens(cookies: Cookies) { - cookies.delete('token', { path: '/' }); - cookies.delete('refreshToken', { path: '/' }); -} \ No newline at end of file + cookies.delete('token', { path: '/' }); + cookies.delete('refreshToken', { path: '/' }); +} diff --git a/SharpbinV3.Client/src/lib/utils/encryption.ts b/SharpbinV3.Client/src/lib/utils/encryption.ts index 120b1dc..1c653fd 100644 --- a/SharpbinV3.Client/src/lib/utils/encryption.ts +++ b/SharpbinV3.Client/src/lib/utils/encryption.ts @@ -1,87 +1,87 @@ export async function encryptAES(content: string, password: string): Promise { - const enc = new TextEncoder(); - const keyMaterial = await window.crypto.subtle.importKey( - 'raw', - enc.encode(password), - { name: 'PBKDF2' }, - false, - ['deriveBits', 'deriveKey'] - ); - const salt = window.crypto.getRandomValues(new Uint8Array(16)); - const key = await window.crypto.subtle.deriveKey( - { - name: 'PBKDF2', - salt: salt, - iterations: 100000, - hash: 'SHA-256' - }, - keyMaterial, - { name: 'AES-GCM', length: 256 }, - false, - ['encrypt'] - ); - const iv = window.crypto.getRandomValues(new Uint8Array(12)); - const encryptedContent = await window.crypto.subtle.encrypt( - { - name: 'AES-GCM', - iv: iv - }, - key, - enc.encode(content) - ); - const combined = new Uint8Array(salt.byteLength + iv.byteLength + encryptedContent.byteLength); - combined.set(salt, 0); - combined.set(iv, salt.byteLength); - combined.set(new Uint8Array(encryptedContent), salt.byteLength + iv.byteLength); - const result = { - version: 1, - kdf: 'PBKDF2', - iterations: 100000, - hash: 'SHA-256', - algorithm: 'AES-GCM', - data: btoa(String.fromCharCode(...combined)) - }; - return JSON.stringify(result); + const enc = new TextEncoder(); + const keyMaterial = await window.crypto.subtle.importKey( + 'raw', + enc.encode(password), + { name: 'PBKDF2' }, + false, + ['deriveBits', 'deriveKey'] + ); + const salt = window.crypto.getRandomValues(new Uint8Array(16)); + const key = await window.crypto.subtle.deriveKey( + { + name: 'PBKDF2', + salt: salt, + iterations: 100000, + hash: 'SHA-256' + }, + keyMaterial, + { name: 'AES-GCM', length: 256 }, + false, + ['encrypt'] + ); + const iv = window.crypto.getRandomValues(new Uint8Array(12)); + const encryptedContent = await window.crypto.subtle.encrypt( + { + name: 'AES-GCM', + iv: iv + }, + key, + enc.encode(content) + ); + const combined = new Uint8Array(salt.byteLength + iv.byteLength + encryptedContent.byteLength); + combined.set(salt, 0); + combined.set(iv, salt.byteLength); + combined.set(new Uint8Array(encryptedContent), salt.byteLength + iv.byteLength); + const result = { + version: 1, + kdf: 'PBKDF2', + iterations: 100000, + hash: 'SHA-256', + algorithm: 'AES-GCM', + data: btoa(String.fromCharCode(...combined)) + }; + return JSON.stringify(result); } export async function decryptAES(result: string, password: string): Promise { - try { - const enc = new TextEncoder(); - const parsed = JSON.parse(result); - const combined = Uint8Array.from(atob(parsed.data), c => c.charCodeAt(0)); - const salt = combined.slice(0, 16); - const iv = combined.slice(16, 28); - const data = combined.slice(28); - const keyMaterial = await window.crypto.subtle.importKey( - 'raw', - enc.encode(password), - { name: 'PBKDF2' }, - false, - ['deriveBits', 'deriveKey'] - ); - const key = await window.crypto.subtle.deriveKey( - { - name: 'PBKDF2', - salt: salt, - iterations: parsed.iterations, - hash: parsed.hash - }, - keyMaterial, - { name: 'AES-GCM', length: 256 }, - false, - ['decrypt'] - ); - const decryptedContent = await window.crypto.subtle.decrypt( - { - name: 'AES-GCM', - iv: iv - }, - key, - data - ); - const dec = new TextDecoder(); - return dec.decode(decryptedContent); - } catch { - return null; - } -} \ No newline at end of file + try { + const enc = new TextEncoder(); + const parsed = JSON.parse(result); + const combined = Uint8Array.from(atob(parsed.data), (c) => c.charCodeAt(0)); + const salt = combined.slice(0, 16); + const iv = combined.slice(16, 28); + const data = combined.slice(28); + const keyMaterial = await window.crypto.subtle.importKey( + 'raw', + enc.encode(password), + { name: 'PBKDF2' }, + false, + ['deriveBits', 'deriveKey'] + ); + const key = await window.crypto.subtle.deriveKey( + { + name: 'PBKDF2', + salt: salt, + iterations: parsed.iterations, + hash: parsed.hash + }, + keyMaterial, + { name: 'AES-GCM', length: 256 }, + false, + ['decrypt'] + ); + const decryptedContent = await window.crypto.subtle.decrypt( + { + name: 'AES-GCM', + iv: iv + }, + key, + data + ); + const dec = new TextDecoder(); + return dec.decode(decryptedContent); + } catch { + return null; + } +} diff --git a/SharpbinV3.Client/src/routes/+page.svelte b/SharpbinV3.Client/src/routes/+page.svelte index 1fa6189..40a6298 100644 --- a/SharpbinV3.Client/src/routes/+page.svelte +++ b/SharpbinV3.Client/src/routes/+page.svelte @@ -38,10 +38,12 @@ { value: 315360000000, label: 'Expire in 10 Years' } ]; - const visibilityOptions = data.options.visibilities.map((visibility: { value: number; displayName: string }) => ({ - value: visibility.value, - label: visibility.displayName - })); + const visibilityOptions = data.options.visibilities.map( + (visibility: { value: number; displayName: string }) => ({ + value: visibility.value, + label: visibility.displayName + }) + ); async function handleSubmit(event: Event) { event.preventDefault(); @@ -91,7 +93,7 @@
-
+

Create a Paste

{ - const { username } = params; - const token = getServerToken(cookies); - const res = await fetch(`/api/user/${username}`, { - headers: { - Authorization: token ? `Bearer ${token}` : '' - } - }); - - const user = await res.json() as User; - return { user }; -} \ No newline at end of file + const { username } = params; + const token = getServerToken(cookies); + const res = await fetch(`/api/user/${username}`, { + headers: { + Authorization: token ? `Bearer ${token}` : '' + } + }); + + const user = (await res.json()) as User; + return { user }; +};