@smplcty/auth 0.6.3 → 1.0.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.
Files changed (93) hide show
  1. package/README.md +198 -223
  2. package/dist/create-session.d.ts +10 -9
  3. package/dist/create-session.d.ts.map +1 -1
  4. package/dist/create-session.js +18 -23
  5. package/dist/create-session.js.map +1 -1
  6. package/dist/dev-otp.d.ts.map +1 -1
  7. package/dist/dev-otp.js +4 -10
  8. package/dist/dev-otp.js.map +1 -1
  9. package/dist/errors.d.ts +19 -4
  10. package/dist/errors.d.ts.map +1 -1
  11. package/dist/errors.js +23 -5
  12. package/dist/errors.js.map +1 -1
  13. package/dist/find-user-by-communication-method.d.ts.map +1 -1
  14. package/dist/find-user-by-communication-method.js +8 -1
  15. package/dist/find-user-by-communication-method.js.map +1 -1
  16. package/dist/get-user-role-names.d.ts.map +1 -1
  17. package/dist/get-user-role-names.js +2 -0
  18. package/dist/get-user-role-names.js.map +1 -1
  19. package/dist/index.d.ts +10 -6
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +13 -7
  22. package/dist/index.js.map +1 -1
  23. package/dist/internal/hash-token.d.ts +16 -0
  24. package/dist/internal/hash-token.d.ts.map +1 -0
  25. package/dist/internal/hash-token.js +21 -0
  26. package/dist/internal/hash-token.js.map +1 -0
  27. package/dist/methods/errors.d.ts +37 -0
  28. package/dist/methods/errors.d.ts.map +1 -0
  29. package/dist/methods/errors.js +48 -0
  30. package/dist/methods/errors.js.map +1 -0
  31. package/dist/methods/oidc/index.d.ts +65 -0
  32. package/dist/methods/oidc/index.d.ts.map +1 -0
  33. package/dist/methods/oidc/index.js +76 -0
  34. package/dist/methods/oidc/index.js.map +1 -0
  35. package/dist/methods/router.d.ts +39 -0
  36. package/dist/methods/router.d.ts.map +1 -0
  37. package/dist/methods/router.js +153 -0
  38. package/dist/methods/router.js.map +1 -0
  39. package/dist/methods/twilio/index.d.ts +44 -0
  40. package/dist/methods/twilio/index.d.ts.map +1 -0
  41. package/dist/methods/twilio/index.js +77 -0
  42. package/dist/methods/twilio/index.js.map +1 -0
  43. package/dist/methods/types.d.ts +124 -0
  44. package/dist/methods/types.d.ts.map +1 -0
  45. package/dist/methods/types.js +2 -0
  46. package/dist/methods/types.js.map +1 -0
  47. package/dist/revoke-session.d.ts +20 -15
  48. package/dist/revoke-session.d.ts.map +1 -1
  49. package/dist/revoke-session.js +66 -20
  50. package/dist/revoke-session.js.map +1 -1
  51. package/dist/scope/flat-tenant.d.ts +36 -0
  52. package/dist/scope/flat-tenant.d.ts.map +1 -0
  53. package/dist/scope/flat-tenant.js +60 -0
  54. package/dist/scope/flat-tenant.js.map +1 -0
  55. package/dist/set-helpers.d.ts +44 -53
  56. package/dist/set-helpers.d.ts.map +1 -1
  57. package/dist/set-helpers.js +67 -74
  58. package/dist/set-helpers.js.map +1 -1
  59. package/dist/touch-session.d.ts +13 -0
  60. package/dist/touch-session.d.ts.map +1 -0
  61. package/dist/touch-session.js +28 -0
  62. package/dist/touch-session.js.map +1 -0
  63. package/dist/types.d.ts +59 -18
  64. package/dist/types.d.ts.map +1 -1
  65. package/dist/validate-session.d.ts +15 -14
  66. package/dist/validate-session.d.ts.map +1 -1
  67. package/dist/validate-session.js +28 -27
  68. package/dist/validate-session.js.map +1 -1
  69. package/dist/with-service-context.d.ts +15 -12
  70. package/dist/with-service-context.d.ts.map +1 -1
  71. package/dist/with-service-context.js +34 -39
  72. package/dist/with-service-context.js.map +1 -1
  73. package/dist/with-session.d.ts +22 -16
  74. package/dist/with-session.d.ts.map +1 -1
  75. package/dist/with-session.js +67 -66
  76. package/dist/with-session.js.map +1 -1
  77. package/package.json +53 -4
  78. package/schema/functions/current_user_id.yaml +18 -0
  79. package/schema/functions/resolve_session.yaml +42 -23
  80. package/schema/post/0001-backfill-audit-by.sql +15 -0
  81. package/schema/tables/auth_domains.yaml +43 -0
  82. package/schema/tables/communication_channels.yaml +5 -1
  83. package/schema/tables/dev_otp_enrollments.yaml +1 -0
  84. package/schema/tables/roles.yaml +43 -7
  85. package/schema/tables/sessions.yaml +10 -0
  86. package/schema/tables/tenants.yaml +24 -1
  87. package/schema/tables/user_communication_methods.yaml +5 -1
  88. package/schema/tables/user_roles.yaml +1 -0
  89. package/schema/tables/users.yaml +27 -0
  90. package/dist/with-transaction.d.ts +0 -31
  91. package/dist/with-transaction.d.ts.map +0 -1
  92. package/dist/with-transaction.js +0 -49
  93. package/dist/with-transaction.js.map +0 -1
@@ -1,7 +1,34 @@
1
1
  table: users
2
+ # Auditable. The `audit` mixin is consumed from @smplcty/schema-std and adds
3
+ # created_at / updated_at / created_by / updated_by (the _by columns FK back
4
+ # to users — a self-reference). The audit triggers stamp created_by/updated_by
5
+ # from app.actor_id, so seeding any user requires an actor: see the app-init
6
+ # seed below + the perTxSqlPath note in schema-flow.config.yaml.
7
+ mixins: [audit, soft_delete]
2
8
  columns:
3
9
  - name: user_id
4
10
  type: serial
5
11
  primary_key: true
6
12
  - name: name
7
13
  type: text
14
+ # 'human' → a person who signs in and holds a session.
15
+ # 'service' → a background principal (ingestion, transform-worker,
16
+ # app-init). Service rows are the actor for background writes so
17
+ # audit attribution (app.actor_id) is never NULL. They never own a
18
+ # session. See withServiceContext.
19
+ - name: kind
20
+ type: text
21
+ nullable: false
22
+ default: "'human'"
23
+ check: "kind IN ('human', 'service')"
24
+ # The bootstrap principal. user_id=1 is the app-init service user: the actor
25
+ # every migration-time seed (this row, the standard roles, any consumer seeds)
26
+ # is attributed to via app.actor_id. It seeds first (users seed before tables
27
+ # that FK to it), and stamps its own created_by=1 (the self-reference resolves
28
+ # because the row exists by the time the FK is checked). Consumers add their
29
+ # own users at user_id >= 2.
30
+ seeds:
31
+ - user_id: 1
32
+ name: 'app-init'
33
+ kind: 'service'
34
+ seeds_on_conflict: 'DO NOTHING'
@@ -1,31 +0,0 @@
1
- import type { Pool, PoolClient } from 'pg';
2
- /**
3
- * Check out a connection from the pool, open a transaction, run `fn`,
4
- * and either COMMIT (on success) or ROLLBACK (on throw). The connection
5
- * is always released back to the pool.
6
- *
7
- * The session variables set inside `fn` via the `set*` helpers from this
8
- * package use transaction scope, so they are discarded automatically when
9
- * this function commits or rolls back. There is no risk of cross-request
10
- * leakage.
11
- *
12
- * This is the lower-level companion to `withSession`. Use it when you
13
- * need to set session variables manually (e.g. when migrating existing
14
- * code that has its own session-extraction logic) and don't want the
15
- * full session-and-role validation that `withSession` performs.
16
- *
17
- * @param pool - The pg connection pool.
18
- * @param fn - Callback that receives the transaction-bound client.
19
- * @returns The value returned by `fn`.
20
- *
21
- * @example
22
- * ```ts
23
- * const result = await withTransaction(pool, async (client) => {
24
- * await setSessionId(client, sessionId);
25
- * await setRoleName(client, 'user');
26
- * return client.query('SELECT * FROM widgets');
27
- * });
28
- * ```
29
- */
30
- export declare function withTransaction<T>(pool: Pool, fn: (client: PoolClient) => Promise<T>): Promise<T>;
31
- //# sourceMappingURL=with-transaction.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"with-transaction.d.ts","sourceRoot":"","sources":["../src/with-transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAsB,eAAe,CAAC,CAAC,EACrC,IAAI,EAAE,IAAI,EACV,EAAE,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,OAAO,CAAC,CAAC,CAAC,GACrC,OAAO,CAAC,CAAC,CAAC,CAiBZ"}
@@ -1,49 +0,0 @@
1
- /**
2
- * Check out a connection from the pool, open a transaction, run `fn`,
3
- * and either COMMIT (on success) or ROLLBACK (on throw). The connection
4
- * is always released back to the pool.
5
- *
6
- * The session variables set inside `fn` via the `set*` helpers from this
7
- * package use transaction scope, so they are discarded automatically when
8
- * this function commits or rolls back. There is no risk of cross-request
9
- * leakage.
10
- *
11
- * This is the lower-level companion to `withSession`. Use it when you
12
- * need to set session variables manually (e.g. when migrating existing
13
- * code that has its own session-extraction logic) and don't want the
14
- * full session-and-role validation that `withSession` performs.
15
- *
16
- * @param pool - The pg connection pool.
17
- * @param fn - Callback that receives the transaction-bound client.
18
- * @returns The value returned by `fn`.
19
- *
20
- * @example
21
- * ```ts
22
- * const result = await withTransaction(pool, async (client) => {
23
- * await setSessionId(client, sessionId);
24
- * await setRoleName(client, 'user');
25
- * return client.query('SELECT * FROM widgets');
26
- * });
27
- * ```
28
- */
29
- export async function withTransaction(pool, fn) {
30
- const client = await pool.connect();
31
- try {
32
- await client.query('BEGIN');
33
- try {
34
- const result = await fn(client);
35
- await client.query('COMMIT');
36
- return result;
37
- }
38
- catch (err) {
39
- await client.query('ROLLBACK').catch(() => {
40
- // Swallow rollback errors so the original error surfaces.
41
- });
42
- throw err;
43
- }
44
- }
45
- finally {
46
- client.release();
47
- }
48
- }
49
- //# sourceMappingURL=with-transaction.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"with-transaction.js","sourceRoot":"","sources":["../src/with-transaction.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,IAAU,EACV,EAAsC;IAEtC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;IACpC,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC5B,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC;YAChC,MAAM,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;YAC7B,OAAO,MAAM,CAAC;QAChB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;gBACxC,0DAA0D;YAC5D,CAAC,CAAC,CAAC;YACH,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,CAAC,OAAO,EAAE,CAAC;IACnB,CAAC;AACH,CAAC"}