@smplcty/auth 9.0.1 → 9.0.2
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/README.md +2 -2
- package/dist/find-user-by-communication-method.d.ts +4 -0
- package/dist/find-user-by-communication-method.d.ts.map +1 -1
- package/dist/find-user-by-communication-method.js +5 -1
- package/dist/find-user-by-communication-method.js.map +1 -1
- package/package.json +6 -6
- package/schema/functions/auth_all_tenants.yaml +1 -1
- package/schema/functions/auth_can_admin_user.yaml +1 -1
- package/schema/functions/auth_create_user.yaml +1 -1
- package/schema/functions/auth_has_role.yaml +1 -1
- package/schema/functions/auth_in_tenant.yaml +1 -1
- package/schema/functions/auth_tenant_ids.yaml +1 -1
- package/schema/functions/resolve_session.yaml +1 -1
- package/schema/tables/user_communication_methods.yaml +4 -1
package/README.md
CHANGED
|
@@ -495,7 +495,7 @@ The library ships its schema as schema-flow YAML inside the package at `@smplcty
|
|
|
495
495
|
|
|
496
496
|
### Consuming with [`@smplcty/schema-flow`](https://www.npmjs.com/package/@smplcty/schema-flow)
|
|
497
497
|
|
|
498
|
-
Requires **`@smplcty/schema-flow >= 0.
|
|
498
|
+
Requires **`@smplcty/schema-flow >= 0.21.1`** — the shipped seeds assign no primary keys (the app-init user and the standard roles are matched by their natural keys, `name`/`kind`) and rely on insert-only seeding, where an existing row is never overwritten. `0.13.0` makes insert-only the default (the old per-table `seeds_on_conflict` knob is gone); on older versions these seeds would upsert and clobber any consumer edits to the standard roles on every migration. `0.14.0` is required so `resolve_session` converges: it canonicalizes function type aliases (`resolve_session` returns a `TABLE(… expires_at timestamptz …)`), so the function no longer reports phantom drift on every `plan`/`run`/`drift`. `0.18.2` is required for the `SECURITY DEFINER` search_path pins (see below): it emits the pin as a valid `SET search_path` clause and compares it against `pg_proc.proconfig` semantically (whitespace/case-normalized), so a pinned function converges to a clean no-op instead of reporting phantom drift. `0.21.1` is required to upgrade an existing database to the case-insensitive `user_communication_methods_channel_code_key`: it swaps a redefined index in without `--allow-destructive` and keeps the old one if the new one cannot be built. Older versions skip the change unless you pass the flag, and can leave the table without a working index if the build fails.
|
|
499
499
|
|
|
500
500
|
If you are upgrading an existing database, run schema-flow with `--allow-destructive` once: `0.14.0` recreates a function whose return type changed via `DROP … CASCADE` + a post-apply convergence pass that restores the dependent RLS policies, and (where a live DB carries a plain `UNIQUE` constraint sharing a name with one of auth's partial-unique indexes) drops the constraint and builds the partial index. Both paths are gated behind the flag; without it the change is reported as blocked rather than silently no-applied.
|
|
501
501
|
|
|
@@ -512,7 +512,7 @@ The `audit` mixin makes `created_by`/`updated_by` NOT NULL, stamped from `app.ac
|
|
|
512
512
|
|
|
513
513
|
### ⚠️ Known limitation — auth must deploy into the `public` schema
|
|
514
514
|
|
|
515
|
-
The library's seven `SECURITY DEFINER` functions (`auth_has_role`, `auth_can_admin_user`, `auth_in_tenant`, `auth_all_tenants`, `auth_tenant_ids`, `resolve_session`, `auth_create_user`) each ship with a **pinned `search_path` of `pg_catalog, public`**. This is a security hardening: a `SECURITY DEFINER` function otherwise inherits the _caller's_ `search_path`, so a caller who plants a shadow object (a fake `user_roles` table, a fake `current_user_id()`) in an earlier-resolving schema could make an unqualified name inside these functions resolve to _their_ object — and it would run with the definer's elevated rights, subverting an authorization check. Pinning to the trusted schema list closes that. (`current_user_id()` is `SECURITY INVOKER`, so it carries no escalation risk and is intentionally left unpinned.)
|
|
515
|
+
The library's seven `SECURITY DEFINER` functions (`auth_has_role`, `auth_can_admin_user`, `auth_in_tenant`, `auth_all_tenants`, `auth_tenant_ids`, `resolve_session`, `auth_create_user`) each ship with a **pinned `search_path` of `pg_catalog, public, pg_temp`**. This is a security hardening: a `SECURITY DEFINER` function otherwise inherits the _caller's_ `search_path`, so a caller who plants a shadow object (a fake `user_roles` table, a fake `current_user_id()`) in an earlier-resolving schema could make an unqualified name inside these functions resolve to _their_ object — and it would run with the definer's elevated rights, subverting an authorization check. Pinning to the trusted schema list closes that. `pg_temp` is listed **last** on purpose: when it is left out, Postgres searches the caller's temporary schema _first_ for table names, so any caller able to run SQL could shadow `user_roles` with a temp table of the same name. Listed last, it can never take precedence over `public`. (`current_user_id()` is `SECURITY INVOKER`, so it carries no escalation risk and is intentionally left unpinned.)
|
|
516
516
|
|
|
517
517
|
The consequence: **`public` is hardcoded into the shipped pin**, so auth must be deployed into the `public` schema. Every consumer today deploys there (schema-flow's default), so this is a no-op for you in practice — but if you deploy auth into a non-`public` schema (e.g. `auth`, `app`), the pinned path won't contain auth's tables and the functions will break. schema-flow does not yet interpolate the deploy `pgSchema` into `set:` values, which is the only reason the pin can't be schema-agnostic.
|
|
518
518
|
|
|
@@ -3,6 +3,10 @@ import type { FindUserQuery, Queryable, UserByCommunicationMethod } from './type
|
|
|
3
3
|
* Look up the user attached to a communication method (an email address,
|
|
4
4
|
* phone number, etc). Returns `null` if no matching record exists.
|
|
5
5
|
*
|
|
6
|
+
* `code` matches in any casing (`Bob@Acme.com` finds `bob@acme.com`); the
|
|
7
|
+
* stored code keeps the casing it was entered with. Normalize phone numbers
|
|
8
|
+
* before storing and before calling this.
|
|
9
|
+
*
|
|
6
10
|
* Returning `null` instead of throwing is deliberate: the caller (a
|
|
7
11
|
* sign-in endpoint, typically) decides whether to expose user-existence
|
|
8
12
|
* information to the client. To avoid the user-enumeration oracle, most
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-user-by-communication-method.d.ts","sourceRoot":"","sources":["../src/find-user-by-communication-method.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAkBtF
|
|
1
|
+
{"version":3,"file":"find-user-by-communication-method.d.ts","sourceRoot":"","sources":["../src/find-user-by-communication-method.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAkBtF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAsB,6BAA6B,CACjD,EAAE,EAAE,SAAS,EACb,KAAK,EAAE,aAAa,GACnB,OAAO,CAAC,yBAAyB,GAAG,IAAI,CAAC,CAgB3C"}
|
|
@@ -8,7 +8,7 @@ const SELECT_BY_COMMUNICATION_METHOD = `
|
|
|
8
8
|
ON cc.communication_channel_id = ucm.communication_channel_id
|
|
9
9
|
JOIN users u ON u.user_id = ucm.user_id
|
|
10
10
|
WHERE cc.name = $1
|
|
11
|
-
AND ucm.code = $2
|
|
11
|
+
AND lower(ucm.code) = lower($2)
|
|
12
12
|
AND ucm.deleted_at IS NULL
|
|
13
13
|
AND cc.deleted_at IS NULL
|
|
14
14
|
AND u.deleted_at IS NULL
|
|
@@ -18,6 +18,10 @@ const SELECT_BY_COMMUNICATION_METHOD = `
|
|
|
18
18
|
* Look up the user attached to a communication method (an email address,
|
|
19
19
|
* phone number, etc). Returns `null` if no matching record exists.
|
|
20
20
|
*
|
|
21
|
+
* `code` matches in any casing (`Bob@Acme.com` finds `bob@acme.com`); the
|
|
22
|
+
* stored code keeps the casing it was entered with. Normalize phone numbers
|
|
23
|
+
* before storing and before calling this.
|
|
24
|
+
*
|
|
21
25
|
* Returning `null` instead of throwing is deliberate: the caller (a
|
|
22
26
|
* sign-in endpoint, typically) decides whether to expose user-existence
|
|
23
27
|
* information to the client. To avoid the user-enumeration oracle, most
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-user-by-communication-method.js","sourceRoot":"","sources":["../src/find-user-by-communication-method.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,MAAM,8BAA8B,GAAG;;;;;;;;;;;;;;CActC,CAAC;AAEF
|
|
1
|
+
{"version":3,"file":"find-user-by-communication-method.js","sourceRoot":"","sources":["../src/find-user-by-communication-method.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,MAAM,8BAA8B,GAAG;;;;;;;;;;;;;;CActC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,CAAC,KAAK,UAAU,6BAA6B,CACjD,EAAa,EACb,KAAoB;IAEpB,IAAI,OAAO,KAAK,EAAE,OAAO,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACrE,MAAM,IAAI,iBAAiB,CAAC,0CAA0C,CAAC,CAAC;IAC1E,CAAC;IACD,IAAI,OAAO,KAAK,EAAE,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC/D,MAAM,IAAI,iBAAiB,CAAC,uCAAuC,CAAC,CAAC;IACvE,CAAC;IAED,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,EAAE,CAAC,KAAK,CAA4B,8BAA8B,EAAE;QACzF,KAAK,CAAC,OAAO;QACb,KAAK,CAAC,IAAI;KACX,CAAC,CAAC;IACH,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,IAAI,CAAC,GAAG;QAAE,OAAO,IAAI,CAAC;IACtB,kEAAkE;IAClE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,yBAAyB,EAAE,MAAM,CAAC,GAAG,CAAC,yBAAyB,CAAC,EAAE,CAAC;AAC1G,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smplcty/auth",
|
|
3
|
-
"version": "9.0.
|
|
3
|
+
"version": "9.0.2",
|
|
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",
|
|
@@ -105,7 +105,7 @@
|
|
|
105
105
|
},
|
|
106
106
|
"devDependencies": {
|
|
107
107
|
"@release-it/keep-a-changelog": "^8.0.1",
|
|
108
|
-
"@smplcty/schema-flow": "^0.21.
|
|
108
|
+
"@smplcty/schema-flow": "^0.21.1",
|
|
109
109
|
"@smplcty/twilio": "^0.1.1",
|
|
110
110
|
"@testcontainers/postgresql": "^12.2.0",
|
|
111
111
|
"@types/node": "^22.20.5",
|
|
@@ -116,11 +116,11 @@
|
|
|
116
116
|
"oauth4webapi": "^3.8.8",
|
|
117
117
|
"oxlint": "^1.87.0",
|
|
118
118
|
"pg": "^8.23.1",
|
|
119
|
-
"prettier": "^3.9.
|
|
120
|
-
"release-it": "^21.1.
|
|
119
|
+
"prettier": "^3.9.10",
|
|
120
|
+
"release-it": "^21.1.1",
|
|
121
121
|
"typescript": "^7.0.2",
|
|
122
|
-
"vite": "^8.3.
|
|
123
|
-
"vitest": "^5.0.
|
|
122
|
+
"vite": "^8.3.4",
|
|
123
|
+
"vitest": "^5.0.4"
|
|
124
124
|
},
|
|
125
125
|
"dependencies": {
|
|
126
126
|
"@smplcty/db": "^4.3.0",
|
|
@@ -4,7 +4,7 @@ volatility: stable
|
|
|
4
4
|
parallel: restricted
|
|
5
5
|
security: definer
|
|
6
6
|
set:
|
|
7
|
-
search_path: pg_catalog, public
|
|
7
|
+
search_path: pg_catalog, public, pg_temp
|
|
8
8
|
# True when the current actor holds any live wildcard role — a user_roles row
|
|
9
9
|
# with tenant_id IS NULL, which reaches every tenant (surfaced elsewhere as the
|
|
10
10
|
# app.all_tenants GUC). The all-access companion to auth_tenant_ids: a wildcard
|
|
@@ -4,7 +4,7 @@ volatility: stable
|
|
|
4
4
|
parallel: restricted
|
|
5
5
|
security: definer
|
|
6
6
|
set:
|
|
7
|
-
search_path: pg_catalog, public
|
|
7
|
+
search_path: pg_catalog, public, pg_temp
|
|
8
8
|
# True when the current actor is a security admin for at least one tenant the
|
|
9
9
|
# target user holds a role in — i.e. they share a tenant where the actor holds
|
|
10
10
|
# `security`. This is the "a security admin manages the users in their tenants"
|
|
@@ -2,7 +2,7 @@ name: auth_create_user
|
|
|
2
2
|
language: plpgsql
|
|
3
3
|
security: definer
|
|
4
4
|
set:
|
|
5
|
-
search_path: pg_catalog, public
|
|
5
|
+
search_path: pg_catalog, public, pg_temp
|
|
6
6
|
# The authority-checked, atomic way for an app_rls request (a security admin) to
|
|
7
7
|
# provision a human user. Raw INSERT on `users` is closed under RLS — a user
|
|
8
8
|
# must never exist without the roles and contact methods that make it usable,
|
|
@@ -4,7 +4,7 @@ volatility: stable
|
|
|
4
4
|
parallel: restricted
|
|
5
5
|
security: definer
|
|
6
6
|
set:
|
|
7
|
-
search_path: pg_catalog, public
|
|
7
|
+
search_path: pg_catalog, public, pg_temp
|
|
8
8
|
# True when the current request actor holds `role_name` with authority over
|
|
9
9
|
# `target_tenant_id`. A wildcard assignment (user_roles.tenant_id IS NULL)
|
|
10
10
|
# reaches every tenant, so it always qualifies. Passing a NULL target means
|
|
@@ -4,7 +4,7 @@ volatility: stable
|
|
|
4
4
|
parallel: restricted
|
|
5
5
|
security: definer
|
|
6
6
|
set:
|
|
7
|
-
search_path: pg_catalog, public
|
|
7
|
+
search_path: pg_catalog, public, pg_temp
|
|
8
8
|
# True when the current actor holds ANY role reaching `target_tenant_id`
|
|
9
9
|
# (directly or via a wildcard assignment). Used where visibility is granted to
|
|
10
10
|
# every member of a tenant regardless of which role they hold — e.g. a tenant
|
|
@@ -4,7 +4,7 @@ volatility: stable
|
|
|
4
4
|
parallel: restricted
|
|
5
5
|
security: definer
|
|
6
6
|
set:
|
|
7
|
-
search_path: pg_catalog, public
|
|
7
|
+
search_path: pg_catalog, public, pg_temp
|
|
8
8
|
# The set of tenant ids the current actor holds a live, tenant-scoped role in
|
|
9
9
|
# (wildcard grants — user_roles.tenant_id IS NULL — are NOT in this set; test
|
|
10
10
|
# them with auth_all_tenants()). Returns nothing when there is no actor.
|
|
@@ -3,7 +3,7 @@ language: plpgsql
|
|
|
3
3
|
volatility: stable
|
|
4
4
|
security: definer
|
|
5
5
|
set:
|
|
6
|
-
search_path: pg_catalog, public
|
|
6
|
+
search_path: pg_catalog, public, pg_temp
|
|
7
7
|
# A PURE resolver. Given a session token hash, it returns the session's
|
|
8
8
|
# user, its expiry, the user's selectable roles (split from privileges by
|
|
9
9
|
# roles.is_privilege), and the user's privileges. It takes NO role
|
|
@@ -32,7 +32,10 @@ columns:
|
|
|
32
32
|
nullable: true
|
|
33
33
|
indexes:
|
|
34
34
|
# Partial so a (channel, code) can be reused after the row is soft-deleted.
|
|
35
|
-
|
|
35
|
+
# Keyed on lower(code): codes are stored as entered but matched in any casing
|
|
36
|
+
# (findUserByCommunicationMethod), so two live codes differing only by case
|
|
37
|
+
# would make that lookup ambiguous. This index also backs the lookup.
|
|
38
|
+
- columns: [communication_channel_id, { expression: lower(code) }]
|
|
36
39
|
unique: true
|
|
37
40
|
where: 'deleted_at IS NULL'
|
|
38
41
|
name: user_communication_methods_channel_code_key
|