| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width,initial-scale=1"> |
| 6 | <title>Connected apps & API access</title> |
| 7 | <style> |
| 8 | body{font-family:system-ui,sans-serif;background:#f4f5f7;margin:0;padding:1rem;color:#222} |
| 9 | main{max-width:46rem;margin:0 auto} |
| 10 | section{background:#fff;border-radius:10px;padding:1rem 1.25rem;margin:1rem 0;box-shadow:0 1px 4px #0002} |
| 11 | table{width:100%;border-collapse:collapse}td,th{text-align:left;padding:.4rem;border-bottom:1px solid #eee;vertical-align:top;font-size:.92rem} |
| 12 | button{padding:.4rem .8rem;border-radius:6px;border:1px solid #888;background:#fff;cursor:pointer} |
| 13 | button.danger{border-color:#b42318;color:#b42318} |
| 14 | input,select{padding:.4rem;font-size:.95rem} |
| 15 | code{background:#f0f0f0;padding:.15rem .3rem;border-radius:4px;word-break:break-all} |
| 16 | .muted{color:#666;font-size:.85rem}.err{color:#b42318} |
| 17 | @media (max-width:600px){td,th{display:block;border:0}tr{display:block;border-bottom:1px solid #eee;padding:.4rem 0}} |
| 18 | </style> |
| 19 | </head> |
| 20 | <body> |
| 21 | <main> |
| 22 | <h1>Connected apps & API access</h1> |
| 23 | <p class="muted">Apps and devices you allowed to use your files through the API. They can never see more than you can. <a href="/">Back to files</a></p> |
| 24 | <p id="msg" class="err"></p> |
| 25 | |
| 26 | <section> |
| 27 | <h2>Authorised apps and tokens</h2> |
| 28 | <table><thead><tr><th>Name</th><th>Type</th><th>Access</th><th>Last used</th><th></th></tr></thead><tbody id="grants"></tbody></table> |
| 29 | <p><button class="danger" id="revokeAll">Revoke everything</button></p> |
| 30 | </section> |
| 31 | |
| 32 | <section> |
| 33 | <h2>New personal access token</h2> |
| 34 | <p class="muted">For scripts and your own tools. Prefer OAuth for apps other people build.</p> |
| 35 | <form id="patForm"> |
| 36 | <input id="patName" placeholder="Name, e.g. backup script" required maxlength="80"> |
| 37 | <select id="patScope"><option value="files:read">Read only</option><option value="files:read files:write">Read and write</option></select> |
| 38 | <select id="patDays"><option value="30">30 days</option><option value="90">90 days</option><option value="365">1 year</option><option value="">No expiry</option></select> |
| 39 | <button type="submit">Create</button> |
| 40 | </form> |
| 41 | <p id="patOut"></p> |
| 42 | </section> |
| 43 | |
| 44 | <section id="clientsSection" hidden> |
| 45 | <h2>OAuth clients (admin)</h2> |
| 46 | <p class="muted">Register an app before it can ask users for access. Redirect URIs: https://…, http://127.0.0.1/… (any port) or a private scheme like <code>com.example.sync:/callback</code>.</p> |
| 47 | <table><thead><tr><th>Name</th><th>client_id</th><th>Redirect URIs</th><th></th></tr></thead><tbody id="clients"></tbody></table> |
| 48 | <form id="clientForm"> |
| 49 | <input id="clientName" placeholder="App name" required maxlength="80"> |
| 50 | <input id="clientUris" placeholder="Redirect URIs, comma separated" required size="40"> |
| 51 | <button type="submit">Register</button> |
| 52 | </form> |
| 53 | </section> |
| 54 | </main> |
| 55 | <script> |
| 56 | (function () |
| 57 | { |
| 58 | 'use strict'; |
| 59 | const SESSION_KEY = 'filemanager.session.v1'; |
| 60 | const CLIENT_BINDING_KEY = 'filemanager.client-binding-id.v1'; |
| 61 | const BASE = '/api/account/api-access'; |
| 62 | |
| 63 | function fingerprint () |
| 64 | { |
| 65 | const nav = navigator; |
| 66 | return [ |
| 67 | localStorage.getItem(CLIENT_BINDING_KEY) || '', |
| 68 | nav.userAgent || '', |
| 69 | nav.language, |
| 70 | nav.platform || (nav.userAgentData && nav.userAgentData.platform) || '', |
| 71 | Intl.DateTimeFormat().resolvedOptions().timeZone, |
| 72 | ].join('|'); |
| 73 | } |
| 74 | |
| 75 | function token () |
| 76 | { |
| 77 | try { return JSON.parse(localStorage.getItem(SESSION_KEY) || 'null').token; } catch { return null; } |
| 78 | } |
| 79 | |
| 80 | const msg = document.getElementById('msg'); |
| 81 | |
| 82 | async function call (method, url, body) |
| 83 | { |
| 84 | const res = await fetch(url, { |
| 85 | method, |
| 86 | headers: { Authorization: 'Bearer ' + token(), 'X-Fp': fingerprint(), 'Content-Type': 'application/json' }, |
| 87 | body: body ? JSON.stringify(body) : undefined, |
| 88 | }); |
| 89 | const data = await res.json().catch(() => ({})); |
| 90 | if (res.status === 401) { msg.textContent = 'Please log in to the file manager first, then reload this page.'; throw new Error('auth'); } |
| 91 | if (!res.ok) { msg.textContent = data.error || 'Request failed.'; throw new Error(data.error); } |
| 92 | msg.textContent = ''; |
| 93 | return data; |
| 94 | } |
| 95 | |
| 96 | function cell (tr, text) |
| 97 | { |
| 98 | const td = document.createElement('td'); |
| 99 | if (text instanceof Node) td.appendChild(text); else td.textContent = text; |
| 100 | tr.appendChild(td); |
| 101 | return td; |
| 102 | } |
| 103 | |
| 104 | function when (ms) { return ms ? new Date(ms).toLocaleString() : 'never'; } |
| 105 | |
| 106 | async function loadGrants () |
| 107 | { |
| 108 | const data = await call('GET', BASE + '/grants'); |
| 109 | const tb = document.getElementById('grants'); |
| 110 | tb.textContent = ''; |
| 111 | if (!data.grants.length) |
| 112 | { |
| 113 | const tr = document.createElement('tr'); cell(tr, 'Nothing authorised yet.'); tb.appendChild(tr); |
| 114 | } |
| 115 | for (const g of data.grants) |
| 116 | { |
| 117 | const tr = document.createElement('tr'); |
| 118 | cell(tr, g.name + (g.clientName && g.clientName !== g.name ? ' (' + g.clientName + ')' : '')); |
| 119 | cell(tr, g.kind === 'oauth' ? 'App / device' : 'Personal token'); |
| 120 | cell(tr, g.scopes.includes('files:write') ? 'Read and write' : 'Read only'); |
| 121 | cell(tr, when(g.lastUsedAt) + (g.lastUsedIp ? ' from ' + g.lastUsedIp : '')); |
| 122 | const btn = document.createElement('button'); |
| 123 | btn.className = 'danger'; btn.textContent = 'Revoke'; |
| 124 | btn.onclick = async () => { if (confirm('Revoke access for ' + g.name + '?')) { await call('DELETE', BASE + '/grants/' + encodeURIComponent(g.id)); loadGrants(); } }; |
| 125 | cell(tr, btn); |
| 126 | tb.appendChild(tr); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | async function loadClients () |
| 131 | { |
| 132 | let data; |
| 133 | try { data = await call('GET', BASE + '/clients'); } catch { msg.textContent = ''; return; } |
| 134 | document.getElementById('clientsSection').hidden = false; |
| 135 | const tb = document.getElementById('clients'); |
| 136 | tb.textContent = ''; |
| 137 | for (const c of data.clients) |
| 138 | { |
| 139 | const tr = document.createElement('tr'); |
| 140 | cell(tr, c.name); |
| 141 | const code = document.createElement('code'); code.textContent = c.clientId; cell(tr, code); |
| 142 | cell(tr, c.redirectUris.join(', ')); |
| 143 | const btn = document.createElement('button'); |
| 144 | btn.className = 'danger'; btn.textContent = 'Delete'; |
| 145 | btn.onclick = async () => { if (confirm('Delete ' + c.name + '? All its grants are revoked.')) { await call('DELETE', BASE + '/clients/' + encodeURIComponent(c.clientId)); loadClients(); loadGrants(); } }; |
| 146 | cell(tr, btn); |
| 147 | tb.appendChild(tr); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | document.getElementById('revokeAll').onclick = async () => |
| 152 | { |
| 153 | if (!confirm('Revoke access for every app, device and token?')) return; |
| 154 | await call('POST', BASE + '/grants/revoke-all'); |
| 155 | loadGrants(); |
| 156 | }; |
| 157 | |
| 158 | document.getElementById('patForm').onsubmit = async (e) => |
| 159 | { |
| 160 | e.preventDefault(); |
| 161 | const out = document.getElementById('patOut'); |
| 162 | const data = await call('POST', BASE + '/tokens', { |
| 163 | name: document.getElementById('patName').value, |
| 164 | scopes: document.getElementById('patScope').value, |
| 165 | expiresInDays: document.getElementById('patDays').value || null, |
| 166 | }); |
| 167 | out.textContent = 'Copy this token now, it is shown only once: '; |
| 168 | const code = document.createElement('code'); code.textContent = data.token; out.appendChild(code); |
| 169 | loadGrants(); |
| 170 | }; |
| 171 | |
| 172 | document.getElementById('clientForm').onsubmit = async (e) => |
| 173 | { |
| 174 | e.preventDefault(); |
| 175 | await call('POST', BASE + '/clients', { |
| 176 | name: document.getElementById('clientName').value, |
| 177 | redirectUris: document.getElementById('clientUris').value.split(',').map((s) => s.trim()).filter(Boolean), |
| 178 | }); |
| 179 | e.target.reset(); |
| 180 | loadClients(); |
| 181 | }; |
| 182 | |
| 183 | if (!token()) { msg.textContent = 'Please log in to the file manager first, then reload this page.'; return; } |
| 184 | loadGrants().then(loadClients).catch(() => {}); |
| 185 | })(); |
| 186 | </script> |
| 187 | </body> |
| 188 | </html> |