{"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  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 lower(s: any): string {\n  return String(s).replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + 32));\n}\n\nexport function expandRoles(initial: string[], parents: any): Set<string> {\n  const seen = new Set<string>(initial);\n  const stack = initial.slice();\n  while (stack.length) {\n    const r = stack.pop() as string;\n    const ps = parents && Object.prototype.hasOwnProperty.call(parents, r) ? parents[r] : null;\n    if (Array.isArray(ps)) {\n      for (const p of ps) {\n        if (!seen.has(p)) {\n          seen.add(p);\n          stack.push(p);\n        }\n      }\n    }\n  }\n  return seen;\n}\n\nexport function isDeny(g: any): boolean {\n  const e = g.effect !== undefined ? g.effect : g.type !== undefined ? g.type : g.allow;\n  if (typeof e === 'boolean') return g.effect === undefined && g.type === undefined ? !e : false;\n  return lower(e === undefined ? '' : e) === 'deny';\n}\n\nexport function isAllow(g: any): boolean {\n  const e = g.effect !== undefined ? g.effect : g.type !== undefined ? g.type : g.allow;\n  if (typeof e === 'boolean') return e;\n  return lower(e === undefined ? '' : e) === 'allow';\n}\n","core.ts":"import {lower, expandRoles, isDeny, isAllow} from './helpers.ts';\n\nexport function run(payload: any, policy: any): any {\n  const subject = payload.subject || {};\n  const resource = 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 (subject.tenant !== resource.tenant) return fin(false, 'tenant', []);\n  if (payload.now - payload.issued_at > policy.token_ttl) return fin(false, 'token', []);\n  if (policy.suspended_denies && subject.suspended) return fin(false, 'suspended', []);\n  if (policy.owner_bypass && subject.owner) return fin(true, 'owner', []);\n\n  let initial: string[] = (subject.roles || []).slice();\n  if (policy.group_union) initial = initial.concat(subject.groups || []);\n  const roles = policy.inherit_roles\n    ? expandRoles(initial, payload.role_parents)\n    : new Set<string>(initial);\n\n  const act = policy.action_casefold ? lower(payload.action) : String(payload.action);\n  const matched: number[] = [];\n  const grants: any[] = payload.grants || [];\n  let anyAllow = false;\n  let anyDeny = false;\n  for (let i = 0; i < grants.length; i++) {\n    const g = grants[i];\n    if (!roles.has(g.role)) continue;\n    const ga = policy.action_casefold ? lower(g.action) : String(g.action);\n    const actionOk = ga === act || (policy.wildcard_action && g.action === '*');\n    if (!actionOk) continue;\n    const scope = g.scope === undefined || g.scope === null ? '' : String(g.scope);\n    const rid = String(resource.id);\n    const scopeOk = policy.resource_scope === 'prefix' ? rid.startsWith(scope) : scope === rid;\n    if (!scopeOk) continue;\n    const ex = g.expires;\n    if (ex !== null && ex !== undefined) {\n      const ok = policy.expiry_inclusive ? payload.now <= ex : payload.now < ex;\n      if (!ok) continue;\n    }\n    matched.push(i);\n    if (isDeny(g)) anyDeny = true;\n    else if (isAllow(g)) anyAllow = 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"}}