@smplcty/auth 0.5.3 → 0.6.3
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-session.d.ts","sourceRoot":"","sources":["../src/with-session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAS3C,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,kBAAkB,EACnB,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"with-session.d.ts","sourceRoot":"","sources":["../src/with-session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAS3C,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,kBAAkB,EACnB,MAAM,YAAY,CAAC;AA6CpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,WAAW,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,EAAE,CAAC,GAAG,OAAO,EAC1E,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,WAAW,CAAC,KAAK,CAAC,EACxB,EAAE,EAAE,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,cAAc,CAAC,KAAK,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EAClE,OAAO,GAAE,kBAAuB,GAC/B,OAAO,CAAC,CAAC,CAAC,CA2DZ"}
|
package/dist/with-session.js
CHANGED
|
@@ -20,44 +20,20 @@ import { withTransaction } from './with-transaction.js';
|
|
|
20
20
|
* role the user has, not just the one being requested. The `_hasRole`
|
|
21
21
|
* column tells us whether the requested role specifically was found.
|
|
22
22
|
*/
|
|
23
|
+
/**
|
|
24
|
+
* Uses the resolve_session() SECURITY DEFINER function so the query
|
|
25
|
+
* bypasses RLS on auth tables (user_communication_methods, user_roles).
|
|
26
|
+
* This lets app_user resolve sessions without needing GUCs set first.
|
|
27
|
+
*/
|
|
23
28
|
const RESOLVE_SESSION = `
|
|
24
|
-
WITH found_session AS (
|
|
25
|
-
SELECT
|
|
26
|
-
s.session_id,
|
|
27
|
-
s.expires_at,
|
|
28
|
-
ucm.user_id
|
|
29
|
-
FROM sessions s
|
|
30
|
-
JOIN user_communication_methods ucm
|
|
31
|
-
ON ucm.user_communication_method_id = s.user_communication_method_id
|
|
32
|
-
WHERE s.session_id = $1
|
|
33
|
-
),
|
|
34
|
-
user_role_data AS (
|
|
35
|
-
SELECT
|
|
36
|
-
ur.user_id,
|
|
37
|
-
ur.tenant_id,
|
|
38
|
-
r.name AS role_name
|
|
39
|
-
FROM found_session fs
|
|
40
|
-
JOIN user_roles ur ON ur.user_id = fs.user_id
|
|
41
|
-
JOIN roles r ON r.role_id = ur.role_id
|
|
42
|
-
)
|
|
43
29
|
SELECT
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
COALESCE(bool_or(urd.tenant_id IS NULL), false) AS "allTenants",
|
|
52
|
-
COALESCE(
|
|
53
|
-
array_agg(DISTINCT urd.role_name)
|
|
54
|
-
FILTER (WHERE urd.role_name IS NOT NULL),
|
|
55
|
-
'{}'
|
|
56
|
-
) AS "roles",
|
|
57
|
-
bool_or(urd.role_name = $2) AS "hasRequestedRole"
|
|
58
|
-
FROM found_session fs
|
|
59
|
-
LEFT JOIN user_role_data urd ON urd.user_id = fs.user_id
|
|
60
|
-
GROUP BY fs.user_id, fs.expires_at
|
|
30
|
+
user_id AS "userId",
|
|
31
|
+
expires_at AS "expiresAt",
|
|
32
|
+
tenant_ids AS "tenantIds",
|
|
33
|
+
all_tenants AS "allTenants",
|
|
34
|
+
roles AS "roles",
|
|
35
|
+
has_requested_role AS "hasRequestedRole"
|
|
36
|
+
FROM resolve_session($1, $2)
|
|
61
37
|
`;
|
|
62
38
|
/**
|
|
63
39
|
* The high-level "just make it work" entry point. Checks out a
|
package/dist/with-session.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-session.js","sourceRoot":"","sources":["../src/with-session.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAMrD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,eAAe,GAAG
|
|
1
|
+
{"version":3,"file":"with-session.js","sourceRoot":"","sources":["../src/with-session.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAMrD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD;;;;;;;;;;;;;;;;GAgBG;AACH;;;;GAIG;AACH,MAAM,eAAe,GAAG;;;;;;;;;CASvB,CAAC;AAWF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,IAAU,EACV,IAAwB,EACxB,EAAkE,EAClE,UAA8B,EAAE;IAEhC,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,IAAI,UAAU,CAAC;IACzC,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAE3C,OAAO,eAAe,CAAC,IAAI,EAA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,EAAE,IAAI,CAAC,QAAQ;YACvB,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,UAAU,EAAE,GAAG,CAAC,UAAU;SAC3B,CAAC,CAAC;QAEH,GAAG,CAAC,KAAK,CACP;YACE,WAAW;YACX,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,WAAW,EAAE,GAAG,CAAC,SAAS,CAAC,MAAM;YACjC,UAAU,EAAE,GAAG,CAAC,UAAU;SAC3B,EACD,qBAAqB,CACtB,CAAC;QAEF,OAAO,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACzB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smplcty/auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.3",
|
|
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",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"typecheck": "tsc --noEmit",
|
|
53
53
|
"test": "vitest run",
|
|
54
54
|
"test:watch": "vitest",
|
|
55
|
-
"release": "
|
|
55
|
+
"release": "GITHUB_TOKEN=$(gh auth token) release-it --ci"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"pg": ">=8.0.0"
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
"pg": "^8.20.0",
|
|
67
67
|
"typescript": "^6.0.2",
|
|
68
68
|
"typescript-eslint": "^8.58.1",
|
|
69
|
+
"release-it": "^19.2.4",
|
|
69
70
|
"vitest": "^4.1.4"
|
|
70
71
|
},
|
|
71
72
|
"dependencies": {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
name: resolve_session
|
|
2
|
+
language: plpgsql
|
|
3
|
+
volatility: stable
|
|
4
|
+
security: definer
|
|
5
|
+
returns: TABLE(user_id integer, expires_at timestamptz, tenant_ids integer[], all_tenants boolean, roles varchar[], has_requested_role boolean)
|
|
6
|
+
args:
|
|
7
|
+
- name: p_session_id
|
|
8
|
+
type: text
|
|
9
|
+
- name: p_role_name
|
|
10
|
+
type: text
|
|
11
|
+
body: |
|
|
12
|
+
BEGIN
|
|
13
|
+
RETURN QUERY
|
|
14
|
+
WITH found_session AS (
|
|
15
|
+
SELECT s.session_id, s.expires_at, ucm.user_id
|
|
16
|
+
FROM sessions s
|
|
17
|
+
JOIN user_communication_methods ucm
|
|
18
|
+
ON ucm.user_communication_method_id = s.user_communication_method_id
|
|
19
|
+
WHERE s.session_id = p_session_id
|
|
20
|
+
),
|
|
21
|
+
user_role_data AS (
|
|
22
|
+
SELECT ur.user_id, ur.tenant_id, r.name AS role_name
|
|
23
|
+
FROM found_session fs
|
|
24
|
+
JOIN user_roles ur ON ur.user_id = fs.user_id
|
|
25
|
+
JOIN roles r ON r.role_id = ur.role_id
|
|
26
|
+
)
|
|
27
|
+
SELECT
|
|
28
|
+
fs.user_id,
|
|
29
|
+
fs.expires_at,
|
|
30
|
+
COALESCE(
|
|
31
|
+
array_agg(DISTINCT urd.tenant_id) FILTER (WHERE urd.tenant_id IS NOT NULL),
|
|
32
|
+
'{}'::integer[]
|
|
33
|
+
),
|
|
34
|
+
COALESCE(bool_or(urd.tenant_id IS NULL), false),
|
|
35
|
+
COALESCE(
|
|
36
|
+
array_agg(DISTINCT urd.role_name) FILTER (WHERE urd.role_name IS NOT NULL),
|
|
37
|
+
'{}'::text[]
|
|
38
|
+
),
|
|
39
|
+
bool_or(urd.role_name = p_role_name)
|
|
40
|
+
FROM found_session fs
|
|
41
|
+
LEFT JOIN user_role_data urd ON urd.user_id = fs.user_id
|
|
42
|
+
GROUP BY fs.user_id, fs.expires_at;
|
|
43
|
+
END;
|
|
44
|
+
comment: >
|
|
45
|
+
SECURITY DEFINER function for resolving sessions without requiring
|
|
46
|
+
RLS GUCs to be set first. Called by withSession so app_user can
|
|
47
|
+
look up session → user → roles through RLS-protected tables.
|
|
48
|
+
Returns aggregate role/tenant info only — does not expose raw PII.
|