@@ -3,12 +3,13 @@ import path from 'path';
33import fs from 'fs' ;
44import { Express } from 'express' ;
55import fetch from 'node-fetch' ;
6- import { IAdminForth , IExpressHttpServer } from '../types/Back.js' ;
6+ import { AdminUserAuthorizeFunction , IAdminForth , IExpressHttpServer , HttpExtra } from '../types/Back.js' ;
77import { WebSocketServer } from 'ws' ;
88import { WebSocketClient } from './common.js' ;
99import { AdminUser } from '../types/Common.js' ;
1010import http from 'http' ;
1111import { randomUUID } from 'crypto' ;
12+ import { listify } from '../modules/utils.js' ;
1213
1314function replaceAtStart ( string , substring ) {
1415 if ( string . startsWith ( substring ) ) {
@@ -216,6 +217,25 @@ class ExpressServer implements IExpressHttpServer {
216217 this . server . listen ( ...args ) ;
217218 }
218219
220+ async processAuthorizeCallbacks ( adminUser : AdminUser , toReturn : { error ?: string , allowed : boolean } , response : Response , extra : any ) {
221+ const adminUserAuthorize = this . adminforth . config . auth . adminUserAuthorize as ( AdminUserAuthorizeFunction [ ] | undefined ) ;
222+
223+ for ( const hook of listify ( adminUserAuthorize ) ) {
224+ const resp = await hook ( {
225+ adminUser,
226+ response,
227+ adminforth : this . adminforth ,
228+ extra,
229+ } ) ;
230+ if ( resp ?. allowed === false || resp ?. error ) {
231+ // delete all items from toReturn and add these:
232+ toReturn . allowed = resp ?. allowed ;
233+ toReturn . error = resp ?. error ;
234+ break ;
235+ }
236+ }
237+ }
238+
219239
220240 authorize ( handler ) {
221241 return async ( req , res , next ) => {
@@ -248,7 +268,13 @@ class ExpressServer implements IExpressHttpServer {
248268 res . status ( 401 ) . send ( 'Unauthorized by AdminForth' ) ;
249269 } else {
250270 req . adminUser = adminforthUser ;
251- handler ( req , res , next ) ;
271+ const toReturn : { error ?: string , allowed : boolean } = { allowed : true } ;
272+ await this . processAuthorizeCallbacks ( adminforthUser , toReturn , res , { } ) ;
273+ if ( ! toReturn . allowed ) {
274+ res . status ( 401 ) . send ( 'Unauthorized by AdminForth' ) ;
275+ } else {
276+ handler ( req , res , next ) ;
277+ }
252278 }
253279 } ;
254280 }
0 commit comments