/** * Lock screen for password-protected PDFs (Pro). * * The page never receives the file URL. The reader enters the password, the * server returns a short-lived signed stream URL, and the viewer mounts with * it. Successful unlocks are remembered for the tab session until expiry. */ export interface ProtectedInfo { unlockUrl: string; title?: string; } interface CachedUnlock { url: string; expires: number; } const cacheKey = (pdfId: number) => `pdfrack-unlock-${pdfId}`; export function cachedUnlockUrl(pdfId: number): string | null { try { const raw = window.sessionStorage.getItem(cacheKey(pdfId)); if (!raw) return null; const cached = JSON.parse(raw) as CachedUnlock; // Keep a minute of margin so a long read doesn't hit an expired link. return cached.expires * 1000 > Date.now() + 60_000 ? cached.url : null; } catch { return null; } } function remember(pdfId: number, data: CachedUnlock): void { try { window.sessionStorage.setItem(cacheKey(pdfId), JSON.stringify(data)); } catch { /* storage unavailable */ } } const LOCK_ICON = ''; /** * Render the lock form into `container`. Resolves `onUnlocked` with the * signed stream URL once the password is accepted. */ export function renderLock( container: HTMLElement, pdfId: number, info: ProtectedInfo, onUnlocked: (url: string) => void, ): () => void { const root = document.createElement('div'); root.className = 'pdfrack-lock'; root.innerHTML = `
${LOCK_ICON}

This document is password protected.

`; const form = root.querySelector('form') as HTMLFormElement; const input = root.querySelector('input') as HTMLInputElement; const button = root.querySelector('button') as HTMLButtonElement; const error = root.querySelector('.pdfrack-lock-error') as HTMLParagraphElement; (root.querySelector('.pdfrack-lock-title') as HTMLElement).textContent = info.title || 'Protected document'; const showError = (message: string) => { error.textContent = message; error.hidden = false; root.classList.remove('pdfrack-lock-shake'); void root.offsetWidth; // restart the animation root.classList.add('pdfrack-lock-shake'); }; form.addEventListener('submit', async (e) => { e.preventDefault(); if (!input.value) { input.focus(); return; } button.disabled = true; button.textContent = 'Checking…'; error.hidden = true; try { const res = await fetch(info.unlockUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: pdfId, password: input.value }), credentials: 'same-origin', }); const body = await res.json().catch(() => ({})); if (!res.ok || !body?.data?.url) { showError(body?.message || 'Incorrect password.'); input.select(); return; } remember(pdfId, { url: body.data.url, expires: body.data.expires }); root.remove(); onUnlocked(body.data.url); } catch { showError('Could not reach the server. Please try again.'); } finally { button.disabled = false; button.textContent = 'Unlock'; } }); // Keep viewer/page shortcuts away while typing. input.addEventListener('keydown', (e) => e.stopPropagation()); container.appendChild(root); return () => root.remove(); }