{"files": {"main.ts": "import {run} from './core.ts';\nimport {POLICY} from './policy.ts';\nexport function evaluate(payload: any): any { return run(payload, POLICY); }\n", "policy.ts": "export const POLICY: any = {\n  action_casefold: true,\n  audit_denials: true,\n  default_allow: true,\n  deny_wins: true,\n  expiry_inclusive: true,\n  group_union: true,\n  inherit_roles: true,\n  owner_bypass: true,\n  resource_scope: 'exact',\n  suspended_denies: true,\n  token_ttl: 100,\n  wildcard_action: true,\n};\n", "helpers.ts": "export function lower(s: string): string {\n  return s.replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + 32));\n}\n\nexport function expandRoles(initial: string[], parents: any, inherit: boolean): Set<string> {\n  const seen = new Set<string>(initial);\n  if (!inherit) return seen;\n  const stack = [...initial];\n  while (stack.length) {\n    const r = stack.pop() as string;\n    const ps = parents && Object.prototype.hasOwnProperty.call(parents, r) ? parents[r] : [];\n    for (const p of ps || []) {\n      if (!seen.has(p)) {\n        seen.add(p);\n        stack.push(p);\n      }\n    }\n  }\n  return seen;\n}\n", "core.ts": "import {lower, expandRoles} from './helpers.ts';\n\nexport function run(payload: any, policy: any): any {\n  const s = payload.subject;\n  const res = payload.resource;\n  const fin = (allowed: boolean, reason: string, matched: number[]) => ({\n    allowed,\n    reason,\n    matched,\n    audit: !allowed && policy.audit_denials ? [reason] : [],\n  });\n  if (s.tenant !== res.tenant) return fin(false, 'tenant', []);\n  if (payload.now - payload.issued_at > policy.token_ttl) return fin(false, 'token', []);\n  if (policy.suspended_denies && s.suspended) return fin(false, 'suspended', []);\n  if (policy.owner_bypass && s.owner) return fin(true, 'owner', []);\n  let initial: string[] = [...(s.roles || [])];\n  if (policy.group_union) initial = initial.concat(s.groups || []);\n  const roles = expandRoles(initial, payload.role_parents || {}, !!policy.inherit_roles);\n  const act: string = payload.action;\n  const matched: number[] = [];\n  let anyAllow = false;\n  let anyDeny = false;\n  const grants: any[] = payload.grants || [];\n  for (let i = 0; i < grants.length; i++) {\n    const g = grants[i];\n    if (!roles.has(g.role)) continue;\n    const same = policy.action_casefold ? lower(g.action) === lower(act) : g.action === act;\n    if (!(same || (policy.wildcard_action && g.action === '*'))) continue;\n    const scopeOk = policy.resource_scope === 'prefix' ? res.id.startsWith(g.scope) : g.scope === res.id;\n    if (!scopeOk) continue;\n    if (g.expires !== null && g.expires !== undefined) {\n      const ok = policy.expiry_inclusive ? payload.now <= g.expires : payload.now < g.expires;\n      if (!ok) continue;\n    }\n    matched.push(i);\n    if (g.allow) anyAllow = true;\n    else anyDeny = true;\n  }\n  if (matched.length === 0) return fin(!!policy.default_allow, 'default', []);\n  const allowed = policy.deny_wins ? anyAllow && !anyDeny : anyAllow;\n  return fin(allowed, 'grants', matched);\n}\n"}}