auth

Main authentication module for my projects
git clone git://git.finwo.net/app/auth
Log | Files | Refs

terminate.js (1273B)


      1 import { app } from '../../../app.js';
      2 import { authenticateAdminSession } from '../../../lib/admin-auth.js';
      3 import { HttpError } from '../../../lib/http/http-error.js';
      4 import { isSystemAdmin } from '../../../lib/policy.js';
      5 import { destroySessionById } from '../../../lib/session.js';
      6 
      7 export default {
      8   method: 'delete',
      9   path: '/api/v1/admin/users/:userId/sessions/:publicId',
     10   name: 'admin.userSessionsGlobal.terminate',
     11   async handler(req) {
     12     const auth = await authenticateAdminSession(req);
     13     if (!isSystemAdmin(auth.user)) {
     14       throw new HttpError(403, 'forbidden', 'System administrator access is required');
     15     }
     16 
     17     // Expired rows stay terminable so stale group and refresh state can be
     18     // cleared; ownership on user_id keeps cross-user guesses at 404.
     19     const row = await auth.systemDb.get(
     20       `SELECT id FROM sessions
     21         WHERE id = (SELECT id FROM sessions WHERE public_id = {publicId})
     22           AND user_id = {userId}`,
     23       { publicId: req.params.publicId, userId: req.params.userId }
     24     );
     25     if (!row) {
     26       throw new HttpError(404, 'session_not_found', 'Session not found');
     27     }
     28 
     29     await destroySessionById(auth.systemDb, row.id);
     30     return new app.HttpOk({ ok: true }, { 'cache-control': 'no-store' });
     31   },
     32 };