@smplcty/auth 1.0.0 → 2.0.1
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 +16 -13
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/methods/errors.d.ts +0 -14
- package/dist/methods/errors.d.ts.map +1 -1
- package/dist/methods/errors.js +0 -20
- package/dist/methods/errors.js.map +1 -1
- package/dist/methods/oidc/index.d.ts +66 -39
- package/dist/methods/oidc/index.d.ts.map +1 -1
- package/dist/methods/oidc/index.js +75 -47
- package/dist/methods/oidc/index.js.map +1 -1
- package/dist/methods/router.d.ts +9 -29
- package/dist/methods/router.d.ts.map +1 -1
- package/dist/methods/router.js +16 -74
- package/dist/methods/router.js.map +1 -1
- package/dist/methods/types.d.ts +19 -37
- package/dist/methods/types.d.ts.map +1 -1
- package/package.json +14 -13
package/README.md
CHANGED
|
@@ -5,6 +5,8 @@ Robust, type-safe **stateful-session** authentication primitives for PostgreSQL
|
|
|
5
5
|
[](https://www.npmjs.com/package/@smplcty/auth)
|
|
6
6
|
[](LICENSE)
|
|
7
7
|
|
|
8
|
+
📖 **[Documentation](https://mabulu-inc.github.io/simplicity-auth)**
|
|
9
|
+
|
|
8
10
|
## Why
|
|
9
11
|
|
|
10
12
|
The library owns **identity, sessions, roles/privileges, tenants, sign-in federation, and per-request context**. Each app owns only its **intra-tenant authorization scope** (its RLS model). See [`docs/v1-design.md`](docs/v1-design.md) for the full design.
|
|
@@ -175,37 +177,38 @@ await touchSession(db, token); // record activity (last_seen_at); returns whethe
|
|
|
175
177
|
|
|
176
178
|
Revoke is a **soft-revoke** — it sets `expires_at = now()`, so the row survives for audit but the session is locked out immediately. Role/privilege changes take effect on the next request automatically (nothing is baked into the token). `revokeTenantSessions` resolves membership from `user_roles.tenant_id` and deliberately spares wildcard (all-tenant) members like global admins.
|
|
177
179
|
|
|
178
|
-
## Sign-in methods
|
|
180
|
+
## Sign-in methods
|
|
179
181
|
|
|
180
|
-
|
|
182
|
+
Sign-in is **tenant-centric**: the app resolves the tenant from the request sub-domain (`tenants.slug`), and the router lists that tenant's IdPs. A tenant has **0..N** OIDC IdPs (mergers, mixed workforces, multi-domain orgs):
|
|
181
183
|
|
|
182
184
|
- **0 IdPs** → OTP only.
|
|
183
|
-
- **1 IdP** → straight redirect, no
|
|
185
|
+
- **1 IdP** → straight redirect, no chooser.
|
|
184
186
|
- **N IdPs** → a chooser (one button per IdP, labeled `displayName`, valued by `auth_domain_id`).
|
|
185
187
|
|
|
186
|
-
The user-bound OTP path is gated by the tenant's `allow_otp` flag, **enforced in the router** (not just hidden in the UI) so an SSO-only tenant can't be bypassed.
|
|
188
|
+
The user-bound **OTP** path is gated by the tenant's `allow_otp` flag, **enforced in the router** (not just hidden in the UI) so an SSO-only tenant can't be bypassed. **OIDC** is handled by a dedicated `@smplcty/auth/oidc` handler (built on [`oauth4webapi`](https://github.com/panva/oauth4webapi)) — its `authorize`/`callback` shape is richer than the OTP two-phase, so it's driven directly rather than through the router. See [ADR-0001](docs/adr/0001-oidc-via-oauth4webapi.md).
|
|
187
189
|
|
|
188
190
|
```ts
|
|
189
191
|
import { createMethodRouter } from '@smplcty/auth';
|
|
190
|
-
import { oidcHandler } from '@smplcty/auth/oidc'; // optional peer:
|
|
192
|
+
import { oidcHandler } from '@smplcty/auth/oidc'; // optional peer: oauth4webapi
|
|
191
193
|
import { twilioVerifyHandler } from '@smplcty/auth/twilio'; // optional peer: @smplcty/twilio
|
|
192
194
|
import { createTwilioVerifyClient } from '@smplcty/twilio';
|
|
193
195
|
|
|
194
196
|
const router = createMethodRouter({
|
|
195
197
|
db: pool,
|
|
196
|
-
handlers: { oidc: oidcHandler() }, // org-bound, by integration_type
|
|
197
198
|
otpHandler: twilioVerifyHandler({ client: createTwilioVerifyClient(cfg) }), // user-bound, tenant-gated
|
|
198
199
|
});
|
|
200
|
+
const oidc = oidcHandler({ clientSecret: (ad) => secrets.get(ad.tenantId) }); // secret from your store, NOT the DB
|
|
199
201
|
|
|
200
202
|
// Sign-in page — app parsed Host → 'acme':
|
|
201
203
|
const opts = await router.signInOptions({ tenantSlug: 'acme' });
|
|
202
204
|
// opts = { tenantId, authDomains: AuthDomain[], otpAllowed: boolean }
|
|
203
|
-
// render: opts.authDomains
|
|
205
|
+
// render: a button per opts.authDomains + an OTP form iff opts.otpAllowed
|
|
204
206
|
|
|
205
|
-
// OIDC — user
|
|
206
|
-
const { redirectUrl } = await
|
|
207
|
-
//
|
|
208
|
-
|
|
207
|
+
// OIDC — user picked an IdP (an AuthDomain from opts.authDomains):
|
|
208
|
+
const { redirectUrl, loginState } = await oidc.initiate(authDomain);
|
|
209
|
+
// persist loginState (signed cookie keyed by loginState.state), redirect to redirectUrl...
|
|
210
|
+
// ...on the provider callback, hand back the stored loginState + the callback URL:
|
|
211
|
+
const user = await oidc.complete({ db: pool, authDomain, callbackUrl, loginState });
|
|
209
212
|
|
|
210
213
|
// OTP (only when opts.otpAllowed):
|
|
211
214
|
await router.initiateOtp({ tenantId: opts.tenantId, identifier: phone });
|
|
@@ -217,9 +220,9 @@ const session = await createSession(pool, {
|
|
|
217
220
|
});
|
|
218
221
|
```
|
|
219
222
|
|
|
220
|
-
Auth **core** depends on neither `
|
|
223
|
+
Auth **core** depends on neither `oauth4webapi` nor Twilio — the handler subpaths do, as **optional peers**. A password-only app installs neither. OIDC is org-bound (`oauth4webapi` does discovery + PKCE + token exchange + `id_token` verification); Twilio Verify is user-bound (phone/email) and integrates the [dev-OTP](#developer-otp--for-devs-whose-phones-cant-receive-sms) fallback automatically.
|
|
221
224
|
|
|
222
|
-
**What stays app-side:** parsing the request `Host` → slug, the chooser/redirect UI, and the OIDC login-state store
|
|
225
|
+
**What stays app-side:** parsing the request `Host` → slug, the chooser/redirect UI, and the OIDC **login-state store** — `oidc.initiate` returns `loginState` (`state`/`nonce`/`codeVerifier`) for you to persist in a short-lived signed cookie keyed by `state`, and hand back to `oidc.complete`. The **`client_secret`** lives in your secret store (passed via the `clientSecret` resolver), **not** in `auth_domains` (which holds only `issuer`/`clientId`).
|
|
223
226
|
|
|
224
227
|
## Developer OTP — for devs whose phones can't receive SMS
|
|
225
228
|
|
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export { withServiceContext } from './with-service-context.js';
|
|
|
11
11
|
export { withTransaction } from '@smplcty/db';
|
|
12
12
|
export { IDENTITY_GUC, setLocal, setActorId, setSessionId, setActiveRole, setPrivileges, setIdentityContext, } from './set-helpers.js';
|
|
13
13
|
export { createMethodRouter } from './methods/router.js';
|
|
14
|
-
export {
|
|
14
|
+
export { OtpNotAllowedError, VerificationFailedError } from './methods/errors.js';
|
|
15
15
|
export type { MethodHandler, MethodInitiateResult, MethodInitiateContext, MethodCompleteContext, ResolvedUser, MethodRouter, MethodRouterOptions, SignInOptions, AuthDomain, } from './methods/types.js';
|
|
16
16
|
export { verifyDevOtp, isDevOtpEnrolled, generateDevOtpSecret, getDevOtpEnrollmentUri } from './dev-otp.js';
|
|
17
17
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,gBAAgB,EAChB,6BAA6B,EAC7B,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAGrB,YAAY,EACV,SAAS,EACT,OAAO,EACP,WAAW,EACX,cAAc,EACd,eAAe,EACf,SAAS,EACT,WAAW,EACX,kBAAkB,EAClB,WAAW,EACX,yBAAyB,EACzB,aAAa,EACb,MAAM,EACN,kBAAkB,GACnB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACx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6B,EAAE,MAAM,wCAAwC,CAAC;AACvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAG5D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAI/D,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAG9C,OAAO,EACL,YAAY,EACZ,QAAQ,EACR,UAAU,EACV,YAAY,EACZ,aAAa,EACb,aAAa,EACb,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAClF,YAAY,EACV,aAAa,EACb,oBAAoB,EACpB,qBAAqB,EACrB,qBAAqB,EACrB,YAAY,EACZ,YAAY,EACZ,mBAAmB,EACnB,aAAa,EACb,UAAU,GACX,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -18,7 +18,7 @@ export { withTransaction } from '@smplcty/db';
|
|
|
18
18
|
export { IDENTITY_GUC, setLocal, setActorId, setSessionId, setActiveRole, setPrivileges, setIdentityContext, } from './set-helpers.js';
|
|
19
19
|
// Authentication methods — pluggable two-phase handlers + auth_domains router.
|
|
20
20
|
export { createMethodRouter } from './methods/router.js';
|
|
21
|
-
export {
|
|
21
|
+
export { OtpNotAllowedError, VerificationFailedError } from './methods/errors.js';
|
|
22
22
|
// Developer OTP — for devs whose phones can't receive Twilio SMS.
|
|
23
23
|
export { verifyDevOtp, isDevOtpEnrolled, generateDevOtpSecret, getDevOtpEnrollmentUri } from './dev-otp.js';
|
|
24
24
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,SAAS;AACT,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,gBAAgB,EAChB,6BAA6B,EAC7B,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAmBrB,oBAAoB;AACpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC9F,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AACvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D,wBAAwB;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,yEAAyE;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAE/D,sEAAsE;AACtE,2EAA2E;AAC3E,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,oEAAoE;AACpE,OAAO,EACL,YAAY,EACZ,QAAQ,EACR,UAAU,EACV,YAAY,EACZ,aAAa,EACb,aAAa,EACb,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAE1B,+EAA+E;AAC/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,SAAS;AACT,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,gBAAgB,EAChB,6BAA6B,EAC7B,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAmBrB,oBAAoB;AACpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC9F,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AACvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D,wBAAwB;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,yEAAyE;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAE/D,sEAAsE;AACtE,2EAA2E;AAC3E,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,oEAAoE;AACpE,OAAO,EACL,YAAY,EACZ,QAAQ,EACR,UAAU,EACV,YAAY,EACZ,aAAa,EACb,aAAa,EACb,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAE1B,+EAA+E;AAC/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAalF,kEAAkE;AAClE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/methods/errors.d.ts
CHANGED
|
@@ -1,18 +1,4 @@
|
|
|
1
1
|
import { AuthError } from '../errors.js';
|
|
2
|
-
/**
|
|
3
|
-
* Thrown by the method router when an identifier matches no `auth_domains`
|
|
4
|
-
* row and no default handler is configured (or a matched row's
|
|
5
|
-
* `integration_type` has no registered handler).
|
|
6
|
-
*/
|
|
7
|
-
export declare class UnknownMethodError extends AuthError {
|
|
8
|
-
readonly name = "UnknownMethodError";
|
|
9
|
-
readonly code: "UNKNOWN_METHOD";
|
|
10
|
-
/** The identifier that couldn't be routed. */
|
|
11
|
-
readonly identifier: string;
|
|
12
|
-
/** The integration_type that had no handler, if a row matched. */
|
|
13
|
-
readonly integrationType?: string;
|
|
14
|
-
constructor(identifier: string, integrationType?: string);
|
|
15
|
-
}
|
|
16
2
|
/**
|
|
17
3
|
* Thrown when OTP (user-bound) sign-in is attempted for a tenant whose
|
|
18
4
|
* `allow_otp` is false — an SSO-only tenant. Enforced in the router so a
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/methods/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC;;;;GAIG;AACH,qBAAa,kBAAmB,SAAQ,SAAS;IAC/C,SAAkB,IAAI,wBAAwB;IAC9C,SAAkB,IAAI,EAAG,
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/methods/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC;;;;GAIG;AACH,qBAAa,kBAAmB,SAAQ,SAAS;IAC/C,SAAkB,IAAI,wBAAwB;IAC9C,SAAkB,IAAI,EAAG,iBAAiB,CAAU;IACpD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;gBAEd,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM;CAI/C;AAED;;;;GAIG;AACH,qBAAa,uBAAwB,SAAQ,SAAS;IACpD,SAAkB,IAAI,6BAA6B;IACnD,SAAkB,IAAI,EAAG,qBAAqB,CAAU;gBAE5C,OAAO,GAAE,MAA8B;CAGpD"}
|
package/dist/methods/errors.js
CHANGED
|
@@ -1,24 +1,4 @@
|
|
|
1
1
|
import { AuthError } from '../errors.js';
|
|
2
|
-
/**
|
|
3
|
-
* Thrown by the method router when an identifier matches no `auth_domains`
|
|
4
|
-
* row and no default handler is configured (or a matched row's
|
|
5
|
-
* `integration_type` has no registered handler).
|
|
6
|
-
*/
|
|
7
|
-
export class UnknownMethodError extends AuthError {
|
|
8
|
-
name = 'UnknownMethodError';
|
|
9
|
-
code = 'UNKNOWN_METHOD';
|
|
10
|
-
/** The identifier that couldn't be routed. */
|
|
11
|
-
identifier;
|
|
12
|
-
/** The integration_type that had no handler, if a row matched. */
|
|
13
|
-
integrationType;
|
|
14
|
-
constructor(identifier, integrationType) {
|
|
15
|
-
super(integrationType
|
|
16
|
-
? `No handler registered for integration_type "${integrationType}"`
|
|
17
|
-
: `No sign-in method matched identifier and no default handler is configured`);
|
|
18
|
-
this.identifier = identifier;
|
|
19
|
-
this.integrationType = integrationType;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
2
|
/**
|
|
23
3
|
* Thrown when OTP (user-bound) sign-in is attempted for a tenant whose
|
|
24
4
|
* `allow_otp` is false — an SSO-only tenant. Enforced in the router so a
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/methods/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC;;;;GAIG;AACH,MAAM,OAAO,kBAAmB,SAAQ,SAAS;IAC7B,IAAI,GAAG,oBAAoB,CAAC;IAC5B,IAAI,GAAG,
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/methods/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC;;;;GAIG;AACH,MAAM,OAAO,kBAAmB,SAAQ,SAAS;IAC7B,IAAI,GAAG,oBAAoB,CAAC;IAC5B,IAAI,GAAG,iBAA0B,CAAC;IAC3C,QAAQ,CAAS;IAE1B,YAAY,QAAgB,EAAE,OAAgB;QAC5C,KAAK,CAAC,OAAO,IAAI,yCAAyC,QAAQ,EAAE,CAAC,CAAC;QACtE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,OAAO,uBAAwB,SAAQ,SAAS;IAClC,IAAI,GAAG,yBAAyB,CAAC;IACjC,IAAI,GAAG,qBAA8B,CAAC;IAExD,YAAY,UAAkB,qBAAqB;QACjD,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF"}
|
|
@@ -1,65 +1,92 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as oauth from 'oauth4webapi';
|
|
2
2
|
import type { Queryable } from '../../types.js';
|
|
3
|
-
import type { AuthDomain,
|
|
3
|
+
import type { AuthDomain, ResolvedUser } from '../types.js';
|
|
4
4
|
/**
|
|
5
|
-
* The OIDC provider config
|
|
6
|
-
* `
|
|
5
|
+
* The OIDC provider config read from `auth_domains.integration_params`.
|
|
6
|
+
* Public values only — the `client_secret` is supplied out-of-band via
|
|
7
|
+
* {@link OidcHandlerOptions.clientSecret} (from the app's secret store), never
|
|
8
|
+
* stored here.
|
|
7
9
|
*/
|
|
8
10
|
export interface OidcParams {
|
|
9
|
-
/** Provider issuer URL
|
|
11
|
+
/** Provider issuer URL (discovery is fetched from `${issuer}/.well-known/openid-configuration`). */
|
|
10
12
|
issuer: string;
|
|
11
13
|
/** This app's OIDC client id. */
|
|
12
14
|
clientId: string;
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/** Where the provider sends the user back. */
|
|
17
|
-
redirectUri?: string;
|
|
18
|
-
/** OAuth scope. Defaults to `openid email`. */
|
|
15
|
+
/** Where the provider sends the user back (must match the app's callback route). */
|
|
16
|
+
redirectUri: string;
|
|
17
|
+
/** OAuth scope. Defaults to `openid email profile`. */
|
|
19
18
|
scope?: string;
|
|
20
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* The transient login state produced by {@link OidcHandler.initiate} that the
|
|
22
|
+
* **app must persist** across the redirect (a short-lived signed cookie keyed
|
|
23
|
+
* by `state`) and hand back to {@link OidcHandler.complete}. The library holds
|
|
24
|
+
* no pre-auth state of its own.
|
|
25
|
+
*/
|
|
26
|
+
export interface OidcLoginState {
|
|
27
|
+
state: string;
|
|
28
|
+
nonce: string;
|
|
29
|
+
codeVerifier: string;
|
|
30
|
+
}
|
|
31
|
+
export interface OidcAuthorization {
|
|
32
|
+
/** Where to redirect the browser to begin the OIDC flow. */
|
|
33
|
+
redirectUrl: string;
|
|
34
|
+
/** Persist this (signed cookie) and pass it to `complete` on callback. */
|
|
35
|
+
loginState: OidcLoginState;
|
|
36
|
+
}
|
|
21
37
|
export interface OidcHandlerOptions {
|
|
22
38
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* Note: v1 does not manage `state` / `nonce` / PKCE here — those need
|
|
28
|
-
* per-request storage the library doesn't own. Supply this hook to add
|
|
29
|
-
* them, or wait for the full auth-code flow in `@smplcty/oidc`.
|
|
39
|
+
* Resolve the confidential client secret for an `auth_domains` row, from the
|
|
40
|
+
* app's secret store (Vercel/SSM) — NOT from the database. Return `undefined`
|
|
41
|
+
* for a public (PKCE-only) client.
|
|
30
42
|
*/
|
|
31
|
-
|
|
32
|
-
authDomain: AuthDomain<OidcParams>;
|
|
33
|
-
identifier?: string;
|
|
34
|
-
}) => string;
|
|
43
|
+
clientSecret?: (authDomain: AuthDomain<OidcParams>) => string | undefined | Promise<string | undefined>;
|
|
35
44
|
/**
|
|
36
|
-
* Provision a user when a verified identity has no existing
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* default roles from `authDomain.integrationParams`). Omit to reject
|
|
40
|
-
* unprovisioned identities.
|
|
45
|
+
* Provision a user when a verified identity has no existing communication
|
|
46
|
+
* method. The app owns the writes + audit attribution (it knows the tenant
|
|
47
|
+
* from `authDomain.tenantId`). Omit to reject unprovisioned identities.
|
|
41
48
|
*/
|
|
42
49
|
provisionUser?: (input: {
|
|
43
50
|
db: Queryable;
|
|
44
51
|
authDomain: AuthDomain<OidcParams>;
|
|
45
|
-
claims:
|
|
52
|
+
claims: ValidatedClaims;
|
|
46
53
|
}) => Promise<ResolvedUser>;
|
|
54
|
+
/** Override `fetch` (tests inject a mock for discovery + token endpoints). */
|
|
55
|
+
fetch?: typeof globalThis.fetch;
|
|
47
56
|
}
|
|
57
|
+
type ValidatedClaims = NonNullable<ReturnType<typeof oauth.getValidatedIdTokenClaims>>;
|
|
48
58
|
/**
|
|
49
|
-
* The org-bound OIDC
|
|
50
|
-
*
|
|
51
|
-
* returned id_token (the `credential`) and matches — or provisions, if a
|
|
52
|
-
* `provisionUser` hook is supplied — the user.
|
|
59
|
+
* The org-bound OIDC handler (opt-in subpath `@smplcty/auth/oidc`), built on
|
|
60
|
+
* **`oauth4webapi`** (ADR-0001). Two phases:
|
|
53
61
|
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
62
|
+
* - `initiate(authDomain)` — discovery + PKCE/state/nonce → an authorization
|
|
63
|
+
* redirect URL and the `loginState` to persist.
|
|
64
|
+
* - `complete({ callbackUrl, loginState })` — validate the callback, exchange
|
|
65
|
+
* the code, verify the `id_token` (signature, claims, nonce) → resolve (or
|
|
66
|
+
* provision) the user.
|
|
67
|
+
*
|
|
68
|
+
* `oauth4webapi` is an **optional peer**; install it to use this subpath.
|
|
58
69
|
*
|
|
59
70
|
* ```ts
|
|
60
|
-
*
|
|
61
|
-
* const
|
|
71
|
+
* const oidc = oidcHandler({ clientSecret: (ad) => secrets.get(ad.tenantId) });
|
|
72
|
+
* const { redirectUrl, loginState } = await oidc.initiate(authDomain);
|
|
73
|
+
* // ...store loginState in a signed cookie keyed by loginState.state, redirect...
|
|
74
|
+
* const user = await oidc.complete({ db: pool, authDomain, callbackUrl, loginState });
|
|
75
|
+
* const session = await createSession(pool, { userCommunicationMethodId: user.userCommunicationMethodId, ttl: '30 days' });
|
|
62
76
|
* ```
|
|
63
77
|
*/
|
|
64
|
-
export
|
|
78
|
+
export interface OidcHandler {
|
|
79
|
+
initiate(authDomain: AuthDomain<OidcParams>, opts?: {
|
|
80
|
+
loginHint?: string;
|
|
81
|
+
}): Promise<OidcAuthorization>;
|
|
82
|
+
complete(input: {
|
|
83
|
+
db: Queryable;
|
|
84
|
+
authDomain: AuthDomain<OidcParams>;
|
|
85
|
+
/** The full callback URL (including the query string from the provider). */
|
|
86
|
+
callbackUrl: string | URL;
|
|
87
|
+
loginState: OidcLoginState;
|
|
88
|
+
}): Promise<ResolvedUser>;
|
|
89
|
+
}
|
|
90
|
+
export declare function oidcHandler(options?: OidcHandlerOptions): OidcHandler;
|
|
91
|
+
export {};
|
|
65
92
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/methods/oidc/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/methods/oidc/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAGtC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE5D;;;;;GAKG;AACH,MAAM,WAAW,UAAU;IACzB,oGAAoG;IACpG,MAAM,EAAE,MAAM,CAAC;IACf,iCAAiC;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,oFAAoF;IACpF,WAAW,EAAE,MAAM,CAAC;IACpB,uDAAuD;IACvD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,iBAAiB;IAChC,4DAA4D;IAC5D,WAAW,EAAE,MAAM,CAAC;IAC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alidInputError } from '../../errors.js';
|
|
3
3
|
import { findUserByCommunicationMethod } from '../../find-user-by-communication-method.js';
|
|
4
4
|
import { VerificationFailedError } from '../errors.js';
|
|
5
5
|
function paramsOf(authDomain) {
|
|
6
|
-
if (
|
|
7
|
-
throw new InvalidInputError(
|
|
6
|
+
if (authDomain.integrationType !== 'oidc') {
|
|
7
|
+
throw new InvalidInputError(`OidcHandler requires integration_type 'oidc', got '${authDomain.integrationType}'`);
|
|
8
8
|
}
|
|
9
|
-
return authDomain;
|
|
10
|
-
}
|
|
11
|
-
function defaultRedirect(authDomain) {
|
|
12
9
|
const p = authDomain.integrationParams;
|
|
13
|
-
if (!p
|
|
14
|
-
throw new InvalidInputError('
|
|
10
|
+
if (!p.issuer || !p.clientId || !p.redirectUri) {
|
|
11
|
+
throw new InvalidInputError('OIDC integration_params must include issuer, clientId, and redirectUri');
|
|
15
12
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
return p;
|
|
14
|
+
}
|
|
15
|
+
// Discovery is cached per issuer for the process lifetime (warm-start friendly).
|
|
16
|
+
// Failures are not cached.
|
|
17
|
+
const discoveryCache = new Map();
|
|
18
|
+
function authorizationServer(issuer, fetchImpl) {
|
|
19
|
+
let cached = discoveryCache.get(issuer);
|
|
20
|
+
if (!cached) {
|
|
21
|
+
const issuerUrl = new URL(issuer);
|
|
22
|
+
const options = fetchImpl ? { [oauth.customFetch]: fetchImpl } : undefined;
|
|
23
|
+
cached = oauth
|
|
24
|
+
.discoveryRequest(issuerUrl, options)
|
|
25
|
+
.then((response) => oauth.processDiscoveryResponse(issuerUrl, response));
|
|
26
|
+
cached.catch(() => discoveryCache.delete(issuer));
|
|
27
|
+
discoveryCache.set(issuer, cached);
|
|
21
28
|
}
|
|
22
|
-
|
|
23
|
-
return url.toString();
|
|
29
|
+
return cached;
|
|
24
30
|
}
|
|
25
|
-
/**
|
|
26
|
-
* The org-bound OIDC method handler (opt-in subpath `@smplcty/auth/oidc`).
|
|
27
|
-
* Phase 1 produces a redirect to the org's IdP; phase 2 verifies the
|
|
28
|
-
* returned id_token (the `credential`) and matches — or provisions, if a
|
|
29
|
-
* `provisionUser` hook is supplied — the user.
|
|
30
|
-
*
|
|
31
|
-
* Org-bound: it requires a matched `auth_domains` row (the per-tenant
|
|
32
|
-
* provider config). Drives `@smplcty/oidc` (an **optional peer**) for the
|
|
33
|
-
* protocol; the `auth_domains` registry lives in auth (own-domain rule),
|
|
34
|
-
* so this handler reads the config and calls `verifyIdToken` itself.
|
|
35
|
-
*
|
|
36
|
-
* ```ts
|
|
37
|
-
* import { oidcHandler } from '@smplcty/auth/oidc';
|
|
38
|
-
* const router = createMethodRouter({ db: pool, handlers: { oidc: oidcHandler() } });
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
31
|
export function oidcHandler(options = {}) {
|
|
42
32
|
return {
|
|
43
|
-
async initiate(
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
33
|
+
async initiate(authDomain, opts = {}) {
|
|
34
|
+
const params = paramsOf(authDomain);
|
|
35
|
+
const as = await authorizationServer(params.issuer, options.fetch);
|
|
36
|
+
if (!as.authorization_endpoint) {
|
|
37
|
+
throw new InvalidInputError(`OIDC issuer ${params.issuer} exposes no authorization_endpoint`);
|
|
38
|
+
}
|
|
39
|
+
const codeVerifier = oauth.generateRandomCodeVerifier();
|
|
40
|
+
const codeChallenge = await oauth.calculatePKCECodeChallenge(codeVerifier);
|
|
41
|
+
const state = oauth.generateRandomState();
|
|
42
|
+
const nonce = oauth.generateRandomNonce();
|
|
43
|
+
const url = new URL(as.authorization_endpoint);
|
|
44
|
+
url.searchParams.set('client_id', params.clientId);
|
|
45
|
+
url.searchParams.set('redirect_uri', params.redirectUri);
|
|
46
|
+
url.searchParams.set('response_type', 'code');
|
|
47
|
+
url.searchParams.set('scope', params.scope ?? 'openid email profile');
|
|
48
|
+
url.searchParams.set('code_challenge', codeChallenge);
|
|
49
|
+
url.searchParams.set('code_challenge_method', 'S256');
|
|
50
|
+
url.searchParams.set('state', state);
|
|
51
|
+
url.searchParams.set('nonce', nonce);
|
|
52
|
+
if (opts.loginHint) {
|
|
53
|
+
url.searchParams.set('login_hint', opts.loginHint);
|
|
54
|
+
}
|
|
55
|
+
return { redirectUrl: url.toString(), loginState: { state, nonce, codeVerifier } };
|
|
49
56
|
},
|
|
50
|
-
async complete(
|
|
51
|
-
const
|
|
52
|
-
const
|
|
57
|
+
async complete({ db, authDomain, callbackUrl, loginState }) {
|
|
58
|
+
const params = paramsOf(authDomain);
|
|
59
|
+
const as = await authorizationServer(params.issuer, options.fetch);
|
|
60
|
+
const client = { client_id: params.clientId };
|
|
61
|
+
const secret = options.clientSecret ? await options.clientSecret(authDomain) : undefined;
|
|
62
|
+
const clientAuth = secret ? oauth.ClientSecretPost(secret) : oauth.None();
|
|
63
|
+
const currentUrl = typeof callbackUrl === 'string' ? new URL(callbackUrl) : callbackUrl;
|
|
64
|
+
const fetchOption = options.fetch ? { [oauth.customFetch]: options.fetch } : undefined;
|
|
53
65
|
let claims;
|
|
54
66
|
try {
|
|
55
|
-
|
|
56
|
-
|
|
67
|
+
const callbackParams = oauth.validateAuthResponse(as, client, currentUrl, loginState.state);
|
|
68
|
+
const response = await oauth.authorizationCodeGrantRequest(as, client, clientAuth, callbackParams, params.redirectUri, loginState.codeVerifier, fetchOption);
|
|
69
|
+
const tokenResponse = await oauth.processAuthorizationCodeResponse(as, client, response, {
|
|
70
|
+
expectedNonce: loginState.nonce,
|
|
71
|
+
requireIdToken: true,
|
|
72
|
+
});
|
|
73
|
+
const validated = oauth.getValidatedIdTokenClaims(tokenResponse);
|
|
74
|
+
if (!validated) {
|
|
75
|
+
throw new VerificationFailedError('OIDC token response carried no id_token');
|
|
76
|
+
}
|
|
77
|
+
claims = validated;
|
|
57
78
|
}
|
|
58
|
-
catch {
|
|
59
|
-
|
|
79
|
+
catch (err) {
|
|
80
|
+
if (err instanceof VerificationFailedError)
|
|
81
|
+
throw err;
|
|
82
|
+
throw new VerificationFailedError('OIDC authorization failed');
|
|
60
83
|
}
|
|
61
|
-
|
|
84
|
+
// Entra often carries the address in preferred_username rather than email.
|
|
85
|
+
const email = typeof claims.email === 'string'
|
|
86
|
+
? claims.email
|
|
87
|
+
: typeof claims.preferred_username === 'string'
|
|
88
|
+
? claims.preferred_username
|
|
89
|
+
: undefined;
|
|
62
90
|
if (!email) {
|
|
63
91
|
throw new VerificationFailedError('OIDC id_token has no email or preferred_username');
|
|
64
92
|
}
|
|
65
|
-
const existing = await findUserByCommunicationMethod(
|
|
93
|
+
const existing = await findUserByCommunicationMethod(db, { channel: 'email', code: email });
|
|
66
94
|
if (existing) {
|
|
67
95
|
return existing;
|
|
68
96
|
}
|
|
69
97
|
if (options.provisionUser) {
|
|
70
|
-
return options.provisionUser({ db
|
|
98
|
+
return options.provisionUser({ db, authDomain, claims });
|
|
71
99
|
}
|
|
72
100
|
throw new VerificationFailedError('No user for verified identity and provisioning is disabled');
|
|
73
101
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/methods/oidc/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/methods/oidc/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,4CAA4C,CAAC;AAE3F,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AA6FvD,SAAS,QAAQ,CAAC,UAA2C;IAC3D,IAAI,UAAU,CAAC,eAAe,KAAK,MAAM,EAAE,CAAC;QAC1C,MAAM,IAAI,iBAAiB,CAAC,sDAAsD,UAAU,CAAC,eAAe,GAAG,CAAC,CAAC;I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AC,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"}
|
package/dist/methods/router.d.ts
CHANGED
|
@@ -1,38 +1,18 @@
|
|
|
1
1
|
import type { MethodRouter, MethodRouterOptions } from './types.js';
|
|
2
2
|
/**
|
|
3
|
-
* Build
|
|
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).
|
|
4
6
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* `integration_type`. The user-bound OTP path is separate and **gated by the
|
|
9
|
-
* tenant's `allow_otp`** — enforced here, not just in the UI.
|
|
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
10
|
*
|
|
11
|
-
* Auth core stays protocol-free: handlers are injected. Install
|
|
12
|
-
* `@smplcty/auth/oidc` / `@smplcty/auth/twilio` (each with its protocol peer)
|
|
13
|
-
* and pass them in.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
11
|
* ```ts
|
|
17
|
-
* const router = createMethodRouter({
|
|
18
|
-
* db: pool,
|
|
19
|
-
* handlers: { oidc: oidcHandler() },
|
|
20
|
-
* otpHandler: twilioVerifyHandler({ client }),
|
|
21
|
-
* });
|
|
22
|
-
*
|
|
23
|
-
* // sign-in page (app already parsed Host → 'acme'):
|
|
12
|
+
* const router = createMethodRouter({ db: pool, otpHandler: twilioVerifyHandler({ client }) });
|
|
24
13
|
* const opts = await router.signInOptions({ tenantSlug: 'acme' });
|
|
25
|
-
* // opts = { tenantId, authDomains: [...], otpAllowed }
|
|
26
|
-
* //
|
|
27
|
-
*
|
|
28
|
-
* // OIDC: user clicked the "Microsoft" button (authDomainId 1)
|
|
29
|
-
* const { redirectUrl } = await router.initiate(1);
|
|
30
|
-
* // ...callback: app loaded the auth_domain it stored against `state`
|
|
31
|
-
* const user = await router.complete(1, idToken);
|
|
32
|
-
*
|
|
33
|
-
* // OTP (only when opts.otpAllowed):
|
|
34
|
-
* await router.initiateOtp({ tenantId: opts.tenantId, identifier: phone });
|
|
35
|
-
* const user2 = await router.completeOtp({ tenantId: opts.tenantId, identifier: phone, credential: code });
|
|
14
|
+
* // opts = { tenantId, authDomains: [...OIDC IdPs...], otpAllowed }
|
|
15
|
+
* // render: a button per authDomain (→ @smplcty/auth/oidc) + an OTP form iff otpAllowed
|
|
36
16
|
* ```
|
|
37
17
|
*/
|
|
38
18
|
export declare function createMethodRouter(options: MethodRouterOptions): MethodRouter;
|
|
@@ -1 +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;
|
|
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"}
|
package/dist/methods/router.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { InvalidInputError } from '../errors.js';
|
|
2
|
-
import { OtpNotAllowedError
|
|
2
|
+
import { OtpNotAllowedError } from './errors.js';
|
|
3
3
|
const SELECT_TENANT_BY_SLUG = `
|
|
4
4
|
SELECT tenant_id AS "tenantId", allow_otp AS "allowOtp"
|
|
5
5
|
FROM tenants
|
|
@@ -12,75 +12,36 @@ const SELECT_ALLOW_OTP = `
|
|
|
12
12
|
WHERE tenant_id = $1 AND deleted_at IS NULL
|
|
13
13
|
LIMIT 1
|
|
14
14
|
`;
|
|
15
|
-
const
|
|
15
|
+
const SELECT_AUTH_DOMAINS_BY_TENANT = `
|
|
16
|
+
SELECT
|
|
16
17
|
auth_domain_id AS "authDomainId",
|
|
17
18
|
tenant_id AS "tenantId",
|
|
18
19
|
display_name AS "displayName",
|
|
19
20
|
integration_type AS "integrationType",
|
|
20
21
|
integration_params AS "integrationParams"
|
|
21
|
-
`;
|
|
22
|
-
const SELECT_AUTH_DOMAINS_BY_TENANT = `
|
|
23
|
-
SELECT ${AUTH_DOMAIN_COLUMNS}
|
|
24
22
|
FROM auth_domains
|
|
25
23
|
WHERE tenant_id = $1 AND deleted_at IS NULL
|
|
26
24
|
ORDER BY display_name
|
|
27
25
|
`;
|
|
28
|
-
const SELECT_AUTH_DOMAIN_BY_ID = `
|
|
29
|
-
SELECT ${AUTH_DOMAIN_COLUMNS}
|
|
30
|
-
FROM auth_domains
|
|
31
|
-
WHERE auth_domain_id = $1 AND deleted_at IS NULL
|
|
32
|
-
LIMIT 1
|
|
33
|
-
`;
|
|
34
26
|
/**
|
|
35
|
-
* Build
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* Host to a slug). The router lists that tenant's `auth_domains` and dispatches
|
|
39
|
-
* the chosen one (by `auth_domain_id`) to the handler registered for its
|
|
40
|
-
* `integration_type`. The user-bound OTP path is separate and **gated by the
|
|
41
|
-
* tenant's `allow_otp`** — enforced here, not just in the UI.
|
|
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).
|
|
42
30
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
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.
|
|
46
34
|
*
|
|
47
|
-
* @example
|
|
48
35
|
* ```ts
|
|
49
|
-
* const router = createMethodRouter({
|
|
50
|
-
* db: pool,
|
|
51
|
-
* handlers: { oidc: oidcHandler() },
|
|
52
|
-
* otpHandler: twilioVerifyHandler({ client }),
|
|
53
|
-
* });
|
|
54
|
-
*
|
|
55
|
-
* // sign-in page (app already parsed Host → 'acme'):
|
|
36
|
+
* const router = createMethodRouter({ db: pool, otpHandler: twilioVerifyHandler({ client }) });
|
|
56
37
|
* const opts = await router.signInOptions({ tenantSlug: 'acme' });
|
|
57
|
-
* // opts = { tenantId, authDomains: [...], otpAllowed }
|
|
58
|
-
* //
|
|
59
|
-
*
|
|
60
|
-
* // OIDC: user clicked the "Microsoft" button (authDomainId 1)
|
|
61
|
-
* const { redirectUrl } = await router.initiate(1);
|
|
62
|
-
* // ...callback: app loaded the auth_domain it stored against `state`
|
|
63
|
-
* const user = await router.complete(1, idToken);
|
|
64
|
-
*
|
|
65
|
-
* // OTP (only when opts.otpAllowed):
|
|
66
|
-
* await router.initiateOtp({ tenantId: opts.tenantId, identifier: phone });
|
|
67
|
-
* const user2 = await router.completeOtp({ tenantId: opts.tenantId, identifier: phone, credential: code });
|
|
38
|
+
* // opts = { tenantId, authDomains: [...OIDC IdPs...], otpAllowed }
|
|
39
|
+
* // render: a button per authDomain (→ @smplcty/auth/oidc) + an OTP form iff otpAllowed
|
|
68
40
|
* ```
|
|
69
41
|
*/
|
|
70
42
|
export function createMethodRouter(options) {
|
|
71
|
-
const {
|
|
72
|
-
const q = db;
|
|
73
|
-
async function loadAuthDomain(authDomainId) {
|
|
74
|
-
if (!Number.isInteger(authDomainId) || authDomainId <= 0) {
|
|
75
|
-
throw new InvalidInputError('authDomainId must be a positive integer');
|
|
76
|
-
}
|
|
77
|
-
const { rows } = await q.query(SELECT_AUTH_DOMAIN_BY_ID, [authDomainId]);
|
|
78
|
-
const authDomain = rows[0];
|
|
79
|
-
if (!authDomain) {
|
|
80
|
-
throw new UnknownMethodError(String(authDomainId));
|
|
81
|
-
}
|
|
82
|
-
return authDomain;
|
|
83
|
-
}
|
|
43
|
+
const { otpHandler } = options;
|
|
44
|
+
const q = options.db;
|
|
84
45
|
async function assertOtpAllowed(tenantId) {
|
|
85
46
|
if (!Number.isInteger(tenantId) || tenantId <= 0) {
|
|
86
47
|
throw new InvalidInputError('tenantId must be a positive integer');
|
|
@@ -112,31 +73,12 @@ export function createMethodRouter(options) {
|
|
|
112
73
|
otpAllowed: tenant.allowOtp === true && !!otpHandler,
|
|
113
74
|
};
|
|
114
75
|
},
|
|
115
|
-
async initiate(authDomainId) {
|
|
116
|
-
const authDomain = await loadAuthDomain(authDomainId);
|
|
117
|
-
const handler = handlers[authDomain.integrationType];
|
|
118
|
-
if (!handler) {
|
|
119
|
-
throw new UnknownMethodError(String(authDomainId), authDomain.integrationType);
|
|
120
|
-
}
|
|
121
|
-
return handler.initiate({ db, authDomain });
|
|
122
|
-
},
|
|
123
|
-
async complete(authDomainId, credential) {
|
|
124
|
-
if (typeof credential !== 'string' || credential.length === 0) {
|
|
125
|
-
throw new InvalidInputError('credential must be a non-empty string');
|
|
126
|
-
}
|
|
127
|
-
const authDomain = await loadAuthDomain(authDomainId);
|
|
128
|
-
const handler = handlers[authDomain.integrationType];
|
|
129
|
-
if (!handler) {
|
|
130
|
-
throw new UnknownMethodError(String(authDomainId), authDomain.integrationType);
|
|
131
|
-
}
|
|
132
|
-
return handler.complete({ db, authDomain, credential });
|
|
133
|
-
},
|
|
134
76
|
async initiateOtp({ tenantId, identifier }) {
|
|
135
77
|
if (typeof identifier !== 'string' || identifier.length === 0) {
|
|
136
78
|
throw new InvalidInputError('identifier must be a non-empty string');
|
|
137
79
|
}
|
|
138
80
|
await assertOtpAllowed(tenantId);
|
|
139
|
-
return otpHandler.initiate({ db
|
|
81
|
+
return otpHandler.initiate({ db: options.db, identifier });
|
|
140
82
|
},
|
|
141
83
|
async completeOtp({ tenantId, identifier, credential }) {
|
|
142
84
|
if (typeof identifier !== 'string' || identifier.length === 0) {
|
|
@@ -146,7 +88,7 @@ export function createMethodRouter(options) {
|
|
|
146
88
|
throw new InvalidInputError('credential must be a non-empty string');
|
|
147
89
|
}
|
|
148
90
|
await assertOtpAllowed(tenantId);
|
|
149
|
-
return otpHandler.complete({ db
|
|
91
|
+
return otpHandler.complete({ db: options.db, identifier, credential });
|
|
150
92
|
},
|
|
151
93
|
};
|
|
152
94
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/methods/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,EAAE,kBAAkB,EAAE,
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/methods/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAGjD,MAAM,qBAAqB,GAAG;;;;;CAK7B,CAAC;AAEF,MAAM,gBAAgB,GAAG;;;;;CAKxB,CAAC;AAEF,MAAM,6BAA6B,GAAG;;;;;;;;;;CAUrC,CAAC;AAOF;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAA4B;IAC7D,MAAM,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC;IAC/B,MAAM,CAAC,GA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|
3
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
|
|
5
|
-
*
|
|
6
|
-
* `
|
|
7
|
-
* label when a tenant has more than one.
|
|
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.
|
|
8
7
|
*
|
|
9
8
|
* @typeParam TParams - Shape of `integration_params`. Defaults to an open
|
|
10
9
|
* record; narrow it in a handler.
|
|
@@ -16,7 +15,7 @@ export interface AuthDomain<TParams = Record<string, unknown>> {
|
|
|
16
15
|
readonly displayName: string;
|
|
17
16
|
/** Selects the handler — e.g. 'oidc'. */
|
|
18
17
|
readonly integrationType: string;
|
|
19
|
-
/** Provider config
|
|
18
|
+
/** Provider config. Handler-specific. */
|
|
20
19
|
readonly integrationParams: TParams;
|
|
21
20
|
}
|
|
22
21
|
/**
|
|
@@ -27,44 +26,34 @@ export interface ResolvedUser {
|
|
|
27
26
|
readonly userCommunicationMethodId: number;
|
|
28
27
|
}
|
|
29
28
|
/**
|
|
30
|
-
* Result of a handler's `initiate` phase
|
|
31
|
-
* (user-bound methods) or the caller should redirect (org-bound methods).
|
|
29
|
+
* Result of a user-bound handler's `initiate` phase.
|
|
32
30
|
*/
|
|
33
31
|
export type MethodInitiateResult = {
|
|
34
32
|
readonly otpSent: true;
|
|
35
|
-
} | {
|
|
36
|
-
readonly redirectUrl: string;
|
|
37
33
|
};
|
|
38
34
|
/**
|
|
39
|
-
* Context handed to a handler's `initiate`.
|
|
35
|
+
* Context handed to a user-bound (OTP) handler's `initiate`.
|
|
40
36
|
*/
|
|
41
37
|
export interface MethodInitiateContext {
|
|
42
38
|
/** A connection or pool the handler may query (e.g. to find the user). */
|
|
43
39
|
readonly db: Queryable;
|
|
44
|
-
/** The
|
|
45
|
-
|
|
46
|
-
readonly authDomain: AuthDomain | null;
|
|
47
|
-
/** The identifier the user entered (email/phone). Required by user-bound
|
|
48
|
-
* handlers; org-bound handlers may use it as a `login_hint`. */
|
|
49
|
-
readonly identifier?: string;
|
|
40
|
+
/** The identifier the user entered (email/phone). */
|
|
41
|
+
readonly identifier: string;
|
|
50
42
|
}
|
|
51
43
|
/**
|
|
52
|
-
* Context handed to a handler's `complete`. Adds the
|
|
53
|
-
* submitted back: an OTP code (user-bound) or an OIDC id_token (org-bound).
|
|
44
|
+
* Context handed to a user-bound handler's `complete`. Adds the submitted OTP.
|
|
54
45
|
*/
|
|
55
46
|
export interface MethodCompleteContext extends MethodInitiateContext {
|
|
56
47
|
readonly credential: string;
|
|
57
48
|
}
|
|
58
49
|
/**
|
|
59
|
-
* A pluggable two-phase
|
|
60
|
-
*
|
|
61
|
-
*
|
|
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.)
|
|
62
54
|
*/
|
|
63
55
|
export interface MethodHandler {
|
|
64
|
-
/** Phase 1: send an OTP, or produce a redirect URL. */
|
|
65
56
|
initiate(ctx: MethodInitiateContext): Promise<MethodInitiateResult>;
|
|
66
|
-
/** Phase 2: verify the submitted credential and resolve (provisioning
|
|
67
|
-
* if the handler's policy allows) the user. */
|
|
68
57
|
complete(ctx: MethodCompleteContext): Promise<ResolvedUser>;
|
|
69
58
|
}
|
|
70
59
|
/**
|
|
@@ -73,29 +62,26 @@ export interface MethodHandler {
|
|
|
73
62
|
export interface MethodRouterOptions {
|
|
74
63
|
/** Used to look up tenants and their `auth_domains`. */
|
|
75
64
|
db: Queryable;
|
|
76
|
-
/** Org-bound handlers keyed by `auth_domains.integration_type` (e.g. `{ oidc }`). */
|
|
77
|
-
handlers: Record<string, MethodHandler>;
|
|
78
65
|
/** The user-bound OTP handler (e.g. a `TwilioVerifyHandler`). Offered only
|
|
79
66
|
* when the tenant's `allow_otp` is true; omit it for an SSO-only product. */
|
|
80
67
|
otpHandler?: MethodHandler;
|
|
81
68
|
}
|
|
82
69
|
/**
|
|
83
|
-
* The sign-in methods available for a tenant — what the app renders
|
|
84
|
-
* sign-in surface.
|
|
70
|
+
* The sign-in methods available for a tenant — what the app renders.
|
|
85
71
|
*/
|
|
86
72
|
export interface SignInOptions {
|
|
87
73
|
readonly tenantId: number;
|
|
88
74
|
/** The tenant's org-bound IdPs. Zero ⇒ OTP only; one ⇒ straight redirect;
|
|
89
|
-
* several ⇒ a chooser
|
|
75
|
+
* several ⇒ a chooser. Drive OIDC with `@smplcty/auth/oidc`. */
|
|
90
76
|
readonly authDomains: readonly AuthDomain[];
|
|
91
77
|
/** Whether to offer OTP: the tenant's `allow_otp` AND an `otpHandler` is set. */
|
|
92
78
|
readonly otpAllowed: boolean;
|
|
93
79
|
}
|
|
94
80
|
/**
|
|
95
|
-
* Tenant-centric sign-in
|
|
96
|
-
* app parses the Host to a slug)
|
|
97
|
-
*
|
|
98
|
-
*
|
|
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.
|
|
99
85
|
*/
|
|
100
86
|
export interface MethodRouter {
|
|
101
87
|
/** The sign-in methods for the tenant owning `tenantSlug`, or null if no
|
|
@@ -103,10 +89,6 @@ export interface MethodRouter {
|
|
|
103
89
|
signInOptions(input: {
|
|
104
90
|
tenantSlug: string;
|
|
105
91
|
}): Promise<SignInOptions | null>;
|
|
106
|
-
/** Org-bound phase 1 for a chosen `auth_domains` row (e.g. OIDC redirect). */
|
|
107
|
-
initiate(authDomainId: number): Promise<MethodInitiateResult>;
|
|
108
|
-
/** Org-bound phase 2 for a chosen `auth_domains` row (verify id_token, etc.). */
|
|
109
|
-
complete(authDomainId: number, credential: string): Promise<ResolvedUser>;
|
|
110
92
|
/** User-bound (OTP) phase 1. Throws {@link OtpNotAllowedError} when the
|
|
111
93
|
* tenant disallows OTP. */
|
|
112
94
|
initiateOtp(input: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/methods/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/methods/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C;;;;;;;;GAQG;AACH,MAAM,WAAW,UAAU,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAC3D,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,wEAAwE;IACxE,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,yCAAyC;IACzC,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,yCAAyC;IACzC,QAAQ,CAAC,iBAAiB,EAAE,OAAO,CAAC;CACrC;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,yBAAyB,EAAE,MAAM,CAAC;CAC5C;AAED;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAA;CAAE,CAAC;AAE9D;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,EAAE,SAAS,CAAC;IACvB,qDAAqD;IACrD,QA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
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smplcty/auth",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
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",
|
|
@@ -85,40 +85,41 @@
|
|
|
85
85
|
]
|
|
86
86
|
},
|
|
87
87
|
"peerDependencies": {
|
|
88
|
-
"@smplcty/oidc": ">=0.2.0",
|
|
89
88
|
"@smplcty/twilio": ">=0.1.0",
|
|
89
|
+
"oauth4webapi": ">=3.0.0",
|
|
90
90
|
"pg": ">=8.0.0"
|
|
91
91
|
},
|
|
92
92
|
"peerDependenciesMeta": {
|
|
93
|
-
"@smplcty/
|
|
93
|
+
"@smplcty/twilio": {
|
|
94
94
|
"optional": true
|
|
95
95
|
},
|
|
96
|
-
"
|
|
96
|
+
"oauth4webapi": {
|
|
97
97
|
"optional": true
|
|
98
98
|
}
|
|
99
99
|
},
|
|
100
100
|
"devDependencies": {
|
|
101
101
|
"@eslint/js": "^10.0.1",
|
|
102
102
|
"@release-it/keep-a-changelog": "^7.1.0",
|
|
103
|
-
"@smplcty/oidc": "^0.2.0",
|
|
104
103
|
"@smplcty/schema-flow": "^0.11.2",
|
|
105
104
|
"@smplcty/twilio": "^0.1.1",
|
|
106
|
-
"@testcontainers/postgresql": "^12.0.
|
|
107
|
-
"@types/node": "^25.
|
|
105
|
+
"@testcontainers/postgresql": "^12.0.2",
|
|
106
|
+
"@types/node": "^25.9.3",
|
|
108
107
|
"@types/pg": "^8.20.0",
|
|
109
|
-
"eslint": "^10.
|
|
108
|
+
"eslint": "^10.5.0",
|
|
110
109
|
"husky": "^9.1.7",
|
|
110
|
+
"jose": "^6.2.3",
|
|
111
111
|
"lint-staged": "^17.0.7",
|
|
112
|
-
"
|
|
112
|
+
"oauth4webapi": "^3.8.6",
|
|
113
|
+
"pg": "^8.21.0",
|
|
113
114
|
"prettier": "^3.8.4",
|
|
114
115
|
"release-it": "^20.2.0",
|
|
115
|
-
"typescript": "^6.0.
|
|
116
|
-
"typescript-eslint": "^8.
|
|
117
|
-
"vitest": "^4.1.
|
|
116
|
+
"typescript": "^6.0.3",
|
|
117
|
+
"typescript-eslint": "^8.61.0",
|
|
118
|
+
"vitest": "^4.1.8"
|
|
118
119
|
},
|
|
119
120
|
"dependencies": {
|
|
120
121
|
"@smplcty/db": "^4.2.0",
|
|
121
122
|
"@smplcty/schema-std": "^0.0.1",
|
|
122
|
-
"otplib": "^13.4.
|
|
123
|
+
"otplib": "^13.4.1"
|
|
123
124
|
}
|
|
124
125
|
}
|