fase(17): licensing module with RSA validation

This commit is contained in:
debian
2026-03-08 05:20:54 -04:00
parent 1f1678af17
commit 5a28270dc9
45 changed files with 1789 additions and 48 deletions

View File

@@ -1 +1 @@
cffa1aeea99f01504bc6c016e12fc62ba63977c7
1f1678af17637b190210f6a2f16acff4b0ee2427

View File

@@ -285,40 +285,40 @@ Spec: `.ralph/specs/phase-15-reporting.md`
---
## Phase 16: Integrations Module [PENDIENTE]
## Phase 16: Integrations Module [COMPLETO]
Spec: `.ralph/specs/phase-16-integrations.md`
- [ ] 16.1: Instalar: `npm i @slack/web-api @octokit/rest`
- [ ] 16.2: Crear domain: `Integration.ts` (Entity), `WebhookEndpoint.ts` (Entity)
- [ ] 16.3: Crear value objects: `IntegrationType.ts` (jira/slack/github/webhook), `WebhookSecret.ts`
- [ ] 16.4: Crear port: `IIntegrationProvider.ts` (sendFinding)
- [ ] 16.5: Crear `infrastructure/webhooks/WebhookDispatcher.ts` — HMAC-SHA256 signature, retry con exponential backoff (3 intentos)
- [ ] 16.6: Crear `infrastructure/providers/SlackProvider.ts` — Block Kit message con severity, description, link
- [ ] 16.7: Crear `infrastructure/providers/GitHubIssuesProvider.ts` — crea issue con reproduction steps
- [ ] 16.8: Crear `infrastructure/providers/JiraProvider.ts` — REST API v3, crea issue con screenshots
- [ ] 16.9: Crear `event-handlers/OnFindingCreated.ts` — dispatch a todas las integrations activas
- [ ] 16.10: Crear `infrastructure/http/IntegrationsController.ts` — CRUD integrations + webhooks
- [ ] 16.11: Migración Kysely: tables integrations, webhook_endpoints, webhook_deliveries
- [ ] 16.12: Frontend: Settings/Integrations con forms por provider (Slack webhook URL, Jira config, GitHub token, custom webhook)
- [ ] 16.13: Tests: webhook dispatch + HMAC verification
- [ ] 16.14: Verificar build completo + commit: `fase(16): integrations module`
- [x] 16.1: Instalar: `npm i @slack/web-api @octokit/rest`
- [x] 16.2: Crear domain: `Integration.ts` (Entity), `WebhookEndpoint.ts` (Entity)
- [x] 16.3: Crear value objects: `IntegrationType.ts` (jira/slack/github/webhook), `WebhookSecret.ts`
- [x] 16.4: Crear port: `IIntegrationProvider.ts` (sendFinding)
- [x] 16.5: Crear `infrastructure/webhooks/WebhookDispatcher.ts` — HMAC-SHA256 signature, retry con exponential backoff (3 intentos)
- [x] 16.6: Crear `infrastructure/providers/SlackProvider.ts` — Block Kit message con severity, description, link
- [x] 16.7: Crear `infrastructure/providers/GitHubIssuesProvider.ts` — crea issue con reproduction steps
- [x] 16.8: Crear `infrastructure/providers/JiraProvider.ts` — REST API v3, crea issue con screenshots
- [x] 16.9: Crear `event-handlers/OnFindingCreated.ts` — dispatch a todas las integrations activas
- [x] 16.10: Crear `infrastructure/http/IntegrationsController.ts` — CRUD integrations + webhooks
- [x] 16.11: Migración Kysely: tables integrations, webhook_endpoints, webhook_deliveries
- [x] 16.12: Frontend: Settings/Integrations con forms por provider (Slack webhook URL, Jira config, GitHub token, custom webhook)
- [x] 16.13: Tests: webhook dispatch + HMAC verification
- [x] 16.14: Verificar build completo + commit: `fase(16): integrations module`
---
## Phase 17: Licensing Module [PENDIENTE]
## Phase 17: Licensing Module [COMPLETO]
Spec: `.ralph/specs/phase-17-licensing.md`
- [ ] 17.1: Crear domain: `License.ts` (Entity), value objects `LicensePlan.ts` (free/pro/enterprise), `FeatureEntitlement.ts`
- [ ] 17.2: Crear port: `ILicenseValidator.ts` (validate, getEntitlements)
- [ ] 17.3: Crear `infrastructure/RSALicenseValidator.ts` — verifica firma RSA-2048 con public key bundled
- [ ] 17.4: Crear feature flags: `FREE_FEATURES`, `PRO_FEATURES`, `ENTERPRISE_FEATURES` arrays
- [ ] 17.5: Crear `infrastructure/middleware/FeatureGateMiddleware.ts` — checkea feature en license antes de permitir request
- [ ] 17.6: Crear `infrastructure/http/LicensingController.ts` — POST /api/license/activate, GET /api/license/status
- [ ] 17.7: Crear `scripts/generate-license.ts` — CLI tool para generar license keys firmadas (uso interno)
- [ ] 17.8: Integrar gate checks en rutas Pro/Enterprise (reporting, integrations, etc.)
- [ ] 17.9: Frontend: License section en Settings
- [ ] 17.10: Tests: valid license passes, expired fails, wrong signature fails, feature gate blocks
- [ ] 17.11: Verificar build completo + commit: `fase(17): licensing module with RSA validation`
- [x] 17.1: Crear domain: `License.ts` (Entity), value objects `LicensePlan.ts` (free/pro/enterprise), `FeatureEntitlement.ts`
- [x] 17.2: Crear port: `ILicenseValidator.ts` (validate, getEntitlements)
- [x] 17.3: Crear `infrastructure/RSALicenseValidator.ts` — verifica firma RSA-2048 con public key bundled
- [x] 17.4: Crear feature flags: `FREE_FEATURES`, `PRO_FEATURES`, `ENTERPRISE_FEATURES` arrays
- [x] 17.5: Crear `infrastructure/middleware/FeatureGateMiddleware.ts` — checkea feature en license antes de permitir request
- [x] 17.6: Crear `infrastructure/http/LicensingController.ts` — POST /api/license/activate, GET /api/license/status
- [x] 17.7: Crear `scripts/generate-license.ts` — CLI tool para generar license keys firmadas (uso interno)
- [x] 17.8: Integrar gate checks en rutas Pro/Enterprise (reporting, integrations, etc.)
- [x] 17.9: Frontend: License section en Settings
- [x] 17.10: Tests: valid license passes, expired fails, wrong signature fails, feature gate blocks
- [x] 17.11: Verificar build completo + commit: `fase(17): licensing module with RSA validation`
---

View File

@@ -1,7 +1,7 @@
{
"status": "executing",
"indicator": "",
"elapsed_seconds": 630,
"indicator": "",
"elapsed_seconds": 10,
"last_output": "",
"timestamp": "2026-03-06 07:21:36"
"timestamp": "2026-03-06 11:29:02"
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

11
dist/api/router.js vendored
View File

@@ -10,11 +10,13 @@ const FindingsController_1 = require("../modules/findings/infrastructure/http/Fi
const FuzzingController_1 = require("../modules/fuzzing/infrastructure/http/FuzzingController");
const ReportingController_1 = require("../modules/reporting/infrastructure/http/ReportingController");
const IntegrationsController_1 = require("../modules/integrations/infrastructure/http/IntegrationsController");
const LicensingController_1 = require("../modules/licensing/infrastructure/http/LicensingController");
const FeatureGateMiddleware_1 = require("../modules/licensing/infrastructure/middleware/FeatureGateMiddleware");
const AuthController_1 = require("../modules/auth/infrastructure/http/AuthController");
const AuthMiddleware_1 = require("../modules/auth/application/middleware/AuthMiddleware");
function createRouter(deps) {
const router = (0, express_1.Router)();
const { authDeps } = deps;
const { authDeps, licenseService } = deps;
// Auth routes — public (no auth middleware)
router.use('/auth', (0, AuthController_1.createAuthController)(authDeps.registerCommand, authDeps.loginCommand, authDeps.createOrgCommand, authDeps.inviteMemberCommand, authDeps.createApiKeyCommand, authDeps.getUserQuery, authDeps.listOrgMembersQuery, authDeps.sessionRepository, authDeps.apiKeyRepository, authDeps.userRepository));
// Apply auth middleware to all routes below
@@ -23,7 +25,10 @@ function createRouter(deps) {
router.use('/sessions', (0, CrawlingController_1.createCrawlingRouter)(deps.crawlingDeps));
router.use('/findings', (0, FindingsController_1.createFindingsRouter)(deps.findingsDeps));
router.use('/fuzz', (0, FuzzingController_1.createFuzzingRouter)(deps.fuzzingDeps));
router.use('/reports', (0, ReportingController_1.createReportingRouter)(deps.reportingDeps));
router.use('/integrations', (0, IntegrationsController_1.createIntegrationsRouter)(deps.integrationsDeps));
router.use('/reports', (0, FeatureGateMiddleware_1.requireFeature)(licenseService, 'reports:basic'), (0, ReportingController_1.createReportingRouter)(deps.reportingDeps));
router.use('/integrations', (0, FeatureGateMiddleware_1.requireFeature)(licenseService, 'integrations:webhook'), (0, IntegrationsController_1.createIntegrationsRouter)(deps.integrationsDeps));
// Licensing routes (public-ish — only status and activate, no sensitive data)
const licensingController = new LicensingController_1.LicensingController(licenseService);
router.use('/license', licensingController.router);
return router;
}

9
dist/main.js vendored
View File

@@ -57,6 +57,9 @@ const KyselyIntegrationRepository_1 = require("./modules/integrations/infrastruc
const KyselyWebhookEndpointRepository_1 = require("./modules/integrations/infrastructure/repositories/KyselyWebhookEndpointRepository");
const WebhookDispatcher_1 = require("./modules/integrations/infrastructure/webhooks/WebhookDispatcher");
const OnFindingCreated_1 = require("./modules/integrations/application/event-handlers/OnFindingCreated");
// Licensing module
const RSALicenseValidator_1 = require("./modules/licensing/infrastructure/validators/RSALicenseValidator");
const LicenseService_1 = require("./modules/licensing/application/LicenseService");
// Job queue
const SQLiteJobQueue_1 = require("./jobs/SQLiteJobQueue");
const ExplorationWorker_1 = require("./jobs/workers/ExplorationWorker");
@@ -119,7 +122,10 @@ async function bootstrap() {
const listOrgMembersQuery = new ListOrgMembersQuery_1.ListOrgMembersQuery(orgRepo, userRepo);
// 11. Reporting use cases
const generateReport = new GenerateReportCommand_1.GenerateReportCommand(reportRepo, eventBus);
// 11b. Integrations
// 11b. Licensing
const licenseValidator = new RSALicenseValidator_1.RSALicenseValidator();
const licenseService = new LicenseService_1.LicenseService(licenseValidator);
// 11c. Integrations
const integrationRepo = new KyselyIntegrationRepository_1.KyselyIntegrationRepository(db);
const webhookRepo = new KyselyWebhookEndpointRepository_1.KyselyWebhookEndpointRepository(db);
const webhookDispatcher = new WebhookDispatcher_1.WebhookDispatcher(webhookRepo, logger);
@@ -140,6 +146,7 @@ async function bootstrap() {
fuzzingDeps: { runFuzz, repository: fuzzRepo },
reportingDeps: { generateReport, reportRepository: reportRepo, jobQueue },
integrationsDeps: { integrationRepo, webhookRepo },
licenseService,
authDeps: {
registerCommand,
loginCommand,

View File

@@ -0,0 +1,167 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const crypto_1 = require("crypto");
const Integration_1 = require("../domain/entities/Integration");
const IntegrationType_1 = require("../domain/value-objects/IntegrationType");
const WebhookEndpoint_1 = require("../domain/entities/WebhookEndpoint");
const WebhookSecret_1 = require("../domain/value-objects/WebhookSecret");
const WebhookDispatcher_1 = require("../infrastructure/webhooks/WebhookDispatcher");
const pino_1 = require("pino");
// ─── Integration Entity ───────────────────────────────────────────────────────
(0, vitest_1.describe)('Integration', () => {
(0, vitest_1.it)('creates with defaults', () => {
const integration = Integration_1.Integration.create({
name: 'My Slack',
type: IntegrationType_1.IntegrationType.slack(),
config: { webhookUrl: 'https://hooks.slack.com/test' },
});
(0, vitest_1.expect)(integration.name).toBe('My Slack');
(0, vitest_1.expect)(integration.type.value).toBe('slack');
(0, vitest_1.expect)(integration.enabled).toBe(true);
(0, vitest_1.expect)(integration.config.webhookUrl).toBe('https://hooks.slack.com/test');
});
(0, vitest_1.it)('enable and disable', () => {
const integration = Integration_1.Integration.create({
name: 'Test',
type: IntegrationType_1.IntegrationType.github(),
config: {},
});
integration.disable();
(0, vitest_1.expect)(integration.enabled).toBe(false);
integration.enable();
(0, vitest_1.expect)(integration.enabled).toBe(true);
});
(0, vitest_1.it)('updateConfig merges config', () => {
const integration = Integration_1.Integration.create({
name: 'Jira',
type: IntegrationType_1.IntegrationType.jira(),
config: { host: 'https://old.atlassian.net' },
});
integration.updateConfig({ host: 'https://new.atlassian.net', token: 'tok' });
(0, vitest_1.expect)(integration.config.host).toBe('https://new.atlassian.net');
(0, vitest_1.expect)(integration.config.token).toBe('tok');
});
});
// ─── IntegrationType ──────────────────────────────────────────────────────────
(0, vitest_1.describe)('IntegrationType', () => {
(0, vitest_1.it)('parses all valid types', () => {
(0, vitest_1.expect)(IntegrationType_1.IntegrationType.fromString('slack').value).toBe('slack');
(0, vitest_1.expect)(IntegrationType_1.IntegrationType.fromString('github').value).toBe('github');
(0, vitest_1.expect)(IntegrationType_1.IntegrationType.fromString('jira').value).toBe('jira');
(0, vitest_1.expect)(IntegrationType_1.IntegrationType.fromString('webhook').value).toBe('webhook');
});
(0, vitest_1.it)('throws on invalid type', () => {
(0, vitest_1.expect)(() => IntegrationType_1.IntegrationType.fromString('unknown')).toThrow();
});
});
// ─── WebhookEndpoint ──────────────────────────────────────────────────────────
(0, vitest_1.describe)('WebhookEndpoint', () => {
(0, vitest_1.it)('creates with auto-generated secret', () => {
const endpoint = WebhookEndpoint_1.WebhookEndpoint.create({ url: 'https://example.com/hook' });
(0, vitest_1.expect)(endpoint.url).toBe('https://example.com/hook');
(0, vitest_1.expect)(endpoint.enabled).toBe(true);
(0, vitest_1.expect)(endpoint.secret.value).toBeTruthy();
(0, vitest_1.expect)(endpoint.secret.value.length).toBeGreaterThan(20);
});
(0, vitest_1.it)('records delivery', () => {
const endpoint = WebhookEndpoint_1.WebhookEndpoint.create({ url: 'https://example.com/hook' });
(0, vitest_1.expect)(endpoint.lastStatus).toBeUndefined();
endpoint.recordDelivery(200);
(0, vitest_1.expect)(endpoint.lastStatus).toBe(200);
(0, vitest_1.expect)(endpoint.lastDeliveredAt).toBeDefined();
});
});
// ─── WebhookSecret ────────────────────────────────────────────────────────────
(0, vitest_1.describe)('WebhookSecret', () => {
(0, vitest_1.it)('generates a secret', () => {
const s = WebhookSecret_1.WebhookSecret.generate();
(0, vitest_1.expect)(s.value.length).toBeGreaterThan(20);
});
(0, vitest_1.it)('fromString round-trips', () => {
const s = WebhookSecret_1.WebhookSecret.fromString('mysecret-at-least-16chars');
(0, vitest_1.expect)(s.value).toBe('mysecret-at-least-16chars');
});
(0, vitest_1.it)('throws when secret too short', () => {
(0, vitest_1.expect)(() => WebhookSecret_1.WebhookSecret.fromString('short')).toThrow();
});
});
// ─── HMAC signature verification ─────────────────────────────────────────────
(0, vitest_1.describe)('HMAC webhook signature', () => {
(0, vitest_1.it)('produces valid sha256 signature', () => {
const secret = 'test-secret-abc123';
const body = JSON.stringify({ event: 'finding.created', data: { id: '1' } });
const sig = (0, crypto_1.createHmac)('sha256', secret).update(body).digest('hex');
(0, vitest_1.expect)(sig).toBeTruthy();
// Verify it's a valid hex string of 64 chars (sha256)
(0, vitest_1.expect)(sig).toMatch(/^[0-9a-f]{64}$/);
});
(0, vitest_1.it)('same body + secret → same signature', () => {
const secret = 'test-secret';
const body = 'hello world';
const sig1 = (0, crypto_1.createHmac)('sha256', secret).update(body).digest('hex');
const sig2 = (0, crypto_1.createHmac)('sha256', secret).update(body).digest('hex');
(0, vitest_1.expect)(sig1).toBe(sig2);
});
(0, vitest_1.it)('different body → different signature', () => {
const secret = 'test-secret';
const sig1 = (0, crypto_1.createHmac)('sha256', secret).update('body1').digest('hex');
const sig2 = (0, crypto_1.createHmac)('sha256', secret).update('body2').digest('hex');
(0, vitest_1.expect)(sig1).not.toBe(sig2);
});
});
// ─── WebhookDispatcher ───────────────────────────────────────────────────────
(0, vitest_1.describe)('WebhookDispatcher', () => {
const logger = (0, pino_1.pino)({ level: 'silent' });
(0, vitest_1.it)('calls fetch for each enabled endpoint', async () => {
const secret = WebhookSecret_1.WebhookSecret.fromString('secret123456789abcdef');
const endpoint = WebhookEndpoint_1.WebhookEndpoint.reconstitute({ url: 'https://example.com/hook', secret, enabled: true, createdAt: new Date() }, { toString: () => 'ep-1', equals: () => false });
const mockRepo = {
save: vitest_1.vi.fn(),
findById: vitest_1.vi.fn(),
findAll: vitest_1.vi.fn(),
findEnabled: vitest_1.vi.fn().mockResolvedValue([endpoint]),
update: vitest_1.vi.fn(),
delete: vitest_1.vi.fn(),
};
const fetchMock = vitest_1.vi.fn().mockResolvedValue({ status: 200, ok: true });
global.fetch = fetchMock;
const dispatcher = new WebhookDispatcher_1.WebhookDispatcher(mockRepo, logger);
const finding = {
id: 'f-1',
title: 'XSS in login form',
severity: 'high',
type: 'xss',
description: 'Reflected XSS',
sessionId: 's-1',
};
await dispatcher.dispatchFinding(finding);
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledOnce();
const [url, opts] = fetchMock.mock.calls[0];
(0, vitest_1.expect)(url).toBe('https://example.com/hook');
(0, vitest_1.expect)(opts.method).toBe('POST');
const headers = opts.headers;
(0, vitest_1.expect)(headers['X-ABE-Event']).toBe('finding.created');
(0, vitest_1.expect)(headers['X-ABE-Signature']).toMatch(/^sha256=[0-9a-f]{64}$/);
});
(0, vitest_1.it)('does not throw when no endpoints', async () => {
const mockRepo = {
save: vitest_1.vi.fn(),
findById: vitest_1.vi.fn(),
findAll: vitest_1.vi.fn(),
findEnabled: vitest_1.vi.fn().mockResolvedValue([]),
update: vitest_1.vi.fn(),
delete: vitest_1.vi.fn(),
};
const dispatcher = new WebhookDispatcher_1.WebhookDispatcher(mockRepo, logger);
const finding = {
id: 'f-1',
title: 'Test',
severity: 'low',
type: 'info',
description: 'Test',
sessionId: 's-1',
};
await (0, vitest_1.expect)(dispatcher.dispatchFinding(finding)).resolves.toBeUndefined();
});
});

View File

@@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LicenseService = void 0;
const Result_1 = require("../../../shared/domain/Result");
const License_1 = require("../domain/entities/License");
class LicenseService {
constructor(validator) {
this.validator = validator;
this.currentLicense = License_1.License.createFree();
}
getCurrentLicense() {
return this.currentLicense;
}
async activate(licenseKey) {
const result = await this.validator.validate(licenseKey);
if ((0, Result_1.isErr)(result))
return result;
this.currentLicense = result.value;
return result;
}
hasFeature(feature) {
return this.currentLicense.hasFeature(feature);
}
getStatus() {
const license = this.currentLicense;
return {
plan: license.plan.toString(),
organizationName: license.organizationName,
email: license.email,
issuedAt: license.issuedAt.toISOString(),
expiresAt: license.expiresAt?.toISOString() ?? null,
isValid: license.isValid,
features: license.getEntitlements().toArray(),
};
}
}
exports.LicenseService = LicenseService;

View File

@@ -0,0 +1,51 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.License = void 0;
const Entity_1 = require("../../../../shared/domain/Entity");
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
const LicensePlan_1 = require("../value-objects/LicensePlan");
const FeatureEntitlement_1 = require("../value-objects/FeatureEntitlement");
class License extends Entity_1.Entity {
static createFree() {
return new License({
plan: LicensePlan_1.LicensePlan.free(),
organizationName: 'Community',
email: '',
expiresAt: null,
issuedAt: new Date(),
signature: 'free',
rawKey: 'free',
}, UniqueId_1.UniqueId.create());
}
static reconstitute(props, id) {
return new License(props, id);
}
get plan() { return this.props.plan; }
get organizationName() { return this.props.organizationName; }
get email() { return this.props.email; }
get expiresAt() { return this.props.expiresAt; }
get issuedAt() { return this.props.issuedAt; }
get signature() { return this.props.signature; }
get rawKey() { return this.props.rawKey; }
get isExpired() {
if (!this.props.expiresAt)
return false;
return this.props.expiresAt < new Date();
}
get isValid() {
return !this.isExpired;
}
getEntitlements() {
if (!this.isValid)
return FeatureEntitlement_1.FeatureEntitlement.forFeatures(FeatureEntitlement_1.FREE_FEATURES);
if (this.props.plan.isEnterprise)
return FeatureEntitlement_1.FeatureEntitlement.forFeatures(FeatureEntitlement_1.ENTERPRISE_FEATURES);
if (this.props.plan.isPro)
return FeatureEntitlement_1.FeatureEntitlement.forFeatures(FeatureEntitlement_1.PRO_FEATURES);
return FeatureEntitlement_1.FeatureEntitlement.forFeatures(FeatureEntitlement_1.FREE_FEATURES);
}
hasFeature(feature) {
return this.getEntitlements().has(feature);
}
}
exports.License = License;

View File

@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@@ -0,0 +1,43 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FeatureEntitlement = exports.ENTERPRISE_FEATURES = exports.PRO_FEATURES = exports.FREE_FEATURES = void 0;
exports.FREE_FEATURES = [
'exploration:basic',
'findings:basic',
'findings:export',
'reports:basic',
'auth:apikeys',
];
exports.PRO_FEATURES = [
...exports.FREE_FEATURES,
'exploration:scheduled',
'reports:pdf',
'integrations:webhook',
'integrations:slack',
'integrations:github',
'integrations:jira',
];
exports.ENTERPRISE_FEATURES = [
...exports.PRO_FEATURES,
'auth:sso',
'auth:ldap',
'audit:logs',
'branding:whitelabel',
'data:retention',
'infra:postgres',
];
class FeatureEntitlement {
constructor(features) {
this.features = features;
}
static forFeatures(features) {
return new FeatureEntitlement(new Set(features));
}
has(feature) {
return this.features.has(feature);
}
toArray() {
return Array.from(this.features);
}
}
exports.FeatureEntitlement = FeatureEntitlement;

View File

@@ -0,0 +1,23 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LicensePlan = void 0;
class LicensePlan {
constructor(value) {
this.value = value;
}
static free() { return new LicensePlan('free'); }
static pro() { return new LicensePlan('pro'); }
static enterprise() { return new LicensePlan('enterprise'); }
static fromString(value) {
if (value === 'free' || value === 'pro' || value === 'enterprise') {
return new LicensePlan(value);
}
throw new Error(`Invalid license plan: ${value}`);
}
get isFree() { return this.value === 'free'; }
get isPro() { return this.value === 'pro'; }
get isEnterprise() { return this.value === 'enterprise'; }
toString() { return this.value; }
equals(other) { return this.value === other.value; }
}
exports.LicensePlan = LicensePlan;

20
dist/modules/licensing/index.js vendored Normal file
View File

@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LicensingController = exports.requireFeature = exports.RSALicenseValidator = exports.LicenseService = exports.ENTERPRISE_FEATURES = exports.PRO_FEATURES = exports.FREE_FEATURES = exports.FeatureEntitlement = exports.LicensePlan = exports.License = void 0;
var License_1 = require("./domain/entities/License");
Object.defineProperty(exports, "License", { enumerable: true, get: function () { return License_1.License; } });
var LicensePlan_1 = require("./domain/value-objects/LicensePlan");
Object.defineProperty(exports, "LicensePlan", { enumerable: true, get: function () { return LicensePlan_1.LicensePlan; } });
var FeatureEntitlement_1 = require("./domain/value-objects/FeatureEntitlement");
Object.defineProperty(exports, "FeatureEntitlement", { enumerable: true, get: function () { return FeatureEntitlement_1.FeatureEntitlement; } });
Object.defineProperty(exports, "FREE_FEATURES", { enumerable: true, get: function () { return FeatureEntitlement_1.FREE_FEATURES; } });
Object.defineProperty(exports, "PRO_FEATURES", { enumerable: true, get: function () { return FeatureEntitlement_1.PRO_FEATURES; } });
Object.defineProperty(exports, "ENTERPRISE_FEATURES", { enumerable: true, get: function () { return FeatureEntitlement_1.ENTERPRISE_FEATURES; } });
var LicenseService_1 = require("./application/LicenseService");
Object.defineProperty(exports, "LicenseService", { enumerable: true, get: function () { return LicenseService_1.LicenseService; } });
var RSALicenseValidator_1 = require("./infrastructure/validators/RSALicenseValidator");
Object.defineProperty(exports, "RSALicenseValidator", { enumerable: true, get: function () { return RSALicenseValidator_1.RSALicenseValidator; } });
var FeatureGateMiddleware_1 = require("./infrastructure/middleware/FeatureGateMiddleware");
Object.defineProperty(exports, "requireFeature", { enumerable: true, get: function () { return FeatureGateMiddleware_1.requireFeature; } });
var LicensingController_1 = require("./infrastructure/http/LicensingController");
Object.defineProperty(exports, "LicensingController", { enumerable: true, get: function () { return LicensingController_1.LicensingController; } });

View File

@@ -0,0 +1,36 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LicensingController = void 0;
const express_1 = require("express");
const Result_1 = require("../../../../shared/domain/Result");
class LicensingController {
constructor(licenseService) {
this.licenseService = licenseService;
this.router = (0, express_1.Router)();
this.registerRoutes();
}
registerRoutes() {
this.router.get('/status', this.getStatus.bind(this));
this.router.post('/activate', this.activate.bind(this));
}
getStatus(_req, res) {
res.json(this.licenseService.getStatus());
}
async activate(req, res) {
const { licenseKey } = req.body;
if (!licenseKey || typeof licenseKey !== 'string') {
res.status(400).json({ error: 'licenseKey is required' });
return;
}
const result = await this.licenseService.activate(licenseKey.trim());
if ((0, Result_1.isErr)(result)) {
res.status(422).json({ error: result.error });
return;
}
res.json({
message: 'License activated successfully',
license: this.licenseService.getStatus(),
});
}
}
exports.LicensingController = LicensingController;

View File

@@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.requireFeature = requireFeature;
function requireFeature(licenseService, feature) {
return (_req, res, next) => {
if (!licenseService.hasFeature(feature)) {
res.status(403).json({
error: 'Feature not available',
feature,
plan: licenseService.getCurrentLicense().plan.toString(),
message: `This feature requires a higher license plan. Current plan: ${licenseService.getCurrentLicense().plan.toString()}`,
});
return;
}
next();
};
}

View File

@@ -0,0 +1,77 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RSALicenseValidator = void 0;
const crypto_1 = __importDefault(require("crypto"));
const Result_1 = require("../../../../shared/domain/Result");
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
const License_1 = require("../../domain/entities/License");
const LicensePlan_1 = require("../../domain/value-objects/LicensePlan");
// Public key used to verify license signatures.
// The corresponding private key is kept secret (used only by generate-license.ts).
const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2a2rwplBQLzHPZe5TNJF
EhkFwUEkMvbzXuRSxW98hGxMgrHPKGLJgNw0qFsLQmhDSmVvnrwYE2vCy2Dgm7Qj
7WKFqbZFkVDe8cROZ9K7rQmn0BqckmJbkm2SJnzYL9e9z6b5R8r5w2r5Q2HZFN7
6B3dKCHWHxhyE3N8MCJSN7qBZ7kX8fJqBwBxQL6bZbGP2O5bXrZpFw3xKyGJ5t
vZ9eTuD4JhKJbZbGJ3Q5Q5nNbm3nXY5z9WbBxFbRLYGJbQ7E8mSYnKJZkJzYM
TmOxJbKtJz5mJ9Q7rBxBxLYGJmQtZmKtXZ5t9WbBxFbRLYGJbQ7E8mSYnKJZk
JwIDAQAB
-----END PUBLIC KEY-----`;
class RSALicenseValidator {
constructor(publicKeyPem) {
const pem = publicKeyPem ?? PUBLIC_KEY;
this.publicKey = crypto_1.default.createPublicKey(pem);
}
async validate(licenseKey) {
try {
// License key format: base64(payload_json).base64(signature)
const parts = licenseKey.trim().split('.');
if (parts.length !== 2) {
return (0, Result_1.Err)('Invalid license key format');
}
const [payloadB64, signatureB64] = parts;
let payloadJson;
let rawPayload;
try {
payloadJson = Buffer.from(payloadB64, 'base64').toString('utf-8');
rawPayload = JSON.parse(payloadJson);
}
catch {
return (0, Result_1.Err)('Invalid license key: cannot decode payload');
}
const signature = Buffer.from(signatureB64, 'base64');
const isValid = crypto_1.default.verify('sha256', Buffer.from(payloadJson, 'utf-8'), this.publicKey, signature);
if (!isValid) {
return (0, Result_1.Err)('Invalid license key: signature verification failed');
}
let plan;
try {
plan = LicensePlan_1.LicensePlan.fromString(rawPayload.plan);
}
catch {
return (0, Result_1.Err)(`Invalid plan in license: ${rawPayload.plan}`);
}
const expiresAt = rawPayload.expiresAt ? new Date(rawPayload.expiresAt) : null;
if (expiresAt && expiresAt < new Date()) {
return (0, Result_1.Err)('License has expired');
}
const license = License_1.License.reconstitute({
plan,
organizationName: rawPayload.organizationName,
email: rawPayload.email,
issuedAt: new Date(rawPayload.issuedAt),
expiresAt,
signature: signatureB64,
rawKey: licenseKey,
}, UniqueId_1.UniqueId.create());
return (0, Result_1.Ok)(license);
}
catch (err) {
return (0, Result_1.Err)(`License validation error: ${String(err)}`);
}
}
}
exports.RSALicenseValidator = RSALicenseValidator;

65
dist/scripts/generate-license.js vendored Normal file
View File

@@ -0,0 +1,65 @@
#!/usr/bin/env ts-node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* ABE License Key Generator (internal tool)
* Usage: ts-node src/scripts/generate-license.ts --plan pro --org "Acme Corp" --email admin@acme.com --expires 2027-01-01
*/
const crypto_1 = __importDefault(require("crypto"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function parseArgs() {
const args = process.argv.slice(2);
const get = (flag) => {
const idx = args.indexOf(flag);
return idx >= 0 ? args[idx + 1] : undefined;
};
const plan = (get('--plan') ?? 'pro');
const org = get('--org') ?? 'Unknown Organization';
const email = get('--email') ?? '';
const expires = get('--expires') ?? null;
const keyFile = get('--key') ?? path_1.default.join(process.cwd(), 'license-private.pem');
return { plan, org, email, expires, keyFile };
}
function generateLicense(args) {
if (!fs_1.default.existsSync(args.keyFile)) {
throw new Error(`Private key not found at ${args.keyFile}.\n` +
'Generate with: openssl genrsa -out license-private.pem 2048');
}
const privateKeyPem = fs_1.default.readFileSync(args.keyFile, 'utf-8');
const privateKey = crypto_1.default.createPrivateKey(privateKeyPem);
const payload = {
plan: args.plan,
organizationName: args.org,
email: args.email,
issuedAt: new Date().toISOString(),
expiresAt: args.expires ? new Date(args.expires).toISOString() : null,
};
const payloadJson = JSON.stringify(payload);
const payloadB64 = Buffer.from(payloadJson, 'utf-8').toString('base64');
const signature = crypto_1.default.sign('sha256', Buffer.from(payloadJson, 'utf-8'), privateKey);
const signatureB64 = signature.toString('base64');
return `${payloadB64}.${signatureB64}`;
}
function main() {
const args = parseArgs();
try {
const licenseKey = generateLicense(args);
console.log('\n=== ABE License Key ===');
console.log(licenseKey);
console.log('\n=== Details ===');
console.log(`Plan: ${args.plan}`);
console.log(`Organization: ${args.org}`);
console.log(`Email: ${args.email}`);
console.log(`Expires: ${args.expires ?? 'Never'}`);
console.log('===================\n');
}
catch (err) {
console.error('Error generating license:', String(err));
process.exit(1);
}
}
main();

View File

@@ -1,8 +1,60 @@
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { useState } from 'react'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
import { Shield } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Textarea } from '@/components/ui/textarea'
import { Shield, CheckCircle2, XCircle, Loader2 } from 'lucide-react'
import { apiFetch } from '@/lib/api'
interface LicenseStatus {
plan: 'free' | 'pro' | 'enterprise'
organizationName: string
email: string
issuedAt: string
expiresAt: string | null
isValid: boolean
features: string[]
}
const PLAN_LABELS: Record<string, string> = {
free: 'Free / OSS',
pro: 'Pro',
enterprise: 'Enterprise',
}
export function LicenseSection() {
const [licenseKey, setLicenseKey] = useState('')
const [activateError, setActivateError] = useState<string | null>(null)
const queryClient = useQueryClient()
const { data: status, isLoading } = useQuery<LicenseStatus>({
queryKey: ['license-status'],
queryFn: () => apiFetch('/api/license/status'),
})
const { mutate: activate, isPending } = useMutation({
mutationFn: (key: string) =>
apiFetch<{ message: string; license: LicenseStatus }>('/api/license/activate', {
method: 'POST',
body: JSON.stringify({ licenseKey: key }),
}),
onSuccess: () => {
setLicenseKey('')
setActivateError(null)
void queryClient.invalidateQueries({ queryKey: ['license-status'] })
},
onError: (err: Error) => {
setActivateError(err.message)
},
})
const handleActivate = () => {
if (!licenseKey.trim()) return
setActivateError(null)
activate(licenseKey.trim())
}
return (
<div className="space-y-6 max-w-xl">
<div>
@@ -17,14 +69,92 @@ export function LicenseSection() {
<CardTitle className="text-base">Current Plan</CardTitle>
</div>
</CardHeader>
<CardContent className="space-y-4">
{isLoading ? (
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<Loader2 className="h-4 w-4 animate-spin" />
Loading license status...
</div>
) : status ? (
<>
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">Plan</span>
<Badge variant={status.plan === 'free' ? 'secondary' : 'default'}>
{PLAN_LABELS[status.plan] ?? status.plan}
</Badge>
</div>
{status.plan !== 'free' && (
<>
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">Organization</span>
<span className="font-medium">{status.organizationName}</span>
</div>
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">Email</span>
<span className="font-medium">{status.email}</span>
</div>
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">Issued</span>
<span>{new Date(status.issuedAt).toLocaleDateString()}</span>
</div>
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">Expires</span>
<span>{status.expiresAt ? new Date(status.expiresAt).toLocaleDateString() : 'Never'}</span>
</div>
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">Status</span>
{status.isValid ? (
<span className="flex items-center gap-1 text-green-600">
<CheckCircle2 className="h-4 w-4" /> Valid
</span>
) : (
<span className="flex items-center gap-1 text-red-600">
<XCircle className="h-4 w-4" /> Expired
</span>
)}
</div>
<div className="space-y-1">
<span className="text-sm text-muted-foreground">Features</span>
<div className="flex flex-wrap gap-1">
{status.features.map((f) => (
<Badge key={f} variant="outline" className="text-xs">
{f}
</Badge>
))}
</div>
</div>
</>
)}
</>
) : null}
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle className="text-base">Activate License</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">Plan</span>
<Badge>Free / OSS</Badge>
</div>
<CardDescription>
License activation will be available in Phase 17 (RSA-signed keys with feature entitlements).
</CardDescription>
<p className="text-sm text-muted-foreground">
Paste your license key below to unlock Pro or Enterprise features.
</p>
<Textarea
placeholder="Paste license key..."
value={licenseKey}
onChange={(e) => setLicenseKey(e.target.value)}
className="font-mono text-xs h-28 resize-none"
/>
{activateError && (
<p className="text-sm text-red-600">{activateError}</p>
)}
<Button
onClick={handleActivate}
disabled={!licenseKey.trim() || isPending}
className="w-full"
>
{isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Activate License
</Button>
</CardContent>
</Card>
</div>

View File

@@ -7,6 +7,9 @@ import { createFindingsRouter } from '../modules/findings/infrastructure/http/Fi
import { createFuzzingRouter } from '../modules/fuzzing/infrastructure/http/FuzzingController';
import { createReportingRouter } from '../modules/reporting/infrastructure/http/ReportingController';
import { createIntegrationsRouter } from '../modules/integrations/infrastructure/http/IntegrationsController';
import { LicensingController } from '../modules/licensing/infrastructure/http/LicensingController';
import { LicenseService } from '../modules/licensing/application/LicenseService';
import { requireFeature } from '../modules/licensing/infrastructure/middleware/FeatureGateMiddleware';
import { createAuthController } from '../modules/auth/infrastructure/http/AuthController';
import { createAuthMiddleware } from '../modules/auth/application/middleware/AuthMiddleware';
import { ServerDependencies } from './server';
@@ -36,7 +39,7 @@ export interface AuthControllerDeps {
export function createRouter(deps: ServerDependencies): Router {
const router = Router();
const { authDeps } = deps;
const { authDeps, licenseService } = deps;
// Auth routes — public (no auth middleware)
router.use(
@@ -66,8 +69,12 @@ export function createRouter(deps: ServerDependencies): Router {
router.use('/sessions', createCrawlingRouter(deps.crawlingDeps));
router.use('/findings', createFindingsRouter(deps.findingsDeps));
router.use('/fuzz', createFuzzingRouter(deps.fuzzingDeps));
router.use('/reports', createReportingRouter(deps.reportingDeps));
router.use('/integrations', createIntegrationsRouter(deps.integrationsDeps));
router.use('/reports', requireFeature(licenseService, 'reports:basic'), createReportingRouter(deps.reportingDeps));
router.use('/integrations', requireFeature(licenseService, 'integrations:webhook'), createIntegrationsRouter(deps.integrationsDeps));
// Licensing routes (public-ish — only status and activate, no sensitive data)
const licensingController = new LicensingController(licenseService);
router.use('/license', licensingController.router);
return router;
}

View File

@@ -21,6 +21,7 @@ import { FuzzingControllerDeps } from '../modules/fuzzing/infrastructure/http/Fu
import { ReportingControllerDeps } from '../modules/reporting/infrastructure/http/ReportingController';
import { IntegrationsDeps } from '../modules/integrations/infrastructure/http/IntegrationsController';
import { AuthControllerDeps } from './router';
import { LicenseService } from '../modules/licensing/application/LicenseService';
export interface ServerDependencies {
config: AppConfig;
@@ -32,6 +33,7 @@ export interface ServerDependencies {
reportingDeps: ReportingControllerDeps;
integrationsDeps: IntegrationsDeps;
authDeps: AuthControllerDeps;
licenseService: LicenseService;
}
export function createServer(deps: ServerDependencies): Express {

View File

@@ -61,6 +61,10 @@ import { KyselyWebhookEndpointRepository } from './modules/integrations/infrastr
import { WebhookDispatcher } from './modules/integrations/infrastructure/webhooks/WebhookDispatcher';
import { OnFindingCreated } from './modules/integrations/application/event-handlers/OnFindingCreated';
// Licensing module
import { RSALicenseValidator } from './modules/licensing/infrastructure/validators/RSALicenseValidator';
import { LicenseService } from './modules/licensing/application/LicenseService';
// Job queue
import { SQLiteJobQueue } from './jobs/SQLiteJobQueue';
import { createExplorationJobHandler, EXPLORATION_JOB_TYPE } from './jobs/workers/ExplorationWorker';
@@ -139,7 +143,11 @@ async function bootstrap(): Promise<void> {
// 11. Reporting use cases
const generateReport = new GenerateReportCommand(reportRepo, eventBus);
// 11b. Integrations
// 11b. Licensing
const licenseValidator = new RSALicenseValidator();
const licenseService = new LicenseService(licenseValidator);
// 11c. Integrations
const integrationRepo = new KyselyIntegrationRepository(db);
const webhookRepo = new KyselyWebhookEndpointRepository(db);
const webhookDispatcher = new WebhookDispatcher(webhookRepo, logger);
@@ -165,6 +173,7 @@ async function bootstrap(): Promise<void> {
fuzzingDeps: { runFuzz, repository: fuzzRepo },
reportingDeps: { generateReport, reportRepository: reportRepo, jobQueue },
integrationsDeps: { integrationRepo, webhookRepo },
licenseService,
authDeps: {
registerCommand,
loginCommand,

View File

@@ -0,0 +1,200 @@
import { describe, it, expect, vi } from 'vitest';
import { createHmac } from 'crypto';
import { Integration } from '../domain/entities/Integration';
import { IntegrationType } from '../domain/value-objects/IntegrationType';
import { WebhookEndpoint } from '../domain/entities/WebhookEndpoint';
import { WebhookSecret } from '../domain/value-objects/WebhookSecret';
import { WebhookDispatcher } from '../infrastructure/webhooks/WebhookDispatcher';
import { FindingPayload } from '../domain/ports/IIntegrationProvider';
import { IWebhookEndpointRepository } from '../domain/ports/IWebhookEndpointRepository';
import { pino } from 'pino';
import type { Logger } from 'pino';
// ─── Integration Entity ───────────────────────────────────────────────────────
describe('Integration', () => {
it('creates with defaults', () => {
const integration = Integration.create({
name: 'My Slack',
type: IntegrationType.slack(),
config: { webhookUrl: 'https://hooks.slack.com/test' },
});
expect(integration.name).toBe('My Slack');
expect(integration.type.value).toBe('slack');
expect(integration.enabled).toBe(true);
expect(integration.config.webhookUrl).toBe('https://hooks.slack.com/test');
});
it('enable and disable', () => {
const integration = Integration.create({
name: 'Test',
type: IntegrationType.github(),
config: {},
});
integration.disable();
expect(integration.enabled).toBe(false);
integration.enable();
expect(integration.enabled).toBe(true);
});
it('updateConfig merges config', () => {
const integration = Integration.create({
name: 'Jira',
type: IntegrationType.jira(),
config: { host: 'https://old.atlassian.net' },
});
integration.updateConfig({ host: 'https://new.atlassian.net', token: 'tok' });
expect(integration.config.host).toBe('https://new.atlassian.net');
expect(integration.config.token).toBe('tok');
});
});
// ─── IntegrationType ──────────────────────────────────────────────────────────
describe('IntegrationType', () => {
it('parses all valid types', () => {
expect(IntegrationType.fromString('slack').value).toBe('slack');
expect(IntegrationType.fromString('github').value).toBe('github');
expect(IntegrationType.fromString('jira').value).toBe('jira');
expect(IntegrationType.fromString('webhook').value).toBe('webhook');
});
it('throws on invalid type', () => {
expect(() => IntegrationType.fromString('unknown')).toThrow();
});
});
// ─── WebhookEndpoint ──────────────────────────────────────────────────────────
describe('WebhookEndpoint', () => {
it('creates with auto-generated secret', () => {
const endpoint = WebhookEndpoint.create({ url: 'https://example.com/hook' });
expect(endpoint.url).toBe('https://example.com/hook');
expect(endpoint.enabled).toBe(true);
expect(endpoint.secret.value).toBeTruthy();
expect(endpoint.secret.value.length).toBeGreaterThan(20);
});
it('records delivery', () => {
const endpoint = WebhookEndpoint.create({ url: 'https://example.com/hook' });
expect(endpoint.lastStatus).toBeUndefined();
endpoint.recordDelivery(200);
expect(endpoint.lastStatus).toBe(200);
expect(endpoint.lastDeliveredAt).toBeDefined();
});
});
// ─── WebhookSecret ────────────────────────────────────────────────────────────
describe('WebhookSecret', () => {
it('generates a secret', () => {
const s = WebhookSecret.generate();
expect(s.value.length).toBeGreaterThan(20);
});
it('fromString round-trips', () => {
const s = WebhookSecret.fromString('mysecret-at-least-16chars');
expect(s.value).toBe('mysecret-at-least-16chars');
});
it('throws when secret too short', () => {
expect(() => WebhookSecret.fromString('short')).toThrow();
});
});
// ─── HMAC signature verification ─────────────────────────────────────────────
describe('HMAC webhook signature', () => {
it('produces valid sha256 signature', () => {
const secret = 'test-secret-abc123';
const body = JSON.stringify({ event: 'finding.created', data: { id: '1' } });
const sig = createHmac('sha256', secret).update(body).digest('hex');
expect(sig).toBeTruthy();
// Verify it's a valid hex string of 64 chars (sha256)
expect(sig).toMatch(/^[0-9a-f]{64}$/);
});
it('same body + secret → same signature', () => {
const secret = 'test-secret';
const body = 'hello world';
const sig1 = createHmac('sha256', secret).update(body).digest('hex');
const sig2 = createHmac('sha256', secret).update(body).digest('hex');
expect(sig1).toBe(sig2);
});
it('different body → different signature', () => {
const secret = 'test-secret';
const sig1 = createHmac('sha256', secret).update('body1').digest('hex');
const sig2 = createHmac('sha256', secret).update('body2').digest('hex');
expect(sig1).not.toBe(sig2);
});
});
// ─── WebhookDispatcher ───────────────────────────────────────────────────────
describe('WebhookDispatcher', () => {
const logger = pino({ level: 'silent' });
it('calls fetch for each enabled endpoint', async () => {
const secret = WebhookSecret.fromString('secret123456789abcdef');
const endpoint = WebhookEndpoint.reconstitute(
{ url: 'https://example.com/hook', secret, enabled: true, createdAt: new Date() },
{ toString: () => 'ep-1', equals: () => false } as never
);
const mockRepo: IWebhookEndpointRepository = {
save: vi.fn(),
findById: vi.fn(),
findAll: vi.fn(),
findEnabled: vi.fn().mockResolvedValue([endpoint]),
update: vi.fn(),
delete: vi.fn(),
};
const fetchMock = vi.fn().mockResolvedValue({ status: 200, ok: true });
global.fetch = fetchMock;
const dispatcher = new WebhookDispatcher(mockRepo, logger as unknown as Logger);
const finding: FindingPayload = {
id: 'f-1',
title: 'XSS in login form',
severity: 'high',
type: 'xss',
description: 'Reflected XSS',
sessionId: 's-1',
};
await dispatcher.dispatchFinding(finding);
expect(fetchMock).toHaveBeenCalledOnce();
const [url, opts] = fetchMock.mock.calls[0] as [string, RequestInit];
expect(url).toBe('https://example.com/hook');
expect(opts.method).toBe('POST');
const headers = opts.headers as Record<string, string>;
expect(headers['X-ABE-Event']).toBe('finding.created');
expect(headers['X-ABE-Signature']).toMatch(/^sha256=[0-9a-f]{64}$/);
});
it('does not throw when no endpoints', async () => {
const mockRepo: IWebhookEndpointRepository = {
save: vi.fn(),
findById: vi.fn(),
findAll: vi.fn(),
findEnabled: vi.fn().mockResolvedValue([]),
update: vi.fn(),
delete: vi.fn(),
};
const dispatcher = new WebhookDispatcher(mockRepo, logger as unknown as Logger);
const finding: FindingPayload = {
id: 'f-1',
title: 'Test',
severity: 'low',
type: 'info',
description: 'Test',
sessionId: 's-1',
};
await expect(dispatcher.dispatchFinding(finding)).resolves.toBeUndefined();
});
});

View File

View File

@@ -0,0 +1,180 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { LicensePlan } from '../domain/value-objects/LicensePlan';
import {
FeatureEntitlement,
FREE_FEATURES,
PRO_FEATURES,
ENTERPRISE_FEATURES,
} from '../domain/value-objects/FeatureEntitlement';
import { License } from '../domain/entities/License';
import { LicenseService } from '../application/LicenseService';
import { ILicenseValidator } from '../domain/ports/ILicenseValidator';
import { Result } from '../../../shared/domain/Result';
import { UniqueId } from '../../../shared/domain/UniqueId';
describe('LicensePlan', () => {
it('creates free plan', () => {
const plan = LicensePlan.free();
expect(plan.isFree).toBe(true);
expect(plan.isPro).toBe(false);
expect(plan.isEnterprise).toBe(false);
expect(plan.toString()).toBe('free');
});
it('creates pro plan', () => {
const plan = LicensePlan.pro();
expect(plan.isPro).toBe(true);
expect(plan.isFree).toBe(false);
});
it('creates enterprise plan', () => {
const plan = LicensePlan.enterprise();
expect(plan.isEnterprise).toBe(true);
});
it('parses from string', () => {
expect(LicensePlan.fromString('pro').isPro).toBe(true);
expect(LicensePlan.fromString('enterprise').isEnterprise).toBe(true);
});
it('throws on invalid plan string', () => {
expect(() => LicensePlan.fromString('invalid')).toThrow();
});
it('equals comparison works', () => {
expect(LicensePlan.free().equals(LicensePlan.free())).toBe(true);
expect(LicensePlan.free().equals(LicensePlan.pro())).toBe(false);
});
});
describe('FeatureEntitlement', () => {
it('free features do not include pro features', () => {
const free = FeatureEntitlement.forFeatures(FREE_FEATURES);
expect(free.has('exploration:basic')).toBe(true);
expect(free.has('reports:pdf')).toBe(false);
expect(free.has('integrations:slack')).toBe(false);
expect(free.has('auth:sso')).toBe(false);
});
it('pro features include free features', () => {
const pro = FeatureEntitlement.forFeatures(PRO_FEATURES);
expect(pro.has('exploration:basic')).toBe(true);
expect(pro.has('reports:pdf')).toBe(true);
expect(pro.has('integrations:slack')).toBe(true);
expect(pro.has('auth:sso')).toBe(false);
});
it('enterprise features include all features', () => {
const ent = FeatureEntitlement.forFeatures(ENTERPRISE_FEATURES);
expect(ent.has('auth:sso')).toBe(true);
expect(ent.has('auth:ldap')).toBe(true);
expect(ent.has('branding:whitelabel')).toBe(true);
});
});
describe('License entity', () => {
it('createFree returns a free plan license', () => {
const license = License.createFree();
expect(license.plan.isFree).toBe(true);
expect(license.isExpired).toBe(false);
expect(license.isValid).toBe(true);
});
it('free license has only free features', () => {
const license = License.createFree();
expect(license.hasFeature('exploration:basic')).toBe(true);
expect(license.hasFeature('reports:pdf')).toBe(false);
});
it('expired license returns free features', () => {
const license = License.reconstitute(
{
plan: LicensePlan.pro(),
organizationName: 'Test',
email: 'test@test.com',
expiresAt: new Date('2020-01-01'), // in the past
issuedAt: new Date('2019-01-01'),
signature: 'sig',
rawKey: 'key',
},
UniqueId.create()
);
expect(license.isExpired).toBe(true);
expect(license.isValid).toBe(false);
expect(license.hasFeature('reports:pdf')).toBe(false);
});
it('pro license has pro features', () => {
const license = License.reconstitute(
{
plan: LicensePlan.pro(),
organizationName: 'Acme',
email: 'admin@acme.com',
expiresAt: null,
issuedAt: new Date(),
signature: 'sig',
rawKey: 'key',
},
UniqueId.create()
);
expect(license.isValid).toBe(true);
expect(license.hasFeature('reports:pdf')).toBe(true);
expect(license.hasFeature('integrations:slack')).toBe(true);
expect(license.hasFeature('auth:sso')).toBe(false);
});
});
describe('LicenseService', () => {
let mockValidator: ILicenseValidator;
let service: LicenseService;
beforeEach(() => {
mockValidator = {
validate: vi.fn(),
};
service = new LicenseService(mockValidator);
});
it('starts with free license', () => {
const status = service.getStatus();
expect(status.plan).toBe('free');
expect(status.isValid).toBe(true);
});
it('activate with valid key updates current license', async () => {
const proLicense = License.reconstitute(
{
plan: LicensePlan.pro(),
organizationName: 'Acme',
email: 'admin@acme.com',
expiresAt: null,
issuedAt: new Date(),
signature: 'sig',
rawKey: 'key',
},
UniqueId.create()
);
vi.mocked(mockValidator.validate).mockResolvedValue(Result.ok(proLicense));
const result = await service.activate('valid-key');
expect(result.isOk()).toBe(true);
expect(service.getStatus().plan).toBe('pro');
expect(service.hasFeature('reports:pdf')).toBe(true);
});
it('activate with invalid key returns error and keeps free license', async () => {
vi.mocked(mockValidator.validate).mockResolvedValue(
Result.err('Invalid license key: signature verification failed')
);
const result = await service.activate('invalid-key');
expect(result.isErr()).toBe(true);
expect(result.error).toContain('signature verification failed');
expect(service.getStatus().plan).toBe('free');
});
it('hasFeature checks current license', () => {
expect(service.hasFeature('exploration:basic')).toBe(true);
expect(service.hasFeature('auth:sso')).toBe(false);
});
});

View File

@@ -0,0 +1,48 @@
import { Result, isErr } from '../../../shared/domain/Result';
import { License } from '../domain/entities/License';
import { Feature } from '../domain/value-objects/FeatureEntitlement';
import { ILicenseValidator } from '../domain/ports/ILicenseValidator';
export class LicenseService {
private currentLicense: License;
constructor(private readonly validator: ILicenseValidator) {
this.currentLicense = License.createFree();
}
getCurrentLicense(): License {
return this.currentLicense;
}
async activate(licenseKey: string): Promise<Result<License, string>> {
const result = await this.validator.validate(licenseKey);
if (isErr(result)) return result;
this.currentLicense = result.value;
return result;
}
hasFeature(feature: Feature): boolean {
return this.currentLicense.hasFeature(feature);
}
getStatus(): {
plan: string;
organizationName: string;
email: string;
issuedAt: string;
expiresAt: string | null;
isValid: boolean;
features: Feature[];
} {
const license = this.currentLicense;
return {
plan: license.plan.toString(),
organizationName: license.organizationName,
email: license.email,
issuedAt: license.issuedAt.toISOString(),
expiresAt: license.expiresAt?.toISOString() ?? null,
isValid: license.isValid,
features: license.getEntitlements().toArray(),
};
}
}

View File

@@ -0,0 +1,69 @@
import { Entity } from '../../../../shared/domain/Entity';
import { UniqueId } from '../../../../shared/domain/UniqueId';
import { LicensePlan } from '../value-objects/LicensePlan';
import {
Feature,
FeatureEntitlement,
FREE_FEATURES,
PRO_FEATURES,
ENTERPRISE_FEATURES,
} from '../value-objects/FeatureEntitlement';
export interface LicenseProps {
plan: LicensePlan;
organizationName: string;
email: string;
expiresAt: Date | null;
issuedAt: Date;
signature: string;
rawKey: string;
}
export class License extends Entity<LicenseProps> {
static createFree(): License {
return new License(
{
plan: LicensePlan.free(),
organizationName: 'Community',
email: '',
expiresAt: null,
issuedAt: new Date(),
signature: 'free',
rawKey: 'free',
},
UniqueId.create()
);
}
static reconstitute(props: LicenseProps, id: UniqueId): License {
return new License(props, id);
}
get plan(): LicensePlan { return this.props.plan; }
get organizationName(): string { return this.props.organizationName; }
get email(): string { return this.props.email; }
get expiresAt(): Date | null { return this.props.expiresAt; }
get issuedAt(): Date { return this.props.issuedAt; }
get signature(): string { return this.props.signature; }
get rawKey(): string { return this.props.rawKey; }
get isExpired(): boolean {
if (!this.props.expiresAt) return false;
return this.props.expiresAt < new Date();
}
get isValid(): boolean {
return !this.isExpired;
}
getEntitlements(): FeatureEntitlement {
if (!this.isValid) return FeatureEntitlement.forFeatures(FREE_FEATURES);
if (this.props.plan.isEnterprise) return FeatureEntitlement.forFeatures(ENTERPRISE_FEATURES);
if (this.props.plan.isPro) return FeatureEntitlement.forFeatures(PRO_FEATURES);
return FeatureEntitlement.forFeatures(FREE_FEATURES);
}
hasFeature(feature: Feature): boolean {
return this.getEntitlements().has(feature);
}
}

View File

@@ -0,0 +1,14 @@
import { Result } from '../../../../shared/domain/Result';
import { License } from '../entities/License';
export interface LicensePayload {
plan: string;
organizationName: string;
email: string;
issuedAt: string;
expiresAt: string | null;
}
export interface ILicenseValidator {
validate(licenseKey: string): Promise<Result<License, string>>;
}

View File

@@ -0,0 +1,62 @@
export type Feature =
| 'exploration:basic'
| 'exploration:scheduled'
| 'findings:basic'
| 'findings:export'
| 'reports:basic'
| 'reports:pdf'
| 'integrations:webhook'
| 'integrations:slack'
| 'integrations:github'
| 'integrations:jira'
| 'auth:apikeys'
| 'auth:sso'
| 'auth:ldap'
| 'audit:logs'
| 'branding:whitelabel'
| 'data:retention'
| 'infra:postgres';
export const FREE_FEATURES: Feature[] = [
'exploration:basic',
'findings:basic',
'findings:export',
'reports:basic',
'auth:apikeys',
];
export const PRO_FEATURES: Feature[] = [
...FREE_FEATURES,
'exploration:scheduled',
'reports:pdf',
'integrations:webhook',
'integrations:slack',
'integrations:github',
'integrations:jira',
];
export const ENTERPRISE_FEATURES: Feature[] = [
...PRO_FEATURES,
'auth:sso',
'auth:ldap',
'audit:logs',
'branding:whitelabel',
'data:retention',
'infra:postgres',
];
export class FeatureEntitlement {
private constructor(private readonly features: ReadonlySet<Feature>) {}
static forFeatures(features: Feature[]): FeatureEntitlement {
return new FeatureEntitlement(new Set(features));
}
has(feature: Feature): boolean {
return this.features.has(feature);
}
toArray(): Feature[] {
return Array.from(this.features);
}
}

View File

@@ -0,0 +1,24 @@
export type LicensePlanType = 'free' | 'pro' | 'enterprise';
export class LicensePlan {
private constructor(private readonly value: LicensePlanType) {}
static free(): LicensePlan { return new LicensePlan('free'); }
static pro(): LicensePlan { return new LicensePlan('pro'); }
static enterprise(): LicensePlan { return new LicensePlan('enterprise'); }
static fromString(value: string): LicensePlan {
if (value === 'free' || value === 'pro' || value === 'enterprise') {
return new LicensePlan(value);
}
throw new Error(`Invalid license plan: ${value}`);
}
get isFree(): boolean { return this.value === 'free'; }
get isPro(): boolean { return this.value === 'pro'; }
get isEnterprise(): boolean { return this.value === 'enterprise'; }
toString(): LicensePlanType { return this.value; }
equals(other: LicensePlan): boolean { return this.value === other.value; }
}

View File

@@ -0,0 +1,14 @@
export { License } from './domain/entities/License';
export { LicensePlan } from './domain/value-objects/LicensePlan';
export {
Feature,
FeatureEntitlement,
FREE_FEATURES,
PRO_FEATURES,
ENTERPRISE_FEATURES,
} from './domain/value-objects/FeatureEntitlement';
export type { ILicenseValidator } from './domain/ports/ILicenseValidator';
export { LicenseService } from './application/LicenseService';
export { RSALicenseValidator } from './infrastructure/validators/RSALicenseValidator';
export { requireFeature } from './infrastructure/middleware/FeatureGateMiddleware';
export { LicensingController } from './infrastructure/http/LicensingController';

View File

@@ -0,0 +1,40 @@
import { Router, Request, Response } from 'express';
import { isErr } from '../../../../shared/domain/Result';
import { LicenseService } from '../../application/LicenseService';
export class LicensingController {
readonly router: Router;
constructor(private readonly licenseService: LicenseService) {
this.router = Router();
this.registerRoutes();
}
private registerRoutes(): void {
this.router.get('/status', this.getStatus.bind(this));
this.router.post('/activate', this.activate.bind(this));
}
private getStatus(_req: Request, res: Response): void {
res.json(this.licenseService.getStatus());
}
private async activate(req: Request, res: Response): Promise<void> {
const { licenseKey } = req.body as { licenseKey?: string };
if (!licenseKey || typeof licenseKey !== 'string') {
res.status(400).json({ error: 'licenseKey is required' });
return;
}
const result = await this.licenseService.activate(licenseKey.trim());
if (isErr(result)) {
res.status(422).json({ error: result.error });
return;
}
res.json({
message: 'License activated successfully',
license: this.licenseService.getStatus(),
});
}
}

View File

@@ -0,0 +1,18 @@
import { Request, Response, NextFunction } from 'express';
import { Feature } from '../../domain/value-objects/FeatureEntitlement';
import { LicenseService } from '../../application/LicenseService';
export function requireFeature(licenseService: LicenseService, feature: Feature) {
return (_req: Request, res: Response, next: NextFunction): void => {
if (!licenseService.hasFeature(feature)) {
res.status(403).json({
error: 'Feature not available',
feature,
plan: licenseService.getCurrentLicense().plan.toString(),
message: `This feature requires a higher license plan. Current plan: ${licenseService.getCurrentLicense().plan.toString()}`,
});
return;
}
next();
};
}

View File

@@ -0,0 +1,99 @@
import crypto from 'crypto';
import { Result, Ok, Err } from '../../../../shared/domain/Result';
import { UniqueId } from '../../../../shared/domain/UniqueId';
import { License } from '../../domain/entities/License';
import { LicensePlan } from '../../domain/value-objects/LicensePlan';
import { ILicenseValidator } from '../../domain/ports/ILicenseValidator';
// Public key used to verify license signatures.
// The corresponding private key is kept secret (used only by generate-license.ts).
const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2a2rwplBQLzHPZe5TNJF
EhkFwUEkMvbzXuRSxW98hGxMgrHPKGLJgNw0qFsLQmhDSmVvnrwYE2vCy2Dgm7Qj
7WKFqbZFkVDe8cROZ9K7rQmn0BqckmJbkm2SJnzYL9e9z6b5R8r5w2r5Q2HZFN7
6B3dKCHWHxhyE3N8MCJSN7qBZ7kX8fJqBwBxQL6bZbGP2O5bXrZpFw3xKyGJ5t
vZ9eTuD4JhKJbZbGJ3Q5Q5nNbm3nXY5z9WbBxFbRLYGJbQ7E8mSYnKJZkJzYM
TmOxJbKtJz5mJ9Q7rBxBxLYGJmQtZmKtXZ5t9WbBxFbRLYGJbQ7E8mSYnKJZk
JwIDAQAB
-----END PUBLIC KEY-----`;
interface RawLicensePayload {
plan: string;
organizationName: string;
email: string;
issuedAt: string;
expiresAt: string | null;
}
export class RSALicenseValidator implements ILicenseValidator {
private readonly publicKey: crypto.KeyObject;
constructor(publicKeyPem?: string) {
const pem = publicKeyPem ?? PUBLIC_KEY;
this.publicKey = crypto.createPublicKey(pem);
}
async validate(licenseKey: string): Promise<Result<License, string>> {
try {
// License key format: base64(payload_json).base64(signature)
const parts = licenseKey.trim().split('.');
if (parts.length !== 2) {
return Err('Invalid license key format');
}
const [payloadB64, signatureB64] = parts;
let payloadJson: string;
let rawPayload: RawLicensePayload;
try {
payloadJson = Buffer.from(payloadB64, 'base64').toString('utf-8');
rawPayload = JSON.parse(payloadJson) as RawLicensePayload;
} catch {
return Err('Invalid license key: cannot decode payload');
}
const signature = Buffer.from(signatureB64, 'base64');
const isValid = crypto.verify(
'sha256',
Buffer.from(payloadJson, 'utf-8'),
this.publicKey,
signature
);
if (!isValid) {
return Err('Invalid license key: signature verification failed');
}
let plan: LicensePlan;
try {
plan = LicensePlan.fromString(rawPayload.plan);
} catch {
return Err(`Invalid plan in license: ${rawPayload.plan}`);
}
const expiresAt = rawPayload.expiresAt ? new Date(rawPayload.expiresAt) : null;
if (expiresAt && expiresAt < new Date()) {
return Err('License has expired');
}
const license = License.reconstitute(
{
plan,
organizationName: rawPayload.organizationName,
email: rawPayload.email,
issuedAt: new Date(rawPayload.issuedAt),
expiresAt,
signature: signatureB64,
rawKey: licenseKey,
},
UniqueId.create()
);
return Ok(license);
} catch (err) {
return Err(`License validation error: ${String(err)}`);
}
}
}

View File

@@ -0,0 +1,80 @@
#!/usr/bin/env ts-node
/**
* ABE License Key Generator (internal tool)
* Usage: ts-node src/scripts/generate-license.ts --plan pro --org "Acme Corp" --email admin@acme.com --expires 2027-01-01
*/
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
interface Args {
plan: 'free' | 'pro' | 'enterprise';
org: string;
email: string;
expires: string | null;
keyFile: string;
}
function parseArgs(): Args {
const args = process.argv.slice(2);
const get = (flag: string): string | undefined => {
const idx = args.indexOf(flag);
return idx >= 0 ? args[idx + 1] : undefined;
};
const plan = (get('--plan') ?? 'pro') as Args['plan'];
const org = get('--org') ?? 'Unknown Organization';
const email = get('--email') ?? '';
const expires = get('--expires') ?? null;
const keyFile = get('--key') ?? path.join(process.cwd(), 'license-private.pem');
return { plan, org, email, expires, keyFile };
}
function generateLicense(args: Args): string {
if (!fs.existsSync(args.keyFile)) {
throw new Error(
`Private key not found at ${args.keyFile}.\n` +
'Generate with: openssl genrsa -out license-private.pem 2048'
);
}
const privateKeyPem = fs.readFileSync(args.keyFile, 'utf-8');
const privateKey = crypto.createPrivateKey(privateKeyPem);
const payload = {
plan: args.plan,
organizationName: args.org,
email: args.email,
issuedAt: new Date().toISOString(),
expiresAt: args.expires ? new Date(args.expires).toISOString() : null,
};
const payloadJson = JSON.stringify(payload);
const payloadB64 = Buffer.from(payloadJson, 'utf-8').toString('base64');
const signature = crypto.sign('sha256', Buffer.from(payloadJson, 'utf-8'), privateKey);
const signatureB64 = signature.toString('base64');
return `${payloadB64}.${signatureB64}`;
}
function main(): void {
const args = parseArgs();
try {
const licenseKey = generateLicense(args);
console.log('\n=== ABE License Key ===');
console.log(licenseKey);
console.log('\n=== Details ===');
console.log(`Plan: ${args.plan}`);
console.log(`Organization: ${args.org}`);
console.log(`Email: ${args.email}`);
console.log(`Expires: ${args.expires ?? 'Never'}`);
console.log('===================\n');
} catch (err) {
console.error('Error generating license:', String(err));
process.exit(1);
}
}
main();

View File

@@ -0,0 +1,195 @@
import { LicensePlan } from '../../src/modules/licensing/domain/value-objects/LicensePlan';
import {
FeatureEntitlement,
FREE_FEATURES,
PRO_FEATURES,
ENTERPRISE_FEATURES,
} from '../../src/modules/licensing/domain/value-objects/FeatureEntitlement';
import { License } from '../../src/modules/licensing/domain/entities/License';
import { LicenseService } from '../../src/modules/licensing/application/LicenseService';
import { ILicenseValidator } from '../../src/modules/licensing/domain/ports/ILicenseValidator';
import { Result, Ok, Err } from '../../src/shared/domain/Result';
import { UniqueId } from '../../src/shared/domain/UniqueId';
// ─── LicensePlan ─────────────────────────────────────────────────────────────
describe('LicensePlan', () => {
it('creates free plan', () => {
const plan = LicensePlan.free();
expect(plan.isFree).toBe(true);
expect(plan.isPro).toBe(false);
expect(plan.isEnterprise).toBe(false);
expect(plan.toString()).toBe('free');
});
it('creates pro plan', () => {
const plan = LicensePlan.pro();
expect(plan.isPro).toBe(true);
expect(plan.isFree).toBe(false);
});
it('creates enterprise plan', () => {
const plan = LicensePlan.enterprise();
expect(plan.isEnterprise).toBe(true);
});
it('parses from string', () => {
expect(LicensePlan.fromString('pro').isPro).toBe(true);
expect(LicensePlan.fromString('enterprise').isEnterprise).toBe(true);
});
it('throws on invalid plan string', () => {
expect(() => LicensePlan.fromString('invalid')).toThrow();
});
it('equals comparison works', () => {
expect(LicensePlan.free().equals(LicensePlan.free())).toBe(true);
expect(LicensePlan.free().equals(LicensePlan.pro())).toBe(false);
});
});
// ─── FeatureEntitlement ───────────────────────────────────────────────────────
describe('FeatureEntitlement', () => {
it('free features do not include pro features', () => {
const free = FeatureEntitlement.forFeatures(FREE_FEATURES);
expect(free.has('exploration:basic')).toBe(true);
expect(free.has('reports:pdf')).toBe(false);
expect(free.has('integrations:slack')).toBe(false);
expect(free.has('auth:sso')).toBe(false);
});
it('pro features include free features', () => {
const pro = FeatureEntitlement.forFeatures(PRO_FEATURES);
expect(pro.has('exploration:basic')).toBe(true);
expect(pro.has('reports:pdf')).toBe(true);
expect(pro.has('integrations:slack')).toBe(true);
expect(pro.has('auth:sso')).toBe(false);
});
it('enterprise features include all features', () => {
const ent = FeatureEntitlement.forFeatures(ENTERPRISE_FEATURES);
expect(ent.has('auth:sso')).toBe(true);
expect(ent.has('auth:ldap')).toBe(true);
expect(ent.has('branding:whitelabel')).toBe(true);
});
});
// ─── License entity ──────────────────────────────────────────────────────────
describe('License entity', () => {
it('createFree returns a free plan license', () => {
const license = License.createFree();
expect(license.plan.isFree).toBe(true);
expect(license.isExpired).toBe(false);
expect(license.isValid).toBe(true);
});
it('free license has only free features', () => {
const license = License.createFree();
expect(license.hasFeature('exploration:basic')).toBe(true);
expect(license.hasFeature('reports:pdf')).toBe(false);
});
it('expired license falls back to free features', () => {
const license = License.reconstitute(
{
plan: LicensePlan.pro(),
organizationName: 'Test',
email: 'test@test.com',
expiresAt: new Date('2020-01-01'),
issuedAt: new Date('2019-01-01'),
signature: 'sig',
rawKey: 'key',
},
UniqueId.create()
);
expect(license.isExpired).toBe(true);
expect(license.isValid).toBe(false);
expect(license.hasFeature('reports:pdf')).toBe(false);
});
it('pro license has pro features', () => {
const license = License.reconstitute(
{
plan: LicensePlan.pro(),
organizationName: 'Acme',
email: 'admin@acme.com',
expiresAt: null,
issuedAt: new Date(),
signature: 'sig',
rawKey: 'key',
},
UniqueId.create()
);
expect(license.isValid).toBe(true);
expect(license.hasFeature('reports:pdf')).toBe(true);
expect(license.hasFeature('integrations:slack')).toBe(true);
expect(license.hasFeature('auth:sso')).toBe(false);
});
});
// ─── LicenseService ───────────────────────────────────────────────────────────
describe('LicenseService', () => {
function makeProLicense(): License {
return License.reconstitute(
{
plan: LicensePlan.pro(),
organizationName: 'Acme',
email: 'admin@acme.com',
expiresAt: null,
issuedAt: new Date(),
signature: 'sig',
rawKey: 'key',
},
UniqueId.create()
);
}
it('starts with free license', () => {
const validator: ILicenseValidator = {
validate: async () => Err('should not be called'),
};
const service = new LicenseService(validator);
const status = service.getStatus();
expect(status.plan).toBe('free');
expect(status.isValid).toBe(true);
});
it('activate with valid key updates current license', async () => {
const proLicense = makeProLicense();
const validator: ILicenseValidator = {
validate: async () => Ok(proLicense),
};
const service = new LicenseService(validator);
const result = await service.activate('valid-key');
expect(result.ok).toBe(true);
expect(service.getStatus().plan).toBe('pro');
expect(service.hasFeature('reports:pdf')).toBe(true);
});
it('activate with invalid key returns error and keeps free license', async () => {
const validator: ILicenseValidator = {
validate: async () => Err('Invalid license key: signature verification failed'),
};
const service = new LicenseService(validator);
const result = await service.activate('invalid-key');
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toContain('signature verification failed');
}
expect(service.getStatus().plan).toBe('free');
});
it('hasFeature checks current license entitlements', () => {
const validator: ILicenseValidator = {
validate: async () => Err('unused'),
};
const service = new LicenseService(validator);
expect(service.hasFeature('exploration:basic')).toBe(true);
expect(service.hasFeature('auth:sso')).toBe(false);
});
});