terminate.js (1145B)
1 import { app } from '../../../app.js'; 2 import { authenticateAdminRequest } from '../../../lib/admin-auth.js'; 3 import { 4 canTerminateSession, 5 findAdminRealmSession, 6 } from '../../../lib/admin-sessions.js'; 7 import { HttpError } from '../../../lib/http/http-error.js'; 8 import { destroySessionById } from '../../../lib/session.js'; 9 10 export default { 11 method: 'delete', 12 path: '/api/v1/admin/realms/:realm/sessions/:publicId', 13 name: 'admin.sessions.terminate', 14 async handler(req) { 15 const auth = await authenticateAdminRequest(req, { 16 realmSlug: req.params.realm, 17 }); 18 const session = await findAdminRealmSession(auth.systemDb, { 19 realmId: auth.realm.id, 20 publicId: req.params.publicId, 21 }); 22 if (!session) { 23 throw new HttpError(404, 'session_not_found', 'Session not found'); 24 } 25 if (!canTerminateSession(auth.user, session, auth.realm.id)) { 26 throw new HttpError( 27 403, 28 'forbidden', 29 'Only home-realm and system admins can terminate this session' 30 ); 31 } 32 33 await destroySessionById(auth.systemDb, session.id); 34 return new app.HttpOk({ ok: true }); 35 }, 36 };