auth

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

terminate-all.js (1216B)


      1 import { app } from '../../../app.js';
      2 import { authenticateAdminRequest } from '../../../lib/admin-auth.js';
      3 import { HttpError } from '../../../lib/http/http-error.js';
      4 import { isSystemAdmin } from '../../../lib/policy.js';
      5 import { destroyUserSessions } from '../../../lib/session.js';
      6 import { findUserById } from '../../../lib/users.js';
      7 
      8 export default {
      9   method: 'delete',
     10   path: '/api/v1/admin/realms/:realm/users/:userId/sessions',
     11   name: 'admin.userSessions.terminateAll',
     12   async handler(req) {
     13     const auth = await authenticateAdminRequest(req, {
     14       realmSlug: req.params.realm,
     15     });
     16     const user = await findUserById(auth.systemDb, req.params.userId);
     17     if (!user) {
     18       throw new HttpError(404, 'user_not_found', 'User not found');
     19     }
     20     if (!isSystemAdmin(auth.user) && user.home_realm_id !== auth.realm.id) {
     21       throw new HttpError(
     22         403,
     23         'forbidden',
     24         'Only home-realm and system admins can terminate sessions for this user'
     25       );
     26     }
     27 
     28     const result = await destroyUserSessions(auth.systemDb, { userId: user.id });
     29     return new app.HttpOk(
     30       { ok: true, destroyed: result.destroyed },
     31       { 'cache-control': 'no-store' }
     32     );
     33   },
     34 };