mirror of
https://dev.azure.com/globalhealthx/EMR/_git/helix-engage-server
synced 2026-05-18 20:08:19 +00:00
Mirrors the existing theme config pattern so website widget settings can be
edited from the admin portal instead of baked into frontend env vars. Fixes
the current symptom where the staging widget is silently disabled because
VITE_WIDGET_KEY is missing from .env.production.
Backend (sidecar):
- src/config/widget.defaults.ts — WidgetConfig type + defaults
(enabled, key, siteId, url, allowedOrigins, hospitalName,
embed.loginPage, version, updatedAt)
- src/config/widget-config.service.ts — file-backed load / update /
rotate-key / reset with backups, mirroring ThemeService. On module init:
* first boot → auto-generates an HMAC-signed site key via
WidgetKeysService, persists both to data/widget.json and to Redis
* subsequent boots → re-registers the key in Redis if missing (handles
Redis flushes so validateKey() keeps working without admin action)
- src/config/widget-config.controller.ts — new endpoints under /api/config:
GET /api/config/widget public subset {enabled, key, url, embed}
GET /api/config/widget/admin full config for the settings UI
PUT /api/config/widget admin update (partial merge)
POST /api/config/widget/rotate-key revoke old siteId + mint a new key
POST /api/config/widget/reset reset to defaults + regenerate
- Move src/widget/widget-keys.service.ts → src/config/widget-keys.service.ts
(it's a config-layer concern now, not widget-layer). config-theme.module
becomes the owner, imports AuthModule for SessionService, and exports
WidgetKeysService + WidgetConfigService alongside ThemeService.
- widget.module stops providing WidgetKeysService (it imports ConfigThemeModule
already, so the guard + controller still get it via DI).
- .gitignore data/widget.json + data/widget-backups/ so each environment
auto-generates its own instance-specific key instead of sharing one via git.
TODO (flagged, out of scope for this pass):
- Protect admin endpoints with an auth guard when settings UI ships.
- Set WIDGET_SECRET env var in staging (currently falls back to the
hardcoded default in widget-keys.service.ts).
- Admin portal settings page for editing widget config (mirror branding-settings).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
95 lines
3.4 KiB
TypeScript
95 lines
3.4 KiB
TypeScript
import { Injectable, Logger } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { createHmac, timingSafeEqual, randomUUID } from 'crypto';
|
|
import { SessionService } from '../auth/session.service';
|
|
import type { WidgetSiteKey } from '../widget/widget.types';
|
|
|
|
const KEY_PREFIX = 'widget:keys:';
|
|
|
|
@Injectable()
|
|
export class WidgetKeysService {
|
|
private readonly logger = new Logger(WidgetKeysService.name);
|
|
private readonly secret: string;
|
|
|
|
constructor(
|
|
private config: ConfigService,
|
|
private session: SessionService,
|
|
) {
|
|
this.secret = process.env.WIDGET_SECRET ?? config.get<string>('WIDGET_SECRET') ?? 'helix-widget-default-secret';
|
|
}
|
|
|
|
generateKey(hospitalName: string, allowedOrigins: string[]): { key: string; siteKey: WidgetSiteKey } {
|
|
const siteId = randomUUID().replace(/-/g, '').substring(0, 16);
|
|
const signature = this.sign(siteId);
|
|
const key = `${siteId}.${signature}`;
|
|
|
|
const siteKey: WidgetSiteKey = {
|
|
siteId,
|
|
hospitalName,
|
|
allowedOrigins,
|
|
active: true,
|
|
createdAt: new Date().toISOString(),
|
|
};
|
|
|
|
return { key, siteKey };
|
|
}
|
|
|
|
async saveKey(siteKey: WidgetSiteKey): Promise<void> {
|
|
await this.session.setCachePersistent(`${KEY_PREFIX}${siteKey.siteId}`, JSON.stringify(siteKey));
|
|
this.logger.log(`Widget key saved: ${siteKey.siteId} (${siteKey.hospitalName})`);
|
|
}
|
|
|
|
async validateKey(rawKey: string): Promise<WidgetSiteKey | null> {
|
|
const dotIndex = rawKey.indexOf('.');
|
|
if (dotIndex === -1) return null;
|
|
|
|
const siteId = rawKey.substring(0, dotIndex);
|
|
const signature = rawKey.substring(dotIndex + 1);
|
|
|
|
const expected = this.sign(siteId);
|
|
try {
|
|
if (!timingSafeEqual(Buffer.from(signature, 'hex'), Buffer.from(expected, 'hex'))) return null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
|
|
const data = await this.session.getCache(`${KEY_PREFIX}${siteId}`);
|
|
if (!data) return null;
|
|
|
|
const siteKey: WidgetSiteKey = JSON.parse(data);
|
|
if (!siteKey.active) return null;
|
|
|
|
return siteKey;
|
|
}
|
|
|
|
validateOrigin(siteKey: WidgetSiteKey, origin: string | undefined): boolean {
|
|
if (!origin) return true; // Allow no-origin for dev/testing
|
|
if (siteKey.allowedOrigins.length === 0) return true;
|
|
return siteKey.allowedOrigins.some(allowed => origin.startsWith(allowed));
|
|
}
|
|
|
|
async listKeys(): Promise<WidgetSiteKey[]> {
|
|
const keys = await this.session.scanKeys(`${KEY_PREFIX}*`);
|
|
const results: WidgetSiteKey[] = [];
|
|
for (const key of keys) {
|
|
const data = await this.session.getCache(key);
|
|
if (data) results.push(JSON.parse(data));
|
|
}
|
|
return results;
|
|
}
|
|
|
|
async revokeKey(siteId: string): Promise<boolean> {
|
|
const data = await this.session.getCache(`${KEY_PREFIX}${siteId}`);
|
|
if (!data) return false;
|
|
const siteKey: WidgetSiteKey = JSON.parse(data);
|
|
siteKey.active = false;
|
|
await this.session.setCachePersistent(`${KEY_PREFIX}${siteId}`, JSON.stringify(siteKey));
|
|
this.logger.log(`Widget key revoked: ${siteId}`);
|
|
return true;
|
|
}
|
|
|
|
private sign(data: string): string {
|
|
return createHmac('sha256', this.secret).update(data).digest('hex');
|
|
}
|
|
}
|