@smplcty/auth 0.6.4 → 2.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.
- package/README.md +187 -201
- package/dist/create-session.d.ts +10 -9
- package/dist/create-session.d.ts.map +1 -1
- package/dist/create-session.js +16 -18
- package/dist/create-session.js.map +1 -1
- package/dist/errors.d.ts +19 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +23 -5
- package/dist/errors.js.map +1 -1
- package/dist/find-user-by-communication-method.d.ts.map +1 -1
- package/dist/find-user-by-communication-method.js +4 -0
- package/dist/find-user-by-communication-method.js.map +1 -1
- package/dist/get-user-role-names.d.ts.map +1 -1
- package/dist/get-user-role-names.js +2 -0
- package/dist/get-user-role-names.js.map +1 -1
- package/dist/index.d.ts +9 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -6
- package/dist/index.js.map +1 -1
- package/dist/internal/hash-token.d.ts +16 -0
- package/dist/internal/hash-token.d.ts.map +1 -0
- package/dist/internal/hash-token.js +21 -0
- package/dist/internal/hash-token.js.map +1 -0
- package/dist/methods/errors.d.ts +23 -0
- package/dist/methods/errors.d.ts.map +1 -0
- package/dist/methods/errors.js +28 -0
- package/dist/methods/errors.js.map +1 -0
- package/dist/methods/oidc/index.d.ts +92 -0
- package/dist/methods/oidc/index.d.ts.map +1 -0
- package/dist/methods/oidc/index.js +104 -0
- package/dist/methods/oidc/index.js.map +1 -0
- package/dist/methods/router.d.ts +19 -0
- package/dist/methods/router.d.ts.map +1 -0
- package/dist/methods/router.js +95 -0
- package/dist/methods/router.js.map +1 -0
- package/dist/methods/twilio/index.d.ts +44 -0
- package/dist/methods/twilio/index.d.ts.map +1 -0
- package/dist/methods/twilio/index.js +77 -0
- package/dist/methods/twilio/index.js.map +1 -0
- package/dist/methods/types.d.ts +106 -0
- package/dist/methods/types.d.ts.map +1 -0
- package/dist/methods/types.js +2 -0
- package/dist/methods/types.js.map +1 -0
- package/dist/revoke-session.d.ts +20 -15
- package/dist/revoke-session.d.ts.map +1 -1
- package/dist/revoke-session.js +66 -20
- package/dist/revoke-session.js.map +1 -1
- package/dist/scope/flat-tenant.d.ts +36 -0
- package/dist/scope/flat-tenant.d.ts.map +1 -0
- package/dist/scope/flat-tenant.js +60 -0
- package/dist/scope/flat-tenant.js.map +1 -0
- package/dist/set-helpers.d.ts +44 -53
- package/dist/set-helpers.d.ts.map +1 -1
- package/dist/set-helpers.js +67 -68
- package/dist/set-helpers.js.map +1 -1
- package/dist/touch-session.d.ts +13 -0
- package/dist/touch-session.d.ts.map +1 -0
- package/dist/touch-session.js +28 -0
- package/dist/touch-session.js.map +1 -0
- package/dist/types.d.ts +59 -18
- package/dist/types.d.ts.map +1 -1
- package/dist/validate-session.d.ts +15 -14
- package/dist/validate-session.d.ts.map +1 -1
- package/dist/validate-session.js +28 -27
- package/dist/validate-session.js.map +1 -1
- package/dist/with-service-context.d.ts +15 -12
- package/dist/with-service-context.d.ts.map +1 -1
- package/dist/with-service-context.js +34 -30
- package/dist/with-service-context.js.map +1 -1
- package/dist/with-session.d.ts +22 -16
- package/dist/with-session.d.ts.map +1 -1
- package/dist/with-session.js +67 -63
- package/dist/with-session.js.map +1 -1
- package/package.json +33 -2
- package/schema/functions/current_user_id.yaml +18 -0
- package/schema/functions/resolve_session.yaml +42 -23
- package/schema/post/0001-backfill-audit-by.sql +15 -0
- package/schema/tables/auth_domains.yaml +43 -0
- package/schema/tables/communication_channels.yaml +5 -1
- package/schema/tables/dev_otp_enrollments.yaml +1 -0
- package/schema/tables/roles.yaml +43 -7
- package/schema/tables/sessions.yaml +10 -0
- package/schema/tables/tenants.yaml +24 -1
- package/schema/tables/user_communication_methods.yaml +5 -1
- package/schema/tables/user_roles.yaml +1 -0
- package/schema/tables/users.yaml +27 -0
- package/dist/with-transaction.d.ts +0 -31
- package/dist/with-transaction.d.ts.map +0 -1
- package/dist/with-transaction.js +0 -49
- package/dist/with-transaction.js.map +0 -1
|
@@ -0,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C;YAED,2EAA2E;YAC3E,MAAM,KAAK,GACT,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ;gBAC9B,CAAC,CAAC,MAAM,CAAC,KAAK;gBACd,CAAC,CAAC,OAAO,MAAM,CAAC,kBAAkB,KAAK,QAAQ;oBAC7C,CAAC,CAAC,MAAM,CAAC,kBAAkB;oBAC3B,CAAC,CAAC,SAAS,CAAC;YAClB,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,MAAM,IAAI,uBAAuB,CAAC,kDAAkD,CAAC,CAAC;YACxF,CAAC;YAED,MAAM,QAAQ,GAAG,MAAM,6BAA6B,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;YAC5F,IAAI,QAAQ,EAAE,CAAC;gBACb,OAAO,QAAQ,CAAC;YAClB,CAAC;YACD,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC;gBAC1B,OAAO,OAAO,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC,CAAC;YAC3D,CAAC;YACD,MAAM,IAAI,uBAAuB,CAAC,4DAA4D,CAAC,CAAC;QAClG,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { MethodRouter, MethodRouterOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Build the tenant-centric sign-in router: **discovery** (`signInOptions`) and
|
|
4
|
+
* the **user-bound OTP path** (`initiateOtp`/`completeOtp`, gated by the
|
|
5
|
+
* tenant's `allow_otp`, enforced here — not just in the UI).
|
|
6
|
+
*
|
|
7
|
+
* OIDC is intentionally not routed here: it has a richer `authorize`/`callback`
|
|
8
|
+
* shape (PKCE + login-state) handled by `@smplcty/auth/oidc`. Resolve the
|
|
9
|
+
* tenant's OIDC `auth_domains` via `signInOptions`, then drive that handler.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const router = createMethodRouter({ db: pool, otpHandler: twilioVerifyHandler({ client }) });
|
|
13
|
+
* const opts = await router.signInOptions({ tenantSlug: 'acme' });
|
|
14
|
+
* // opts = { tenantId, authDomains: [...OIDC IdPs...], otpAllowed }
|
|
15
|
+
* // render: a button per authDomain (→ @smplcty/auth/oidc) + an OTP form iff otpAllowed
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function createMethodRouter(options: MethodRouterOptions): MethodRouter;
|
|
19
|
+
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/methods/router.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAc,YAAY,EAAE,mBAAmB,EAA+B,MAAM,YAAY,CAAC;AAiC7G;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY,CAwD7E"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { InvalidInputError } from '../errors.js';
|
|
2
|
+
import { OtpNotAllowedError } from './errors.js';
|
|
3
|
+
const SELECT_TENANT_BY_SLUG = `
|
|
4
|
+
SELECT tenant_id AS "tenantId", allow_otp AS "allowOtp"
|
|
5
|
+
FROM tenants
|
|
6
|
+
WHERE slug = $1 AND deleted_at IS NULL
|
|
7
|
+
LIMIT 1
|
|
8
|
+
`;
|
|
9
|
+
const SELECT_ALLOW_OTP = `
|
|
10
|
+
SELECT allow_otp AS "allowOtp"
|
|
11
|
+
FROM tenants
|
|
12
|
+
WHERE tenant_id = $1 AND deleted_at IS NULL
|
|
13
|
+
LIMIT 1
|
|
14
|
+
`;
|
|
15
|
+
const SELECT_AUTH_DOMAINS_BY_TENANT = `
|
|
16
|
+
SELECT
|
|
17
|
+
auth_domain_id AS "authDomainId",
|
|
18
|
+
tenant_id AS "tenantId",
|
|
19
|
+
display_name AS "displayName",
|
|
20
|
+
integration_type AS "integrationType",
|
|
21
|
+
integration_params AS "integrationParams"
|
|
22
|
+
FROM auth_domains
|
|
23
|
+
WHERE tenant_id = $1 AND deleted_at IS NULL
|
|
24
|
+
ORDER BY display_name
|
|
25
|
+
`;
|
|
26
|
+
/**
|
|
27
|
+
* Build the tenant-centric sign-in router: **discovery** (`signInOptions`) and
|
|
28
|
+
* the **user-bound OTP path** (`initiateOtp`/`completeOtp`, gated by the
|
|
29
|
+
* tenant's `allow_otp`, enforced here — not just in the UI).
|
|
30
|
+
*
|
|
31
|
+
* OIDC is intentionally not routed here: it has a richer `authorize`/`callback`
|
|
32
|
+
* shape (PKCE + login-state) handled by `@smplcty/auth/oidc`. Resolve the
|
|
33
|
+
* tenant's OIDC `auth_domains` via `signInOptions`, then drive that handler.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const router = createMethodRouter({ db: pool, otpHandler: twilioVerifyHandler({ client }) });
|
|
37
|
+
* const opts = await router.signInOptions({ tenantSlug: 'acme' });
|
|
38
|
+
* // opts = { tenantId, authDomains: [...OIDC IdPs...], otpAllowed }
|
|
39
|
+
* // render: a button per authDomain (→ @smplcty/auth/oidc) + an OTP form iff otpAllowed
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export function createMethodRouter(options) {
|
|
43
|
+
const { otpHandler } = options;
|
|
44
|
+
const q = options.db;
|
|
45
|
+
async function assertOtpAllowed(tenantId) {
|
|
46
|
+
if (!Number.isInteger(tenantId) || tenantId <= 0) {
|
|
47
|
+
throw new InvalidInputError('tenantId must be a positive integer');
|
|
48
|
+
}
|
|
49
|
+
if (!otpHandler) {
|
|
50
|
+
throw new OtpNotAllowedError(tenantId, 'No OTP handler is configured');
|
|
51
|
+
}
|
|
52
|
+
const { rows } = await q.query(SELECT_ALLOW_OTP, [tenantId]);
|
|
53
|
+
const row = rows[0];
|
|
54
|
+
// Unknown tenant or allow_otp=false both deny — fail closed.
|
|
55
|
+
if (!row || row.allowOtp !== true) {
|
|
56
|
+
throw new OtpNotAllowedError(tenantId);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
async signInOptions({ tenantSlug }) {
|
|
61
|
+
if (typeof tenantSlug !== 'string' || tenantSlug.length === 0) {
|
|
62
|
+
throw new InvalidInputError('tenantSlug must be a non-empty string');
|
|
63
|
+
}
|
|
64
|
+
const { rows } = await q.query(SELECT_TENANT_BY_SLUG, [tenantSlug]);
|
|
65
|
+
const tenant = rows[0];
|
|
66
|
+
if (!tenant) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const { rows: authDomains } = await q.query(SELECT_AUTH_DOMAINS_BY_TENANT, [tenant.tenantId]);
|
|
70
|
+
return {
|
|
71
|
+
tenantId: tenant.tenantId,
|
|
72
|
+
authDomains,
|
|
73
|
+
otpAllowed: tenant.allowOtp === true && !!otpHandler,
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
async initiateOtp({ tenantId, identifier }) {
|
|
77
|
+
if (typeof identifier !== 'string' || identifier.length === 0) {
|
|
78
|
+
throw new InvalidInputError('identifier must be a non-empty string');
|
|
79
|
+
}
|
|
80
|
+
await assertOtpAllowed(tenantId);
|
|
81
|
+
return otpHandler.initiate({ db: options.db, identifier });
|
|
82
|
+
},
|
|
83
|
+
async completeOtp({ tenantId, identifier, credential }) {
|
|
84
|
+
if (typeof identifier !== 'string' || identifier.length === 0) {
|
|
85
|
+
throw new InvalidInputError('identifier must be a non-empty string');
|
|
86
|
+
}
|
|
87
|
+
if (typeof credential !== 'string' || credential.length === 0) {
|
|
88
|
+
throw new InvalidInputError('credential must be a non-empty string');
|
|
89
|
+
}
|
|
90
|
+
await assertOtpAllowed(tenantId);
|
|
91
|
+
return otpHandler.complete({ db: options.db, identifier, credential });
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.js","sourceRo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AQ,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC9D,MAAM,IAAI,iBAAiB,CAAC,uCAAuC,CAAC,CAAC;YACvE,CAAC;YACD,MAAM,gBAAgB,CAAC,QAAQ,CAAC,CAAC;YACjC,OAAO,UAAW,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,CAAC;QAC1E,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { TwilioVerifyClient, VerificationChannel } from '@smplcty/twilio';
|
|
2
|
+
import type { MethodHandler } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* How an identifier maps onto a communication channel.
|
|
5
|
+
*
|
|
6
|
+
* - `channelName` — the `communication_channels.name` to match the user
|
|
7
|
+
* on (e.g. 'email' / 'phone').
|
|
8
|
+
* - `verifyChannel` — the Twilio Verify delivery channel ('sms' / 'email').
|
|
9
|
+
*/
|
|
10
|
+
export interface ResolvedChannel {
|
|
11
|
+
channelName: string;
|
|
12
|
+
verifyChannel: VerificationChannel;
|
|
13
|
+
}
|
|
14
|
+
export interface TwilioVerifyHandlerOptions {
|
|
15
|
+
/** A Twilio Verify client from `createTwilioVerifyClient` (in the app). */
|
|
16
|
+
client: TwilioVerifyClient;
|
|
17
|
+
/** Map an identifier to its channel. Default: contains '@' → email,
|
|
18
|
+
* otherwise phone/sms. */
|
|
19
|
+
resolveChannel?: (identifier: string) => ResolvedChannel;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The user-bound Twilio Verify method handler (opt-in subpath
|
|
23
|
+
* `@smplcty/auth/twilio`). Phase 1 sends an OTP (skipping the send for
|
|
24
|
+
* dev-OTP-enrolled users); phase 2 verifies the submitted code — dev-OTP
|
|
25
|
+
* first, then Twilio — and resolves the existing user.
|
|
26
|
+
*
|
|
27
|
+
* User-bound: it never provisions. The user must already have a
|
|
28
|
+
* `user_communication_methods` row for the identifier. To avoid a
|
|
29
|
+
* user-enumeration oracle, `initiate` reports `otpSent` even for an unknown
|
|
30
|
+
* identifier (it just doesn't send); `complete` fails verification.
|
|
31
|
+
*
|
|
32
|
+
* Drives `@smplcty/twilio` (an **optional peer**); auth core depends on
|
|
33
|
+
* neither it nor `jose`. Install the peer and pass a client:
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { createTwilioVerifyClient } from '@smplcty/twilio';
|
|
37
|
+
* import { twilioVerifyHandler } from '@smplcty/auth/twilio';
|
|
38
|
+
*
|
|
39
|
+
* const handler = twilioVerifyHandler({ client: createTwilioVerifyClient(cfg) });
|
|
40
|
+
* const router = createMethodRouter({ db: pool, handlers: {}, defaultHandler: handler });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function twilioVerifyHandler(options: TwilioVerifyHandlerOptions): MethodHandler;
|
|
44
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/methods/twilio/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAK/E,OAAO,KAAK,EAEV,aAAa,EAId,MAAM,aAAa,CAAC;AAErB;;;;;;GAMG;AACH,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,mBAAmB,CAAC;CACpC;AAED,MAAM,WAAW,0BAA0B;IACzC,2EAA2E;IAC3E,MAAM,EAAE,kBAAkB,CAAC;IAC3B;+BAC2B;IAC3B,cAAc,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,eAAe,CAAC;CAC1D;AAiBD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,0BAA0B,GAAG,aAAa,CA2CtF"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { InvalidInputError } from '../../errors.js';
|
|
2
|
+
import { findUserByCommunicationMethod } from '../../find-user-by-communication-method.js';
|
|
3
|
+
import { verifyDevOtp, isDevOtpEnrolled } from '../../dev-otp.js';
|
|
4
|
+
import { VerificationFailedError } from '../errors.js';
|
|
5
|
+
function defaultResolveChannel(identifier) {
|
|
6
|
+
return identifier.includes('@')
|
|
7
|
+
? { channelName: 'email', verifyChannel: 'email' }
|
|
8
|
+
: { channelName: 'phone', verifyChannel: 'sms' };
|
|
9
|
+
}
|
|
10
|
+
// User-bound: the identifier (phone/email) is required. The router always
|
|
11
|
+
// supplies it via initiateOtp/completeOtp; this guards direct handler use.
|
|
12
|
+
function requireIdentifier(identifier) {
|
|
13
|
+
if (typeof identifier !== 'string' || identifier.length === 0) {
|
|
14
|
+
throw new InvalidInputError('the Twilio handler requires a non-empty identifier');
|
|
15
|
+
}
|
|
16
|
+
return identifier;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The user-bound Twilio Verify method handler (opt-in subpath
|
|
20
|
+
* `@smplcty/auth/twilio`). Phase 1 sends an OTP (skipping the send for
|
|
21
|
+
* dev-OTP-enrolled users); phase 2 verifies the submitted code — dev-OTP
|
|
22
|
+
* first, then Twilio — and resolves the existing user.
|
|
23
|
+
*
|
|
24
|
+
* User-bound: it never provisions. The user must already have a
|
|
25
|
+
* `user_communication_methods` row for the identifier. To avoid a
|
|
26
|
+
* user-enumeration oracle, `initiate` reports `otpSent` even for an unknown
|
|
27
|
+
* identifier (it just doesn't send); `complete` fails verification.
|
|
28
|
+
*
|
|
29
|
+
* Drives `@smplcty/twilio` (an **optional peer**); auth core depends on
|
|
30
|
+
* neither it nor `jose`. Install the peer and pass a client:
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { createTwilioVerifyClient } from '@smplcty/twilio';
|
|
34
|
+
* import { twilioVerifyHandler } from '@smplcty/auth/twilio';
|
|
35
|
+
*
|
|
36
|
+
* const handler = twilioVerifyHandler({ client: createTwilioVerifyClient(cfg) });
|
|
37
|
+
* const router = createMethodRouter({ db: pool, handlers: {}, defaultHandler: handler });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function twilioVerifyHandler(options) {
|
|
41
|
+
const { client } = options;
|
|
42
|
+
const resolveChannel = options.resolveChannel ?? defaultResolveChannel;
|
|
43
|
+
return {
|
|
44
|
+
async initiate(ctx) {
|
|
45
|
+
const identifier = requireIdentifier(ctx.identifier);
|
|
46
|
+
const { channelName, verifyChannel } = resolveChannel(identifier);
|
|
47
|
+
const user = await findUserByCommunicationMethod(ctx.db, { channel: channelName, code: identifier });
|
|
48
|
+
// Unknown identifier: report sent without sending (anti-enumeration).
|
|
49
|
+
if (!user) {
|
|
50
|
+
return { otpSent: true };
|
|
51
|
+
}
|
|
52
|
+
// Dev-OTP-enrolled devs generate their code from an authenticator
|
|
53
|
+
// app, so skip the (likely undeliverable) SMS.
|
|
54
|
+
if (await isDevOtpEnrolled(ctx.db, user.userCommunicationMethodId)) {
|
|
55
|
+
return { otpSent: true };
|
|
56
|
+
}
|
|
57
|
+
await client.sendVerificationCode({ channel: verifyChannel, to: identifier });
|
|
58
|
+
return { otpSent: true };
|
|
59
|
+
},
|
|
60
|
+
async complete(ctx) {
|
|
61
|
+
const identifier = requireIdentifier(ctx.identifier);
|
|
62
|
+
const { channelName } = resolveChannel(identifier);
|
|
63
|
+
const user = await findUserByCommunicationMethod(ctx.db, { channel: channelName, code: identifier });
|
|
64
|
+
if (!user) {
|
|
65
|
+
throw new VerificationFailedError();
|
|
66
|
+
}
|
|
67
|
+
// Dev-OTP first (authenticator-app code), then Twilio Verify.
|
|
68
|
+
const okDev = await verifyDevOtp(ctx.db, user.userCommunicationMethodId, ctx.credential);
|
|
69
|
+
const ok = okDev || (await client.verifyVerificationCode({ to: identifier, code: ctx.credential }));
|
|
70
|
+
if (!ok) {
|
|
71
|
+
throw new VerificationFailedError();
|
|
72
|
+
}
|
|
73
|
+
return user;
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/methods/twilio/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,4CAA4C,CAAC;AAC3F,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AA6BvD,SAAS,qBAAqB,CAAC,UAAkB;IAC/C,OAAO,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC;QAC7B,CAAC,CAAC,EAAE,WAAW,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE;QAClD,CAAC,CAAC,EAAE,WAAW,EAAE,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC;AACrD,CAAC;AAED,0EAA0E;AAC1E,2EAA2E;AAC3E,SAAS,iBAAiB,CAAC,UAA8B;IACvD,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9D,MAAM,IAAI,iBAAiB,CAAC,oDAAoD,CAAC,CAAC;IACpF,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAmC;IACrE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC3B,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,qBAAqB,CAAC;IAEvE,OAAO;QACL,KAAK,CAAC,QAAQ,CAAC,GAA0B;YACvC,MAAM,UAAU,GAAG,iBAAiB,CAAC,GAAG,CAAC,UAAU,CAAC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,GAAG,KAAK,IAAI,CAAC,MAAM,MAAM,CAAC,sBAAsB,CAAC,EAAE,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC;YACpG,IAAI,CAAC,EAAE,EAAE,CAAC;gBACR,MAAM,IAAI,uBAAuB,EAAE,CAAC;YACtC,CAAC;YAED,OAAO,IAAI,CAAC;QACd,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { Queryable } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* An `auth_domains` row — one of a tenant's org-bound sign-in IdPs. A tenant
|
|
4
|
+
* has 0..N of these; the router lists them for a tenant. OIDC rows are driven
|
|
5
|
+
* by the dedicated `@smplcty/auth/oidc` handler (not the generic OTP path);
|
|
6
|
+
* `displayName` is the chooser-button label when a tenant has more than one.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam TParams - Shape of `integration_params`. Defaults to an open
|
|
9
|
+
* record; narrow it in a handler.
|
|
10
|
+
*/
|
|
11
|
+
export interface AuthDomain<TParams = Record<string, unknown>> {
|
|
12
|
+
readonly authDomainId: number;
|
|
13
|
+
readonly tenantId: number;
|
|
14
|
+
/** Button label when the tenant has several IdPs (e.g. "Microsoft"). */
|
|
15
|
+
readonly displayName: string;
|
|
16
|
+
/** Selects the handler — e.g. 'oidc'. */
|
|
17
|
+
readonly integrationType: string;
|
|
18
|
+
/** Provider config. Handler-specific. */
|
|
19
|
+
readonly integrationParams: TParams;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A user verified by a method handler, ready for `createSession`.
|
|
23
|
+
*/
|
|
24
|
+
export interface ResolvedUser {
|
|
25
|
+
readonly userId: number;
|
|
26
|
+
readonly userCommunicationMethodId: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Result of a user-bound handler's `initiate` phase.
|
|
30
|
+
*/
|
|
31
|
+
export type MethodInitiateResult = {
|
|
32
|
+
readonly otpSent: true;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Context handed to a user-bound (OTP) handler's `initiate`.
|
|
36
|
+
*/
|
|
37
|
+
export interface MethodInitiateContext {
|
|
38
|
+
/** A connection or pool the handler may query (e.g. to find the user). */
|
|
39
|
+
readonly db: Queryable;
|
|
40
|
+
/** The identifier the user entered (email/phone). */
|
|
41
|
+
readonly identifier: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Context handed to a user-bound handler's `complete`. Adds the submitted OTP.
|
|
45
|
+
*/
|
|
46
|
+
export interface MethodCompleteContext extends MethodInitiateContext {
|
|
47
|
+
readonly credential: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A pluggable **user-bound** (OTP-style) two-phase method: `initiate` sends a
|
|
51
|
+
* code, `complete` verifies it. Injected into the router as `otpHandler`. Auth
|
|
52
|
+
* core depends on no protocol library. (Org-bound OIDC is handled separately by
|
|
53
|
+
* `@smplcty/auth/oidc`'s richer `authorize`/`callback` shape, not this.)
|
|
54
|
+
*/
|
|
55
|
+
export interface MethodHandler {
|
|
56
|
+
initiate(ctx: MethodInitiateContext): Promise<MethodInitiateResult>;
|
|
57
|
+
complete(ctx: MethodCompleteContext): Promise<ResolvedUser>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Options for {@link createMethodRouter}.
|
|
61
|
+
*/
|
|
62
|
+
export interface MethodRouterOptions {
|
|
63
|
+
/** Used to look up tenants and their `auth_domains`. */
|
|
64
|
+
db: Queryable;
|
|
65
|
+
/** The user-bound OTP handler (e.g. a `TwilioVerifyHandler`). Offered only
|
|
66
|
+
* when the tenant's `allow_otp` is true; omit it for an SSO-only product. */
|
|
67
|
+
otpHandler?: MethodHandler;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The sign-in methods available for a tenant — what the app renders.
|
|
71
|
+
*/
|
|
72
|
+
export interface SignInOptions {
|
|
73
|
+
readonly tenantId: number;
|
|
74
|
+
/** The tenant's org-bound IdPs. Zero ⇒ OTP only; one ⇒ straight redirect;
|
|
75
|
+
* several ⇒ a chooser. Drive OIDC with `@smplcty/auth/oidc`. */
|
|
76
|
+
readonly authDomains: readonly AuthDomain[];
|
|
77
|
+
/** Whether to offer OTP: the tenant's `allow_otp` AND an `otpHandler` is set. */
|
|
78
|
+
readonly otpAllowed: boolean;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Tenant-centric sign-in **discovery + the user-bound OTP path**. Discovery is
|
|
82
|
+
* by sub-domain → tenant (the app parses the Host to a slug). OIDC completion
|
|
83
|
+
* is not routed here — resolve the tenant's OIDC `auth_domains` via
|
|
84
|
+
* `signInOptions`, then drive `@smplcty/auth/oidc`'s handler directly.
|
|
85
|
+
*/
|
|
86
|
+
export interface MethodRouter {
|
|
87
|
+
/** The sign-in methods for the tenant owning `tenantSlug`, or null if no
|
|
88
|
+
* such (live) tenant. */
|
|
89
|
+
signInOptions(input: {
|
|
90
|
+
tenantSlug: string;
|
|
91
|
+
}): Promise<SignInOptions | null>;
|
|
92
|
+
/** User-bound (OTP) phase 1. Throws {@link OtpNotAllowedError} when the
|
|
93
|
+
* tenant disallows OTP. */
|
|
94
|
+
initiateOtp(input: {
|
|
95
|
+
tenantId: number;
|
|
96
|
+
identifier: string;
|
|
97
|
+
}): Promise<MethodInitiateResult>;
|
|
98
|
+
/** User-bound (OTP) phase 2. Throws {@link OtpNotAllowedError} when the
|
|
99
|
+
* tenant disallows OTP. */
|
|
100
|
+
completeOtp(input: {
|
|
101
|
+
tenantId: number;
|
|
102
|
+
identifier: string;
|
|
103
|
+
credential: string;
|
|
104
|
+
}): Promise<ResolvedUser>;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/methods/types.ts"],"names":[],"mappings":"AAAA,OAAO,K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,aAAa,CAAC;CAC5B;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B;qEACiE;IACjE,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;IAC5C,iFAAiF;IACjF,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;CAC9B;AAED;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IAC3B;8BAC0B;IAC1B,aAAa,CAAC,KAAK,EAAE;QAAE,UAAU,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAC;IAC5E;gCAC4B;IAC5B,WAAW,CAAC,KAAK,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;IAC5F;gCAC4B;IAC5B,WAAW,CAAC,KAAK,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CACzG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/methods/types.ts"],"names":[],"mappings":""}
|
package/dist/revoke-session.d.ts
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
import type { Queryable } from './types.js';
|
|
2
2
|
/**
|
|
3
|
-
* Revoke a session by
|
|
3
|
+
* Revoke a single session by its raw token (force sign-off of that one
|
|
4
|
+
* session). Idempotent — revoking an unknown or already-expired session is
|
|
5
|
+
* not an error. The row is preserved for audit.
|
|
4
6
|
*
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* is not an error.
|
|
7
|
+
* @throws {InvalidInputError} If `token` is not a non-empty string.
|
|
8
|
+
*/
|
|
9
|
+
export declare function revokeSession(db: Queryable, token: string): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Revoke every active session belonging to a user (force sign-off
|
|
12
|
+
* everywhere). Use on password reset, account lockout, or a
|
|
13
|
+
* role/privilege change that should drop existing sessions.
|
|
13
14
|
*
|
|
14
|
-
* @
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
* @throws {InvalidInputError} If `userId` is not a positive integer.
|
|
16
|
+
*/
|
|
17
|
+
export declare function revokeUserSessions(db: Queryable, userId: number): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Revoke every active session of every user explicitly in a tenant
|
|
20
|
+
* (tenant-wide sign-off). Membership is resolved from `user_roles.tenant_id`
|
|
21
|
+
* — wildcard members (NULL tenant) are intentionally **not** signed out,
|
|
22
|
+
* since they belong to no single tenant.
|
|
18
23
|
*
|
|
19
|
-
* @throws {InvalidInputError} If `
|
|
24
|
+
* @throws {InvalidInputError} If `tenantId` is not a positive integer.
|
|
20
25
|
*/
|
|
21
|
-
export declare function
|
|
26
|
+
export declare function revokeTenantSessions(db: Queryable, tenantId: number): Promise<void>;
|
|
22
27
|
//# sourceMappingURL=revoke-session.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"revoke-session.d.ts","sourceRoot":"","sources":["../src/revoke-session.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"revoke-session.d.ts","sourceRoot":"","sources":["../src/revoke-session.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAyC5C;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAK/E;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAKrF;AAED;;;;;;;GAOG;AACH,wBAAsB,oBAAoB,CAAC,EAAE,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAKzF"}
|
package/dist/revoke-session.js
CHANGED
|
@@ -1,33 +1,79 @@
|
|
|
1
1
|
import { InvalidInputError } from './errors.js';
|
|
2
|
-
|
|
2
|
+
import { hashToken } from './internal/hash-token.js';
|
|
3
|
+
// Soft-revoke: set expires_at = now() so the row survives for audit but
|
|
4
|
+
// the session is locked out immediately (withSession/validateSession both
|
|
5
|
+
// reject expires_at <= now()). Guarded so we don't move an already-past
|
|
6
|
+
// expiry around.
|
|
7
|
+
const EXPIRE_BY_HASH = `
|
|
3
8
|
UPDATE sessions
|
|
4
9
|
SET expires_at = now()
|
|
5
10
|
WHERE session_id = $1
|
|
6
|
-
AND
|
|
11
|
+
AND expires_at > now()
|
|
12
|
+
`;
|
|
13
|
+
const EXPIRE_BY_USER = `
|
|
14
|
+
UPDATE sessions s
|
|
15
|
+
SET expires_at = now()
|
|
16
|
+
FROM user_communication_methods ucm
|
|
17
|
+
WHERE ucm.user_communication_method_id = s.user_communication_method_id
|
|
18
|
+
AND ucm.user_id = $1
|
|
19
|
+
AND s.expires_at > now()
|
|
20
|
+
`;
|
|
21
|
+
// Tenant-wide sign-off. Membership IS user_roles.tenant_id (the contract;
|
|
22
|
+
// there is no user_tenants table), so the library resolves "users in
|
|
23
|
+
// tenant X" itself and fans out. The match is tenant_id = $1, NOT
|
|
24
|
+
// tenant_id IS NULL: wildcard members (NULL = all-tenants global admins /
|
|
25
|
+
// service principals) are not members of any one tenant and must not be
|
|
26
|
+
// signed out by a single tenant's sign-off.
|
|
27
|
+
const EXPIRE_BY_TENANT = `
|
|
28
|
+
UPDATE sessions s
|
|
29
|
+
SET expires_at = now()
|
|
30
|
+
FROM user_communication_methods ucm
|
|
31
|
+
WHERE ucm.user_communication_method_id = s.user_communication_method_id
|
|
32
|
+
AND s.expires_at > now()
|
|
33
|
+
AND ucm.user_id IN (
|
|
34
|
+
SELECT DISTINCT ur.user_id
|
|
35
|
+
FROM user_roles ur
|
|
36
|
+
WHERE ur.tenant_id = $1
|
|
37
|
+
)
|
|
7
38
|
`;
|
|
8
39
|
/**
|
|
9
|
-
* Revoke a session by
|
|
40
|
+
* Revoke a single session by its raw token (force sign-off of that one
|
|
41
|
+
* session). Idempotent — revoking an unknown or already-expired session is
|
|
42
|
+
* not an error. The row is preserved for audit.
|
|
10
43
|
*
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
44
|
+
* @throws {InvalidInputError} If `token` is not a non-empty string.
|
|
45
|
+
*/
|
|
46
|
+
export async function revokeSession(db, token) {
|
|
47
|
+
if (typeof token !== 'string' || token.length === 0) {
|
|
48
|
+
throw new InvalidInputError('token must be a non-empty string');
|
|
49
|
+
}
|
|
50
|
+
await db.query(EXPIRE_BY_HASH, [hashToken(token)]);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Revoke every active session belonging to a user (force sign-off
|
|
54
|
+
* everywhere). Use on password reset, account lockout, or a
|
|
55
|
+
* role/privilege change that should drop existing sessions.
|
|
19
56
|
*
|
|
20
|
-
* @
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
57
|
+
* @throws {InvalidInputError} If `userId` is not a positive integer.
|
|
58
|
+
*/
|
|
59
|
+
export async function revokeUserSessions(db, userId) {
|
|
60
|
+
if (!Number.isInteger(userId) || userId <= 0) {
|
|
61
|
+
throw new InvalidInputError('userId must be a positive integer');
|
|
62
|
+
}
|
|
63
|
+
await db.query(EXPIRE_BY_USER, [userId]);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Revoke every active session of every user explicitly in a tenant
|
|
67
|
+
* (tenant-wide sign-off). Membership is resolved from `user_roles.tenant_id`
|
|
68
|
+
* — wildcard members (NULL tenant) are intentionally **not** signed out,
|
|
69
|
+
* since they belong to no single tenant.
|
|
24
70
|
*
|
|
25
|
-
* @throws {InvalidInputError} If `
|
|
71
|
+
* @throws {InvalidInputError} If `tenantId` is not a positive integer.
|
|
26
72
|
*/
|
|
27
|
-
export async function
|
|
28
|
-
if (
|
|
29
|
-
throw new InvalidInputError('
|
|
73
|
+
export async function revokeTenantSessions(db, tenantId) {
|
|
74
|
+
if (!Number.isInteger(tenantId) || tenantId <= 0) {
|
|
75
|
+
throw new InvalidInputError('tenantId must be a positive integer');
|
|
30
76
|
}
|
|
31
|
-
await db.query(
|
|
77
|
+
await db.query(EXPIRE_BY_TENANT, [tenantId]);
|
|
32
78
|
}
|
|
33
79
|
//# sourceMappingURL=revoke-session.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"revoke-session.js","sourceRoot":"","sources":["../src/revoke-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"revoke-session.js","sourceRoot":"","sources":["../src/revoke-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAGrD,wEAAwE;AACxE,0EAA0E;AAC1E,wEAAwE;AACxE,iBAAiB;AACjB,MAAM,cAAc,GAAG;;;;;CAKtB,CAAC;AAEF,MAAM,cAAc,GAAG;;;;;;;CAOtB,CAAC;AAEF,0EAA0E;AAC1E,qEAAqE;AACrE,kEAAkE;AAClE,0EAA0E;AAC1E,wEAAwE;AACxE,4CAA4C;AAC5C,MAAM,gBAAgB,GAAG;;;;;;;;;;;CAWxB,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,EAAa,EAAE,KAAa;IAC9D,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACpD,MAAM,IAAI,iBAAiB,CAAC,kCAAkC,CAAC,CAAC;IAClE,CAAC;IACD,MAAM,EAAE,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACrD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,EAAa,EAAE,MAAc;IACpE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,iBAAiB,CAAC,mCAAmC,CAAC,CAAC;IACnE,CAAC;IACD,MAAM,EAAE,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;AAC3C,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,EAAa,EAAE,QAAgB;IACxE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,QAAQ,IAAI,CAAC,EAAE,CAAC;QACjD,MAAM,IAAI,iBAAiB,CAAC,qCAAqC,CAAC,CAAC;IACrE,CAAC;IACD,MAAM,EAAE,CAAC,KAAK,CAAC,gBAAgB,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ScopeHook } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The flat-tenant scope GUCs. Set by the {@link flatTenantScope} preset;
|
|
4
|
+
* **not** part of the core identity contract. Reference them in RLS
|
|
5
|
+
* policies if you adopt the preset.
|
|
6
|
+
*
|
|
7
|
+
* - `app.tenant_ids` — comma-separated tenant ids the user belongs to.
|
|
8
|
+
* - `app.all_tenants` — `'true'` when the user has a wildcard
|
|
9
|
+
* (`tenant_id IS NULL`) membership; such users see every tenant.
|
|
10
|
+
*/
|
|
11
|
+
export declare const FLAT_TENANT_GUC: {
|
|
12
|
+
readonly tenantIds: "app.tenant_ids";
|
|
13
|
+
readonly allTenants: "app.all_tenants";
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* A ready-made scope hook implementing the flat `tenant_ids` /
|
|
17
|
+
* `all_tenants` model (the 0.6.x behavior), now opt-in rather than baked
|
|
18
|
+
* into the core.
|
|
19
|
+
*
|
|
20
|
+
* Adopt it for simple multi-tenant apps that don't need a custom scope:
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { withSession } from '@smplcty/auth';
|
|
24
|
+
* import { flatTenantScope } from '@smplcty/auth/flat-tenant';
|
|
25
|
+
*
|
|
26
|
+
* const scope = flatTenantScope();
|
|
27
|
+
* await withSession(pool, { token }, fn, { scope });
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* It sets `app.tenant_ids` and `app.all_tenants` from the user's
|
|
31
|
+
* `user_roles` rows. Your RLS policies read those GUCs to filter by
|
|
32
|
+
* tenant. Apps with a richer intra-tenant model (producer/region/plant,
|
|
33
|
+
* rep hierarchy, …) ship their own scope hook instead.
|
|
34
|
+
*/
|
|
35
|
+
export declare function flatTenantScope(): ScopeHook;
|
|
36
|
+
//# sourceMappingURL=flat-tenant.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flat-tenant.d.ts","sourceRoot":"","sources":["../../src/scope/flat-tenant.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAmB,MAAM,aAAa,CAAC;AAE9D;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe;;;CAGlB,CAAC;AAyBX;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,IAAI,SAAS,CAS3C"}
|