Skip to content
Merged
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
22 changes: 21 additions & 1 deletion packages/app/src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,24 @@ $('#connection').onsubmit=async e=>{e.preventDefault();const d=new FormData(e.cu
const ampPayload=()=>{const d=new FormData($('#amp-connection'));return {project:d.get('project'),accessToken:d.get('accessToken'),apiBaseUrl:d.get('apiBaseUrl')}};$('#test-amp').onclick=async()=>{const m=$('#amp-message');try{const x=await request('/api/connections/amp/test',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(ampPayload())});m.textContent=x.ok?'Amp connection succeeded.':'Amp connection needs attention.'}catch(x){m.textContent=x.message}};$('#amp-connection').onsubmit=async e=>{e.preventDefault();const m=$('#amp-message');try{await request('/api/connections/amp',{method:'PUT',headers:{'content-type':'application/json'},body:JSON.stringify(ampPayload())});e.currentTarget.reset();m.textContent='Amp connection saved. The token remains write-only.';await load()}catch(x){m.textContent=x.message}};bindActions();load();`);

export const apiKeysSettingsPage = (viewer: { email: string }) => shell("API Keys — Factorize", viewer, settingsLayout("api-keys", `${dashboardHead("API Keys", "Manage API keys for programmatic access to Factorize.")}
<section class="${card} mt-10 p-8" aria-labelledby="api-keys-coming-soon"><h2 id="api-keys-coming-soon" class="text-lg font-semibold">API key management is coming soon</h2><p class="mt-2 max-w-2xl text-sm leading-6 text-slate-500 dark:text-slate-400">This page establishes the settings navigation. Key creation and management will be added in a follow-up.</p></section>`));
<div id="access-error" role="alert" class="mt-6 hidden rounded-xl bg-red-50 p-4 text-sm text-red-800 dark:bg-red-950/40 dark:text-red-200"></div>
<section class="mt-10" aria-labelledby="authorized-clients-title"><div><h2 id="authorized-clients-title" class="text-xl font-semibold">Authorized clients</h2><p class="mt-1 text-sm text-slate-500 dark:text-slate-400">OAuth, MCP, and API clients you have allowed to access Factorize.</p></div><div id="clients-loading" class="${card} mt-5 p-6 text-sm text-slate-500" role="status">Loading authorized clients…</div><div id="clients-empty" class="${card} mt-5 hidden p-8 text-center"><h3 class="font-semibold">No authorized clients</h3><p class="mt-2 text-sm text-slate-500 dark:text-slate-400">Clients appear here after you authorize them.</p></div><div id="authorized-clients" class="mt-5 hidden space-y-3"></div></section>
<section class="mt-12" aria-labelledby="access-tokens-title"><div class="flex flex-wrap items-end justify-between gap-4"><div><h2 id="access-tokens-title" class="text-xl font-semibold">Access Tokens</h2><p class="mt-1 text-sm text-slate-500 dark:text-slate-400">Create scoped bearer tokens for scripts and API requests.</p></div><button id="open-token-form" type="button" class="min-h-11 rounded-xl bg-slate-950 px-4 py-2.5 text-sm font-semibold text-white focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-factorize-600 focus-visible:ring-offset-2 dark:bg-white dark:text-slate-950 dark:focus-visible:ring-offset-slate-950">Create access token</button></div>
<form id="token-form" class="${card} mt-5 hidden p-6" aria-labelledby="create-token-title"><div class="flex items-start justify-between gap-4"><div><h3 id="create-token-title" class="font-semibold">Create access token</h3><p class="mt-1 text-sm text-slate-500 dark:text-slate-400">Choose only the permissions this token needs.</p></div><button id="cancel-token-form" type="button" class="min-h-11 px-2 text-sm font-semibold text-slate-500">Cancel</button></div><div class="mt-5 grid gap-5 sm:grid-cols-2"><label class="label">Token name<input class="field" name="name" maxlength="100" required autocomplete="off" placeholder="Deployment script"></label><label class="label">Expiry<select class="field" name="expiryDays" required><option value="7">7 days</option><option value="30" selected>30 days</option><option value="90">90 days</option></select></label></div><fieldset class="mt-6"><legend class="label">Scopes</legend><div class="mt-2 flex flex-wrap gap-2" aria-label="Scope presets"><button type="button" data-scope-preset="read" class="min-h-11 rounded-lg border border-stone-300 px-3 py-2 text-sm font-semibold dark:border-slate-700">Read only</button><button type="button" data-scope-preset="write" class="min-h-11 rounded-lg border border-stone-300 px-3 py-2 text-sm font-semibold dark:border-slate-700">Read and write</button></div><div class="mt-4 grid gap-3 sm:grid-cols-2">${[["flows:read","Read flows"],["flows:write","Create and update flows"],["runs:read","Read runs"],["runs:write","Start and update runs"]].map(([scope,label])=>`<label class="flex min-h-11 items-center gap-3 rounded-lg border border-stone-200 px-3 py-2 text-sm dark:border-slate-700"><input type="checkbox" name="scope" value="${scope}" class="h-4 w-4 rounded border-stone-300 text-factorize-600 focus:ring-factorize-600"><span><span class="block font-medium">${label}</span><span class="block font-mono text-xs text-slate-500">${scope}</span></span></label>`).join("")}</div></fieldset><p id="token-form-error" role="alert" class="mt-4 hidden text-sm text-red-700 dark:text-red-300"></p><button class="mt-6 min-h-11 rounded-xl bg-factorize-500 px-4 py-2.5 text-sm font-semibold text-slate-950 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-factorize-600 focus-visible:ring-offset-2">Create token</button></form>
<div id="new-token" class="mt-5 hidden rounded-xl border border-amber-300 bg-amber-50 p-6 dark:border-amber-800 dark:bg-amber-950/30" role="status" tabindex="-1"><h3 class="font-semibold text-amber-950 dark:text-amber-100">Copy your new bearer token now</h3><p class="mt-2 text-sm text-amber-900 dark:text-amber-200">This token cannot be retrieved again after you leave or refresh this page.</p><div class="mt-4 flex flex-col gap-2 sm:flex-row"><code id="new-token-value" class="min-w-0 flex-1 overflow-x-auto rounded-lg bg-slate-950 p-3 text-sm text-white"></code><button id="copy-token" type="button" class="min-h-11 rounded-lg border border-amber-400 px-4 py-2 text-sm font-semibold dark:border-amber-700">Copy</button></div></div>
<div class="${card} mt-5 p-5"><h3 class="text-sm font-semibold">Authorization header</h3><pre class="mt-3 overflow-x-auto rounded-lg bg-slate-950 p-4 text-sm text-slate-100"><code>Authorization: Bearer &lt;token&gt;</code></pre></div>
<div id="tokens-loading" class="${card} mt-5 p-6 text-sm text-slate-500" role="status">Loading access tokens…</div><div id="tokens-empty" class="${card} mt-5 hidden p-8 text-center"><h3 class="font-semibold">No access tokens</h3><p class="mt-2 text-sm text-slate-500 dark:text-slate-400">Create a token to access Factorize programmatically.</p></div><div id="access-tokens" class="mt-5 hidden space-y-3"></div></section>`), `${uiHelpers}
const date=value=>value?new Intl.DateTimeFormat(undefined,{dateStyle:'medium',timeStyle:'short'}).format(new Date(value)):'Never';
const scopes=value=>(value||[]).map(scope=>'<code class="rounded bg-stone-100 px-1.5 py-0.5 text-xs dark:bg-slate-800">'+esc(scope)+'</code>').join(' ')||'<span class="text-slate-500">None</span>';
const accessError=message=>{const error=$('#access-error');error.textContent=message||'Could not complete the request. Try again.';error.classList.remove('hidden');error.scrollIntoView({block:'nearest'})};
const clientRow=client=>'<article class="${card} p-5"><div class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between"><div class="min-w-0"><h3 class="break-words font-semibold">'+esc(client.clientName)+'</h3><p class="mt-1 break-all text-xs text-slate-500">Client '+esc(client.clientId)+'</p><div class="mt-3 flex flex-wrap gap-1.5">'+scopes(client.scopes)+'</div><dl class="mt-4 grid gap-x-6 gap-y-2 text-sm sm:grid-cols-3"><div><dt class="text-slate-500">Authorized</dt><dd>'+date(client.authorizationDate)+'</dd></div><div><dt class="text-slate-500">Last used</dt><dd>'+date(client.lastUsedAt)+'</dd></div><div><dt class="text-slate-500">Authorization expires</dt><dd>'+date(client.expiresAt)+'</dd></div></dl></div><button type="button" data-disconnect-client="'+esc(client.clientId)+'" data-client-name="'+esc(client.clientName)+'" class="min-h-11 shrink-0 rounded-lg border border-red-200 px-3 py-2 text-sm font-semibold text-red-700 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-red-500 dark:border-red-900 dark:text-red-300">Disconnect</button></div></article>';
const tokenRow=token=>'<article class="${card} p-5"><div class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between"><div class="min-w-0"><h3 class="break-words font-semibold">'+esc(token.name)+'</h3><div class="mt-3 flex flex-wrap gap-1.5">'+scopes(token.scopes)+'</div><dl class="mt-4 grid gap-x-6 gap-y-2 text-sm sm:grid-cols-3"><div><dt class="text-slate-500">Expires</dt><dd>'+date(token.expires_at)+'</dd></div><div><dt class="text-slate-500">Created</dt><dd>'+date(token.created_at)+'</dd></div><div><dt class="text-slate-500">Last used</dt><dd>'+date(token.last_used_at)+'</dd></div></dl></div><button type="button" data-revoke-token="'+esc(token.id)+'" data-token-name="'+esc(token.name)+'" class="min-h-11 shrink-0 rounded-lg border border-red-200 px-3 py-2 text-sm font-semibold text-red-700 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-red-500 dark:border-red-900 dark:text-red-300">Revoke</button></div></article>';
async function loadClients(){const loading=$('#clients-loading'),root=$('#authorized-clients'),empty=$('#clients-empty');loading.classList.remove('hidden');try{const clients=await request('/api/access/authorized-clients');root.innerHTML=clients.map(clientRow).join('');root.classList.toggle('hidden',!clients.length);empty.classList.toggle('hidden',clients.length>0);bindClientActions()}catch(error){empty.classList.remove('hidden');accessError(error.message)}finally{loading.classList.add('hidden')}}
async function loadTokens(){const loading=$('#tokens-loading'),root=$('#access-tokens'),empty=$('#tokens-empty');loading.classList.remove('hidden');try{const tokens=(await request('/api/access-tokens')).filter(token=>!token.revoked_at);root.innerHTML=tokens.map(tokenRow).join('');root.classList.toggle('hidden',!tokens.length);empty.classList.toggle('hidden',tokens.length>0);bindTokenActions()}catch(error){empty.classList.remove('hidden');accessError(error.message)}finally{loading.classList.add('hidden')}}
function bindClientActions(){document.querySelectorAll('[data-disconnect-client]').forEach(button=>button.onclick=async()=>{if(!confirm('Disconnect '+button.dataset.clientName+'? All of its tokens will stop working immediately.'))return;button.disabled=true;try{await request('/api/access/authorized-clients/'+encodeURIComponent(button.dataset.disconnectClient),{method:'DELETE'});await loadClients()}catch(error){button.disabled=false;accessError(error.message)}})}
function bindTokenActions(){document.querySelectorAll('[data-revoke-token]').forEach(button=>button.onclick=async()=>{if(!confirm('Revoke the access token “'+button.dataset.tokenName+'”? It will stop working immediately.'))return;button.disabled=true;try{await request('/api/access-tokens/'+encodeURIComponent(button.dataset.revokeToken),{method:'DELETE'});await loadTokens()}catch(error){button.disabled=false;accessError(error.message)}})}
const form=$('#token-form');$('#open-token-form').onclick=()=>{form.classList.remove('hidden');$('#open-token-form').classList.add('hidden');form.elements.name.focus()};$('#cancel-token-form').onclick=()=>{form.reset();form.classList.add('hidden');$('#open-token-form').classList.remove('hidden');$('#token-form-error').classList.add('hidden')};
document.querySelectorAll('[data-scope-preset]').forEach(button=>button.onclick=()=>{const read=button.dataset.scopePreset==='read';form.querySelectorAll('[name=scope]').forEach(input=>input.checked=!read||input.value.endsWith(':read'))});
form.onsubmit=async event=>{event.preventDefault();const error=$('#token-form-error'),selected=[...form.querySelectorAll('[name=scope]:checked')].map(input=>input.value);error.classList.add('hidden');if(!selected.length){error.textContent='Select at least one scope.';error.classList.remove('hidden');return}const submit=form.querySelector('button[type=submit]');submit.disabled=true;try{const result=await request('/api/access-tokens',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({name:form.elements.name.value,expiryDays:Number(form.elements.expiryDays.value),scopes:selected})});$('#new-token-value').textContent=result.token;const reveal=$('#new-token');reveal.classList.remove('hidden');reveal.focus();form.reset();form.classList.add('hidden');$('#open-token-form').classList.remove('hidden');await loadTokens()}catch(x){error.textContent=x.message;error.classList.remove('hidden')}finally{submit.disabled=false}};
$('#copy-token').onclick=async()=>{const button=$('#copy-token');try{await navigator.clipboard.writeText($('#new-token-value').textContent);button.textContent='Copied'}catch{button.textContent='Select and copy the token'}setTimeout(()=>button.textContent='Copy',2000)};
Promise.all([loadClients(),loadTokens()]);`);
12 changes: 11 additions & 1 deletion packages/app/test/ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@ describe("pages", () => {
expect(integrations).toContain('href="/settings/integrations" aria-current="page"');
expect(integrations).toContain('href="/settings/api-keys"');
expect(apiKeys).toContain('href="/settings/api-keys" aria-current="page"');
expect(apiKeys).toContain("API key management is coming soon");
expect(apiKeys).toContain("Authorized clients");
expect(apiKeys).toContain("Access Tokens");
expect(apiKeys).toContain("Read only");
expect(apiKeys).toContain("Read and write");
expect(apiKeys).toContain("7 days");
expect(apiKeys).toContain("90 days");
expect(apiKeys).toContain("cannot be retrieved again");
expect(apiKeys).toContain("Authorization: Bearer &lt;token&gt;");
expect(apiKeys).toContain("All of its tokens will stop working immediately");
expect(apiKeys).toContain("/api/access-tokens");
expectInlineScriptsToParse(apiKeys);
});

it("separates available providers from installed integration management", () => {
Expand Down