feat(auth): implement JWT-based authentication with admin access controls
This commit is contained in:
56
ts/opsserver/helpers/guards.ts
Normal file
56
ts/opsserver/helpers/guards.ts
Normal file
@ -0,0 +1,56 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type { AdminHandler } from '../handlers/admin.handler.js';
|
||||
import * as interfaces from '../../../ts_interfaces/index.js';
|
||||
|
||||
/**
|
||||
* Helper function to use identity guards in handlers
|
||||
*
|
||||
* @example
|
||||
* // In a handler:
|
||||
* await passGuards(toolsArg, this.opsServerRef.adminHandler.validIdentityGuard, dataArg);
|
||||
*/
|
||||
export async function passGuards<T extends { identity?: any }>(
|
||||
toolsArg: any,
|
||||
guard: plugins.smartguard.Guard<T>,
|
||||
dataArg: T
|
||||
): Promise<void> {
|
||||
const result = await guard.exec(dataArg);
|
||||
if (!result) {
|
||||
const failedHint = await guard.getFailedHint(dataArg);
|
||||
throw new plugins.typedrequest.TypedResponseError(failedHint || 'Guard check failed');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to check admin identity in handlers
|
||||
*/
|
||||
export async function requireAdminIdentity<T extends { identity?: interfaces.data.IIdentity }>(
|
||||
adminHandler: AdminHandler,
|
||||
dataArg: T
|
||||
): Promise<void> {
|
||||
if (!dataArg.identity) {
|
||||
throw new plugins.typedrequest.TypedResponseError('No identity provided');
|
||||
}
|
||||
|
||||
const passed = await adminHandler.adminIdentityGuard.exec({ identity: dataArg.identity });
|
||||
if (!passed) {
|
||||
throw new plugins.typedrequest.TypedResponseError('Admin access required');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to check valid identity in handlers
|
||||
*/
|
||||
export async function requireValidIdentity<T extends { identity?: interfaces.data.IIdentity }>(
|
||||
adminHandler: AdminHandler,
|
||||
dataArg: T
|
||||
): Promise<void> {
|
||||
if (!dataArg.identity) {
|
||||
throw new plugins.typedrequest.TypedResponseError('No identity provided');
|
||||
}
|
||||
|
||||
const passed = await adminHandler.validIdentityGuard.exec({ identity: dataArg.identity });
|
||||
if (!passed) {
|
||||
throw new plugins.typedrequest.TypedResponseError('Valid identity required');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user