auth

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

admin-realm-hosts.js (3618B)


      1 import { HttpError } from './http/http-error.js';
      2 import { pagedResult, searchClause } from './pagination.js';
      3 import {
      4   addRealmHost,
      5   findOidcClientHost,
      6   findRealmHost,
      7   listRealmHosts,
      8   normalizeHost,
      9   removeRealmHost,
     10   validateRealmHostname,
     11 } from './realm.js';
     12 
     13 function invalidRequest(message) {
     14   return new HttpError(400, 'invalid_request', message);
     15 }
     16 
     17 export function normalizeReservedHosts(cfg) {
     18   const sources = [cfg?.server?.public_url, cfg?.apps?.admin, cfg?.apps?.account];
     19   const reserved = new Set();
     20   for (const value of sources) {
     21     if (!value) continue;
     22     let hostname;
     23     try {
     24       hostname = new URL(value).hostname;
     25     } catch {
     26       continue;
     27     }
     28     const normalized = normalizeHost(hostname);
     29     if (normalized) reserved.add(normalized);
     30   }
     31   return reserved;
     32 }
     33 
     34 export async function listAdminRealmHosts({ systemDb, realm }) {
     35   const rows = await listRealmHosts(systemDb, realm.id);
     36   return rows.map((row) => ({ host: row.host, created_at: row.created_at }));
     37 }
     38 
     39 export async function listAdminRealmHostsPage({ systemDb, realm, pagination }) {
     40   const params = { realmId: realm.id, limit: pagination.limit, offset: pagination.offset };
     41   let filter = 'realm_id = {realmId}';
     42   if (pagination.q) {
     43     const search = searchClause(pagination.q, ['host']);
     44     filter += ` AND ${search.clause}`;
     45     Object.assign(params, search.params);
     46   }
     47 
     48   const rows = await systemDb.all(
     49     `SELECT host, created_at FROM realm_hosts
     50       WHERE ${filter}
     51       ORDER BY host ASC
     52       LIMIT {limit} OFFSET {offset}`,
     53     params
     54   );
     55   const count = await systemDb.get(
     56     `SELECT COUNT(*) AS total FROM realm_hosts WHERE ${filter}`,
     57     params
     58   );
     59 
     60   return pagedResult(
     61     'hosts',
     62     rows.map((row) => ({ host: row.host, created_at: row.created_at })),
     63     { limit: pagination.limit, offset: pagination.offset, total: count.total }
     64   );
     65 }
     66 
     67 export async function addAdminRealmHost({
     68   systemDb,
     69   realm,
     70   host,
     71   isSystemAdmin = false,
     72   reservedHosts,
     73 }) {
     74   const normalized = validateRealmHostname(host);
     75 
     76   if (reservedHosts && reservedHosts.has(normalized)) {
     77     throw new HttpError(
     78       409,
     79       'host_reserved',
     80       'This hostname is reserved for the authentication service.'
     81     );
     82   }
     83 
     84   const clientHost = await findOidcClientHost(normalized);
     85   if (clientHost) {
     86     throw new HttpError(
     87       409,
     88       'host_in_use',
     89       isSystemAdmin
     90         ? 'This hostname is already assigned to a client application.'
     91         : 'This hostname is already assigned.'
     92     );
     93   }
     94 
     95   const existing = await findRealmHost(normalized);
     96   if (existing) {
     97     if (existing.id === realm.id) {
     98       throw new HttpError(409, 'host_in_use', 'This hostname is already assigned to this realm.');
     99     }
    100     throw new HttpError(
    101       409,
    102       'host_in_use',
    103       isSystemAdmin
    104         ? `This hostname is already assigned to ${existing.slug}.`
    105         : 'This hostname is already assigned to another realm.'
    106     );
    107   }
    108 
    109   const createdAt = Date.now();
    110   try {
    111     await addRealmHost(realm.id, normalized);
    112   } catch {
    113     throw new HttpError(409, 'host_in_use', 'This hostname is already assigned.');
    114   }
    115 
    116   return { host: normalized, created_at: createdAt };
    117 }
    118 
    119 export async function deleteAdminRealmHost({ systemDb, realm, host }) {
    120   const normalized = normalizeHost(host);
    121   if (!normalized) throw invalidRequest('host is required');
    122 
    123   const removed = await removeRealmHost(systemDb, realm.id, normalized);
    124   if (!removed) {
    125     throw new HttpError(404, 'host_not_found', 'Host is not assigned to this realm.');
    126   }
    127 
    128   return { removed: true, host: normalized };
    129 }