@smplcty/auth 8.0.0 → 8.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smplcty/auth",
3
- "version": "8.0.0",
3
+ "version": "8.1.0",
4
4
  "description": "Robust, type-safe session and role-based authentication primitives for PostgreSQL apps using Row-Level Security.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -0,0 +1,28 @@
1
+ name: auth_all_tenants
2
+ language: plpgsql
3
+ volatility: stable
4
+ security: definer
5
+ # True when the current actor holds any live wildcard role — a user_roles row
6
+ # with tenant_id IS NULL, which reaches every tenant (surfaced elsewhere as the
7
+ # app.all_tenants GUC). The all-access companion to auth_tenant_ids: a wildcard
8
+ # grant can't be enumerated into a finite id set, so scope tests OR this against
9
+ # `tenant_id IN (SELECT auth_tenant_ids())`. False when there is no actor.
10
+ #
11
+ # Wrap the call in a subquery — `(SELECT auth_all_tenants())` — in a policy so
12
+ # the planner evaluates it as an InitPlan ONCE per query instead of per row.
13
+ returns: boolean
14
+ args: []
15
+ body: |
16
+ BEGIN
17
+ RETURN EXISTS (
18
+ SELECT 1
19
+ FROM user_roles ur
20
+ WHERE ur.user_id = current_user_id()
21
+ AND ur.deleted_at IS NULL
22
+ AND ur.tenant_id IS NULL
23
+ );
24
+ END;
25
+ comment: >
26
+ True if the current actor holds any live wildcard (all-tenants) role. The
27
+ all-access companion to auth_tenant_ids. SECURITY DEFINER so RLS policies can
28
+ use it without recursing on user_roles.
@@ -0,0 +1,37 @@
1
+ name: auth_tenant_ids
2
+ language: plpgsql
3
+ volatility: stable
4
+ security: definer
5
+ # The set of tenant ids the current actor holds a live, tenant-scoped role in
6
+ # (wildcard grants — user_roles.tenant_id IS NULL — are NOT in this set; test
7
+ # them with auth_all_tenants()). Returns nothing when there is no actor.
8
+ #
9
+ # This is the set-based, planner-friendly counterpart to auth_in_tenant. Used in
10
+ # an *uncorrelated* subquery an RLS policy resolves ONCE per query rather than
11
+ # once per row:
12
+ #
13
+ # USING (
14
+ # (SELECT auth_all_tenants()) -- InitPlan, evaluated once
15
+ # OR tenant_id IN (SELECT auth_tenant_ids()) -- hashed SubPlan, materialized once
16
+ # )
17
+ #
18
+ # Because the membership set is independent of the scanned row, the planner
19
+ # folds it into a hashed SubPlan and probes it per row instead of re-running a
20
+ # SECURITY DEFINER function per row (auth_in_tenant's cost on large scans). Keep
21
+ # auth_in_tenant for scalar/one-off call sites; reach for this on hot scan paths.
22
+ returns: SETOF bigint
23
+ args: []
24
+ body: |
25
+ BEGIN
26
+ RETURN QUERY
27
+ SELECT ur.tenant_id
28
+ FROM user_roles ur
29
+ WHERE ur.user_id = current_user_id()
30
+ AND ur.deleted_at IS NULL
31
+ AND ur.tenant_id IS NOT NULL;
32
+ END;
33
+ comment: >
34
+ The set of tenant ids the current actor holds a live, tenant-scoped role in
35
+ (wildcard grants excluded — see auth_all_tenants). SECURITY DEFINER so RLS
36
+ policies can use it in an uncorrelated subquery — resolved once per query, not
37
+ per row — without recursing on user_roles.
@@ -54,7 +54,14 @@ policies:
54
54
  for: SELECT
55
55
  to: app_rls
56
56
  permissive: true
57
- using: auth_in_tenant(tenants.tenant_id)
57
+ # Decorrelated scope test: the actor's reachable tenants are resolved once
58
+ # per query (InitPlan + hashed SubPlan) rather than once per row, so this
59
+ # scales on large scans. Equivalent to auth_in_tenant(tenants.tenant_id) —
60
+ # which stays for scalar call sites — but planner-friendly. See
61
+ # auth_tenant_ids / auth_all_tenants.
62
+ using: |-
63
+ (SELECT auth_all_tenants())
64
+ OR tenants.tenant_id IN (SELECT auth_tenant_ids())
58
65
  - name: tenants_insert
59
66
  for: INSERT
60
67
  to: app_rls