diff --git a/package-lock.json b/package-lock.json index 733128a6..8e937671 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3606,6 +3606,23 @@ "undici-types": "~8.9.0" } }, + "node_modules/@types/whatwg-mimetype": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/whatwg-mimetype/-/whatwg-mimetype-3.0.2.tgz", + "integrity": "sha512-c2AKvDT8ToxLIOUlN51gTiHXflsfIFisS4pO7pDPoKouJCESkhZnEy623gwP9laCy5lnLDAw1vAzu2vM2YLOrA==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/ws": { + "version": "8.18.1", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.18.1.tgz", + "integrity": "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@vitest/expect": { "version": "3.2.7", "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-3.2.7.tgz", @@ -3784,6 +3801,19 @@ "node": ">=8" } }, + "node_modules/buffer-image-size": { + "version": "0.6.4", + "resolved": "https://registry.npmjs.org/buffer-image-size/-/buffer-image-size-0.6.4.tgz", + "integrity": "sha512-nEh+kZOPY1w+gcCMobZ6ETUp9WfibndnosbpwB1iJk/8Gt5ZF2bhS6+B6bPYz424KtwsR6Rflc3tCz1/ghX2dQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + }, + "engines": { + "node": ">=4.0" + } + }, "node_modules/cac": { "version": "6.7.14", "resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz", @@ -3958,6 +3988,19 @@ "node": ">=10.13.0" } }, + "node_modules/entities": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/entities/-/entities-7.0.1.tgz", + "integrity": "sha512-TWrgLOFUQTH994YUyl1yT4uyavY5nNB5muff+RtWaqNVCAK408b5ZnnbNAUEWLTCpum9w6arT70i1XdQ4UeOPA==", + "dev": true, + "license": "BSD-2-Clause", + "engines": { + "node": ">=0.12" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, "node_modules/error-stack-parser-es": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/error-stack-parser-es/-/error-stack-parser-es-1.0.5.tgz", @@ -4118,6 +4161,25 @@ "dev": true, "license": "ISC" }, + "node_modules/happy-dom": { + "version": "20.14.5", + "resolved": "https://registry.npmjs.org/happy-dom/-/happy-dom-20.14.5.tgz", + "integrity": "sha512-x/RzkpWO40bTjIoT30iQtt64FLLmH/iRcUCN2X//bLx7H3ifkdfPXyqsro/OYtqzIAhiLMMA7mmiOR9C3NOKjQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": ">=20.0.0", + "@types/whatwg-mimetype": "^3.0.2", + "@types/ws": "^8.18.1", + "buffer-image-size": "^0.6.4", + "entities": "^7.0.1", + "whatwg-mimetype": "^3.0.0", + "ws": "^8.21.0" + }, + "engines": { + "node": ">=20.0.0" + } + }, "node_modules/hono": { "version": "4.13.7", "resolved": "https://registry.npmjs.org/hono/-/hono-4.13.7.tgz", @@ -5178,6 +5240,16 @@ } } }, + "node_modules/whatwg-mimetype": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz", + "integrity": "sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + } + }, "node_modules/why-is-node-running": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-2.3.0.tgz", @@ -5810,6 +5882,7 @@ "@tailwindcss/cli": "^4.1.14", "@types/mustache": "^4.2.6", "@types/node": "^26.5.1", + "happy-dom": "^20.14.5", "tailwindcss": "^4.1.14", "typescript": "^5.9.3", "vitest": "^3.2.4", diff --git a/packages/app/package.json b/packages/app/package.json index eb3ba05e..ddcbbdbd 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -26,6 +26,7 @@ "@tailwindcss/cli": "^4.1.14", "@types/mustache": "^4.2.6", "@types/node": "^26.5.1", + "happy-dom": "^20.14.5", "tailwindcss": "^4.1.14", "typescript": "^5.9.3", "vitest": "^3.2.4", diff --git a/packages/app/src/ui.ts b/packages/app/src/ui.ts index bea4b86c..4c083c1c 100644 --- a/packages/app/src/ui.ts +++ b/packages/app/src/ui.ts @@ -19,7 +19,7 @@ const shell = (title: string, viewer: Viewer, body: string, script = "") => `${body}`; @@ -136,9 +136,9 @@ ${[["linear","Linear","Issues and projects"],["github","GitHub","Repositories an

Connect Linear

You’ll leave Factorize to authorize access to your Linear workspace, then return here.

Install the GitHub App

You’ll choose an account and repositories on GitHub, then return to Factorize.

-

Connect Exe.dev

This saves a write-only token and adds the VM as an execution target.

-

Connect Amp

This verifies and stores a write-only token for an Amp cloud project.

-

Install Cloudflare Tail

Name the installation and paste a secret, or have Factorize generate one.

+

Connect Exe.dev

This saves a write-only token and adds the VM as an execution target.

+

Connect Amp

This verifies and stores a write-only token for an Amp cloud project.

+

Install Cloudflare Tail

Name the installation and paste a secret, or have Factorize generate one.

Installed integrations

Check status and manage providers already connected to Factorize.

Loading installed integrations…
`), `${uiHelpers} const badge=(id,label,tone)=>{const el=$(id);el.textContent=label;el.className='text-xs font-medium '+(tone==='warn'?'text-amber-700 dark:text-amber-300':'text-slate-500 dark:text-slate-400')}; const testButton=(kind,id)=>''; @@ -159,7 +159,7 @@ export const apiKeysSettingsPage = (viewer: { email: string }) => shell("API Key

Authorized clients

OAuth, MCP, and API clients you have allowed to access Factorize.

Loading authorized clients…

Access Tokens

Create scoped bearer tokens for scripts and API requests.

- +

Authorization header

Authorization: Bearer <token>
Loading access tokens…
`), `${uiHelpers} @@ -174,6 +174,6 @@ function bindClientActions(){document.querySelectorAll('[data-disconnect-client] 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}}; +form.onsubmit=async event=>{event.preventDefault();const error=$('#token-form-error'),submit=form.querySelector('button[type="submit"]');error.classList.add('hidden');if(!submit){error.textContent='The create-token action is unavailable. Refresh and try again.';error.classList.remove('hidden');return}const selected=[...form.querySelectorAll('[name=scope]:checked')].map(input=>input.value);if(!selected.length){error.textContent='Select at least one scope.';error.classList.remove('hidden');return}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||'Could not create the access token. Try again.';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()]);`); diff --git a/packages/app/test/api-keys.browser.test.ts b/packages/app/test/api-keys.browser.test.ts new file mode 100644 index 00000000..974e0f2a --- /dev/null +++ b/packages/app/test/api-keys.browser.test.ts @@ -0,0 +1,84 @@ +// @vitest-environment happy-dom +import { SubmitEvent } from "happy-dom"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { apiKeysSettingsPage } from "../src/ui"; + +declare const document: any; + +const json = (body: unknown, status = 200) => new Response(JSON.stringify(body), { + status, + headers: { "content-type": "application/json" }, +}); +const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); + +const renderPage = (fetchMock: ReturnType) => { + vi.stubGlobal("fetch", fetchMock); + const html = apiKeysSettingsPage({ email: "owner@example.com" }); + document.body.innerHTML = html.match(/]*>([\s\S]*?)<\/body>/)?.[1] ?? ""; + const script = [...html.matchAll(/