auth

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

client-hosts.js (2777B)


      1 import { app } from '../../app.js';
      2 import { authenticateAdminRequest } from '../../lib/admin-auth.js';
      3 import {
      4   addAdminClientHost,
      5   deleteAdminClientHost,
      6   listAdminClientHostsPage,
      7 } from '../../lib/admin-client-hosts.js';
      8 import { normalizeReservedHosts } from '../../lib/admin-realm-hosts.js';
      9 import { HttpError } from '../../lib/http/http-error.js';
     10 import { parsePagination } from '../../lib/pagination.js';
     11 import { isSystemAdmin } from '../../lib/policy.js';
     12 
     13 const reservedHosts = normalizeReservedHosts(app.config);
     14 
     15 function assertBody(body) {
     16   if (body === null || typeof body !== 'object' || Array.isArray(body)) {
     17     throw new HttpError(400, 'invalid_request', 'Request body must be a JSON object');
     18   }
     19   for (const field of Object.keys(body)) {
     20     if (field !== 'host') {
     21       throw new HttpError(400, 'invalid_request', `Unknown field: ${field}`);
     22     }
     23   }
     24 }
     25 
     26 export default [
     27   {
     28     method: 'get',
     29     path: '/api/v1/admin/realms/:realm/clients/:clientId/hosts',
     30     name: 'admin.client-hosts.list',
     31     async handler(req) {
     32       const auth = await authenticateAdminRequest(req, {
     33         realmSlug: req.params.realm,
     34       });
     35       const pagination = parsePagination(
     36         Object.fromEntries(new URL(req.url, 'http://localhost').searchParams)
     37       );
     38 
     39       const result = await listAdminClientHostsPage({
     40         systemDb: auth.systemDb,
     41         realm: auth.realm,
     42         clientId: req.params.clientId,
     43         pagination,
     44       });
     45 
     46       return new app.HttpOk(result, { 'cache-control': 'no-store' });
     47     },
     48   },
     49   {
     50     method: 'post',
     51     path: '/api/v1/admin/realms/:realm/clients/:clientId/hosts',
     52     name: 'admin.client-hosts.create',
     53     async handler(req) {
     54       const auth = await authenticateAdminRequest(req, {
     55         realmSlug: req.params.realm,
     56       });
     57       const body = req.body || {};
     58       assertBody(body);
     59       const host = await addAdminClientHost({
     60         systemDb: auth.systemDb,
     61         realm: auth.realm,
     62         clientId: req.params.clientId,
     63         host: body.host,
     64         isSystemAdmin: isSystemAdmin(auth.user),
     65         reservedHosts,
     66       });
     67 
     68       return new app.HttpOk({ ok: true, host }, { 'cache-control': 'no-store' });
     69     },
     70   },
     71   {
     72     method: 'delete',
     73     path: '/api/v1/admin/realms/:realm/clients/:clientId/hosts/:host',
     74     name: 'admin.client-hosts.delete',
     75     async handler(req) {
     76       const auth = await authenticateAdminRequest(req, {
     77         realmSlug: req.params.realm,
     78       });
     79       const result = await deleteAdminClientHost({
     80         systemDb: auth.systemDb,
     81         realm: auth.realm,
     82         clientId: req.params.clientId,
     83         host: req.params.host,
     84       });
     85 
     86       return new app.HttpOk({ ok: true, ...result }, { 'cache-control': 'no-store' });
     87     },
     88   },
     89 ];