auth

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

context.js (2423B)


      1 import { HttpError } from '../http/http-error.js';
      2 import { resolveClientForHost, resolveHostContext } from '../host-context.js';
      3 
      4 export function requestOrigin(req) {
      5   const forwarded = String(req.headers['x-forwarded-proto'] || '')
      6     .split(',')[0]
      7     .trim();
      8   const proto = forwarded || (req.socket?.encrypted ? 'https' : 'http');
      9   return `${proto}://${req.headers.host}`;
     10 }
     11 
     12 export function canonicalBaseUrl(publicUrl) {
     13   return String(publicUrl || '').replace(/\/+$/, '');
     14 }
     15 
     16 export function canonicalIssuer(publicUrl, realmSlug) {
     17   return `${canonicalBaseUrl(publicUrl)}/oidc/${encodeURIComponent(realmSlug)}`;
     18 }
     19 
     20 export function systemIssuer(publicUrl) {
     21   return canonicalBaseUrl(publicUrl);
     22 }
     23 
     24 function pathSegments(req) {
     25   const pathname = String(req.url || '/').split('?')[0];
     26   return pathname
     27     .split('/')
     28     .filter(Boolean)
     29     .map((segment) => decodeURIComponent(segment));
     30 }
     31 
     32 export async function resolveOidcContext(req, { publicUrl } = {}) {
     33   const segments = pathSegments(req);
     34   const origin = requestOrigin(req);
     35   const hostContext = await resolveHostContext(req, { publicUrl });
     36 
     37   if (hostContext.mode === 'client') {
     38     if (segments.length !== 1) throw new HttpError(404, 'not_found', 'Not found');
     39     return {
     40       mode: 'client',
     41       host: hostContext.host,
     42       origin,
     43       realm: hostContext.realm,
     44       clientId: hostContext.clientId,
     45       systemClient: false,
     46       action: segments[0],
     47     };
     48   }
     49 
     50   if (segments.length !== 2) throw new HttpError(404, 'not_found', 'Not found');
     51   const clientId = segments[0];
     52   const action = segments[1];
     53   const resolved = await resolveClientForHost(hostContext, clientId);
     54 
     55   return {
     56     mode: hostContext.mode,
     57     host: hostContext.host,
     58     origin,
     59     realm: resolved.realm,
     60     clientId: resolved.clientId,
     61     systemClient: resolved.systemClient,
     62     action,
     63   };
     64 }
     65 
     66 export function endpointBase(context) {
     67   if (context.mode === 'client') return `${context.origin}/oidc`;
     68   return `${context.origin}/oidc/${encodeURIComponent(context.clientId)}`;
     69 }
     70 
     71 export function endpointUrls(context) {
     72   const base = endpointBase(context);
     73   return {
     74     openid_configuration: `${base}/openid-configuration`,
     75     login: `${base}/login`,
     76     authorize: `${base}/authorize`,
     77     consent: `${base}/consent`,
     78     token: `${base}/token`,
     79     userinfo: `${base}/userinfo`,
     80     jwks: `${base}/jwks`,
     81     logout: `${base}/logout`,
     82   };
     83 }