kavachos 0.3.0 → 0.4.0

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.
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import { eq, like, sql, and, lt, gte, lte, desc, asc, gt, inArray, or, notInArra
2
2
  export { and, eq, like } from 'drizzle-orm';
3
3
  import { sqliteTable, integer, text } from 'drizzle-orm/sqlite-core';
4
4
  import * as jose2 from 'jose';
5
- import { jwtVerify, generateKeyPair, exportJWK, importJWK, SignJWT, decodeJwt, createRemoteJWKSet, errors, compactVerify } from 'jose';
5
+ import { jwtVerify, generateKeyPair, exportJWK, importJWK, SignJWT, decodeJwt, createRemoteJWKSet, CompactSign, errors, compactVerify } from 'jose';
6
6
  import { z } from 'zod';
7
7
  import { deflateRaw } from 'zlib';
8
8
 
@@ -319,6 +319,8 @@ var permissions = sqliteTable("kavach_permissions", {
319
319
  actions: text("actions", { mode: "json" }).notNull().$type(),
320
320
  // ["read", "write", "execute"]
321
321
  constraints: text("constraints", { mode: "json" }).$type(),
322
+ // When set, the policy engine consults the ReBAC graph for this permission.
323
+ relation: text("relation"),
322
324
  createdAt: integer("created_at", { mode: "timestamp" }).notNull()
323
325
  });
324
326
  var delegationChains = sqliteTable("kavach_delegation_chains", {
@@ -348,6 +350,8 @@ var auditLogs = sqliteTable("kavach_audit_logs", {
348
350
  tokensCost: integer("tokens_cost"),
349
351
  ip: text("ip"),
350
352
  userAgent: text("user_agent"),
353
+ // True when this audit row corresponds to a policy-engine cache-hit evaluation.
354
+ cacheHit: integer("cache_hit", { mode: "boolean" }).notNull().default(false),
351
355
  timestamp: integer("timestamp", { mode: "timestamp" }).notNull()
352
356
  });
353
357
  var rateLimits = sqliteTable("kavach_rate_limits", {
@@ -4844,6 +4848,77 @@ var HibpApiError = class extends Error {
4844
4848
  this.name = "HibpApiError";
4845
4849
  }
4846
4850
  };
4851
+
4852
+ // src/standards/claims.ts
4853
+ var AGENTIC_JWT_CLAIMS = {
4854
+ /**
4855
+ * Stable identifier of the agent making the call.
4856
+ *
4857
+ * @see draft-goswami-agentic-jwt-00 §3.1
4858
+ */
4859
+ AGENT_ID: "agent_id",
4860
+ /**
4861
+ * Operational mode of the agent: `autonomous`, `delegated`, or `supervised`.
4862
+ *
4863
+ * @see draft-goswami-agentic-jwt-00 §3.2
4864
+ */
4865
+ AGENT_TYPE: "agent_type",
4866
+ /**
4867
+ * Subject principal the agent is acting on behalf of (human user or upstream agent).
4868
+ *
4869
+ * @see draft-goswami-agentic-jwt-00 §3.3
4870
+ */
4871
+ ON_BEHALF_OF: "on_behalf_of",
4872
+ /**
4873
+ * Actor claim per RFC 8693. Identifies the current actor in an
4874
+ * impersonation or delegation chain.
4875
+ *
4876
+ * @see draft-goswami-agentic-jwt-00 §3.4
4877
+ * @see RFC 8693
4878
+ */
4879
+ ACT: "act",
4880
+ /**
4881
+ * Authorized future actors for delegation chains (RFC 8693 `may_act`).
4882
+ *
4883
+ * @see draft-goswami-agentic-jwt-00 §3.5
4884
+ * @see RFC 8693
4885
+ */
4886
+ MAY_ACT: "may_act",
4887
+ /**
4888
+ * Trust score band at token issuance (e.g. `standard`, `elevated`).
4889
+ *
4890
+ * @see draft-goswami-agentic-jwt-00 §3.6
4891
+ */
4892
+ TRUST_TIER: "trust_tier",
4893
+ /**
4894
+ * Correlation id for tracing this token back to an entry in the audit log.
4895
+ *
4896
+ * @see draft-goswami-agentic-jwt-00 §3.7
4897
+ */
4898
+ AUDIT_REF: "audit_ref",
4899
+ /**
4900
+ * Per-tool budget or rate constraints encoded as a structured object.
4901
+ *
4902
+ * @see draft-goswami-agentic-jwt-00 §3.8
4903
+ */
4904
+ TOOL_CONSTRAINTS: "tool_constraints",
4905
+ /**
4906
+ * Workload Identity Token (WIT). Present only when three-layer cryptographic
4907
+ * binding is active (draft-liu §4.2). Absent in standard issuance paths.
4908
+ *
4909
+ * @see draft-liu-agent-operation-authorization-01 §4.2
4910
+ * TODO(v3): populate from WIT issuance when three-layer binding is implemented.
4911
+ */
4912
+ WORKLOAD_BINDING: "wit",
4913
+ /**
4914
+ * The scoped operation this token authorizes (e.g. `read:documents`).
4915
+ *
4916
+ * @see draft-liu-agent-operation-authorization-01 §4.3
4917
+ */
4918
+ OPERATION: "operation"
4919
+ };
4920
+
4921
+ // src/auth/jwt-session.ts
4847
4922
  var configSchema = z.object({
4848
4923
  secret: z.union([z.string().min(1), z.instanceof(Object)]),
4849
4924
  algorithm: z.string().optional(),
@@ -4924,6 +4999,18 @@ function createJwtSessionModule(config, db) {
4924
4999
  });
4925
5000
  Object.assign(claimsFromUser, extra);
4926
5001
  }
5002
+ if (config.emitAgenticJwtClaims === true && user.agenticContext !== void 0) {
5003
+ const ac = user.agenticContext;
5004
+ if (ac.agentId !== void 0) {
5005
+ claimsFromUser[AGENTIC_JWT_CLAIMS.AGENT_ID] = ac.agentId;
5006
+ }
5007
+ if (ac.agentType !== void 0) {
5008
+ claimsFromUser[AGENTIC_JWT_CLAIMS.AGENT_TYPE] = ac.agentType;
5009
+ }
5010
+ if (ac.trustTier !== void 0) {
5011
+ claimsFromUser[AGENTIC_JWT_CLAIMS.TRUST_TIER] = ac.trustTier;
5012
+ }
5013
+ }
4927
5014
  let builder = new SignJWT({
4928
5015
  sub: user.id,
4929
5016
  ...user.email !== void 0 ? { email: user.email } : {},
@@ -5954,7 +6041,8 @@ var AUTHORIZATION_URL2 = "https://discord.com/api/oauth2/authorize";
5954
6041
  var TOKEN_URL2 = "https://discord.com/api/oauth2/token";
5955
6042
  var USER_INFO_URL = "https://discord.com/api/users/@me";
5956
6043
  var CDN_BASE = "https://cdn.discordapp.com";
5957
- var DEFAULT_SCOPES2 = ["identify", "email"];
6044
+ var DEFAULT_DISCORD_SCOPES = ["identify", "email"];
6045
+ var DEFAULT_SCOPES2 = DEFAULT_DISCORD_SCOPES;
5958
6046
  function createDiscordProvider(config) {
5959
6047
  const scopes = mergeScopes2(DEFAULT_SCOPES2, config.scopes);
5960
6048
  async function getAuthorizationUrl(state, codeVerifier, redirectUri) {
@@ -6555,7 +6643,8 @@ function mergeScopes7(defaults, extras) {
6555
6643
  var AUTHORIZATION_URL8 = "https://slack.com/oauth/v2/authorize";
6556
6644
  var TOKEN_URL8 = "https://slack.com/api/oauth.v2.access";
6557
6645
  var USER_INFO_URL6 = "https://slack.com/api/openid.connect.userInfo";
6558
- var DEFAULT_SCOPES8 = ["openid", "profile", "email"];
6646
+ var DEFAULT_SLACK_SCOPES = ["openid", "profile", "email"];
6647
+ var DEFAULT_SCOPES8 = DEFAULT_SLACK_SCOPES;
6559
6648
  function createSlackProvider(config) {
6560
6649
  const scopes = mergeScopes8(DEFAULT_SCOPES8, config.scopes);
6561
6650
  async function getAuthorizationUrl(state, codeVerifier, redirectUri) {
@@ -15584,6 +15673,7 @@ function buildStatements(provider) {
15584
15673
  resource TEXT NOT NULL,
15585
15674
  actions ${json2} NOT NULL,
15586
15675
  constraints ${json2},
15676
+ relation TEXT,
15587
15677
  created_at ${ts} NOT NULL
15588
15678
  )`
15589
15679
  },
@@ -15622,6 +15712,7 @@ function buildStatements(provider) {
15622
15712
  tokens_cost INTEGER,
15623
15713
  ip TEXT,
15624
15714
  user_agent TEXT,
15715
+ cache_hit ${bool} NOT NULL DEFAULT ${isPostgres ? "FALSE" : "0"},
15625
15716
  timestamp ${ts} NOT NULL
15626
15717
  )`
15627
15718
  },
@@ -17480,6 +17571,9 @@ function validateArgPatterns(patterns, args) {
17480
17571
  return { valid: true };
17481
17572
  }
17482
17573
  async function checkRateLimit(db, agentId, resource, maxCallsPerHour) {
17574
+ if (!agentId) {
17575
+ return { allowed: true };
17576
+ }
17483
17577
  const oneHourAgo = new Date(Date.now() - 60 * 60 * 1e3);
17484
17578
  const rows = await db.select().from(rateLimits).where(
17485
17579
  and(
@@ -17510,66 +17604,20 @@ async function checkRateLimit(db, agentId, resource, maxCallsPerHour) {
17510
17604
  }
17511
17605
  return { allowed: true };
17512
17606
  }
17513
- function createPermissionEngine(config) {
17514
- const { db, auditAll } = config;
17515
- async function authorize(agent, request) {
17516
- const startTime = performance.now();
17517
- const auditId = generateId();
17518
- const matchingPermission = agent.permissions.find(
17519
- (p2) => matchResource(p2.resource, request.resource) && matchAction(p2.actions, request.action)
17520
- );
17521
- if (!matchingPermission) {
17522
- const result2 = {
17523
- allowed: false,
17524
- reason: `No permission grants agent "${agent.name}" access to "${request.action}" on "${request.resource}"`,
17525
- auditId
17526
- };
17527
- if (auditAll) {
17528
- await writeAuditLog(db, agent, request, result2, startTime, auditId);
17529
- }
17530
- return result2;
17531
- }
17532
- if (matchingPermission.constraints) {
17533
- const constraintResult = await evaluateConstraints(
17534
- db,
17535
- agent,
17536
- request,
17537
- matchingPermission.constraints
17538
- );
17539
- if (!constraintResult.allowed) {
17540
- const result2 = {
17541
- allowed: false,
17542
- reason: constraintResult.reason,
17543
- auditId
17544
- };
17545
- if (auditAll) {
17546
- await writeAuditLog(db, agent, request, result2, startTime, auditId);
17547
- }
17548
- return result2;
17549
- }
17550
- }
17551
- const result = { allowed: true, auditId };
17552
- if (auditAll) {
17553
- await writeAuditLog(db, agent, request, result, startTime, auditId);
17554
- }
17555
- return result;
17556
- }
17557
- return { authorize };
17558
- }
17559
- async function evaluateConstraints(db, agent, request, constraints) {
17607
+ async function evaluateConstraints(db, input, constraints) {
17560
17608
  if (constraints.maxCallsPerHour) {
17561
17609
  const rateResult = await checkRateLimit(
17562
17610
  db,
17563
- agent.id,
17564
- request.resource,
17611
+ input.subjectId,
17612
+ input.resource,
17565
17613
  constraints.maxCallsPerHour
17566
17614
  );
17567
17615
  if (!rateResult.allowed) {
17568
17616
  return rateResult;
17569
17617
  }
17570
17618
  }
17571
- if (constraints.allowedArgPatterns && request.arguments) {
17572
- const patternResult = validateArgPatterns(constraints.allowedArgPatterns, request.arguments);
17619
+ if (constraints.allowedArgPatterns && input.arguments) {
17620
+ const patternResult = validateArgPatterns(constraints.allowedArgPatterns, input.arguments);
17573
17621
  if (!patternResult.valid) {
17574
17622
  return { allowed: false, reason: patternResult.reason };
17575
17623
  }
@@ -17593,21 +17641,77 @@ async function evaluateConstraints(db, agent, request, constraints) {
17593
17641
  }
17594
17642
  }
17595
17643
  if (constraints.ipAllowlist && constraints.ipAllowlist.length > 0) {
17596
- if (!request.ip) {
17644
+ if (!input.ip) {
17597
17645
  return {
17598
17646
  allowed: false,
17599
17647
  reason: "IP_NOT_ALLOWED: No IP address provided; resource requires an IP allowlist match"
17600
17648
  };
17601
17649
  }
17602
- if (!isIPAllowed(constraints.ipAllowlist, request.ip)) {
17650
+ if (!isIPAllowed(constraints.ipAllowlist, input.ip)) {
17603
17651
  return {
17604
17652
  allowed: false,
17605
- reason: `IP_NOT_ALLOWED: IP "${request.ip}" is not in the allowlist for this resource`
17653
+ reason: `IP_NOT_ALLOWED: IP "${input.ip}" is not in the allowlist for this resource`
17606
17654
  };
17607
17655
  }
17608
17656
  }
17609
17657
  return { allowed: true };
17610
17658
  }
17659
+ function isCacheUnsafe(constraints) {
17660
+ if (!constraints) return false;
17661
+ return Boolean(constraints.maxCallsPerHour) || Boolean(constraints.timeWindow) || Array.isArray(constraints.allowedArgPatterns) && constraints.allowedArgPatterns.length > 0;
17662
+ }
17663
+
17664
+ // src/permission/engine.ts
17665
+ function createPermissionEngine(config) {
17666
+ const { db, auditAll } = config;
17667
+ async function authorize(agent, request) {
17668
+ const startTime = performance.now();
17669
+ const auditId = generateId();
17670
+ const matchingPermission = agent.permissions.find(
17671
+ (p2) => matchResource(p2.resource, request.resource) && matchAction(p2.actions, request.action)
17672
+ );
17673
+ if (!matchingPermission) {
17674
+ const result2 = {
17675
+ allowed: false,
17676
+ reason: `No permission grants agent "${agent.name}" access to "${request.action}" on "${request.resource}"`,
17677
+ auditId
17678
+ };
17679
+ if (auditAll) {
17680
+ await writeAuditLog(db, agent, request, result2, startTime, auditId);
17681
+ }
17682
+ return result2;
17683
+ }
17684
+ if (matchingPermission.constraints) {
17685
+ const constraintResult = await evaluateConstraints(
17686
+ db,
17687
+ {
17688
+ subjectId: agent.id,
17689
+ resource: request.resource,
17690
+ arguments: request.arguments,
17691
+ ip: request.ip
17692
+ },
17693
+ matchingPermission.constraints
17694
+ );
17695
+ if (!constraintResult.allowed) {
17696
+ const result2 = {
17697
+ allowed: false,
17698
+ reason: constraintResult.reason,
17699
+ auditId
17700
+ };
17701
+ if (auditAll) {
17702
+ await writeAuditLog(db, agent, request, result2, startTime, auditId);
17703
+ }
17704
+ return result2;
17705
+ }
17706
+ }
17707
+ const result = { allowed: true, auditId };
17708
+ if (auditAll) {
17709
+ await writeAuditLog(db, agent, request, result, startTime, auditId);
17710
+ }
17711
+ return result;
17712
+ }
17713
+ return { authorize };
17714
+ }
17611
17715
  async function writeAuditLog(db, agent, request, result, startTime, auditId) {
17612
17716
  const durationMs = Math.round(performance.now() - startTime);
17613
17717
  await db.insert(auditLogs).values({
@@ -17990,6 +18094,509 @@ function createPolicyModule(db) {
17990
18094
  return { create, get, list, update, remove, checkBudget, recordUsage, resetDaily, resetMonthly };
17991
18095
  }
17992
18096
 
18097
+ // src/policy/cache.ts
18098
+ function readEnvNumber(name) {
18099
+ if (typeof process === "undefined") return void 0;
18100
+ const raw = process.env[name];
18101
+ if (raw === void 0 || raw === "") return void 0;
18102
+ const n = Number(raw);
18103
+ return Number.isFinite(n) ? n : void 0;
18104
+ }
18105
+ function readEnvBool(name) {
18106
+ if (typeof process === "undefined") return void 0;
18107
+ const raw = process.env[name];
18108
+ if (raw === void 0 || raw === "") return void 0;
18109
+ return raw.toLowerCase() !== "false" && raw !== "0";
18110
+ }
18111
+ var DEFAULT_MAX_ENTRIES = 1e4;
18112
+ var DEFAULT_TTL_MS = 6e4;
18113
+ function createPolicyCache(config) {
18114
+ const rawMax = config.maxEntries ?? readEnvNumber("KAVACH_POLICY_CACHE_MAX") ?? DEFAULT_MAX_ENTRIES;
18115
+ const maxEntries = rawMax > 0 ? rawMax : DEFAULT_MAX_ENTRIES;
18116
+ const ttlMs = config.ttlMs ?? readEnvNumber("KAVACH_POLICY_CACHE_TTL_MS") ?? DEFAULT_TTL_MS;
18117
+ const enabled = config.enabled ?? readEnvBool("KAVACH_POLICY_CACHE") ?? true;
18118
+ const store = /* @__PURE__ */ new Map();
18119
+ let hits = 0;
18120
+ let misses = 0;
18121
+ let evictions = 0;
18122
+ function evictOldest() {
18123
+ const firstKey = store.keys().next().value;
18124
+ if (firstKey !== void 0) {
18125
+ store.delete(firstKey);
18126
+ evictions++;
18127
+ }
18128
+ }
18129
+ function isExpired(entry) {
18130
+ return Date.now() >= entry.expiresAt;
18131
+ }
18132
+ function get(key) {
18133
+ if (!enabled) {
18134
+ misses++;
18135
+ return void 0;
18136
+ }
18137
+ const entry = store.get(key);
18138
+ if (entry === void 0) {
18139
+ misses++;
18140
+ return void 0;
18141
+ }
18142
+ if (isExpired(entry)) {
18143
+ store.delete(key);
18144
+ evictions++;
18145
+ misses++;
18146
+ return void 0;
18147
+ }
18148
+ store.delete(key);
18149
+ store.set(key, entry);
18150
+ hits++;
18151
+ return entry.value;
18152
+ }
18153
+ function set(key, value, options) {
18154
+ if (!enabled || options?.skipCache === true) return;
18155
+ const existing = store.get(key);
18156
+ if (existing !== void 0) {
18157
+ store.delete(key);
18158
+ }
18159
+ while (store.size >= maxEntries) {
18160
+ evictOldest();
18161
+ }
18162
+ store.set(key, { value, expiresAt: Date.now() + ttlMs });
18163
+ }
18164
+ function del(key) {
18165
+ return store.delete(key);
18166
+ }
18167
+ function clear() {
18168
+ store.clear();
18169
+ }
18170
+ function invalidatePrefix(prefix) {
18171
+ let count = 0;
18172
+ for (const key of store.keys()) {
18173
+ if (key.startsWith(prefix)) {
18174
+ store.delete(key);
18175
+ count++;
18176
+ }
18177
+ }
18178
+ return count;
18179
+ }
18180
+ function stats() {
18181
+ return {
18182
+ hits,
18183
+ misses,
18184
+ size: store.size,
18185
+ evictions
18186
+ };
18187
+ }
18188
+ return { get, set, delete: del, clear, invalidatePrefix, stats };
18189
+ }
18190
+
18191
+ // src/policy/types.ts
18192
+ var POLICY_ERROR_CODES = {
18193
+ INVALID_INPUT: "POLICY_INVALID_INPUT",
18194
+ SUBJECT_NOT_FOUND: "POLICY_SUBJECT_NOT_FOUND",
18195
+ GRAPH_QUERY_FAILED: "POLICY_GRAPH_QUERY_FAILED",
18196
+ NO_MATCHING_PERMISSION: "POLICY_NO_MATCHING_PERMISSION",
18197
+ CONSTRAINT_FAILED: "POLICY_CONSTRAINT_FAILED"
18198
+ };
18199
+
18200
+ // src/policy/combiner.ts
18201
+ function combine(partials, strategy = "deny-overrides") {
18202
+ if (partials.length === 0) {
18203
+ return {
18204
+ allowed: false,
18205
+ effect: "indeterminate",
18206
+ reason: POLICY_ERROR_CODES.NO_MATCHING_PERMISSION
18207
+ };
18208
+ }
18209
+ if (strategy === "deny-overrides") {
18210
+ const firstDeny = partials.find((p2) => p2.effect === "deny");
18211
+ if (firstDeny !== void 0) {
18212
+ return {
18213
+ allowed: false,
18214
+ effect: "deny",
18215
+ reason: firstDeny.reason,
18216
+ matchedPermissionId: firstDeny.matchedPermissionId,
18217
+ matchedRelation: firstDeny.matchedRelation
18218
+ };
18219
+ }
18220
+ const firstPermit = partials.find((p2) => p2.effect === "permit");
18221
+ if (firstPermit !== void 0) {
18222
+ return {
18223
+ allowed: true,
18224
+ effect: "permit",
18225
+ reason: firstPermit.reason,
18226
+ matchedPermissionId: firstPermit.matchedPermissionId,
18227
+ matchedRelation: firstPermit.matchedRelation
18228
+ };
18229
+ }
18230
+ }
18231
+ if (strategy === "permit-overrides") {
18232
+ const firstPermit = partials.find((p2) => p2.effect === "permit");
18233
+ if (firstPermit !== void 0) {
18234
+ return {
18235
+ allowed: true,
18236
+ effect: "permit",
18237
+ reason: firstPermit.reason,
18238
+ matchedPermissionId: firstPermit.matchedPermissionId,
18239
+ matchedRelation: firstPermit.matchedRelation
18240
+ };
18241
+ }
18242
+ const firstDeny = partials.find((p2) => p2.effect === "deny");
18243
+ if (firstDeny !== void 0) {
18244
+ return {
18245
+ allowed: false,
18246
+ effect: "deny",
18247
+ reason: firstDeny.reason,
18248
+ matchedPermissionId: firstDeny.matchedPermissionId,
18249
+ matchedRelation: firstDeny.matchedRelation
18250
+ };
18251
+ }
18252
+ }
18253
+ return {
18254
+ allowed: false,
18255
+ effect: "indeterminate",
18256
+ reason: POLICY_ERROR_CODES.NO_MATCHING_PERMISSION
18257
+ };
18258
+ }
18259
+ function parsePermissionString(raw) {
18260
+ const idx = raw.lastIndexOf(":");
18261
+ if (idx === -1) return null;
18262
+ const resource = raw.slice(0, idx);
18263
+ const action = raw.slice(idx + 1);
18264
+ if (!resource || !action) return null;
18265
+ return { resource, action };
18266
+ }
18267
+ function toPermissions(rawStrings) {
18268
+ const map = /* @__PURE__ */ new Map();
18269
+ for (const raw of rawStrings) {
18270
+ const parsed = parsePermissionString(raw);
18271
+ if (!parsed) continue;
18272
+ const existing = map.get(parsed.resource);
18273
+ if (existing) {
18274
+ existing.add(parsed.action);
18275
+ } else {
18276
+ map.set(parsed.resource, /* @__PURE__ */ new Set([parsed.action]));
18277
+ }
18278
+ }
18279
+ return Array.from(map.entries()).map(([resource, actionsSet]) => ({
18280
+ resource,
18281
+ actions: Array.from(actionsSet).sort()
18282
+ }));
18283
+ }
18284
+ function deduplicate(permissions2) {
18285
+ const seen = /* @__PURE__ */ new Set();
18286
+ const result = [];
18287
+ for (const perm of permissions2) {
18288
+ const key = `${perm.resource}|${[...perm.actions].sort().join(",")}`;
18289
+ if (!seen.has(key)) {
18290
+ seen.add(key);
18291
+ result.push(perm);
18292
+ }
18293
+ }
18294
+ return result;
18295
+ }
18296
+ function createRbacResolver(deps) {
18297
+ const { db } = deps;
18298
+ async function resolveRolePermissions(input) {
18299
+ const { userId, orgId } = input;
18300
+ const baseWhere = orgId ? and(eq(orgMembers.userId, userId), eq(orgMembers.orgId, orgId)) : eq(orgMembers.userId, userId);
18301
+ const roleRows = await db.select({ permissions: orgRoles.permissions }).from(orgMembers).innerJoin(
18302
+ orgRoles,
18303
+ and(eq(orgRoles.orgId, orgMembers.orgId), eq(orgRoles.name, orgMembers.role))
18304
+ ).where(baseWhere);
18305
+ if (roleRows.length === 0) return [];
18306
+ const allRawPermissions = roleRows.flatMap((r) => r.permissions);
18307
+ if (allRawPermissions.length === 0) return [];
18308
+ return deduplicate(toPermissions(allRawPermissions));
18309
+ }
18310
+ return { resolveRolePermissions };
18311
+ }
18312
+
18313
+ // src/policy/rebac-bridge.ts
18314
+ var WILDCARD_REASON = "rebac:wildcard-resource-not-supported";
18315
+ var INVALID_SUBJECT_REASON = "rebac:invalid-subject";
18316
+ var GRAPH_FAILED_REASON = "rebac:graph-query-failed";
18317
+ var MATCHED_REASON = "rebac:matched";
18318
+ var NO_TUPLE_REASON = "rebac:no-tuple";
18319
+ function parseResource(resource) {
18320
+ if (resource.includes("*")) return null;
18321
+ const colonIdx = resource.indexOf(":");
18322
+ if (colonIdx === -1) return null;
18323
+ const objectType = resource.slice(0, colonIdx);
18324
+ const objectId = resource.slice(colonIdx + 1);
18325
+ if (!objectType || !objectId) return null;
18326
+ return { objectType, objectId };
18327
+ }
18328
+ function parseSubject(subject) {
18329
+ if (subject.agentId !== void 0 && subject.userId !== void 0) {
18330
+ return null;
18331
+ }
18332
+ if (subject.agentId !== void 0) {
18333
+ return { subjectType: "agent", subjectId: subject.agentId };
18334
+ }
18335
+ if (subject.userId !== void 0) {
18336
+ return { subjectType: "user", subjectId: subject.userId };
18337
+ }
18338
+ if (subject.orgId !== void 0) {
18339
+ return { subjectType: "org", subjectId: subject.orgId };
18340
+ }
18341
+ return null;
18342
+ }
18343
+ function createRebacBridge(deps) {
18344
+ const rebac = createReBACModule({}, deps.db);
18345
+ async function checkRelation(input) {
18346
+ const parsed = parseResource(input.resource);
18347
+ if (!parsed) {
18348
+ return { matched: false, reason: WILDCARD_REASON };
18349
+ }
18350
+ const subject = parseSubject(input.subject);
18351
+ if (!subject) {
18352
+ return { matched: false, reason: INVALID_SUBJECT_REASON };
18353
+ }
18354
+ const { objectType, objectId } = parsed;
18355
+ const { subjectType, subjectId } = subject;
18356
+ let result;
18357
+ try {
18358
+ result = await rebac.check({
18359
+ subjectType,
18360
+ subjectId,
18361
+ permission: input.relation,
18362
+ objectType,
18363
+ objectId
18364
+ });
18365
+ } catch {
18366
+ return { matched: false, reason: GRAPH_FAILED_REASON };
18367
+ }
18368
+ if (!result.success) {
18369
+ return { matched: false, reason: GRAPH_FAILED_REASON };
18370
+ }
18371
+ return result.data.allowed ? { matched: true, reason: MATCHED_REASON } : { matched: false, reason: NO_TUPLE_REASON };
18372
+ }
18373
+ return { checkRelation };
18374
+ }
18375
+
18376
+ // src/policy/engine.ts
18377
+ function createPolicyEngine(deps) {
18378
+ const { db, config = {} } = deps;
18379
+ const cache = createPolicyCache(config.cache ?? {});
18380
+ const rbac = createRbacResolver({ db });
18381
+ const rebac = createRebacBridge({ db });
18382
+ const strategy = config.combineStrategy ?? "deny-overrides";
18383
+ const auditEnabled = config.audit !== false;
18384
+ const sampleRate = clampSampleRate(config.auditSampleRate);
18385
+ async function evaluate(input) {
18386
+ const start = performance.now();
18387
+ const validation = validateInput(input);
18388
+ if (!validation.ok) {
18389
+ return finalize(
18390
+ {
18391
+ allowed: false,
18392
+ effect: "indeterminate",
18393
+ reason: validation.reason,
18394
+ cacheHit: false,
18395
+ durationMs: 0
18396
+ },
18397
+ start
18398
+ );
18399
+ }
18400
+ const cacheKey = buildCacheKey(input);
18401
+ const cached = cache.get(cacheKey);
18402
+ if (cached) {
18403
+ const auditIdForHit = willEmitAudit(input) ? generateId() : void 0;
18404
+ const decision2 = {
18405
+ ...cached,
18406
+ auditId: auditIdForHit,
18407
+ cacheHit: true,
18408
+ durationMs: Math.round(performance.now() - start)
18409
+ };
18410
+ emitAudit(decision2, input, auditIdForHit).catch(noop);
18411
+ return decision2;
18412
+ }
18413
+ let effective;
18414
+ try {
18415
+ effective = await resolveEffectivePermissions(db, rbac, input);
18416
+ } catch {
18417
+ return finalize(
18418
+ {
18419
+ allowed: false,
18420
+ effect: "indeterminate",
18421
+ reason: POLICY_ERROR_CODES.SUBJECT_NOT_FOUND,
18422
+ cacheHit: false,
18423
+ durationMs: 0
18424
+ },
18425
+ start
18426
+ );
18427
+ }
18428
+ const partials = [];
18429
+ let cacheUnsafeSeen = false;
18430
+ for (const perm of effective) {
18431
+ if (!matchResource(perm.resource, input.resource)) continue;
18432
+ if (!matchAction(perm.actions, input.action)) continue;
18433
+ if (perm.relation) {
18434
+ const result = await rebac.checkRelation({
18435
+ subject: input.subject,
18436
+ relation: perm.relation,
18437
+ resource: input.resource
18438
+ });
18439
+ if (result.reason === "rebac:graph-query-failed") {
18440
+ return finalize(
18441
+ {
18442
+ allowed: false,
18443
+ effect: "indeterminate",
18444
+ reason: POLICY_ERROR_CODES.GRAPH_QUERY_FAILED,
18445
+ cacheHit: false,
18446
+ durationMs: 0
18447
+ },
18448
+ start
18449
+ );
18450
+ }
18451
+ if (!result.matched) continue;
18452
+ }
18453
+ if (perm.constraints) {
18454
+ if (isCacheUnsafe(perm.constraints)) cacheUnsafeSeen = true;
18455
+ const constraintResult = await evaluateConstraints(
18456
+ db,
18457
+ {
18458
+ subjectId: input.subject.agentId ?? "",
18459
+ resource: input.resource,
18460
+ arguments: input.context?.arguments,
18461
+ ip: input.context?.ip
18462
+ },
18463
+ perm.constraints
18464
+ );
18465
+ if (!constraintResult.allowed) {
18466
+ partials.push({
18467
+ effect: "deny",
18468
+ reason: constraintResult.reason ?? POLICY_ERROR_CODES.CONSTRAINT_FAILED,
18469
+ matchedRelation: perm.relation
18470
+ });
18471
+ continue;
18472
+ }
18473
+ }
18474
+ partials.push({
18475
+ effect: "permit",
18476
+ reason: "matched",
18477
+ matchedRelation: perm.relation
18478
+ });
18479
+ }
18480
+ const combined = combine(partials, strategy);
18481
+ const auditIdForMiss = willEmitAudit(input) ? generateId() : void 0;
18482
+ const decision = {
18483
+ ...combined,
18484
+ auditId: auditIdForMiss,
18485
+ cacheHit: false,
18486
+ durationMs: Math.round(performance.now() - start)
18487
+ };
18488
+ if (!cacheUnsafeSeen) {
18489
+ cache.set(cacheKey, decision);
18490
+ }
18491
+ emitAudit(decision, input, auditIdForMiss).catch(noop);
18492
+ return decision;
18493
+ }
18494
+ function willEmitAudit(input) {
18495
+ if (!auditEnabled) return false;
18496
+ if (!input.subject.agentId) return false;
18497
+ return sampleRate >= 1 || Math.random() < sampleRate;
18498
+ }
18499
+ function invalidate(scope) {
18500
+ if (scope.agentId) cache.invalidatePrefix(`${scope.agentId}|`);
18501
+ if (scope.userId) cache.invalidatePrefix(`${scope.userId}|`);
18502
+ if (scope.resource) cache.clear();
18503
+ }
18504
+ function stats() {
18505
+ return cache.stats();
18506
+ }
18507
+ async function emitAudit(decision, input, auditId) {
18508
+ if (!auditId || !input.subject.agentId) return;
18509
+ let userId = input.subject.userId;
18510
+ if (!userId) {
18511
+ const ownerRows = await db.select({ ownerId: agents.ownerId }).from(agents).where(eq(agents.id, input.subject.agentId)).limit(1);
18512
+ userId = ownerRows[0]?.ownerId;
18513
+ if (!userId) return;
18514
+ }
18515
+ try {
18516
+ await db.insert(auditLogs).values({
18517
+ id: auditId,
18518
+ agentId: input.subject.agentId,
18519
+ userId,
18520
+ action: input.action,
18521
+ resource: input.resource,
18522
+ parameters: input.context?.arguments ?? {},
18523
+ result: decision.allowed ? "allowed" : "denied",
18524
+ reason: decision.reason ?? null,
18525
+ durationMs: decision.durationMs,
18526
+ timestamp: /* @__PURE__ */ new Date(),
18527
+ ip: input.context?.ip ?? null,
18528
+ userAgent: typeof input.context?.userAgent === "string" ? input.context.userAgent : null,
18529
+ cacheHit: decision.cacheHit
18530
+ });
18531
+ } catch {
18532
+ }
18533
+ }
18534
+ return { evaluate, invalidate, stats };
18535
+ }
18536
+ function buildCacheKey(input) {
18537
+ const subjectKey = input.subject.agentId ?? input.subject.userId ?? "";
18538
+ const orgKey = input.subject.orgId ?? "__noorg__";
18539
+ const ipKey = input.context?.ip ?? "";
18540
+ return `${subjectKey}|${orgKey}|${input.action}|${input.resource}|${ipKey}`;
18541
+ }
18542
+ function validateInput(input) {
18543
+ if (!input || !input.subject || !input.action || !input.resource) {
18544
+ return { ok: false, reason: POLICY_ERROR_CODES.INVALID_INPUT };
18545
+ }
18546
+ if (!input.subject.agentId && !input.subject.userId) {
18547
+ return { ok: false, reason: POLICY_ERROR_CODES.INVALID_INPUT };
18548
+ }
18549
+ return { ok: true };
18550
+ }
18551
+ function clampSampleRate(rate) {
18552
+ if (typeof rate !== "number" || Number.isNaN(rate)) return 1;
18553
+ if (rate < 0) return 0;
18554
+ if (rate > 1) return 1;
18555
+ return rate;
18556
+ }
18557
+ function finalize(decision, start) {
18558
+ return {
18559
+ ...decision,
18560
+ durationMs: Math.round(performance.now() - start)
18561
+ };
18562
+ }
18563
+ async function resolveEffectivePermissions(db, rbac, input) {
18564
+ const direct = input.subject.agentId ? await fetchDirectPermissions(db, input.subject.agentId) : [];
18565
+ const delegated = input.subject.agentId ? await fetchDelegatedPermissions(db, input.subject.agentId) : [];
18566
+ const roles = input.subject.userId ? await rbac.resolveRolePermissions({
18567
+ userId: input.subject.userId,
18568
+ orgId: input.subject.orgId
18569
+ }) : [];
18570
+ return [...direct, ...delegated, ...roles];
18571
+ }
18572
+ async function fetchDirectPermissions(db, agentId) {
18573
+ const rows = await db.select().from(permissions).where(eq(permissions.agentId, agentId));
18574
+ return rows.map((r) => ({
18575
+ resource: r.resource,
18576
+ actions: r.actions,
18577
+ constraints: r.constraints ?? void 0,
18578
+ relation: r.relation ?? void 0
18579
+ }));
18580
+ }
18581
+ async function fetchDelegatedPermissions(db, agentId) {
18582
+ const now = /* @__PURE__ */ new Date();
18583
+ const chains = await db.select().from(delegationChains).where(
18584
+ and(
18585
+ eq(delegationChains.toAgentId, agentId),
18586
+ eq(delegationChains.status, "active"),
18587
+ gt(delegationChains.expiresAt, now)
18588
+ )
18589
+ );
18590
+ return chains.flatMap(
18591
+ (chain) => chain.permissions.map((perm) => ({
18592
+ resource: perm.resource,
18593
+ actions: perm.actions
18594
+ }))
18595
+ );
18596
+ }
18597
+ function noop() {
18598
+ }
18599
+
17993
18600
  // src/redirect/chain.ts
17994
18601
  var DEFAULT_COOKIE_NAME = "kavach_redirect";
17995
18602
  var DEFAULT_MAX_AGE = 600;
@@ -18554,6 +19161,7 @@ async function createKavach(config) {
18554
19161
  if (!config.database.skipMigrations) {
18555
19162
  await createTables(db, config.database.provider, config);
18556
19163
  }
19164
+ const policyEngine = createPolicyEngine({ db, config: config.policy });
18557
19165
  const agentConfig = {
18558
19166
  db,
18559
19167
  maxPerUser: config.agents?.maxPerUser ?? 10,
@@ -19203,6 +19811,34 @@ async function createKavach(config) {
19203
19811
  * ```
19204
19812
  */
19205
19813
  redirects: redirectChain,
19814
+ /**
19815
+ * Unified policy engine.
19816
+ *
19817
+ * Single decision point that combines RBAC role expansion, ABAC constraint
19818
+ * evaluation, and ReBAC graph queries. Backed by a process-local LRU cache
19819
+ * with deterministic invalidation.
19820
+ *
19821
+ * @example
19822
+ * ```typescript
19823
+ * const decision = await kavach.policy.evaluate({
19824
+ * subject: { agentId: 'agent-abc' },
19825
+ * action: 'read',
19826
+ * resource: 'tool:github:list_issues',
19827
+ * });
19828
+ * if (!decision.allowed) throw new Error(decision.reason);
19829
+ *
19830
+ * // Flush cached decisions after a permission change
19831
+ * kavach.policy.invalidate({ agentId: 'agent-abc' });
19832
+ *
19833
+ * // Inspect cache health
19834
+ * const { hits, misses, size, evictions } = kavach.policy.stats();
19835
+ * ```
19836
+ */
19837
+ policy: {
19838
+ evaluate: policyEngine.evaluate,
19839
+ invalidate: policyEngine.invalidate,
19840
+ stats: policyEngine.stats
19841
+ },
19206
19842
  /**
19207
19843
  * Plugin system.
19208
19844
  *
@@ -20341,7 +20977,7 @@ var CredentialSubjectSchema = z.object({
20341
20977
  ).optional(),
20342
20978
  name: z.string().optional(),
20343
20979
  type: z.string().optional()
20344
- });
20980
+ }).passthrough();
20345
20981
  var VerifiableCredentialSchema = z.object({
20346
20982
  "@context": z.array(z.string()).min(1),
20347
20983
  id: z.string().optional(),
@@ -20362,8 +20998,156 @@ var VerifiablePresentationSchema = z.object({
20362
20998
  proof: ProofSchema.optional()
20363
20999
  });
20364
21000
 
20365
- // src/vc/issuer.ts
21001
+ // src/vc/audit-export.ts
21002
+ var KAVACHOS_AUDIT_CREDENTIAL = "KavachosAuditCredential";
21003
+ var KAVACHOS_AUDIT_CONTEXT = "https://kavachos.com/contexts/audit/v1.jsonld";
21004
+ var KAVACHOS_VERSION = "0.3.0";
20366
21005
  var DEFAULT_TTL_SECONDS2 = 86400;
21006
+ function toDecision(result) {
21007
+ if (result === "allowed") return "allow";
21008
+ return "deny";
21009
+ }
21010
+ function buildAuditCredential(record, issuerDid) {
21011
+ const subject = {
21012
+ id: record.id,
21013
+ agentId: record.agentId,
21014
+ ...record.userId ? { principalId: record.userId } : {},
21015
+ operation: record.action,
21016
+ target: record.resource,
21017
+ decision: toDecision(record.result),
21018
+ ...record.reason ? { policyName: record.reason } : {},
21019
+ timestamp: record.timestamp.toISOString(),
21020
+ kavachosVersion: KAVACHOS_VERSION
21021
+ };
21022
+ return {
21023
+ "@context": [VC_CONTEXT_V2, KAVACHOS_AUDIT_CONTEXT],
21024
+ id: `urn:uuid:${generateId()}`,
21025
+ type: [VC_TYPE_CREDENTIAL, KAVACHOS_AUDIT_CREDENTIAL],
21026
+ issuer: issuerDid,
21027
+ issuanceDate: (/* @__PURE__ */ new Date()).toISOString(),
21028
+ expirationDate: new Date(Date.now() + DEFAULT_TTL_SECONDS2 * 1e3).toISOString(),
21029
+ // Cast: AuditCredentialSubject is intentionally wider than CredentialSubject
21030
+ // because the VC schema uses an open-ended subject. The additional fields
21031
+ // (operation, target, decision, etc.) are preserved via spread at runtime.
21032
+ credentialSubject: subject
21033
+ };
21034
+ }
21035
+ async function signAsJsonLd(credential, config) {
21036
+ const { issuerDid, privateKeyJwk } = config;
21037
+ const kid = `${issuerDid}#${issuerDid.split(":").pop() ?? "key-1"}`;
21038
+ const key = await importJWK(privateKeyJwk, "EdDSA");
21039
+ const { proof: _proof, ...vcWithoutProof } = credential;
21040
+ const payload = new TextEncoder().encode(JSON.stringify(vcWithoutProof));
21041
+ const jws = await new CompactSign(payload).setProtectedHeader({ alg: "EdDSA", kid }).sign(key);
21042
+ const proof = {
21043
+ type: "JsonWebSignature2020",
21044
+ created: (/* @__PURE__ */ new Date()).toISOString(),
21045
+ verificationMethod: kid,
21046
+ proofPurpose: "assertionMethod",
21047
+ jws
21048
+ };
21049
+ return { ...credential, proof };
21050
+ }
21051
+ async function signAsJwt(credential, config) {
21052
+ const { issuerDid, privateKeyJwk } = config;
21053
+ const ttl = config.defaultTtl ?? DEFAULT_TTL_SECONDS2;
21054
+ const kid = `${issuerDid}#${issuerDid.split(":").pop() ?? "key-1"}`;
21055
+ const key = await importJWK(privateKeyJwk, "EdDSA");
21056
+ const { proof: _proof, ...vcWithoutProof } = credential;
21057
+ const builder = new SignJWT({ vc: vcWithoutProof }).setProtectedHeader({ alg: "EdDSA", kid, typ: "JWT" }).setIssuer(issuerDid).setIssuedAt().setExpirationTime(Math.floor(Date.now() / 1e3) + ttl);
21058
+ if (credential.id) builder.setJti(credential.id);
21059
+ if (credential.credentialSubject.id) builder.setSubject(credential.credentialSubject.id);
21060
+ const jwt = await builder.sign(key);
21061
+ return { credential, jwt };
21062
+ }
21063
+ async function signPresentationAsJsonLd(presentation, config) {
21064
+ const { issuerDid, privateKeyJwk } = config;
21065
+ const kid = `${issuerDid}#${issuerDid.split(":").pop() ?? "key-1"}`;
21066
+ const key = await importJWK(privateKeyJwk, "EdDSA");
21067
+ const { proof: _proof, ...vpWithoutProof } = presentation;
21068
+ const payload = new TextEncoder().encode(JSON.stringify(vpWithoutProof));
21069
+ const jws = await new CompactSign(payload).setProtectedHeader({ alg: "EdDSA", kid }).sign(key);
21070
+ const proof = {
21071
+ type: "JsonWebSignature2020",
21072
+ created: (/* @__PURE__ */ new Date()).toISOString(),
21073
+ verificationMethod: kid,
21074
+ proofPurpose: "assertionMethod",
21075
+ jws
21076
+ };
21077
+ return { ...presentation, proof };
21078
+ }
21079
+ async function exportAuditAsVC(options) {
21080
+ const {
21081
+ since,
21082
+ until,
21083
+ issuerDid,
21084
+ issuerConfig,
21085
+ format = "ldp_vc",
21086
+ output = "individual",
21087
+ filter,
21088
+ records
21089
+ } = options;
21090
+ const inRange = records.filter((r) => {
21091
+ const t = r.timestamp.getTime();
21092
+ return t >= since.getTime() && t <= until.getTime();
21093
+ });
21094
+ const filtered = filter ? inRange.filter(filter) : inRange;
21095
+ if (filtered.length === 0) {
21096
+ return {
21097
+ credentials: [],
21098
+ format,
21099
+ issuedAt: /* @__PURE__ */ new Date(),
21100
+ count: 0
21101
+ };
21102
+ }
21103
+ const credentials = [];
21104
+ const jwts = [];
21105
+ for (const record of filtered) {
21106
+ const base = buildAuditCredential(record, issuerDid);
21107
+ if (format === "jwt_vc") {
21108
+ const { credential, jwt } = await signAsJwt(base, issuerConfig);
21109
+ credentials.push(credential);
21110
+ jwts.push(jwt);
21111
+ } else {
21112
+ const signed = await signAsJsonLd(base, issuerConfig);
21113
+ credentials.push(signed);
21114
+ }
21115
+ }
21116
+ const issuedAt = /* @__PURE__ */ new Date();
21117
+ if (output === "individual") {
21118
+ return {
21119
+ credentials,
21120
+ ...format === "jwt_vc" ? { jwts } : {},
21121
+ format,
21122
+ issuedAt,
21123
+ count: credentials.length
21124
+ };
21125
+ }
21126
+ const basePresentation = {
21127
+ "@context": [VC_CONTEXT_V2, KAVACHOS_AUDIT_CONTEXT],
21128
+ id: `urn:uuid:${generateId()}`,
21129
+ type: [VC_TYPE_PRESENTATION],
21130
+ holder: issuerDid,
21131
+ verifiableCredential: credentials
21132
+ };
21133
+ const presentation = format === "jwt_vc" ? basePresentation : await signPresentationAsJsonLd(basePresentation, issuerConfig);
21134
+ return {
21135
+ credentials,
21136
+ ...format === "jwt_vc" ? { jwts } : {},
21137
+ presentation,
21138
+ format,
21139
+ issuedAt,
21140
+ count: credentials.length
21141
+ };
21142
+ }
21143
+ function listAuditRecords(records, since, until, filter) {
21144
+ const inRange = records.filter((r) => {
21145
+ const t = r.timestamp.getTime();
21146
+ return t >= since.getTime() && t <= until.getTime();
21147
+ });
21148
+ return filter ? inRange.filter(filter) : inRange;
21149
+ }
21150
+ var DEFAULT_TTL_SECONDS3 = 86400;
20367
21151
  function makeError8(code2, message, details) {
20368
21152
  return { code: code2, message, ...{} };
20369
21153
  }
@@ -20374,9 +21158,9 @@ function futureISO(seconds) {
20374
21158
  return new Date(Date.now() + seconds * 1e3).toISOString();
20375
21159
  }
20376
21160
  function createVCIssuer(config) {
20377
- const { issuerDid, privateKeyJwk, defaultTtl = DEFAULT_TTL_SECONDS2 } = config;
21161
+ const { issuerDid, privateKeyJwk, defaultTtl = DEFAULT_TTL_SECONDS3 } = config;
20378
21162
  const kid = `${issuerDid}#${issuerDid.split(":").pop() ?? "key-1"}`;
20379
- async function signAsJwt(credential, subject, ttl) {
21163
+ async function signAsJwt2(credential, subject, ttl) {
20380
21164
  try {
20381
21165
  const key = await importJWK(privateKeyJwk, "EdDSA");
20382
21166
  const { proof: _proof, ...vcWithoutProof } = credential;
@@ -20401,13 +21185,13 @@ function createVCIssuer(config) {
20401
21185
  };
20402
21186
  }
20403
21187
  }
20404
- async function signAsJsonLd(credential) {
21188
+ async function signAsJsonLd2(credential) {
20405
21189
  try {
20406
21190
  const key = await importJWK(privateKeyJwk, "EdDSA");
20407
21191
  const { proof: _proof, ...vcWithoutProof } = credential;
20408
21192
  const payload = new TextEncoder().encode(JSON.stringify(vcWithoutProof));
20409
- const { CompactSign } = await import('jose');
20410
- const jws = await new CompactSign(payload).setProtectedHeader({ alg: "EdDSA", kid }).sign(key);
21193
+ const { CompactSign: CompactSign2 } = await import('jose');
21194
+ const jws = await new CompactSign2(payload).setProtectedHeader({ alg: "EdDSA", kid }).sign(key);
20411
21195
  const proof = {
20412
21196
  type: "JsonWebSignature2020",
20413
21197
  created: nowISO(),
@@ -20443,9 +21227,9 @@ function createVCIssuer(config) {
20443
21227
  }
20444
21228
  async function signCredential(credential, subject, ttl, format) {
20445
21229
  if (format === "jwt") {
20446
- return signAsJwt(credential, subject, ttl);
21230
+ return signAsJwt2(credential, subject, ttl);
20447
21231
  }
20448
- return signAsJsonLd(credential);
21232
+ return signAsJsonLd2(credential);
20449
21233
  }
20450
21234
  async function issueAgentCredential(input) {
20451
21235
  const {
@@ -20970,6 +21754,6 @@ async function verifyWebhookSignature3(secret, rawBody, signature) {
20970
21754
  return diff === 0;
20971
21755
  }
20972
21756
 
20973
- export { CredentialStatusSchema, CredentialSubjectSchema, EVENT_TYPES, HibpApiError, HibpBreachedError, KAVACH_AGENT_CREDENTIAL, KAVACH_DELEGATION_CREDENTIAL, KAVACH_PERMISSION_CREDENTIAL, KVStore, MemoryStore, MultiSessionLimitError, OAuthProxyError, OneTapVerifyError, ProofSchema, RefreshTokenError, SSO_ERROR, SsoError, VC_CONTEXT_V1, VC_CONTEXT_V2, VC_TYPE_CREDENTIAL, VC_TYPE_PRESENTATION, VerifiableCredentialSchema, VerifiablePresentationSchema, additionalFields, admin, agentCards, agentDids, agents, anonymousAuth, apiKeys2 as apiKeys, apiKeys as apiKeysTable, approvalRequests, auditLogs, bearerAuth, budgetPolicies, buildDidDocument, buildSessionMetadata, classifyViolation, constantTimeEqual, createAdditionalFieldsModule, createAdminModule, createAgentModule, createAnonymousAuthModule, createApiKeyManagerModule, createAppleProvider, createApprovalModule, createAuditModule, createCaptchaModule, createCookieSessionManager, createCostAttributionModule, createCustomSessionModule, createDatabase, createDatabaseSync, createDelegationModule, createDeviceAuthModule, createDidModule, createDiscordProvider, createEmailOtpModule, createEmailTemplates, createEmailVerificationModule, createEphemeralSessionModule, createEventStreamModule, createFederationModule, createGdprModule, createGithubProvider, createGitlabProvider, createGoogleProvider, createHibpModule, createI18n, createJwtSessionModule, createKavach, createLastLoginModule, createLinkedInProvider, createMagicLinkModule, createMicrosoftProvider, createMultiSessionModule, createOAuthModule, createOAuthProxyModule, createOidcProviderModule, createOneTapModule, createOneTimeTokenModule, createOpenApiModule, createOrgModule, createPasskeyModule, createPasswordResetModule, createPermissionEngine, createPhoneAuthModule, createPluginRouter, createPolarModule, createPolicyModule, createPresentation, createPrivilegeAnalyzer, createRateLimiter, createReBACModule, createRedirectChain, createScimModule, createSessionFreshnessModule, createSessionManager, createSessionRefresher, createSiweModule, createSlackProvider, createSsoModule, createStripeModule, createTables, createTenantModule, createTokenFamilyStore, createTotpModule, createTrustModule, createTrustedDeviceModule, createTwitterProvider, createUsernameAuthModule, createVCIssuer, createVCVerifier, createWebhookModule2 as createWebhookModule, customAuth, customSession, de, delegationChains, deviceAuth, deviceLabelFromRequest, emailOtp, emailOtps, en, es, fr, fromBase64Url, fromHex, gdpr, generateCsrfToken, generateDidKey, generateDidWeb, generateId, generateOpenAPISpec, getCookie2 as getCookie, getDidWebUrl, getPermissionTemplate, headerAuth, hmacSha1Raw, hmacSha256, hmacSha256Raw, importHmacKey, initializePlugins, ja, kvStore, magicLink, magicLinks, mcpServers, oauth, oauthAccessTokens, oauthAuthorizationCodes, oauthClients, oauthProxy, oneTap, orgInvitations, orgMembers, orgRoles, organization, organizations, parseCookies2 as parseCookies, parseCookiesFromRequest, passkey, passkeyChallenges, passkeyCredentials, pbkdf2Hash, pbkdf2Verify, permissionTemplates, permissions, polar, randomBytes, randomBytesHex, rateLimit, rateLimits, resolveDidKey, resolveDidWeb, scim, serializeCookie, serializeCookieDeletion, sessions, sha1, sha256, sha256Raw, signPayload2 as signPayload, siwe, ssoConnections, stripe, tenants, toBase64Url, toHex, totpRecords, trustScores, twoFactor, users, validateCsrfToken, validateOrigin, verifyPayload, verifyPresentation, verifyWebhookSignature3 as verifyWebhookSignature, withRateLimit, zh };
21757
+ export { CredentialStatusSchema, CredentialSubjectSchema, EVENT_TYPES, HibpApiError, HibpBreachedError, KAVACHOS_AUDIT_CONTEXT, KAVACHOS_AUDIT_CREDENTIAL, KAVACH_AGENT_CREDENTIAL, KAVACH_DELEGATION_CREDENTIAL, KAVACH_PERMISSION_CREDENTIAL, KVStore, MemoryStore, MultiSessionLimitError, OAuthProxyError, OneTapVerifyError, ProofSchema, RefreshTokenError, SSO_ERROR, SsoError, VC_CONTEXT_V1, VC_CONTEXT_V2, VC_TYPE_CREDENTIAL, VC_TYPE_PRESENTATION, VerifiableCredentialSchema, VerifiablePresentationSchema, additionalFields, admin, agentCards, agentDids, agents, anonymousAuth, apiKeys2 as apiKeys, apiKeys as apiKeysTable, approvalRequests, auditLogs, bearerAuth, budgetPolicies, buildDidDocument, buildSessionMetadata, classifyViolation, constantTimeEqual, createAdditionalFieldsModule, createAdminModule, createAgentModule, createAnonymousAuthModule, createApiKeyManagerModule, createAppleProvider, createApprovalModule, createAuditModule, createCaptchaModule, createCookieSessionManager, createCostAttributionModule, createCustomSessionModule, createDatabase, createDatabaseSync, createDelegationModule, createDeviceAuthModule, createDidModule, createDiscordProvider, createEmailOtpModule, createEmailTemplates, createEmailVerificationModule, createEphemeralSessionModule, createEventStreamModule, createFederationModule, createGdprModule, createGithubProvider, createGitlabProvider, createGoogleProvider, createHibpModule, createI18n, createJwtSessionModule, createKavach, createLastLoginModule, createLinkedInProvider, createMagicLinkModule, createMicrosoftProvider, createMultiSessionModule, createOAuthModule, createOAuthProxyModule, createOidcProviderModule, createOneTapModule, createOneTimeTokenModule, createOpenApiModule, createOrgModule, createPasskeyModule, createPasswordResetModule, createPermissionEngine, createPhoneAuthModule, createPluginRouter, createPolarModule, createPolicyModule, createPresentation, createPrivilegeAnalyzer, createRateLimiter, createReBACModule, createRedirectChain, createScimModule, createSessionFreshnessModule, createSessionManager, createSessionRefresher, createSiweModule, createSlackProvider, createSsoModule, createStripeModule, createTables, createTenantModule, createTokenFamilyStore, createTotpModule, createTrustModule, createTrustedDeviceModule, createTwitterProvider, createUsernameAuthModule, createVCIssuer, createVCVerifier, createWebhookModule2 as createWebhookModule, customAuth, customSession, de, delegationChains, deviceAuth, deviceLabelFromRequest, emailOtp, emailOtps, en, es, exportAuditAsVC, fr, fromBase64Url, fromHex, gdpr, generateCsrfToken, generateDidKey, generateDidWeb, generateId, generateOpenAPISpec, getCookie2 as getCookie, getDidWebUrl, getPermissionTemplate, headerAuth, hmacSha1Raw, hmacSha256, hmacSha256Raw, importHmacKey, initializePlugins, ja, kvStore, listAuditRecords, magicLink, magicLinks, mcpServers, oauth, oauthAccessTokens, oauthAuthorizationCodes, oauthClients, oauthProxy, oneTap, orgInvitations, orgMembers, orgRoles, organization, organizations, parseCookies2 as parseCookies, parseCookiesFromRequest, passkey, passkeyChallenges, passkeyCredentials, pbkdf2Hash, pbkdf2Verify, permissionTemplates, permissions, polar, randomBytes, randomBytesHex, rateLimit, rateLimits, resolveDidKey, resolveDidWeb, scim, serializeCookie, serializeCookieDeletion, sessions, sha1, sha256, sha256Raw, signPayload2 as signPayload, siwe, ssoConnections, stripe, tenants, toBase64Url, toHex, totpRecords, trustScores, twoFactor, users, validateCsrfToken, validateOrigin, verifyPayload, verifyPresentation, verifyWebhookSignature3 as verifyWebhookSignature, withRateLimit, zh };
20974
21758
  //# sourceMappingURL=index.js.map
20975
21759
  //# sourceMappingURL=index.js.map