auth

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

clients.js (3724B)


      1 import crypto from 'node:crypto';
      2 import { normalizeBrandingObject } from '../admin-branding.js';
      3 
      4 const SYSTEM_CLIENTS = {
      5   admin: {
      6     id: 'system:admin',
      7     client_id: 'admin',
      8     name: 'Admin',
      9     access_mode: 'admin',
     10     enabled: 1,
     11     require_pkce: 1,
     12     token_endpoint_auth_method: 'none',
     13     redirect_uris: [],
     14     post_logout_redirect_uris: [],
     15     branding: {},
     16     required_roles: [],
     17     system_client: true,
     18   },
     19   account: {
     20     id: 'system:account',
     21     client_id: 'account',
     22     name: 'Account',
     23     access_mode: 'public',
     24     enabled: 1,
     25     require_pkce: 1,
     26     token_endpoint_auth_method: 'none',
     27     redirect_uris: [],
     28     post_logout_redirect_uris: [],
     29     branding: {},
     30     required_roles: [],
     31     system_client: true,
     32   },
     33 };
     34 
     35 function parseJson(raw, fallback) {
     36   try {
     37     return JSON.parse(raw || 'null') ?? fallback;
     38   } catch {
     39     return fallback;
     40   }
     41 }
     42 
     43 export function hashClientSecret(secret) {
     44   return crypto.createHash('sha256').update(secret).digest('hex');
     45 }
     46 
     47 export function isSystemClient(clientId) {
     48   return Object.hasOwn(SYSTEM_CLIENTS, clientId);
     49 }
     50 
     51 export function getSystemClient(clientId) {
     52   const client = SYSTEM_CLIENTS[clientId];
     53   return client ? { ...client } : null;
     54 }
     55 
     56 export async function getClient(db, clientId) {
     57   const system = SYSTEM_CLIENTS[clientId];
     58   if (system) return { ...system };
     59 
     60   const row = await db.get(
     61     `SELECT id, client_id, name, redirect_uris, scopes, access_mode, enabled,
     62             client_secret_hash, token_endpoint_auth_method, require_pkce,
     63             post_logout_redirect_uris, branding, consent_mode
     64        FROM oidc_clients
     65       WHERE client_id = {clientId}`,
     66     { clientId }
     67   );
     68   if (!row) return null;
     69 
     70   const roles = await db.all(
     71     `SELECT r.slug
     72        FROM oidc_client_roles cr
     73        JOIN roles r ON r.id = cr.role_id
     74       WHERE cr.client_id = {id}`,
     75     { id: row.id }
     76   );
     77 
     78   return {
     79     ...row,
     80     enabled: Boolean(row.enabled),
     81     require_pkce: Boolean(row.require_pkce),
     82     redirect_uris: parseJson(row.redirect_uris, []),
     83     post_logout_redirect_uris: parseJson(row.post_logout_redirect_uris, []),
     84     branding: normalizeBrandingObject(row.branding),
     85     required_roles: roles.map((role) => role.slug),
     86     system_client: false,
     87   };
     88 }
     89 
     90 export function redirectUriAllowed(client, redirectUri, systemCallbacks = []) {
     91   if (client.system_client) return systemCallbacks.includes(redirectUri);
     92   return client.redirect_uris.includes(redirectUri);
     93 }
     94 
     95 export function postLogoutRedirectAllowed(client, redirectUri, systemCallbacks = []) {
     96   if (!redirectUri) return true;
     97   if (client.system_client) return systemCallbacks.includes(redirectUri);
     98   return client.post_logout_redirect_uris.includes(redirectUri);
     99 }
    100 
    101 export function authenticateClient(req, client, body = {}) {
    102   const method = client.token_endpoint_auth_method || 'none';
    103 
    104   if (method === 'none') {
    105     return !body.client_id || body.client_id === client.client_id;
    106   }
    107 
    108   if (method !== 'client_secret_basic' || !client.client_secret_hash) {
    109     return false;
    110   }
    111 
    112   const header = String(req.headers.authorization || '');
    113   if (!header.startsWith('Basic ')) return false;
    114 
    115   let decoded;
    116   try {
    117     decoded = Buffer.from(header.slice(6), 'base64').toString('utf8');
    118   } catch {
    119     return false;
    120   }
    121 
    122   const colon = decoded.indexOf(':');
    123   if (colon === -1) return false;
    124   const clientId = decoded.slice(0, colon);
    125   const secret = decoded.slice(colon + 1);
    126   if (clientId !== client.client_id) return false;
    127 
    128   const expected = Buffer.from(client.client_secret_hash, 'hex');
    129   const actual = Buffer.from(hashClientSecret(secret), 'hex');
    130   return expected.length === actual.length && crypto.timingSafeEqual(expected, actual);
    131 }