auth

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

admin-settings.js (4114B)


      1 import {
      2   applyBrandingPatch,
      3   hasBrandingPatch,
      4   normalizeBrandingPatch,
      5   serializeBranding,
      6 } from './admin-branding.js';
      7 import { HttpError } from './http/http-error.js';
      8 
      9 const DISPLAY_NAME_MAX_LENGTH = 100;
     10 const ALLOWED_FIELDS = new Set([
     11   'display_name',
     12   'allow_session_duration_choice',
     13   'logo_url',
     14   'primary_color',
     15 ]);
     16 
     17 function invalidRequest(message) {
     18   return new HttpError(400, 'invalid_request', message);
     19 }
     20 
     21 function isPlainObject(value) {
     22   return value !== null && typeof value === 'object' && !Array.isArray(value);
     23 }
     24 
     25 export function validateDisplayName(value) {
     26   if (typeof value !== 'string') {
     27     throw invalidRequest('display_name must be a string');
     28   }
     29 
     30   const displayName = value.trim();
     31   if (!displayName) {
     32     throw invalidRequest('display_name must not be empty');
     33   }
     34   if (displayName.length > DISPLAY_NAME_MAX_LENGTH) {
     35     throw invalidRequest(
     36       `display_name must be at most ${DISPLAY_NAME_MAX_LENGTH} characters`
     37     );
     38   }
     39   if (/[\r\n]/.test(displayName)) {
     40     throw invalidRequest('display_name must be a single line');
     41   }
     42 
     43   return displayName;
     44 }
     45 
     46 function validateAllowSessionDurationChoice(value) {
     47   if (typeof value !== 'boolean') {
     48     throw invalidRequest('allow_session_duration_choice must be a boolean');
     49   }
     50   return value;
     51 }
     52 
     53 function serializeSettings(row, realmSlug) {
     54   return {
     55     slug: realmSlug,
     56     display_name: row.display_name,
     57     allow_session_duration_choice: Boolean(row.allow_session_duration_choice),
     58     ...serializeBranding(row.branding),
     59     created_at: row.created_at,
     60     updated_at: row.updated_at,
     61   };
     62 }
     63 
     64 async function loadSettingsRow(realmDb, realmSlug) {
     65   const row = await realmDb.get(
     66     `SELECT slug, display_name, branding, allow_session_duration_choice,
     67             created_at, updated_at
     68        FROM realm_settings
     69       WHERE id = 1`
     70   );
     71 
     72   if (!row || row.slug !== realmSlug) {
     73     throw new HttpError(404, 'realm_not_found', 'Realm settings not found');
     74   }
     75 
     76   return row;
     77 }
     78 
     79 export function normalizeAdminRealmSettingsPatch(body) {
     80   if (!isPlainObject(body)) {
     81     throw invalidRequest('Request body must be a JSON object');
     82   }
     83 
     84   for (const field of Object.keys(body)) {
     85     if (!ALLOWED_FIELDS.has(field)) {
     86       throw invalidRequest(`Unknown field: ${field}`);
     87     }
     88   }
     89 
     90   if (Object.keys(body).length === 0) {
     91     throw invalidRequest('At least one setting must be provided');
     92   }
     93 
     94   const input = {};
     95 
     96   if (Object.hasOwn(body, 'display_name')) {
     97     input.display_name = validateDisplayName(body.display_name);
     98   }
     99   if (Object.hasOwn(body, 'allow_session_duration_choice')) {
    100     input.allow_session_duration_choice = validateAllowSessionDurationChoice(
    101       body.allow_session_duration_choice
    102     );
    103   }
    104 
    105   Object.assign(input, normalizeBrandingPatch(body));
    106 
    107   return input;
    108 }
    109 
    110 export async function getAdminRealmSettings(realmDb, realmSlug) {
    111   const row = await loadSettingsRow(realmDb, realmSlug);
    112   return serializeSettings(row, realmSlug);
    113 }
    114 
    115 export async function updateAdminRealmSettings({
    116   realmDb,
    117   realmSlug,
    118   body,
    119   now = Date.now(),
    120 }) {
    121   const input = normalizeAdminRealmSettingsPatch(body);
    122   const current = await loadSettingsRow(realmDb, realmSlug);
    123 
    124   const displayName = Object.hasOwn(input, 'display_name')
    125     ? input.display_name
    126     : current.display_name;
    127   const allowSessionDurationChoice = Object.hasOwn(input, 'allow_session_duration_choice')
    128     ? input.allow_session_duration_choice
    129     : Boolean(current.allow_session_duration_choice);
    130   const branding = hasBrandingPatch(input)
    131     ? applyBrandingPatch(current.branding, input)
    132     : current.branding;
    133 
    134   await realmDb.run(
    135     `UPDATE realm_settings
    136         SET display_name = {displayName},
    137             branding = {branding},
    138             allow_session_duration_choice = {allowSessionDurationChoice},
    139             updated_at = {now}
    140       WHERE id = 1`,
    141     {
    142       displayName,
    143       branding,
    144       allowSessionDurationChoice: allowSessionDurationChoice ? 1 : 0,
    145       now,
    146     }
    147   );
    148 
    149   const updated = await loadSettingsRow(realmDb, realmSlug);
    150   return serializeSettings(updated, realmSlug);
    151 }