{
  "files": {
    "main.ts": "import { run } from './core.ts';\nimport { POLICY } from './policy.ts';\n\nexport function evaluate(payload: any): any {\n  return run(payload, POLICY);\n}\n",
    "core.ts": "import { asciiLower, collectRoles, result } from './helpers.ts';\n\nexport function run(payload: any, policy: any): any {\n  const { subject, resource, now, issued_at, action, grants, role_parents } = payload;\n\n  if (subject.tenant !== resource.tenant) {\n    return result(false, 'tenant', [], policy);\n  }\n  if (now - issued_at > policy.token_ttl) {\n    return result(false, 'token', [], policy);\n  }\n  if (policy.suspended_denies && subject.suspended) {\n    return result(false, 'suspended', [], policy);\n  }\n  if (policy.owner_bypass && subject.owner) {\n    return result(true, 'owner', [], policy);\n  }\n\n  const roles = collectRoles(subject, role_parents, policy);\n  const requestedAction = policy.action_casefold ? asciiLower(action) : action;\n  const matched: number[] = [];\n  let hasAllow = false;\n  let hasDeny = false;\n\n  for (let index = 0; index < grants.length; index++) {\n    const grant = grants[index];\n    if (!roles.has(grant.role)) continue;\n\n    const grantAction = policy.action_casefold ? asciiLower(grant.action) : grant.action;\n    if (grantAction !== requestedAction && !(policy.wildcard_action && grant.action === '*')) {\n      continue;\n    }\n\n    const scopeMatches = policy.resource_scope === 'exact'\n      ? grant.scope === resource.id\n      : resource.id.startsWith(grant.scope);\n    if (!scopeMatches) continue;\n\n    if (grant.expires !== null) {\n      const unexpired = policy.expiry_inclusive\n        ? now <= grant.expires\n        : now < grant.expires;\n      if (!unexpired) continue;\n    }\n\n    matched.push(index);\n    if (grant.effect === 'allow') hasAllow = true;\n    if (grant.effect === 'deny') hasDeny = true;\n  }\n\n  if (matched.length === 0) {\n    return result(policy.default_allow, 'default', matched, policy);\n  }\n  const allowed = hasAllow && (!policy.deny_wins || !hasDeny);\n  return result(allowed, 'grants', matched, policy);\n}\n",
    "policy.ts": "export const POLICY: any = {\n  deny_wins: true,\n  expiry_inclusive: true,\n  inherit_roles: true,\n  owner_bypass: true,\n  action_casefold: true,\n  group_union: true,\n  resource_scope: 'exact',\n  suspended_denies: true,\n  token_ttl: 100,\n  wildcard_action: true,\n  audit_denials: true,\n  default_allow: true\n};\n",
    "helpers.ts": "export function asciiLower(value: string): string {\n  return value.replace(/[A-Z]/g, character => character.toLowerCase());\n}\n\nexport function collectRoles(subject: any, roleParents: any, policy: any): Set<string> {\n  const roles = new Set<string>(subject.roles);\n  if (policy.group_union) {\n    for (const group of subject.groups) roles.add(group);\n  }\n  if (policy.inherit_roles) {\n    const pending = Array.from(roles);\n    for (let index = 0; index < pending.length; index++) {\n      const role = pending[index];\n      const parents = roleParents && Object.prototype.hasOwnProperty.call(roleParents, role)\n        ? roleParents[role]\n        : [];\n      for (const parent of parents) {\n        if (!roles.has(parent)) {\n          roles.add(parent);\n          pending.push(parent);\n        }\n      }\n    }\n  }\n  return roles;\n}\n\nexport function result(allowed: boolean, reason: string, matched: number[], policy: any): any {\n  return {\n    allowed,\n    reason,\n    matched,\n    audit: !allowed && policy.audit_denials ? [reason] : []\n  };\n}\n"
  }
}