BlueGit

timfa filemanager

Files

filemanager / server / api / manage.js

manage.js

manage.js 3.1 KiB Download Raw
1'use strict';
2
3// Management of API access for logged-in web users (session auth):
4// see and revoke connected apps/devices, create personal access tokens,
5// and (admins) register OAuth clients.
6
7const express = require('express');
8const store = require('./store');
9const { requireAuth, requireAdmin } = require('../auth');
10
11const router = express.Router();
12router.use(express.json({ limit: '32kb' }));
13router.use(requireAuth);
14
15router.get('/grants', (req, res) =>
16{
17 res.json({ grants: store.listGrantsForUser(req.user.username), scopes: store.SCOPE_DESCRIPTIONS });
18});
19
20router.delete('/grants/:id', (req, res) =>
21{
22 const g = store.getGrant(req.params.id);
23 if (!g || g.username !== req.user.username || g.revoked_at)
24 {
25 return res.status(404).json({ error: 'Not found.' });
26 }
27 store.revokeGrant(g.id);
28 res.json({ ok: true });
29});
30
31router.post('/grants/revoke-all', (req, res) =>
32{
33 store.revokeAllForUser(req.user.username);
34 res.json({ ok: true });
35});
36
37router.post('/tokens', (req, res) =>
38{
39 const body = req.body || {};
40 const name = String(body.name || '').trim().slice(0, 80);
41 if (!name) return res.status(400).json({ error: 'name is required.' });
42 const scopes = store.normalizeScopes(body.scopes || 'files:read');
43 if (!scopes || !scopes.length) return res.status(400).json({ error: `Allowed scopes: ${store.SCOPES.join(', ')}` });
44 let expiresAt = null;
45 if (body.expiresInDays != null && body.expiresInDays !== '')
46 {
47 const days = Number(body.expiresInDays);
48 if (!Number.isFinite(days) || days <= 0 || days > 3650) return res.status(400).json({ error: 'expiresInDays must be between 1 and 3650.' });
49 expiresAt = Date.now() + Math.round(days * 86400000);
50 }
51 const { id, token } = store.createPersonalToken({ username: req.user.username, name, scopes, expiresAt });
52 res.set('Cache-Control', 'no-store');
53 res.status(201).json({ id, token, scopes, expiresAt, note: 'Store this token now; it is not shown again.' });
54});
55
56router.get('/clients', requireAdmin, (req, res) =>
57{
58 res.json({ clients: store.listClients().filter((c) => !c.disabled) });
59});
60
61router.post('/clients', requireAdmin, (req, res) =>
62{
63 const body = req.body || {};
64 const name = String(body.name || '').trim().slice(0, 80);
65 const uris = Array.isArray(body.redirectUris) ? body.redirectUris.map((u) => String(u).trim()).filter(Boolean) : [];
66 if (!name) return res.status(400).json({ error: 'name is required.' });
67 if (!uris.length || uris.length > 10) return res.status(400).json({ error: 'Provide 1-10 redirectUris.' });
68 const bad = uris.find((u) => !store.validateRedirectUri(u));
69 if (bad) return res.status(400).json({ error: `Invalid redirect URI: ${bad}. Use https://, http://127.0.0.1 (loopback) or a private-use scheme like com.example.app:/callback.` });
70 res.status(201).json({ client: store.createClient({ name, redirectUris: uris, createdBy: req.user.username }) });
71});
72
73router.delete('/clients/:id', requireAdmin, (req, res) =>
74{
75 const c = store.getClient(req.params.id);
76 if (!c || c.disabled) return res.status(404).json({ error: 'Not found.' });
77 store.deleteClient(c.clientId);
78 res.json({ ok: true });
79});
80
81module.exports = router;