auth

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

apps.js (2576B)


      1 import fs from 'node:fs';
      2 import path from 'node:path';
      3 import { fileURLToPath } from 'node:url';
      4 import { app } from '../app.js';
      5 import { canonicalBaseUrl } from './oidc/context.js';
      6 
      7 const here = path.dirname(fileURLToPath(import.meta.url));
      8 const repoRoot = path.resolve(here, '../../..');
      9 const appNames = ['login', 'account', 'admin'];
     10 const cache = new Map();
     11 
     12 function appDist(name) {
     13   if (!appNames.includes(name)) throw new Error(`unknown app: ${name}`);
     14   return path.join(repoRoot, 'app', name, 'dist');
     15 }
     16 
     17 export function appIndex(name) {
     18   const file = path.join(appDist(name), 'index.html');
     19   let modified;
     20   try {
     21     modified = fs.statSync(file).mtimeMs;
     22   } catch {
     23     cache.set(name, null);
     24     return null;
     25   }
     26 
     27   const cached = cache.get(name);
     28   if (cached && cached.modified === modified) return cached.content;
     29 
     30   const content = fs.readFileSync(file);
     31   cache.set(name, { modified, content });
     32   return content;
     33 }
     34 
     35 export function serveAppDocument(res, name) {
     36   const content = appIndex(name);
     37   if (!content) {
     38     res.writeHead(503, { 'content-type': 'text/plain; charset=utf-8' });
     39     res.end(`${name} app build missing`);
     40     return;
     41   }
     42   res.writeHead(200, {
     43     'content-type': 'text/html; charset=utf-8',
     44     'cache-control': 'no-cache',
     45   });
     46   res.end(content);
     47 }
     48 
     49 function addOriginUrl(urls, base, pathname) {
     50   if (!base) return;
     51   urls.add(`${String(base).replace(/\/+$/, '')}${pathname}`);
     52 }
     53 
     54 export function systemClientRedirectUris(context, clientId) {
     55   const urls = new Set();
     56   const publicBase = canonicalBaseUrl(app.config.server.public_url);
     57 
     58   if (clientId === 'admin') {
     59     addOriginUrl(urls, publicBase, '/app/admin/callback');
     60   } else if (clientId === 'account') {
     61     addOriginUrl(urls, publicBase, '/app/account/callback');
     62     if (context.mode === 'realm') {
     63       addOriginUrl(urls, context.origin, '/app/account/callback');
     64     }
     65   }
     66 
     67   const staticOrigin = app.config.apps?.[clientId];
     68   addOriginUrl(urls, staticOrigin, '/callback');
     69   return [...urls];
     70 }
     71 
     72 export function systemClientPostLogoutUris(context, clientId) {
     73   const urls = new Set();
     74   const publicBase = canonicalBaseUrl(app.config.server.public_url);
     75 
     76   if (clientId === 'admin') {
     77     addOriginUrl(urls, publicBase, '/app/admin/');
     78   } else if (clientId === 'account') {
     79     addOriginUrl(urls, publicBase, '/app/account/');
     80     if (context.mode === 'realm') {
     81       addOriginUrl(urls, context.origin, '/app/account/');
     82     }
     83   }
     84 
     85   const staticOrigin = app.config.apps?.[clientId];
     86   addOriginUrl(urls, staticOrigin, '/');
     87   return [...urls];
     88 }