feat(ops-server): implement TypedRouter integration and modular handler classes
This commit is contained in:
148
ts/opsserver/handlers/admin.handler.ts
Normal file
148
ts/opsserver/handlers/admin.handler.ts
Normal file
@ -0,0 +1,148 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type { OpsServer } from '../classes.opsserver.js';
|
||||
import * as interfaces from '../../../ts_interfaces/index.js';
|
||||
|
||||
export class AdminHandler {
|
||||
public typedrouter = new plugins.typedrequest.TypedRouter();
|
||||
|
||||
// Simple in-memory session storage (in production, use proper session management)
|
||||
private sessions = new Map<string, {
|
||||
identity: interfaces.data.IIdentity;
|
||||
createdAt: number;
|
||||
lastAccess: number;
|
||||
}>();
|
||||
|
||||
constructor(private opsServerRef: OpsServer) {
|
||||
// Add this handler's router to the parent
|
||||
this.opsServerRef.typedrouter.addTypedRouter(this.typedrouter);
|
||||
this.registerHandlers();
|
||||
}
|
||||
|
||||
private registerHandlers(): void {
|
||||
// Admin Login Handler
|
||||
this.typedrouter.addTypedHandler(
|
||||
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_AdminLoginWithUsernameAndPassword>(
|
||||
'adminLoginWithUsernameAndPassword',
|
||||
async (dataArg, toolsArg) => {
|
||||
try {
|
||||
// TODO: Implement proper authentication
|
||||
// For now, use a simple hardcoded check
|
||||
if (dataArg.username === 'admin' && dataArg.password === 'admin') {
|
||||
const token = plugins.uuid.v4();
|
||||
const identity: interfaces.data.IIdentity = {
|
||||
token,
|
||||
expiresAt: Date.now() + 24 * 60 * 60 * 1000, // 24 hours
|
||||
permissions: ['admin'],
|
||||
};
|
||||
|
||||
// Store session
|
||||
this.sessions.set(token, {
|
||||
identity,
|
||||
createdAt: Date.now(),
|
||||
lastAccess: Date.now(),
|
||||
});
|
||||
|
||||
// Clean up old sessions
|
||||
this.cleanupSessions();
|
||||
|
||||
return {
|
||||
identity,
|
||||
};
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// Admin Logout Handler
|
||||
this.typedrouter.addTypedHandler(
|
||||
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_AdminLogout>(
|
||||
'adminLogout',
|
||||
async (dataArg, toolsArg) => {
|
||||
if (dataArg.identity?.token && this.sessions.has(dataArg.identity.token)) {
|
||||
this.sessions.delete(dataArg.identity.token);
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// Verify Identity Handler
|
||||
this.typedrouter.addTypedHandler(
|
||||
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_VerifyIdentity>(
|
||||
'verifyIdentity',
|
||||
async (dataArg, toolsArg) => {
|
||||
if (!dataArg.identity?.token) {
|
||||
return {
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
|
||||
const session = this.sessions.get(dataArg.identity.token);
|
||||
if (session && session.identity.expiresAt > Date.now()) {
|
||||
// Update last access
|
||||
session.lastAccess = Date.now();
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
identity: session.identity,
|
||||
};
|
||||
} else {
|
||||
// Clean up expired session
|
||||
if (session) {
|
||||
this.sessions.delete(dataArg.identity.token);
|
||||
}
|
||||
return {
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up expired sessions (older than 24 hours)
|
||||
*/
|
||||
private cleanupSessions(): void {
|
||||
const now = Date.now();
|
||||
const maxAge = 24 * 60 * 60 * 1000; // 24 hours
|
||||
|
||||
for (const [token, session] of this.sessions.entries()) {
|
||||
if (now - session.lastAccess > maxAge) {
|
||||
this.sessions.delete(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a guard for authentication
|
||||
* This can be used by other handlers to protect endpoints
|
||||
*/
|
||||
public createAuthGuard() {
|
||||
return async (dataArg: { identity?: interfaces.data.IIdentity }) => {
|
||||
if (!dataArg.identity?.token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const session = this.sessions.get(dataArg.identity.token);
|
||||
if (session && session.identity.expiresAt > Date.now()) {
|
||||
// Update last access
|
||||
session.lastAccess = Date.now();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user