@caronte-sdk/node 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.md +66 -0
  2. package/bin/caronte-node.js +62 -0
  3. package/dist/adapters/apollo.cjs +21 -3
  4. package/dist/adapters/apollo.cjs.map +1 -1
  5. package/dist/adapters/apollo.d.cts +1 -1
  6. package/dist/adapters/apollo.d.ts +1 -1
  7. package/dist/adapters/apollo.js +18 -4
  8. package/dist/adapters/apollo.js.map +1 -1
  9. package/dist/adapters/express.cjs +10 -0
  10. package/dist/adapters/express.cjs.map +1 -1
  11. package/dist/adapters/express.d.cts +1 -1
  12. package/dist/adapters/express.d.ts +1 -1
  13. package/dist/adapters/express.js +7 -1
  14. package/dist/adapters/express.js.map +1 -1
  15. package/dist/adapters/fastify.cjs +10 -1
  16. package/dist/adapters/fastify.cjs.map +1 -1
  17. package/dist/adapters/fastify.d.cts +1 -1
  18. package/dist/adapters/fastify.d.ts +1 -1
  19. package/dist/adapters/fastify.js +10 -1
  20. package/dist/adapters/fastify.js.map +1 -1
  21. package/dist/adapters/nest/index.cjs +70 -1
  22. package/dist/adapters/nest/index.cjs.map +1 -1
  23. package/dist/adapters/nest/index.d.cts +13 -1
  24. package/dist/adapters/nest/index.d.ts +13 -1
  25. package/dist/adapters/nest/index.js +24 -2
  26. package/dist/adapters/nest/index.js.map +1 -1
  27. package/dist/{chunk-XMA2NAR7.js → chunk-M46BRRWA.js} +50 -3
  28. package/dist/chunk-M46BRRWA.js.map +1 -0
  29. package/dist/{client-TcOSwgrn.d.cts → client-CSokWFYn.d.cts} +3 -0
  30. package/dist/{client-TcOSwgrn.d.ts → client-CSokWFYn.d.ts} +3 -0
  31. package/dist/index.cjs +48 -0
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.d.cts +17 -3
  34. package/dist/index.d.ts +17 -3
  35. package/dist/index.js +1 -1
  36. package/package.json +16 -2
  37. package/d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AA;AAAA,QACF;AAEA,QAAA,MAAM,UAAU,MAAA,CAAO,eAAA,CAAgB,OAAA,CAAQ,WAAA,EAAa,IAAI,cAAc,CAAA;AAC9E,QAAA,IAAI,CAAC,OAAA,EAAS;AACZ,UAAA,MAAM,KAAA,CAAM,MAAA,CAAO,GAAG,CAAA,CAAE,IAAA,CAAK,EAAE,KAAA,EAAO,WAAA,EAAa,MAAA,EAAQ,2BAAA,EAA6B,CAAA;AAAA,QAC1F;AAAA,MACF,CAAA;AAAA,IACF;AAAA,GACF;AACF,CAAA;AAEO,IAAM,oBAAA,GAAuB,GAAG,aAAA,EAAe;AAAA,EACpD,IAAA,EAAM,SAAA;AAAA,EACN,OAAA,EAAS;AACX,CAAC","file":"fastify.js","sourcesContent":["import type { FastifyInstance, FastifyPluginAsync, FastifyReply, FastifyRequest } from 'fastify';\nimport fp from 'fastify-plugin';\nimport { CaronteClient } from '../client.js';\nimport type { TokenClaims } from '../models.js';\nimport { detectMethod, registerOperation } from '../registry.js';\n\ndeclare module 'fastify' {\n interface FastifyRequest {\n caronteUser?: TokenClaims;\n }\n}\n\nexport interface CarontePluginOptions {\n client: CaronteClient;\n}\n\nconst carontePlugin: FastifyPluginAsync<CarontePluginOptions> = async (\n fastify: FastifyInstance,\n { client }: CarontePluginOptions,\n) => {\n // Decorate request with caronteUser\n fastify.decorateRequest<TokenClaims | undefined>('caronteUser', undefined);\n\n // Global hook — validates Bearer token on every request\n fastify.addHook('onRequest', async (request: FastifyRequest) => {\n const auth = request.headers.authorization ?? '';\n const [scheme, token] = auth.split(' ');\n if (scheme?.toLowerCase() === 'bearer' && token) {\n try {\n request.caronteUser = await client.validateToken(token);\n } catch {\n // invalid token — caronteUser stays null; guard handles the 401\n }\n }\n });\n\n // Decorate fastify with a guard factory\n fastify.decorate(\n 'caronteGuard',\n (id: string, level: string, method?: string) => {\n const resolvedMethod = method ?? detectMethod(id);\n registerOperation({ identifier: id, method: resolvedMethod, level });\n\n return async (request: FastifyRequest, reply: FastifyReply): Promise<void> => {\n if (level === 'public') return;\n\n if (!request.caronteUser) {\n await reply.status(401).send({ error: 'unauthorized', detail: 'Bearer token required.' });\n return;\n }\n\n const allowed = client.checkPermission(request.caronteUser, id, resolvedMethod);\n if (!allowed) {\n await reply.status(403).send({ error: 'forbidden', detail: 'Insufficient permissions.' });\n }\n };\n },\n );\n};\n\nexport const caronteFastifyPlugin = fp(carontePlugin, {\n name: 'caronte',\n fastify: '>=4',\n});\n\ndeclare module 'fastify' {\n interface FastifyInstance {\n caronteGuard: (\n id: string,\n level: string,\n method?: string,\n ) => (req: FastifyRequest, reply: FastifyReply) => Promise<void>;\n }\n}"]}
1
+ {"version":3,"sources":["../../src/adapters/fastify.ts"],"names":[],"mappings":";;;;AAiBA,IAAM,aAAA,GAA0D,OAC9D,OAAA,EACA,EAAE,QAAO,KACN;AAEH,EAAA,OAAA,CAAQ,eAAA,CAAyC,eAAe,MAAS,CAAA;AAGzE,EAAA,OAAA,CAAQ,OAAA,CAAQ,WAAA,EAAa,OAAO,OAAA,KAA4B;AAC9D,IAAA,MAAM,IAAA,GAAO,OAAA,CAAQ,OAAA,CAAQ,aAAA,IAAiB,EAAA;AAC9C,IAAA,MAAM,CAAC,MAAA,EAAQ,KAAK,CAAA,GAAI,IAAA,CAAK,MAAM,GAAG,CAAA;AACtC,IAAA,IAAI,MAAA,EAAQ,WAAA,EAAY,KAAM,QAAA,IAAY,KAAA,EAAO;AAC/C,MAAA,IAAI;AACF,QAAA,OAAA,CAAQ,WAAA,GAAc,MAAM,MAAA,CAAO,aAAA,CAAc,KAAK,CAAA;AACtD,QAAA,KAAA,CAAM,eAAc,EAAG,YAAA,CAAa,cAAA,EAAgB,OAAA,CAAQ,YAAY,GAAG,CAAA;AAAA,MAC7E,CAAA,CAAA,MAAQ;AAAA,MAER;AAAA,IACF;AAAA,EACF,CAAC,CAAA;AAGD,EAAA,OAAA,CAAQ,QAAA;AAAA,IACN,cAAA;AAAA,IACA,CAAC,EAAA,EAAY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;AACX,CAAC","file":"fastify.js","sourcesContent":["import type { FastifyInstance, FastifyPluginAsync, FastifyReply, FastifyRequest } from 'fastify';\nimport fp from 'fastify-plugin';\nimport { trace } from '@opentelemetry/api';\nimport { CaronteClient } from '../client.js';\nimport type { TokenClaims } from '../models.js';\nimport { detectMethod, registerOperation } from '../registry.js';\n\ndeclare module 'fastify' {\n interface FastifyRequest {\n caronteUser?: TokenClaims;\n }\n}\n\nexport interface CarontePluginOptions {\n client: CaronteClient;\n}\n\nconst carontePlugin: FastifyPluginAsync<CarontePluginOptions> = async (\n fastify: FastifyInstance,\n { client }: CarontePluginOptions,\n) => {\n // Decorate request with caronteUser\n fastify.decorateRequest<TokenClaims | undefined>('caronteUser', undefined);\n\n // Global hook — validates Bearer token on every request\n fastify.addHook('onRequest', async (request: FastifyRequest) => {\n const auth = request.headers.authorization ?? '';\n const [scheme, token] = auth.split(' ');\n if (scheme?.toLowerCase() === 'bearer' && token) {\n try {\n request.caronteUser = await client.validateToken(token);\n trace.getActiveSpan()?.setAttribute('caronte.user', request.caronteUser.sub);\n } catch {\n // invalid token — caronteUser stays null; guard handles the 401\n }\n }\n });\n\n // Decorate fastify with a guard factory\n fastify.decorate(\n 'caronteGuard',\n (id: string, level: string, method?: string) => {\n const resolvedMethod = method ?? detectMethod(id);\n registerOperation({ identifier: id, method: resolvedMethod, level });\n\n return async (request: FastifyRequest, reply: FastifyReply): Promise<void> => {\n const span = trace.getActiveSpan();\n span?.setAttribute('caronte.operation_id', id);\n\n if (level === 'public') {\n span?.setAttribute('caronte.permission_result', 'public');\n return;\n }\n\n if (!request.caronteUser) {\n span?.setAttribute('caronte.permission_result', 'unauthorized');\n await reply.status(401).send({ error: 'unauthorized', detail: 'Bearer token required.' });\n return;\n }\n\n const allowed = client.checkPermission(request.caronteUser, id, resolvedMethod);\n span?.setAttribute('caronte.permission_result', allowed ? 'allowed' : 'forbidden');\n if (!allowed) {\n await reply.status(403).send({ error: 'forbidden', detail: 'Insufficient permissions.' });\n }\n };\n },\n );\n};\n\nexport const caronteFastifyPlugin = fp(carontePlugin, {\n name: 'caronte',\n fastify: '>=4',\n});\n\ndeclare module 'fastify' {\n interface FastifyInstance {\n caronteGuard: (\n id: string,\n level: string,\n method?: string,\n ) => (req: FastifyRequest, reply: FastifyReply) => Promise<void>;\n }\n}"]}
@@ -2,7 +2,18 @@
2
2
 
3
3
  var common = require('@nestjs/common');
4
4
  var core = require('@nestjs/core');
5
+ var api = require('@opentelemetry/api');
5
6
  var jose = require('jose');
7
+ var sdkNode = require('@opentelemetry/sdk-node');
8
+ var exporterTraceOtlpHttp = require('@opentelemetry/exporter-trace-otlp-http');
9
+ var exporterMetricsOtlpHttp = require('@opentelemetry/exporter-metrics-otlp-http');
10
+ var exporterLogsOtlpHttp = require('@opentelemetry/exporter-logs-otlp-http');
11
+ var sdkMetrics = require('@opentelemetry/sdk-metrics');
12
+ var sdkLogs = require('@opentelemetry/sdk-logs');
13
+ var autoInstrumentationsNode = require('@opentelemetry/auto-instrumentations-node');
14
+ var resources = require('@opentelemetry/resources');
15
+ var semanticConventions = require('@opentelemetry/semantic-conventions');
16
+ var apiLogs = require('@opentelemetry/api-logs');
6
17
 
7
18
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
8
19
  var __decorateClass = (decorators, target, key, kind) => {
@@ -59,6 +70,38 @@ var CaronteSyncError = class extends CaronteError {
59
70
  };
60
71
  var CaronteConfigError = class extends CaronteError {
61
72
  };
73
+ var started = false;
74
+ function initOtel(options) {
75
+ if (started) return;
76
+ try {
77
+ const base = options.otelCollectorUrl.replace(/\/$/, "");
78
+ const resource = resources.defaultResource().merge(resources.resourceFromAttributes({
79
+ [semanticConventions.ATTR_SERVICE_NAME]: options.appId,
80
+ [semanticConventions.ATTR_SERVICE_NAMESPACE]: options.realmId
81
+ }));
82
+ const sdk = new sdkNode.NodeSDK({
83
+ resource,
84
+ traceExporter: new exporterTraceOtlpHttp.OTLPTraceExporter({ url: `${base}/v1/traces` }),
85
+ metricReaders: [
86
+ new sdkMetrics.PeriodicExportingMetricReader({
87
+ exporter: new exporterMetricsOtlpHttp.OTLPMetricExporter({ url: `${base}/v1/metrics` })
88
+ })
89
+ ],
90
+ logRecordProcessors: [
91
+ new sdkLogs.BatchLogRecordProcessor({ exporter: new exporterLogsOtlpHttp.OTLPLogExporter({ url: `${base}/v1/logs` }) })
92
+ ],
93
+ instrumentations: [autoInstrumentationsNode.getNodeAutoInstrumentations()]
94
+ });
95
+ sdk.start();
96
+ started = true;
97
+ apiLogs.logs.getLogger("caronte-sdk").emit({
98
+ severityNumber: apiLogs.SeverityNumber.INFO,
99
+ body: `caronte-sdk OTel initialized for app_id=${options.appId} realm_id=${options.realmId}`
100
+ });
101
+ } catch (err) {
102
+ console.warn(`[caronte-sdk] OTel initialization skipped: ${err.message}`);
103
+ }
104
+ }
62
105
 
63
106
  // src/client.ts
64
107
  var CaronteClient = class _CaronteClient {
@@ -66,6 +109,7 @@ var CaronteClient = class _CaronteClient {
66
109
  realmId;
67
110
  appId;
68
111
  secret;
112
+ otelCollectorUrl;
69
113
  appToken = null;
70
114
  jwks = null;
71
115
  operations = [];
@@ -75,6 +119,7 @@ var CaronteClient = class _CaronteClient {
75
119
  this.realmId = options.realmId;
76
120
  this.appId = options.appId;
77
121
  this.secret = options.secret;
122
+ this.otelCollectorUrl = options.otelCollectorUrl;
78
123
  }
79
124
  get realmBase() {
80
125
  return `${this.base}/realms/${this.realmId}`;
@@ -88,6 +133,9 @@ var CaronteClient = class _CaronteClient {
88
133
  "No operations registered. Call registerOperation() before startup()."
89
134
  );
90
135
  }
136
+ if (this.otelCollectorUrl) {
137
+ initOtel({ otelCollectorUrl: this.otelCollectorUrl, appId: this.appId, realmId: this.realmId });
138
+ }
91
139
  await this.authenticate();
92
140
  this.initJwks();
93
141
  await this.syncOperations();
@@ -221,20 +269,29 @@ exports.CaronteGuard = class CaronteGuard {
221
269
  if (!meta) return true;
222
270
  const { id, level, method } = meta;
223
271
  const request = ctx.switchToHttp().getRequest();
224
- if (level === "public") return true;
272
+ const span = api.trace.getActiveSpan();
273
+ span?.setAttribute("caronte.operation_id", id);
274
+ if (level === "public") {
275
+ span?.setAttribute("caronte.permission_result", "public");
276
+ return true;
277
+ }
225
278
  const auth = request.headers["authorization"] ?? "";
226
279
  const [scheme, token] = auth.split(" ");
227
280
  if (scheme?.toLowerCase() !== "bearer" || !token) {
281
+ span?.setAttribute("caronte.permission_result", "unauthorized");
228
282
  throw new common.UnauthorizedException("Bearer token required.");
229
283
  }
230
284
  let claims;
231
285
  try {
232
286
  claims = await this.client.validateToken(token);
233
287
  } catch {
288
+ span?.setAttribute("caronte.permission_result", "unauthorized");
234
289
  throw new common.UnauthorizedException("Invalid token.");
235
290
  }
236
291
  request.caronteUser = claims;
292
+ span?.setAttribute("caronte.user", claims.sub);
237
293
  const allowed = this.client.checkPermission(claims, id, method);
294
+ span?.setAttribute("caronte.permission_result", allowed ? "allowed" : "forbidden");
238
295
  if (!allowed) throw new common.ForbiddenException("Insufficient permissions.");
239
296
  return true;
240
297
  }
@@ -266,6 +323,18 @@ exports.CaronteModule = class CaronteModule {
266
323
  * })
267
324
  * export class AppModule {}
268
325
  * ```
326
+ *
327
+ * @remarks
328
+ * If `options.otelCollectorUrl` is set, this alone does **not** get you
329
+ * request tracing: `NestFactory.create()` requires express/fastify
330
+ * internally to build the HTTP adapter *before* this module's provider
331
+ * factory (and its `startup()`/`initOtel()` call) ever runs, so
332
+ * auto-instrumentation attaches too late. Call `initOtel()` from
333
+ * `@caronte-sdk/node` yourself, before `import { NestFactory } from
334
+ * '@nestjs/core'` in `main.ts` (CommonJS only — `require()` order is
335
+ * preserved textually there). See the README's "Running with
336
+ * otelCollectorUrl on NestJS" section for the full example. The call
337
+ * this module makes is a harmless idempotent no-op either way.
269
338
  */
270
339
  static forRootAsync(options) {
271
340
  return {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/registry.ts","../../../src/adapters/nest/decorator.ts","../../../src/exceptions.ts","../../../src/client.ts","../../../src/adapters/nest/guard.ts","../../../src/adapters/nest/module.ts"],"names":["SetMetadata","createParamDecorator","createRemoteJWKSet","jwtVerify","CaronteGuard","UnauthorizedException","ForbiddenException","Injectable","Reflector","CaronteModule","Module"],"mappings":";;;;;;;;;;;;;;;;;AAEA,IAAM,YAAmC,EAAC;AAKnC,SAAS,aAAa,EAAA,EAAoB;AAC/C,EAAA,MAAM,IAAA,GAAA,CAAQ,GAAG,KAAA,CAAM,GAAG,EAAE,GAAA,EAAI,IAAK,IAAI,WAAA,EAAY;AACrD,EAAA,IAAI,wBAAA,CAAyB,IAAA,CAAK,IAAI,CAAA,EAAS,OAAO,MAAA;AACtD,EAAA,IAAI,0BAAA,CAA2B,IAAA,CAAK,IAAI,CAAA,EAAQ,OAAO,QAAA;AACvD,EAAA,IAAI,iCAAA,CAAkC,IAAA,CAAK,IAAI,CAAA,EAAG,OAAO,QAAA;AACzD,EAAA,OAAO,OAAA;AACT;AAGO,SAAS,kBAAkB,EAAA,EAA+B;AAC/D,EAAA,MAAM,SAAS,SAAA,CAAU,IAAA;AAAA,IACvB,OAAK,CAAA,CAAE,UAAA,KAAe,GAAG,UAAA,IAAc,CAAA,CAAE,WAAW,EAAA,CAAG;AAAA,GACzD;AACA,EAAA,IAAI,CAAC,MAAA,EAAQ,SAAA,CAAU,IAAA,CAAK,EAAE,CAAA;AAChC;AAEO,SAAS,WAAA,GAAqC;AACnD,EAAA,OAAO,CAAC,GAAG,SAAS,CAAA;AACtB;;;ACrBO,IAAM,aAAA,GAAgB;AAkBtB,SAAS,SAAA,CAAU,EAAA,EAAY,KAAA,EAAe,MAAA,EAAkC;AACrF,EAAA,MAAM,cAAA,GAAiB,MAAA,IAAU,YAAA,CAAa,EAAE,CAAA;AAChD,EAAA,iBAAA,CAAkB,EAAE,UAAA,EAAY,EAAA,EAAI,MAAA,EAAQ,cAAA,EAAgB,OAAO,CAAA;AACnE,EAAA,OAAOA,mBAAmC,aAAA,EAAe,EAAE,IAAI,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","sourcesContent":["import type { OperationDescriptor } from './models.js';\n\nconst _registry: OperationDescriptor[] = [];\n\n/** Infer operation method from the last segment of the operation id.\n * e.g. \"tasks:list\" → \"read\", \"tasks:create\" → \"write\", \"tasks:delete\" → \"delete\"\n */\nexport function detectMethod(id: string): string {\n const last = (id.split(':').pop() ?? '').toLowerCase();\n if (/^(get|list|fetch|read)/.test(last)) return 'read';\n if (/^(delete|remove|destroy)/.test(last)) return 'delete';\n if (/(stream|subscribe|watch|listen)/.test(last)) return 'stream';\n return 'write';\n}\n\n/** Register an operation in the global registry (used by startup to sync). */\nexport function registerOperation(op: OperationDescriptor): void {\n const exists = _registry.some(\n o => o.identifier === op.identifier && o.method === op.method,\n );\n if (!exists) _registry.push(op);\n}\n\nexport function getRegistry(): OperationDescriptor[] {\n return [..._registry];\n}\n\nexport function clearRegistry(): void {\n _registry.length = 0;\n}","import { createParamDecorator, ExecutionContext, SetMetadata } from '@nestjs/common';\nimport type { TokenClaims } from '../../models.js';\nimport { detectMethod, registerOperation } from '../../registry.js';\n\nexport const OPERATION_KEY = 'caronte:operation';\n\nexport interface OperationMeta {\n id: string;\n level: string;\n method: string;\n}\n\n/**\n * Marks a controller method as a caronte operation.\n *\n * @example\n * ```ts\n * @Get()\n * @Operation('tasks:list', 'private')\n * listTasks(@CaronteUser() user: TokenClaims) { ... }\n * ```\n */\nexport function Operation(id: string, level: string, method?: string): MethodDecorator {\n const resolvedMethod = method ?? detectMethod(id);\n registerOperation({ identifier: id, method: resolvedMethod, level });\n return SetMetadata<string, OperationMeta>(OPERATION_KEY, { id, level, method: resolvedMethod });\n}\n\n/** Injects the authenticated user's TokenClaims into a parameter. */\nexport const CaronteUser = createParamDecorator(\n (_data: unknown, ctx: ExecutionContext): TokenClaims | undefined =>\n ctx.switchToHttp().getRequest().caronteUser,\n);","export class CaronteError extends Error {\n constructor(message: string) {\n super(message);\n this.name = this.constructor.name;\n }\n}\n\nexport class CaronteAuthError extends CaronteError {}\nexport class CaronteTokenError extends CaronteError {}\nexport class CaronteForbiddenError extends CaronteError {}\nexport class CaronteSyncError extends CaronteError {}\nexport class CaronteConfigError extends CaronteError {}","import { createRemoteJWKSet, jwtVerify, type JWTPayload } from 'jose';\nimport {\n CaronteAuthError, CaronteConfigError, CaronteError,\n CaronteSyncError, CaronteTokenError,\n} from './exceptions.js';\nimport type { OperationDescriptor, OperationWithGroups, TokenClaims } from './models.js';\nimport { getRegistry } from './registry.js';\n\nexport interface CaronteClientOptions {\n authorizerUrl: string;\n realmId: string;\n appId: string;\n secret: string;\n}\n\nexport class CaronteClient {\n private readonly base: string;\n private readonly realmId: string;\n private readonly appId: string;\n private readonly secret: string;\n\n private appToken: string | null = null;\n private jwks: ReturnType<typeof createRemoteJWKSet> | null = null;\n private operations: OperationWithGroups[] = [];\n private registeredOperations: OperationDescriptor[] = [];\n\n constructor(options: CaronteClientOptions) {\n this.base = options.authorizerUrl.replace(/\\/$/, '');\n this.realmId = options.realmId;\n this.appId = options.appId;\n this.secret = options.secret;\n }\n\n private get realmBase(): string {\n return `${this.base}/realms/${this.realmId}`;\n }\n\n registerOperation(op: OperationDescriptor): void {\n this.registeredOperations.push(op);\n }\n\n async startup(): Promise<void> {\n if (this.registeredOperations.length === 0) {\n throw new CaronteConfigError(\n 'No operations registered. Call registerOperation() before startup().',\n );\n }\n await this.authenticate();\n this.initJwks();\n await this.syncOperations();\n await this.fetchOperations();\n }\n\n async authenticate(): Promise<void> {\n const res = await fetch(`${this.realmBase}/apps/token`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ app_id: this.appId, secret: this.secret }),\n });\n if (!res.ok) {\n throw new CaronteAuthError(\n `Authentication failed: HTTP ${res.status} — ${await res.text()}`,\n );\n }\n const data = await res.json() as { access_token?: string };\n if (!data.access_token) throw new CaronteAuthError('Authorizer returned no access_token.');\n this.appToken = data.access_token;\n }\n\n private initJwks(): void {\n this.jwks = createRemoteJWKSet(\n new URL(`${this.realmBase}/protocol/openid-connect/certs`),\n );\n }\n\n async syncOperations(): Promise<void> {\n this.ensureAppToken();\n const res = await fetch(`${this.realmBase}/apps/operations/sync`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.appToken}`,\n },\n body: JSON.stringify({\n operations: this.registeredOperations.map(op => ({\n identifier: op.identifier,\n method: op.method,\n level: op.level,\n description: op.description ?? null,\n })),\n }),\n });\n if (!res.ok) {\n throw new CaronteSyncError(`Sync failed: HTTP ${res.status} — ${await res.text()}`);\n }\n }\n\n async fetchOperations(): Promise<void> {\n this.ensureAppToken();\n const res = await fetch(`${this.realmBase}/apps/operations`, {\n headers: { Authorization: `Bearer ${this.appToken}` },\n });\n if (!res.ok) {\n throw new CaronteError(\n `Fetch operations failed: HTTP ${res.status} — ${await res.text()}`,\n );\n }\n const data = await res.json() as { operations: any[] };\n this.operations = data.operations.map(op => ({\n operationId: op.operation_id,\n identifier: op.identifier,\n method: op.method,\n level: op.level,\n allowedGroups: op.allowed_groups ?? [],\n }));\n }\n\n async validateToken(token: string): Promise<TokenClaims> {\n if (!this.jwks) this.initJwks();\n try {\n const { payload } = await jwtVerify(token, this.jwks!);\n const p = payload as JWTPayload & Record<string, unknown>;\n const tokenRealm = (p['realm_id'] as string) ?? '';\n\n // As chaves JWKS já são buscadas em /realms/{this.realmId}/... (ver\n // realmBase), então um token de outro realm normalmente já falharia a\n // verificação de assinatura acima. Esta checagem é defesa em\n // profundidade explícita — não depende desse efeito colateral, e falha\n // com uma mensagem clara em vez de erro de assinatura.\n if (tokenRealm.toLowerCase() !== this.realmId.toLowerCase()) {\n throw new CaronteTokenError(\n `Token realm '${tokenRealm}' does not match this app's realm '${this.realmId}'.`,\n );\n }\n\n return {\n sub: payload.sub ?? '',\n realmId: tokenRealm,\n groups: (p['groups'] as string[]) ?? [],\n exp: payload.exp ?? 0,\n raw: p,\n };\n } catch (err) {\n if (err instanceof CaronteTokenError) throw err;\n throw new CaronteTokenError(`Invalid token: ${(err as Error).message}`);\n }\n }\n\n checkPermission(claims: TokenClaims, identifier: string, method: string): boolean {\n const op = this.operations.find(\n o => o.identifier === identifier && o.method === method,\n );\n if (!op) return false;\n if (op.level === 'public') return true;\n if (op.level === 'private') return claims.groups.length > 0;\n if (op.level === 'protected') return claims.groups.some(g => op.allowedGroups.includes(g));\n return false;\n }\n\n /** Load operations from the global registry and bootstrap the client. */\n static async create(options: CaronteClientOptions): Promise<CaronteClient> {\n const client = new CaronteClient(options);\n for (const op of getRegistry()) client.registerOperation(op);\n await client.startup();\n return client;\n }\n\n private ensureAppToken(): void {\n if (!this.appToken) {\n throw new CaronteAuthError('No app token. Call authenticate() or startup() first.');\n }\n }\n}","import {\n CanActivate, ExecutionContext, ForbiddenException,\n Inject, Injectable, UnauthorizedException,\n} from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport { CaronteClient } from '../../client.js';\nimport type { TokenClaims } from '../../models.js';\nimport { OPERATION_KEY, type OperationMeta } from './decorator.js';\n\n@Injectable()\nexport class CaronteGuard implements CanActivate {\n // @Inject() explícito -- não depender de emitDecoratorMetadata (design:paramtypes)\n // pra resolver os parâmetros: o build desta lib usa tsup/esbuild, que não emite\n // essa metadata (limitação conhecida do esbuild), então a injeção implícita por\n // tipo do Nest falha silenciosamente (reflector fica undefined em runtime,\n // TypeError em qualquer rota guardada por CaronteGuard).\n constructor(\n @Inject(CaronteClient) private readonly client: CaronteClient,\n @Inject(Reflector) private readonly reflector: Reflector,\n ) {}\n\n async canActivate(ctx: ExecutionContext): Promise<boolean> {\n const meta = this.reflector.get<OperationMeta | undefined>(\n OPERATION_KEY,\n ctx.getHandler(),\n );\n\n if (!meta) return true; // no @Operation — pass through\n\n const { id, level, method } = meta;\n const request = ctx.switchToHttp().getRequest<{ headers: Record<string, string>; caronteUser?: TokenClaims }>();\n\n if (level === 'public') return true;\n\n const auth = request.headers['authorization'] ?? '';\n const [scheme, token] = auth.split(' ');\n\n if (scheme?.toLowerCase() !== 'bearer' || !token) {\n throw new UnauthorizedException('Bearer token required.');\n }\n\n let claims: TokenClaims;\n try {\n claims = await this.client.validateToken(token);\n } catch {\n // Token malformado/expirado/inválido -- 401, igual aos adapters express/\n // fastify (que engolem o erro de validateToken pelo mesmo motivo), não\n // deixar virar 500 por exceção não tratada.\n throw new UnauthorizedException('Invalid token.');\n }\n request.caronteUser = claims;\n\n const allowed = this.client.checkPermission(claims, id, method);\n if (!allowed) throw new ForbiddenException('Insufficient permissions.');\n\n return true;\n }\n}","import { DynamicModule, Module } from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport { CaronteClient, type CaronteClientOptions } from '../../client.js';\nimport { getRegistry } from '../../registry.js';\nimport { CaronteGuard } from './guard.js';\n\nexport interface CaronteModuleOptions extends CaronteClientOptions {}\n\n@Module({})\nexport class CaronteModule {\n /**\n * Register the caronte module globally.\n *\n * Creates the `CaronteClient`, bootstraps it (authenticate + sync + fetch),\n * and provides `CaronteGuard` for injection.\n *\n * @example\n * ```ts\n * // app.module.ts\n * @Module({\n * imports: [\n * CaronteModule.forRootAsync({\n * authorizerUrl: process.env.AUTHORIZER_URL!,\n * realmId: process.env.REALM_ID!,\n * appId: process.env.APP_ID!,\n * secret: process.env.APP_SECRET!,\n * }),\n * ],\n * })\n * export class AppModule {}\n * ```\n */\n static forRootAsync(options: CaronteModuleOptions): DynamicModule {\n return {\n module: CaronteModule,\n global: true,\n providers: [\n Reflector,\n {\n provide: CaronteClient,\n useFactory: async () => {\n const client = new CaronteClient(options);\n for (const op of getRegistry()) client.registerOperation(op);\n await client.startup();\n return client;\n },\n },\n CaronteGuard,\n ],\n exports: [CaronteClient, CaronteGuard],\n };\n }\n}"]}
1
+ {"version":3,"sources":["../../../src/registry.ts","../../../src/adapters/nest/decorator.ts","../../../src/exceptions.ts","../../../src/otel.ts","../../../src/client.ts","../../../src/adapters/nest/guard.ts","../../../src/adapters/nest/module.ts"],"names":["SetMetadata","createParamDecorator","defaultResource","resourceFromAttributes","ATTR_SERVICE_NAME","ATTR_SERVICE_NAMESPACE","NodeSDK","OTLPTraceExporter","PeriodicExportingMetricReader","OTLPMetricExporter","BatchLogRecordProcessor","OTLPLogExporter","getNodeAutoInstrumentations","logs","SeverityNumber","createRemoteJWKSet","jwtVerify","CaronteGuard","trace","UnauthorizedException","ForbiddenException","Injectable","Reflector","CaronteModule","Module"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,IAAM,YAAmC,EAAC;AAKnC,SAAS,aAAa,EAAA,EAAoB;AAC/C,EAAA,MAAM,IAAA,GAAA,CAAQ,GAAG,KAAA,CAAM,GAAG,EAAE,GAAA,EAAI,IAAK,IAAI,WAAA,EAAY;AACrD,EAAA,IAAI,wBAAA,CAAyB,IAAA,CAAK,IAAI,CAAA,EAAS,OAAO,MAAA;AACtD,EAAA,IAAI,0BAAA,CAA2B,IAAA,CAAK,IAAI,CAAA,EAAQ,OAAO,QAAA;AACvD,EAAA,IAAI,iCAAA,CAAkC,IAAA,CAAK,IAAI,CAAA,EAAG,OAAO,QAAA;AACzD,EAAA,OAAO,OAAA;AACT;AAGO,SAAS,kBAAkB,EAAA,EAA+B;AAC/D,EAAA,MAAM,SAAS,SAAA,CAAU,IAAA;AAAA,IACvB,OAAK,CAAA,CAAE,UAAA,KAAe,GAAG,UAAA,IAAc,CAAA,CAAE,WAAW,EAAA,CAAG;AAAA,GACzD;AACA,EAAA,IAAI,CAAC,MAAA,EAAQ,SAAA,CAAU,IAAA,CAAK,EAAE,CAAA;AAChC;AAEO,SAAS,WAAA,GAAqC;AACnD,EAAA,OAAO,CAAC,GAAG,SAAS,CAAA;AACtB;;;ACrBO,IAAM,aAAA,GAAgB;AAkBtB,SAAS,SAAA,CAAU,EAAA,EAAY,KAAA,EAAe,MAAA,EAAkC;AACrF,EAAA,MAAM,cAAA,GAAiB,MAAA,IAAU,YAAA,CAAa,EAAE,CAAA;AAChD,EAAA,iBAAA,CAAkB,EAAE,UAAA,EAAY,EAAA,EAAI,MAAA,EAAQ,cA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AAA;AAAA,EADNI,iBAAA,EAAW;AAAA,EAQP,iCAAO,aAAa,CAAA,CAAA;AAAA,EACpB,iCAAOC,cAAS,CAAA;AAAA,CAAA,EARRL,oBAAA,CAAA;ACFAM,wBAAN,mBAAA,CAAoB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmCzB,OAAO,aAAa,OAAA,EAA8C;AAChE,IAAA,OAAO;AAAA,MACL,MAAA,EAAQA,qBAAA;AAAA,MACR,MAAA,EAAQ,IAAA;AAAA,MACR,SAAA,EAAW;AAAA,QACTD,cAAAA;AAAA,QACA;AAAA,UACE,OAAA,EAAS,aAAA;AAAA,UACT,YAAY,YAAY;AACtB,YAAA,MAAM,MAAA,GAAS,IAAI,aAAA,CAAc,OAAO,CAAA;AACxC,YAAA,KAAA,MAAW,EAAA,IAAM,WAAA,EAAY,EAAG,MAAA,CAAO,kBAAkB,EAAE,CAAA;AAC3D,YAAA,MAAM,OAAO,OAAA,EAAQ;AACrB,YAAA,OAAO,MAAA;AAAA,UACT;AAAA,SACF;AAAA,QACAL;AAAA,OACF;AAAA,MACA,OAAA,EAAS,CAAC,aAAA,EAAeA,oBAAY;AAAA,KACvC;AAAA,EACF;AACF;AAvDaM,qBAAA,GAAN,eAAA,CAAA;AAAA,EADNC,aAAA,CAAO,EAAE;AAAA,CAAA,EACGD,qBAAA,CAAA","file":"index.cjs","sourcesContent":["import type { OperationDescriptor } from './models.js';\n\nconst _registry: OperationDescriptor[] = [];\n\n/** Infer operation method from the last segment of the operation id.\n * e.g. \"tasks:list\" → \"read\", \"tasks:create\" → \"write\", \"tasks:delete\" → \"delete\"\n */\nexport function detectMethod(id: string): string {\n const last = (id.split(':').pop() ?? '').toLowerCase();\n if (/^(get|list|fetch|read)/.test(last)) return 'read';\n if (/^(delete|remove|destroy)/.test(last)) return 'delete';\n if (/(stream|subscribe|watch|listen)/.test(last)) return 'stream';\n return 'write';\n}\n\n/** Register an operation in the global registry (used by startup to sync). */\nexport function registerOperation(op: OperationDescriptor): void {\n const exists = _registry.some(\n o => o.identifier === op.identifier && o.method === op.method,\n );\n if (!exists) _registry.push(op);\n}\n\nexport function getRegistry(): OperationDescriptor[] {\n return [..._registry];\n}\n\nexport function clearRegistry(): void {\n _registry.length = 0;\n}","import { createParamDecorator, ExecutionContext, SetMetadata } from '@nestjs/common';\nimport type { TokenClaims } from '../../models.js';\nimport { detectMethod, registerOperation } from '../../registry.js';\n\nexport const OPERATION_KEY = 'caronte:operation';\n\nexport interface OperationMeta {\n id: string;\n level: string;\n method: string;\n}\n\n/**\n * Marks a controller method as a caronte operation.\n *\n * @example\n * ```ts\n * @Get()\n * @Operation('tasks:list', 'private')\n * listTasks(@CaronteUser() user: TokenClaims) { ... }\n * ```\n */\nexport function Operation(id: string, level: string, method?: string): MethodDecorator {\n const resolvedMethod = method ?? detectMethod(id);\n registerOperation({ identifier: id, method: resolvedMethod, level });\n return SetMetadata<string, OperationMeta>(OPERATION_KEY, { id, level, method: resolvedMethod });\n}\n\n/** Injects the authenticated user's TokenClaims into a parameter. */\nexport const CaronteUser = createParamDecorator(\n (_data: unknown, ctx: ExecutionContext): TokenClaims | undefined =>\n ctx.switchToHttp().getRequest().caronteUser,\n);","export class CaronteError extends Error {\n constructor(message: string) {\n super(message);\n this.name = this.constructor.name;\n }\n}\n\nexport class CaronteAuthError extends CaronteError {}\nexport class CaronteTokenError extends CaronteError {}\nexport class CaronteForbiddenError extends CaronteError {}\nexport class CaronteSyncError extends CaronteError {}\nexport class CaronteConfigError extends CaronteError {}","import { NodeSDK } from '@opentelemetry/sdk-node';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';\nimport { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';\nimport { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';\nimport { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';\nimport { BatchLogRecordProcessor } from '@opentelemetry/sdk-logs';\nimport { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';\nimport { defaultResource, resourceFromAttributes } from '@opentelemetry/resources';\nimport { ATTR_SERVICE_NAME, ATTR_SERVICE_NAMESPACE } from '@opentelemetry/semantic-conventions';\nimport { logs, SeverityNumber } from '@opentelemetry/api-logs';\n\nexport interface OtelInitOptions {\n otelCollectorUrl: string;\n appId: string;\n realmId: string;\n}\n\nlet started = false;\n\n/**\n * Best-effort OTel bootstrap: a misconfigured or unreachable collector must\n * never break CaronteClient.startup() (auth/JWKS still have to work), so\n * every failure here is swallowed. Guarded by `started` because NodeSDK is a\n * process-wide singleton — re-running .start() would double-register the\n * auto-instrumentations' require hooks.\n */\nexport function initOtel(options: OtelInitOptions): void {\n if (started) return;\n try {\n const base = options.otelCollectorUrl.replace(/\\/$/, '');\n const resource = defaultResource().merge(resourceFromAttributes({\n [ATTR_SERVICE_NAME]: options.appId,\n [ATTR_SERVICE_NAMESPACE]: options.realmId,\n }));\n\n const sdk = new NodeSDK({\n resource,\n traceExporter: new OTLPTraceExporter({ url: `${base}/v1/traces` }),\n metricReaders: [\n new PeriodicExportingMetricReader({\n exporter: new OTLPMetricExporter({ url: `${base}/v1/metrics` }),\n }),\n ],\n logRecordProcessors: [\n new BatchLogRecordProcessor({ exporter: new OTLPLogExporter({ url: `${base}/v1/logs` }) }),\n ],\n instrumentations: [getNodeAutoInstrumentations()],\n });\n sdk.start();\n started = true;\n\n // Auto-instrumentation only yields traces (and, from real HTTP traffic,\n // metrics) — nothing captures console output as OTel logs. Emit one\n // bootstrap record so the log pipeline (client -> Collector -> Loki) is\n // provably wired even before the app receives its first request.\n logs.getLogger('caronte-sdk').emit({\n severityNumber: SeverityNumber.INFO,\n body: `caronte-sdk OTel initialized for app_id=${options.appId} realm_id=${options.realmId}`,\n });\n } catch (err) {\n console.warn(`[caronte-sdk] OTel initialization skipped: ${(err as Error).message}`);\n }\n}\n","import { createRemoteJWKSet, jwtVerify, type JWTPayload } from 'jose';\nimport {\n CaronteAuthError, CaronteConfigError, CaronteError,\n CaronteSyncError, CaronteTokenError,\n} from './exceptions.js';\nimport type { OperationDescriptor, OperationWithGroups, TokenClaims } from './models.js';\nimport { initOtel } from './otel.js';\nimport { getRegistry } from './registry.js';\n\nexport interface CaronteClientOptions {\n authorizerUrl: string;\n realmId: string;\n appId: string;\n secret: string;\n /** Opt-in: URL of an OTLP collector (e.g. http://localhost:4318). Omit to skip OTel entirely. */\n otelCollectorUrl?: string;\n}\n\nexport class CaronteClient {\n private readonly base: string;\n private readonly realmId: string;\n private readonly appId: string;\n private readonly secret: string;\n private readonly otelCollectorUrl?: string;\n\n private appToken: string | null = null;\n private jwks: ReturnType<typeof createRemoteJWKSet> | null = null;\n private operations: OperationWithGroups[] = [];\n private registeredOperations: OperationDescriptor[] = [];\n\n constructor(options: CaronteClientOptions) {\n this.base = options.authorizerUrl.replace(/\\/$/, '');\n this.realmId = options.realmId;\n this.appId = options.appId;\n this.secret = options.secret;\n this.otelCollectorUrl = options.otelCollectorUrl;\n }\n\n private get realmBase(): string {\n return `${this.base}/realms/${this.realmId}`;\n }\n\n registerOperation(op: OperationDescriptor): void {\n this.registeredOperations.push(op);\n }\n\n async startup(): Promise<void> {\n if (this.registeredOperations.length === 0) {\n throw new CaronteConfigError(\n 'No operations registered. Call registerOperation() before startup().',\n );\n }\n if (this.otelCollectorUrl) {\n initOtel({ otelCollectorUrl: this.otelCollectorUrl, appId: this.appId, realmId: this.realmId });\n }\n await this.authenticate();\n this.initJwks();\n await this.syncOperations();\n await this.fetchOperations();\n }\n\n async authenticate(): Promise<void> {\n const res = await fetch(`${this.realmBase}/apps/token`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ app_id: this.appId, secret: this.secret }),\n });\n if (!res.ok) {\n throw new CaronteAuthError(\n `Authentication failed: HTTP ${res.status} — ${await res.text()}`,\n );\n }\n const data = await res.json() as { access_token?: string };\n if (!data.access_token) throw new CaronteAuthError('Authorizer returned no access_token.');\n this.appToken = data.access_token;\n }\n\n private initJwks(): void {\n this.jwks = createRemoteJWKSet(\n new URL(`${this.realmBase}/protocol/openid-connect/certs`),\n );\n }\n\n async syncOperations(): Promise<void> {\n this.ensureAppToken();\n const res = await fetch(`${this.realmBase}/apps/operations/sync`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.appToken}`,\n },\n body: JSON.stringify({\n operations: this.registeredOperations.map(op => ({\n identifier: op.identifier,\n method: op.method,\n level: op.level,\n description: op.description ?? null,\n })),\n }),\n });\n if (!res.ok) {\n throw new CaronteSyncError(`Sync failed: HTTP ${res.status} — ${await res.text()}`);\n }\n }\n\n async fetchOperations(): Promise<void> {\n this.ensureAppToken();\n const res = await fetch(`${this.realmBase}/apps/operations`, {\n headers: { Authorization: `Bearer ${this.appToken}` },\n });\n if (!res.ok) {\n throw new CaronteError(\n `Fetch operations failed: HTTP ${res.status} — ${await res.text()}`,\n );\n }\n const data = await res.json() as { operations: any[] };\n this.operations = data.operations.map(op => ({\n operationId: op.operation_id,\n identifier: op.identifier,\n method: op.method,\n level: op.level,\n allowedGroups: op.allowed_groups ?? [],\n }));\n }\n\n async validateToken(token: string): Promise<TokenClaims> {\n if (!this.jwks) this.initJwks();\n try {\n const { payload } = await jwtVerify(token, this.jwks!);\n const p = payload as JWTPayload & Record<string, unknown>;\n const tokenRealm = (p['realm_id'] as string) ?? '';\n\n // As chaves JWKS já são buscadas em /realms/{this.realmId}/... (ver\n // realmBase), então um token de outro realm normalmente já falharia a\n // verificação de assinatura acima. Esta checagem é defesa em\n // profundidade explícita — não depende desse efeito colateral, e falha\n // com uma mensagem clara em vez de erro de assinatura.\n if (tokenRealm.toLowerCase() !== this.realmId.toLowerCase()) {\n throw new CaronteTokenError(\n `Token realm '${tokenRealm}' does not match this app's realm '${this.realmId}'.`,\n );\n }\n\n return {\n sub: payload.sub ?? '',\n realmId: tokenRealm,\n groups: (p['groups'] as string[]) ?? [],\n exp: payload.exp ?? 0,\n raw: p,\n };\n } catch (err) {\n if (err instanceof CaronteTokenError) throw err;\n throw new CaronteTokenError(`Invalid token: ${(err as Error).message}`);\n }\n }\n\n checkPermission(claims: TokenClaims, identifier: string, method: string): boolean {\n const op = this.operations.find(\n o => o.identifier === identifier && o.method === method,\n );\n if (!op) return false;\n if (op.level === 'public') return true;\n if (op.level === 'private') return claims.groups.length > 0;\n if (op.level === 'protected') return claims.groups.some(g => op.allowedGroups.includes(g));\n return false;\n }\n\n /** Load operations from the global registry and bootstrap the client. */\n static async create(options: CaronteClientOptions): Promise<CaronteClient> {\n const client = new CaronteClient(options);\n for (const op of getRegistry()) client.registerOperation(op);\n await client.startup();\n return client;\n }\n\n private ensureAppToken(): void {\n if (!this.appToken) {\n throw new CaronteAuthError('No app token. Call authenticate() or startup() first.');\n }\n }\n}","import {\n CanActivate, ExecutionContext, ForbiddenException,\n Inject, Injectable, UnauthorizedException,\n} from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport { trace } from '@opentelemetry/api';\nimport { CaronteClient } from '../../client.js';\nimport type { TokenClaims } from '../../models.js';\nimport { OPERATION_KEY, type OperationMeta } from './decorator.js';\n\n@Injectable()\nexport class CaronteGuard implements CanActivate {\n // @Inject() explícito -- não depender de emitDecoratorMetadata (design:paramtypes)\n // pra resolver os parâmetros: o build desta lib usa tsup/esbuild, que não emite\n // essa metadata (limitação conhecida do esbuild), então a injeção implícita por\n // tipo do Nest falha silenciosamente (reflector fica undefined em runtime,\n // TypeError em qualquer rota guardada por CaronteGuard).\n constructor(\n @Inject(CaronteClient) private readonly client: CaronteClient,\n @Inject(Reflector) private readonly reflector: Reflector,\n ) {}\n\n async canActivate(ctx: ExecutionContext): Promise<boolean> {\n const meta = this.reflector.get<OperationMeta | undefined>(\n OPERATION_KEY,\n ctx.getHandler(),\n );\n\n if (!meta) return true; // no @Operation — pass through\n\n const { id, level, method } = meta;\n const request = ctx.switchToHttp().getRequest<{ headers: Record<string, string>; caronteUser?: TokenClaims }>();\n const span = trace.getActiveSpan();\n span?.setAttribute('caronte.operation_id', id);\n\n if (level === 'public') {\n span?.setAttribute('caronte.permission_result', 'public');\n return true;\n }\n\n const auth = request.headers['authorization'] ?? '';\n const [scheme, token] = auth.split(' ');\n\n if (scheme?.toLowerCase() !== 'bearer' || !token) {\n span?.setAttribute('caronte.permission_result', 'unauthorized');\n throw new UnauthorizedException('Bearer token required.');\n }\n\n let claims: TokenClaims;\n try {\n claims = await this.client.validateToken(token);\n } catch {\n // Token malformado/expirado/inválido -- 401, igual aos adapters express/\n // fastify (que engolem o erro de validateToken pelo mesmo motivo), não\n // deixar virar 500 por exceção não tratada.\n span?.setAttribute('caronte.permission_result', 'unauthorized');\n throw new UnauthorizedException('Invalid token.');\n }\n request.caronteUser = claims;\n span?.setAttribute('caronte.user', claims.sub);\n\n const allowed = this.client.checkPermission(claims, id, method);\n span?.setAttribute('caronte.permission_result', allowed ? 'allowed' : 'forbidden');\n if (!allowed) throw new ForbiddenException('Insufficient permissions.');\n\n return true;\n }\n}","import { DynamicModule, Module } from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport { CaronteClient, type CaronteClientOptions } from '../../client.js';\nimport { getRegistry } from '../../registry.js';\nimport { CaronteGuard } from './guard.js';\n\nexport interface CaronteModuleOptions extends CaronteClientOptions {}\n\n@Module({})\nexport class CaronteModule {\n /**\n * Register the caronte module globally.\n *\n * Creates the `CaronteClient`, bootstraps it (authenticate + sync + fetch),\n * and provides `CaronteGuard` for injection.\n *\n * @example\n * ```ts\n * // app.module.ts\n * @Module({\n * imports: [\n * CaronteModule.forRootAsync({\n * authorizerUrl: process.env.AUTHORIZER_URL!,\n * realmId: process.env.REALM_ID!,\n * appId: process.env.APP_ID!,\n * secret: process.env.APP_SECRET!,\n * }),\n * ],\n * })\n * export class AppModule {}\n * ```\n *\n * @remarks\n * If `options.otelCollectorUrl` is set, this alone does **not** get you\n * request tracing: `NestFactory.create()` requires express/fastify\n * internally to build the HTTP adapter *before* this module's provider\n * factory (and its `startup()`/`initOtel()` call) ever runs, so\n * auto-instrumentation attaches too late. Call `initOtel()` from\n * `@caronte-sdk/node` yourself, before `import { NestFactory } from\n * '@nestjs/core'` in `main.ts` (CommonJS only — `require()` order is\n * preserved textually there). See the README's \"Running with\n * otelCollectorUrl on NestJS\" section for the full example. The call\n * this module makes is a harmless idempotent no-op either way.\n */\n static forRootAsync(options: CaronteModuleOptions): DynamicModule {\n return {\n module: CaronteModule,\n global: true,\n providers: [\n Reflector,\n {\n provide: CaronteClient,\n useFactory: async () => {\n const client = new CaronteClient(options);\n for (const op of getRegistry()) client.registerOperation(op);\n await client.startup();\n return client;\n },\n },\n CaronteGuard,\n ],\n exports: [CaronteClient, CaronteGuard],\n };\n }\n}"]}
@@ -1,6 +1,6 @@
1
1
  import { CanActivate, ExecutionContext, DynamicModule } from '@nestjs/common';
2
2
  import { Reflector } from '@nestjs/core';
3
- import { C as CaronteClient, a as CaronteClientOptions } from '../../client-TcOSwgrn.cjs';
3
+ import { C as CaronteClient, a as CaronteClientOptions } from '../../client-CSokWFYn.cjs';
4
4
 
5
5
  declare const OPERATION_KEY = "caronte:operation";
6
6
  interface OperationMeta {
@@ -53,6 +53,18 @@ declare class CaronteModule {
53
53
  * })
54
54
  * export class AppModule {}
55
55
  * ```
56
+ *
57
+ * @remarks
58
+ * If `options.otelCollectorUrl` is set, this alone does **not** get you
59
+ * request tracing: `NestFactory.create()` requires express/fastify
60
+ * internally to build the HTTP adapter *before* this module's provider
61
+ * factory (and its `startup()`/`initOtel()` call) ever runs, so
62
+ * auto-instrumentation attaches too late. Call `initOtel()` from
63
+ * `@caronte-sdk/node` yourself, before `import { NestFactory } from
64
+ * '@nestjs/core'` in `main.ts` (CommonJS only — `require()` order is
65
+ * preserved textually there). See the README's "Running with
66
+ * otelCollectorUrl on NestJS" section for the full example. The call
67
+ * this module makes is a harmless idempotent no-op either way.
56
68
  */
57
69
  static forRootAsync(options: CaronteModuleOptions): DynamicModule;
58
70
  }
@@ -1,6 +1,6 @@
1
1
  import { CanActivate, ExecutionContext, DynamicModule } from '@nestjs/common';
2
2
  import { Reflector } from '@nestjs/core';
3
- import { C as CaronteClient, a as CaronteClientOptions } from '../../client-TcOSwgrn.js';
3
+ import { C as CaronteClient, a as CaronteClientOptions } from '../../client-CSokWFYn.js';
4
4
 
5
5
  declare const OPERATION_KEY = "caronte:operation";
6
6
  interface OperationMeta {
@@ -53,6 +53,18 @@ declare class CaronteModule {
53
53
  * })
54
54
  * export class AppModule {}
55
55
  * ```
56
+ *
57
+ * @remarks
58
+ * If `options.otelCollectorUrl` is set, this alone does **not** get you
59
+ * request tracing: `NestFactory.create()` requires express/fastify
60
+ * internally to build the HTTP adapter *before* this module's provider
61
+ * factory (and its `startup()`/`initOtel()` call) ever runs, so
62
+ * auto-instrumentation attaches too late. Call `initOtel()` from
63
+ * `@caronte-sdk/node` yourself, before `import { NestFactory } from
64
+ * '@nestjs/core'` in `main.ts` (CommonJS only — `require()` order is
65
+ * preserved textually there). See the README's "Running with
66
+ * otelCollectorUrl on NestJS" section for the full example. The call
67
+ * this module makes is a harmless idempotent no-op either way.
56
68
  */
57
69
  static forRootAsync(options: CaronteModuleOptions): DynamicModule;
58
70
  }
@@ -1,8 +1,9 @@
1
- import { CaronteClient } from '../../chunk-XMA2NAR7.js';
1
+ import { CaronteClient } from '../../chunk-M46BRRWA.js';
2
2
  import '../../chunk-VEDVQOBD.js';
3
3
  import { __decorateClass, __decorateParam, getRegistry, detectMethod, registerOperation } from '../../chunk-HV6X5RJY.js';
4
4
  import { createParamDecorator, Injectable, Inject, Module, UnauthorizedException, ForbiddenException, SetMetadata } from '@nestjs/common';
5
5
  import { Reflector } from '@nestjs/core';
6
+ import { trace } from '@opentelemetry/api';
6
7
 
7
8
  var OPERATION_KEY = "caronte:operation";
8
9
  function Operation(id, level, method) {
@@ -33,20 +34,29 @@ var CaronteGuard = class {
33
34
  if (!meta) return true;
34
35
  const { id, level, method } = meta;
35
36
  const request = ctx.switchToHttp().getRequest();
36
- if (level === "public") return true;
37
+ const span = trace.getActiveSpan();
38
+ span?.setAttribute("caronte.operation_id", id);
39
+ if (level === "public") {
40
+ span?.setAttribute("caronte.permission_result", "public");
41
+ return true;
42
+ }
37
43
  const auth = request.headers["authorization"] ?? "";
38
44
  const [scheme, token] = auth.split(" ");
39
45
  if (scheme?.toLowerCase() !== "bearer" || !token) {
46
+ span?.setAttribute("caronte.permission_result", "unauthorized");
40
47
  throw new UnauthorizedException("Bearer token required.");
41
48
  }
42
49
  let claims;
43
50
  try {
44
51
  claims = await this.client.validateToken(token);
45
52
  } catch {
53
+ span?.setAttribute("caronte.permission_result", "unauthorized");
46
54
  throw new UnauthorizedException("Invalid token.");
47
55
  }
48
56
  request.caronteUser = claims;
57
+ span?.setAttribute("caronte.user", claims.sub);
49
58
  const allowed = this.client.checkPermission(claims, id, method);
59
+ span?.setAttribute("caronte.permission_result", allowed ? "allowed" : "forbidden");
50
60
  if (!allowed) throw new ForbiddenException("Insufficient permissions.");
51
61
  return true;
52
62
  }
@@ -78,6 +88,18 @@ var CaronteModule = class {
78
88
  * })
79
89
  * export class AppModule {}
80
90
  * ```
91
+ *
92
+ * @remarks
93
+ * If `options.otelCollectorUrl` is set, this alone does **not** get you
94
+ * request tracing: `NestFactory.create()` requires express/fastify
95
+ * internally to build the HTTP adapter *before* this module's provider
96
+ * factory (and its `startup()`/`initOtel()` call) ever runs, so
97
+ * auto-instrumentation attaches too late. Call `initOtel()` from
98
+ * `@caronte-sdk/node` yourself, before `import { NestFactory } from
99
+ * '@nestjs/core'` in `main.ts` (CommonJS only — `require()` order is
100
+ * preserved textually there). See the README's "Running with
101
+ * otelCollectorUrl on NestJS" section for the full example. The call
102
+ * this module makes is a harmless idempotent no-op either way.
81
103
  */
82
104
  static forRootAsync(options) {
83
105
  return {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/adapters/nest/decorator.ts","../../../src/adapters/nest/guard.ts","../../../src/adapters/nest/module.ts"],"names":["Reflector"],"mappings":";;;;;;AAIO,IAAM,aAAA,GAAgB;AAkBtB,SAAS,SAAA,CAAU,EAAA,EAAY,KAAA,EAAe,MAAA,EAAkC;AACrF,EAAA,MAAM,cAAA,GAAiB,MAAA,IAAU,YAAA,CAAa,EAAE,CAAA;AAChD,EAAA,iBAAA,CAAkB,EAAE,UAAA,EAAY,EAAA,EAAI,MAAA,EAAQ,cAAA,EAAgB,OAAO,CAAA;AACnE,EAAA,OAAO,YAAmC,aAAA,EAAe,EAAE,IAAI,KAAA,EAAO,MAAA,EAAQ,gBAAgB,CAAA;AAChG;AAGO,IAAM,WAAA,GAAc,oBAAA;AAAA,EACzB,CAAC,KAAA,EAAgB,GAAA,KACf,IAAI,YAAA,EAAa,CAAE,YAAW,CAAE;AACpC;ACtBO,IAAM,eAAN,MAA0C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAM/C,WAAA,CAC0C,QACJ,SAAA,EACpC;AAFwC,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACJ,IAAA,IAAA,CAAA,SAAA,GAAA,SAAA;AAAA,EACnC;AAAA,EAFuC,MAAA;AAAA,EACJ,SAAA;AAAA,EAGtC,MAAM,YAAY,GAAA,EAAyC;AACzD,IAAA,MAAM,IAAA,GAAO,KAAK,SAAA,CAAU,GAAA;AAAA,MAC1B,aAAA;AAAA,MACA,IAAI,UAAA;AAAW,KACjB;AAEA,IAAA,IAAI,CAAC,MAAM,OAAO,IAAA;AAElB,IAAA,MAAM,EAAE,EAAA,EAAI,KAAA,EAAO,MAAA,EAAO,GAAI,IAAA;AAC9B,IAAA,MAAM,OAAA,GAAU,GAAA,CAAI,YAAA,EAAa,CAAE,UAAA,EAA2E;AAE9G,IAAA,IAAI,KAAA,KAAU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EAAW;AAAA,QACTA,SAAAA;AAAA,QACA;AAAA,UACE,OAAA,EAAS,aAAA;AAAA,UACT,YAAY,YAAY;AACtB,YAAA,MAAM,MAAA,GAAS,IAAI,aAAA,CAAc,OAAO,CAAA;AACxC,YAAA,KAAA,MAAW,EAAA,IAAM,WAAA,EAAY,EAAG,MAAA,CAAO,kBAAkB,EAAE,CAAA;AAC3D,YAAA,MAAM,OAAO,OAAA,EAAQ;AACrB,YAAA,OAAO,MAAA;AAAA,UACT;AAAA,SACF;AAAA,QACA;AAAA,OACF;AAAA,MACA,OAAA,EAAS,CAAC,aAAA,EAAe,YAAY;AAAA,KACvC;AAAA,EACF;AACF;AA3Ca,aAAA,GAAN,eAAA,CAAA;AAAA,EADN,MAAA,CAAO,EAAE;AAAA,CAAA,EACG,aAAA,CAAA","file":"index.js","sourcesContent":["import { createParamDecorator, ExecutionContext, SetMetadata } from '@nestjs/common';\nimport type { TokenClaims } from '../../models.js';\nimport { detectMethod, registerOperation } from '../../registry.js';\n\nexport const OPERATION_KEY = 'caronte:operation';\n\nexport interface OperationMeta {\n id: string;\n level: string;\n method: string;\n}\n\n/**\n * Marks a controller method as a caronte operation.\n *\n * @example\n * ```ts\n * @Get()\n * @Operation('tasks:list', 'private')\n * listTasks(@CaronteUser() user: TokenClaims) { ... }\n * ```\n */\nexport function Operation(id: string, level: string, method?: string): MethodDecorator {\n const resolvedMethod = method ?? detectMethod(id);\n registerOperation({ identifier: id, method: resolvedMethod, level });\n return SetMetadata<string, OperationMeta>(OPERATION_KEY, { id, level, method: resolvedMethod });\n}\n\n/** Injects the authenticated user's TokenClaims into a parameter. */\nexport const CaronteUser = createParamDecorator(\n (_data: unknown, ctx: ExecutionContext): TokenClaims | undefined =>\n ctx.switchToHttp().getRequest().caronteUser,\n);","import {\n CanActivate, ExecutionContext, ForbiddenException,\n Inject, Injectable, UnauthorizedException,\n} from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport { CaronteClient } from '../../client.js';\nimport type { TokenClaims } from '../../models.js';\nimport { OPERATION_KEY, type OperationMeta } from './decorator.js';\n\n@Injectable()\nexport class CaronteGuard implements CanActivate {\n // @Inject() explícito -- não depender de emitDecoratorMetadata (design:paramtypes)\n // pra resolver os parâmetros: o build desta lib usa tsup/esbuild, que não emite\n // essa metadata (limitação conhecida do esbuild), então a injeção implícita por\n // tipo do Nest falha silenciosamente (reflector fica undefined em runtime,\n // TypeError em qualquer rota guardada por CaronteGuard).\n constructor(\n @Inject(CaronteClient) private readonly client: CaronteClient,\n @Inject(Reflector) private readonly reflector: Reflector,\n ) {}\n\n async canActivate(ctx: ExecutionContext): Promise<boolean> {\n const meta = this.reflector.get<OperationMeta | undefined>(\n OPERATION_KEY,\n ctx.getHandler(),\n );\n\n if (!meta) return true; // no @Operation — pass through\n\n const { id, level, method } = meta;\n const request = ctx.switchToHttp().getRequest<{ headers: Record<string, string>; caronteUser?: TokenClaims }>();\n\n if (level === 'public') return true;\n\n const auth = request.headers['authorization'] ?? '';\n const [scheme, token] = auth.split(' ');\n\n if (scheme?.toLowerCase() !== 'bearer' || !token) {\n throw new UnauthorizedException('Bearer token required.');\n }\n\n let claims: TokenClaims;\n try {\n claims = await this.client.validateToken(token);\n } catch {\n // Token malformado/expirado/inválido -- 401, igual aos adapters express/\n // fastify (que engolem o erro de validateToken pelo mesmo motivo), não\n // deixar virar 500 por exceção não tratada.\n throw new UnauthorizedException('Invalid token.');\n }\n request.caronteUser = claims;\n\n const allowed = this.client.checkPermission(claims, id, method);\n if (!allowed) throw new ForbiddenException('Insufficient permissions.');\n\n return true;\n }\n}","import { DynamicModule, Module } from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport { CaronteClient, type CaronteClientOptions } from '../../client.js';\nimport { getRegistry } from '../../registry.js';\nimport { CaronteGuard } from './guard.js';\n\nexport interface CaronteModuleOptions extends CaronteClientOptions {}\n\n@Module({})\nexport class CaronteModule {\n /**\n * Register the caronte module globally.\n *\n * Creates the `CaronteClient`, bootstraps it (authenticate + sync + fetch),\n * and provides `CaronteGuard` for injection.\n *\n * @example\n * ```ts\n * // app.module.ts\n * @Module({\n * imports: [\n * CaronteModule.forRootAsync({\n * authorizerUrl: process.env.AUTHORIZER_URL!,\n * realmId: process.env.REALM_ID!,\n * appId: process.env.APP_ID!,\n * secret: process.env.APP_SECRET!,\n * }),\n * ],\n * })\n * export class AppModule {}\n * ```\n */\n static forRootAsync(options: CaronteModuleOptions): DynamicModule {\n return {\n module: CaronteModule,\n global: true,\n providers: [\n Reflector,\n {\n provide: CaronteClient,\n useFactory: async () => {\n const client = new CaronteClient(options);\n for (const op of getRegistry()) client.registerOperation(op);\n await client.startup();\n return client;\n },\n },\n CaronteGuard,\n ],\n exports: [CaronteClient, CaronteGuard],\n };\n }\n}"]}
1
+ {"version":3,"sources":["../../../src/adapters/nest/decorator.ts","../../../src/adapters/nest/guard.ts","../../../src/adapters/nest/module.ts"],"names":["Reflector"],"mappings":";;;;;;;AAIO,IAAM,aAAA,GAAgB;AAkBtB,SAAS,SAAA,CAAU,EAAA,EAAY,KAAA,EAAe,MAAA,EAAkC;AACrF,EAAA,MAAM,cAAA,GAAiB,MAAA,IAAU,YAAA,CAAa,EAAE,CAAA;AAChD,EAAA,iBAAA,CAAkB,EAAE,UAAA,EAAY,EAAA,EAAI,MAAA,EAAQ,cAAA,EAAgB,OAAO,CAAA;AACnE,EAAA,OAAO,YAAmC,aAAA,EAAe,EAAE,IAAI,KAAA,EAAO,MAAA,EAAQ,gBAAgB,CAAA;AAChG;AAGO,IAAM,WAAA,GAAc,oBAAA;AAAA,EACzB,CAAC,KAAA,EAAgB,GAAA,KACf,IAAI,YAAA,EAAa,CAAE,YAAW,CAAE;AACpC;ACrBO,IAAM,eAAN,MAA0C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAM/C,WAAA,CAC0C,QACJ,SAAA,EACpC;AAFwC,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACJ,IAAA,IAAA,CAAA,SAAA,GA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rB,YAAA,OAAO,MAAA;AAAA,UACT;AAAA,SACF;AAAA,QACA;AAAA,OACF;AAAA,MACA,OAAA,EAAS,CAAC,aAAA,EAAe,YAAY;AAAA,KACvC;AAAA,EACF;AACF;AAvDa,aAAA,GAAN,eAAA,CAAA;AAAA,EADN,MAAA,CAAO,EAAE;AAAA,CAAA,EACG,aAAA,CAAA","file":"index.js","sourcesContent":["import { createParamDecorator, ExecutionContext, SetMetadata } from '@nestjs/common';\nimport type { TokenClaims } from '../../models.js';\nimport { detectMethod, registerOperation } from '../../registry.js';\n\nexport const OPERATION_KEY = 'caronte:operation';\n\nexport interface OperationMeta {\n id: string;\n level: string;\n method: string;\n}\n\n/**\n * Marks a controller method as a caronte operation.\n *\n * @example\n * ```ts\n * @Get()\n * @Operation('tasks:list', 'private')\n * listTasks(@CaronteUser() user: TokenClaims) { ... }\n * ```\n */\nexport function Operation(id: string, level: string, method?: string): MethodDecorator {\n const resolvedMethod = method ?? detectMethod(id);\n registerOperation({ identifier: id, method: resolvedMethod, level });\n return SetMetadata<string, OperationMeta>(OPERATION_KEY, { id, level, method: resolvedMethod });\n}\n\n/** Injects the authenticated user's TokenClaims into a parameter. */\nexport const CaronteUser = createParamDecorator(\n (_data: unknown, ctx: ExecutionContext): TokenClaims | undefined =>\n ctx.switchToHttp().getRequest().caronteUser,\n);","import {\n CanActivate, ExecutionContext, ForbiddenException,\n Inject, Injectable, UnauthorizedException,\n} from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport { trace } from '@opentelemetry/api';\nimport { CaronteClient } from '../../client.js';\nimport type { TokenClaims } from '../../models.js';\nimport { OPERATION_KEY, type OperationMeta } from './decorator.js';\n\n@Injectable()\nexport class CaronteGuard implements CanActivate {\n // @Inject() explícito -- não depender de emitDecoratorMetadata (design:paramtypes)\n // pra resolver os parâmetros: o build desta lib usa tsup/esbuild, que não emite\n // essa metadata (limitação conhecida do esbuild), então a injeção implícita por\n // tipo do Nest falha silenciosamente (reflector fica undefined em runtime,\n // TypeError em qualquer rota guardada por CaronteGuard).\n constructor(\n @Inject(CaronteClient) private readonly client: CaronteClient,\n @Inject(Reflector) private readonly reflector: Reflector,\n ) {}\n\n async canActivate(ctx: ExecutionContext): Promise<boolean> {\n const meta = this.reflector.get<OperationMeta | undefined>(\n OPERATION_KEY,\n ctx.getHandler(),\n );\n\n if (!meta) return true; // no @Operation — pass through\n\n const { id, level, method } = meta;\n const request = ctx.switchToHttp().getRequest<{ headers: Record<string, string>; caronteUser?: TokenClaims }>();\n const span = trace.getActiveSpan();\n span?.setAttribute('caronte.operation_id', id);\n\n if (level === 'public') {\n span?.setAttribute('caronte.permission_result', 'public');\n return true;\n }\n\n const auth = request.headers['authorization'] ?? '';\n const [scheme, token] = auth.split(' ');\n\n if (scheme?.toLowerCase() !== 'bearer' || !token) {\n span?.setAttribute('caronte.permission_result', 'unauthorized');\n throw new UnauthorizedException('Bearer token required.');\n }\n\n let claims: TokenClaims;\n try {\n claims = await this.client.validateToken(token);\n } catch {\n // Token malformado/expirado/inválido -- 401, igual aos adapters express/\n // fastify (que engolem o erro de validateToken pelo mesmo motivo), não\n // deixar virar 500 por exceção não tratada.\n span?.setAttribute('caronte.permission_result', 'unauthorized');\n throw new UnauthorizedException('Invalid token.');\n }\n request.caronteUser = claims;\n span?.setAttribute('caronte.user', claims.sub);\n\n const allowed = this.client.checkPermission(claims, id, method);\n span?.setAttribute('caronte.permission_result', allowed ? 'allowed' : 'forbidden');\n if (!allowed) throw new ForbiddenException('Insufficient permissions.');\n\n return true;\n }\n}","import { DynamicModule, Module } from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport { CaronteClient, type CaronteClientOptions } from '../../client.js';\nimport { getRegistry } from '../../registry.js';\nimport { CaronteGuard } from './guard.js';\n\nexport interface CaronteModuleOptions extends CaronteClientOptions {}\n\n@Module({})\nexport class CaronteModule {\n /**\n * Register the caronte module globally.\n *\n * Creates the `CaronteClient`, bootstraps it (authenticate + sync + fetch),\n * and provides `CaronteGuard` for injection.\n *\n * @example\n * ```ts\n * // app.module.ts\n * @Module({\n * imports: [\n * CaronteModule.forRootAsync({\n * authorizerUrl: process.env.AUTHORIZER_URL!,\n * realmId: process.env.REALM_ID!,\n * appId: process.env.APP_ID!,\n * secret: process.env.APP_SECRET!,\n * }),\n * ],\n * })\n * export class AppModule {}\n * ```\n *\n * @remarks\n * If `options.otelCollectorUrl` is set, this alone does **not** get you\n * request tracing: `NestFactory.create()` requires express/fastify\n * internally to build the HTTP adapter *before* this module's provider\n * factory (and its `startup()`/`initOtel()` call) ever runs, so\n * auto-instrumentation attaches too late. Call `initOtel()` from\n * `@caronte-sdk/node` yourself, before `import { NestFactory } from\n * '@nestjs/core'` in `main.ts` (CommonJS only — `require()` order is\n * preserved textually there). See the README's \"Running with\n * otelCollectorUrl on NestJS\" section for the full example. The call\n * this module makes is a harmless idempotent no-op either way.\n */\n static forRootAsync(options: CaronteModuleOptions): DynamicModule {\n return {\n module: CaronteModule,\n global: true,\n providers: [\n Reflector,\n {\n provide: CaronteClient,\n useFactory: async () => {\n const client = new CaronteClient(options);\n for (const op of getRegistry()) client.registerOperation(op);\n await client.startup();\n return client;\n },\n },\n CaronteGuard,\n ],\n exports: [CaronteClient, CaronteGuard],\n };\n }\n}"]}
@@ -1,12 +1,55 @@
1
1
  import { CaronteConfigError, CaronteAuthError, CaronteSyncError, CaronteError, CaronteTokenError } from './chunk-VEDVQOBD.js';
2
2
  import { getRegistry } from './chunk-HV6X5RJY.js';
3
+ import { NodeSDK } from '@opentelemetry/sdk-node';
4
+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
5
+ import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
6
+ import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
7
+ import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
8
+ import { BatchLogRecordProcessor } from '@opentelemetry/sdk-logs';
9
+ import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
10
+ import { defaultResource, resourceFromAttributes } from '@opentelemetry/resources';
11
+ import { ATTR_SERVICE_NAMESPACE, ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
12
+ import { logs, SeverityNumber } from '@opentelemetry/api-logs';
3
13
  import { createRemoteJWKSet, jwtVerify } from 'jose';
4
14
 
15
+ var started = false;
16
+ function initOtel(options) {
17
+ if (started) return;
18
+ try {
19
+ const base = options.otelCollectorUrl.replace(/\/$/, "");
20
+ const resource = defaultResource().merge(resourceFromAttributes({
21
+ [ATTR_SERVICE_NAME]: options.appId,
22
+ [ATTR_SERVICE_NAMESPACE]: options.realmId
23
+ }));
24
+ const sdk = new NodeSDK({
25
+ resource,
26
+ traceExporter: new OTLPTraceExporter({ url: `${base}/v1/traces` }),
27
+ metricReaders: [
28
+ new PeriodicExportingMetricReader({
29
+ exporter: new OTLPMetricExporter({ url: `${base}/v1/metrics` })
30
+ })
31
+ ],
32
+ logRecordProcessors: [
33
+ new BatchLogRecordProcessor({ exporter: new OTLPLogExporter({ url: `${base}/v1/logs` }) })
34
+ ],
35
+ instrumentations: [getNodeAutoInstrumentations()]
36
+ });
37
+ sdk.start();
38
+ started = true;
39
+ logs.getLogger("caronte-sdk").emit({
40
+ severityNumber: SeverityNumber.INFO,
41
+ body: `caronte-sdk OTel initialized for app_id=${options.appId} realm_id=${options.realmId}`
42
+ });
43
+ } catch (err) {
44
+ console.warn(`[caronte-sdk] OTel initialization skipped: ${err.message}`);
45
+ }
46
+ }
5
47
  var CaronteClient = class _CaronteClient {
6
48
  base;
7
49
  realmId;
8
50
  appId;
9
51
  secret;
52
+ otelCollectorUrl;
10
53
  appToken = null;
11
54
  jwks = null;
12
55
  operations = [];
@@ -16,6 +59,7 @@ var CaronteClient = class _CaronteClient {
16
59
  this.realmId = options.realmId;
17
60
  this.appId = options.appId;
18
61
  this.secret = options.secret;
62
+ this.otelCollectorUrl = options.otelCollectorUrl;
19
63
  }
20
64
  get realmBase() {
21
65
  return `${this.base}/realms/${this.realmId}`;
@@ -29,6 +73,9 @@ var CaronteClient = class _CaronteClient {
29
73
  "No operations registered. Call registerOperation() before startup()."
30
74
  );
31
75
  }
76
+ if (this.otelCollectorUrl) {
77
+ initOtel({ otelCollectorUrl: this.otelCollectorUrl, appId: this.appId, realmId: this.realmId });
78
+ }
32
79
  await this.authenticate();
33
80
  this.initJwks();
34
81
  await this.syncOperations();
@@ -141,6 +188,6 @@ var CaronteClient = class _CaronteClient {
141
188
  }
142
189
  };
143
190
 
144
- export { CaronteClient };
145
- //# sourceMappingURL=chunk-XMA2NAR7.js.map
146
- //# sourceMappingURL=chunk-XMA2NAR7.js.map
191
+ export { CaronteClient, initOtel };
192
+ //# sourceMappingURL=chunk-M46BRRWA.js.map
193
+ //# sourceMappingURL=chunk-M46BRRWA.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/otel.ts","../src/client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAiBA,IAAI,OAAA,GAAU,KAAA;AASP,SAAS,SAAS,OAAA,EAAgC;AACvD,EAAA,IAAI,OAAA,EAAS;AACb,EAAA,IAAI;AACF,IAAA,MAAM,IAAA,GAAO,OAAA,CAAQ,gBAAA,CAAiB,OAAA,CAAQ,OAAO,EAAE,CAAA;AACvD,IAAA,MAAM,QAAA,GAAW,eAAA,EAAgB,CAAE,KAAA,CAAM,sBAAA,CAAuB;AAAA,MAC9D,CAAC,iBAAiB,GAAG,OAAA,CAAQ,KAAA;AAAA,MAC7B,CAAC,sBAAsB,GAAG,OAAA,CAAQ;AAAA,KACnC,CAAC,CAAA;AAEF,IAAA,MAAM,GAAA,GAAM,IAAI,OAAA,CAAQ;AAAA,MACtB,QAAA;AAAA,MACA,aAAA,EAAe,IAAI,iBAAA,CAAkB,EAAE,KAAK,CAAA,EAAG,IAAI,cAAc,CAAA;AAAA,MACjE,aAAA,EAAe;AAAA,QACb,IAAI,6BAAA,CAA8B;AAAA,UAChC,QAAA,EAAU,IAAI,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,aAAA,CAAc,QAAA,CAAS,CAAC,CAAC,CAAA;AACzF,IAAA,OAAO,KAAA;AAAA,EACT;AAAA;AAAA,EAGA,aAAa,OAAO,OAAA,EAAuD;AACzE,IAAA,MAAM,MAAA,GAAS,IAAI,cAAA,CAAc,OAAO,CAAA;AACxC,IAAA,KAAA,MAAW,EAAA,IAAM,WAAA,EAAY,EAAG,MAAA,CAAO,kBAAkB,EAAE,CAAA;AAC3D,IAAA,MAAM,OAAO,OAAA,EAAQ;AACrB,IAAA,OAAO,MAAA;AAAA,EACT;AAAA,EAEQ,cAAA,GAAuB;AAC7B,IAAA,IAAI,CAAC,KAAK,QAAA,EAAU;AAClB,MAAA,MAAM,IAAI,iBAAiB,uDAAuD,CAAA;AAAA,IACpF;AAAA,EACF;AACF","file":"chunk-M46BRRWA.js","sourcesContent":["import { NodeSDK } from '@opentelemetry/sdk-node';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';\nimport { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';\nimport { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';\nimport { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';\nimport { BatchLogRecordProcessor } from '@opentelemetry/sdk-logs';\nimport { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';\nimport { defaultResource, resourceFromAttributes } from '@opentelemetry/resources';\nimport { ATTR_SERVICE_NAME, ATTR_SERVICE_NAMESPACE } from '@opentelemetry/semantic-conventions';\nimport { logs, SeverityNumber } from '@opentelemetry/api-logs';\n\nexport interface OtelInitOptions {\n otelCollectorUrl: string;\n appId: string;\n realmId: string;\n}\n\nlet started = false;\n\n/**\n * Best-effort OTel bootstrap: a misconfigured or unreachable collector must\n * never break CaronteClient.startup() (auth/JWKS still have to work), so\n * every failure here is swallowed. Guarded by `started` because NodeSDK is a\n * process-wide singleton — re-running .start() would double-register the\n * auto-instrumentations' require hooks.\n */\nexport function initOtel(options: OtelInitOptions): void {\n if (started) return;\n try {\n const base = options.otelCollectorUrl.replace(/\\/$/, '');\n const resource = defaultResource().merge(resourceFromAttributes({\n [ATTR_SERVICE_NAME]: options.appId,\n [ATTR_SERVICE_NAMESPACE]: options.realmId,\n }));\n\n const sdk = new NodeSDK({\n resource,\n traceExporter: new OTLPTraceExporter({ url: `${base}/v1/traces` }),\n metricReaders: [\n new PeriodicExportingMetricReader({\n exporter: new OTLPMetricExporter({ url: `${base}/v1/metrics` }),\n }),\n ],\n logRecordProcessors: [\n new BatchLogRecordProcessor({ exporter: new OTLPLogExporter({ url: `${base}/v1/logs` }) }),\n ],\n instrumentations: [getNodeAutoInstrumentations()],\n });\n sdk.start();\n started = true;\n\n // Auto-instrumentation only yields traces (and, from real HTTP traffic,\n // metrics) — nothing captures console output as OTel logs. Emit one\n // bootstrap record so the log pipeline (client -> Collector -> Loki) is\n // provably wired even before the app receives its first request.\n logs.getLogger('caronte-sdk').emit({\n severityNumber: SeverityNumber.INFO,\n body: `caronte-sdk OTel initialized for app_id=${options.appId} realm_id=${options.realmId}`,\n });\n } catch (err) {\n console.warn(`[caronte-sdk] OTel initialization skipped: ${(err as Error).message}`);\n }\n}\n","import { createRemoteJWKSet, jwtVerify, type JWTPayload } from 'jose';\nimport {\n CaronteAuthError, CaronteConfigError, CaronteError,\n CaronteSyncError, CaronteTokenError,\n} from './exceptions.js';\nimport type { OperationDescriptor, OperationWithGroups, TokenClaims } from './models.js';\nimport { initOtel } from './otel.js';\nimport { getRegistry } from './registry.js';\n\nexport interface CaronteClientOptions {\n authorizerUrl: string;\n realmId: string;\n appId: string;\n secret: string;\n /** Opt-in: URL of an OTLP collector (e.g. http://localhost:4318). Omit to skip OTel entirely. */\n otelCollectorUrl?: string;\n}\n\nexport class CaronteClient {\n private readonly base: string;\n private readonly realmId: string;\n private readonly appId: string;\n private readonly secret: string;\n private readonly otelCollectorUrl?: string;\n\n private appToken: string | null = null;\n private jwks: ReturnType<typeof createRemoteJWKSet> | null = null;\n private operations: OperationWithGroups[] = [];\n private registeredOperations: OperationDescriptor[] = [];\n\n constructor(options: CaronteClientOptions) {\n this.base = options.authorizerUrl.replace(/\\/$/, '');\n this.realmId = options.realmId;\n this.appId = options.appId;\n this.secret = options.secret;\n this.otelCollectorUrl = options.otelCollectorUrl;\n }\n\n private get realmBase(): string {\n return `${this.base}/realms/${this.realmId}`;\n }\n\n registerOperation(op: OperationDescriptor): void {\n this.registeredOperations.push(op);\n }\n\n async startup(): Promise<void> {\n if (this.registeredOperations.length === 0) {\n throw new CaronteConfigError(\n 'No operations registered. Call registerOperation() before startup().',\n );\n }\n if (this.otelCollectorUrl) {\n initOtel({ otelCollectorUrl: this.otelCollectorUrl, appId: this.appId, realmId: this.realmId });\n }\n await this.authenticate();\n this.initJwks();\n await this.syncOperations();\n await this.fetchOperations();\n }\n\n async authenticate(): Promise<void> {\n const res = await fetch(`${this.realmBase}/apps/token`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ app_id: this.appId, secret: this.secret }),\n });\n if (!res.ok) {\n throw new CaronteAuthError(\n `Authentication failed: HTTP ${res.status} — ${await res.text()}`,\n );\n }\n const data = await res.json() as { access_token?: string };\n if (!data.access_token) throw new CaronteAuthError('Authorizer returned no access_token.');\n this.appToken = data.access_token;\n }\n\n private initJwks(): void {\n this.jwks = createRemoteJWKSet(\n new URL(`${this.realmBase}/protocol/openid-connect/certs`),\n );\n }\n\n async syncOperations(): Promise<void> {\n this.ensureAppToken();\n const res = await fetch(`${this.realmBase}/apps/operations/sync`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.appToken}`,\n },\n body: JSON.stringify({\n operations: this.registeredOperations.map(op => ({\n identifier: op.identifier,\n method: op.method,\n level: op.level,\n description: op.description ?? null,\n })),\n }),\n });\n if (!res.ok) {\n throw new CaronteSyncError(`Sync failed: HTTP ${res.status} — ${await res.text()}`);\n }\n }\n\n async fetchOperations(): Promise<void> {\n this.ensureAppToken();\n const res = await fetch(`${this.realmBase}/apps/operations`, {\n headers: { Authorization: `Bearer ${this.appToken}` },\n });\n if (!res.ok) {\n throw new CaronteError(\n `Fetch operations failed: HTTP ${res.status} — ${await res.text()}`,\n );\n }\n const data = await res.json() as { operations: any[] };\n this.operations = data.operations.map(op => ({\n operationId: op.operation_id,\n identifier: op.identifier,\n method: op.method,\n level: op.level,\n allowedGroups: op.allowed_groups ?? [],\n }));\n }\n\n async validateToken(token: string): Promise<TokenClaims> {\n if (!this.jwks) this.initJwks();\n try {\n const { payload } = await jwtVerify(token, this.jwks!);\n const p = payload as JWTPayload & Record<string, unknown>;\n const tokenRealm = (p['realm_id'] as string) ?? '';\n\n // As chaves JWKS já são buscadas em /realms/{this.realmId}/... (ver\n // realmBase), então um token de outro realm normalmente já falharia a\n // verificação de assinatura acima. Esta checagem é defesa em\n // profundidade explícita — não depende desse efeito colateral, e falha\n // com uma mensagem clara em vez de erro de assinatura.\n if (tokenRealm.toLowerCase() !== this.realmId.toLowerCase()) {\n throw new CaronteTokenError(\n `Token realm '${tokenRealm}' does not match this app's realm '${this.realmId}'.`,\n );\n }\n\n return {\n sub: payload.sub ?? '',\n realmId: tokenRealm,\n groups: (p['groups'] as string[]) ?? [],\n exp: payload.exp ?? 0,\n raw: p,\n };\n } catch (err) {\n if (err instanceof CaronteTokenError) throw err;\n throw new CaronteTokenError(`Invalid token: ${(err as Error).message}`);\n }\n }\n\n checkPermission(claims: TokenClaims, identifier: string, method: string): boolean {\n const op = this.operations.find(\n o => o.identifier === identifier && o.method === method,\n );\n if (!op) return false;\n if (op.level === 'public') return true;\n if (op.level === 'private') return claims.groups.length > 0;\n if (op.level === 'protected') return claims.groups.some(g => op.allowedGroups.includes(g));\n return false;\n }\n\n /** Load operations from the global registry and bootstrap the client. */\n static async create(options: CaronteClientOptions): Promise<CaronteClient> {\n const client = new CaronteClient(options);\n for (const op of getRegistry()) client.registerOperation(op);\n await client.startup();\n return client;\n }\n\n private ensureAppToken(): void {\n if (!this.appToken) {\n throw new CaronteAuthError('No app token. Call authenticate() or startup() first.');\n }\n }\n}"]}
@@ -21,12 +21,15 @@ interface CaronteClientOptions {
21
21
  realmId: string;
22
22
  appId: string;
23
23
  secret: string;
24
+ /** Opt-in: URL of an OTLP collector (e.g. http://localhost:4318). Omit to skip OTel entirely. */
25
+ otelCollectorUrl?: string;
24
26
  }
25
27
  declare class CaronteClient {
26
28
  private readonly base;
27
29
  private readonly realmId;
28
30
  private readonly appId;
29
31
  private readonly secret;
32
+ private readonly otelCollectorUrl?;
30
33
  private appToken;
31
34
  private jwks;
32
35
  private operations;
@@ -21,12 +21,15 @@ interface CaronteClientOptions {
21
21
  realmId: string;
22
22
  appId: string;
23
23
  secret: string;
24
+ /** Opt-in: URL of an OTLP collector (e.g. http://localhost:4318). Omit to skip OTel entirely. */
25
+ otelCollectorUrl?: string;
24
26
  }
25
27
  declare class CaronteClient {
26
28
  private readonly base;
27
29
  private readonly realmId;
28
30
  private readonly appId;
29
31
  private readonly secret;
32
+ private readonly otelCollectorUrl?;
30
33
  private appToken;
31
34
  private jwks;
32
35
  private operations;