auth

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

admin-user-sessions.js (2456B)


      1 import { ADMIN_SESSION_FIELDS, serialize } from './admin-sessions.js';
      2 import { HttpError } from './http/http-error.js';
      3 import { pagedResult, searchClause } from './pagination.js';
      4 import { isSystemAdmin } from './policy.js';
      5 import { findUserById } from './users.js';
      6 
      7 const SEARCH_COLUMNS = ['s.user_agent', 's.ip', 's.scope_host', 's.client_id', 's.scope_type'];
      8 
      9 // A null realmId drops the session_realms join. The single-row NULL stub keeps
     10 // the ADMIN_SESSION_FIELDS projection and serialize() shape without a realm
     11 // context; can_terminate then falls out of the system-admin check below.
     12 function realmBinding(realmId) {
     13   if (realmId === null) {
     14     return 'CROSS JOIN (SELECT NULL AS revoked_at, NULL AS revoked_by) sr';
     15   }
     16   return `LEFT JOIN session_realms sr
     17          ON sr.session_id = s.id AND sr.realm_id = {realmId}`;
     18 }
     19 
     20 export async function listUserSessions(
     21   systemDb,
     22   { userId, actor, realmId, pagination }
     23 ) {
     24   const user = await findUserById(systemDb, userId);
     25   if (!user) {
     26     throw new HttpError(404, 'user_not_found', 'User not found');
     27   }
     28   if (!isSystemAdmin(actor)) {
     29     if (realmId === null) {
     30       throw new HttpError(403, 'forbidden', 'System administrator access is required');
     31     }
     32     if (user.home_realm_id !== realmId) {
     33       throw new HttpError(
     34         403,
     35         'forbidden',
     36         'Only home-realm and system admins can list sessions for this user'
     37       );
     38     }
     39   }
     40 
     41   const params = {
     42     userId: user.id,
     43     now: Date.now(),
     44     limit: pagination.limit,
     45     offset: pagination.offset,
     46   };
     47   if (realmId !== null) params.realmId = realmId;
     48 
     49   let filter = 's.user_id = {userId} AND s.expires_at > {now}';
     50   if (pagination.q) {
     51     const search = searchClause(pagination.q, SEARCH_COLUMNS);
     52     filter += ` AND ${search.clause}`;
     53     Object.assign(params, search.params);
     54   }
     55 
     56   const rows = await systemDb.all(
     57     `SELECT ${ADMIN_SESSION_FIELDS}
     58        FROM sessions s
     59        JOIN users u ON u.id = s.user_id
     60        ${realmBinding(realmId)}
     61       WHERE ${filter}
     62       ORDER BY s.last_seen_at DESC, s.created_at DESC, s.id DESC
     63       LIMIT {limit} OFFSET {offset}`,
     64     params
     65   );
     66   const count = await systemDb.get(
     67     `SELECT COUNT(*) AS total
     68        FROM sessions s
     69       WHERE ${filter}`,
     70     params
     71   );
     72 
     73   return pagedResult('sessions', rows.map((row) => serialize(row, actor, realmId)), {
     74     limit: pagination.limit,
     75     offset: pagination.offset,
     76     total: count.total,
     77   });
     78 }