@smplcty/auth 11.0.0 → 11.1.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 +42 -1
- package/dist/methods/email-code/index.d.ts +44 -0
- package/dist/methods/email-code/index.d.ts.map +1 -0
- package/dist/methods/email-code/index.js +150 -0
- package/dist/methods/email-code/index.js.map +1 -0
- package/package.json +6 -1
- package/schema/tables/email_sign_in_codes.yaml +39 -0
package/README.md
CHANGED
|
@@ -252,6 +252,46 @@ Auth **core** depends on neither `oauth4webapi` nor Twilio — the handler subpa
|
|
|
252
252
|
|
|
253
253
|
**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`). The [`@smplcty/auth/http`](#http-transport--smpltyauthhttp) tier handles the routes, cookies, and login-state store for you — drop it in and you write only the slug parser + the sign-in screen.
|
|
254
254
|
|
|
255
|
+
### Emailed codes — `@smplcty/auth/email-code`
|
|
256
|
+
|
|
257
|
+
`emailCodeHandler` (`@smplcty/auth/email-code`) signs users in with a code you
|
|
258
|
+
email them. The library issues, stores and checks the code; you deliver it with
|
|
259
|
+
`sendCode`, through Amazon SES or any other provider, so auth depends on no mail
|
|
260
|
+
provider. It is the alternative to Twilio Verify's email channel, which works
|
|
261
|
+
only through SendGrid.
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
import { emailCodeHandler } from '@smplcty/auth/email-code';
|
|
265
|
+
|
|
266
|
+
const otpHandler = emailCodeHandler({
|
|
267
|
+
codeSecret: process.env.EMAIL_CODE_SECRET, // at least 32 bytes, never in the database
|
|
268
|
+
sendCode: ({ to, code }) => sendSignInEmail(to, code), // your message, your provider
|
|
269
|
+
});
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
What it enforces, which Twilio Verify would otherwise do for you:
|
|
273
|
+
|
|
274
|
+
- A code is six random digits and is valid for **10 minutes**.
|
|
275
|
+
- A code works **once**, and dies after **5 wrong guesses**, even if the next one is right.
|
|
276
|
+
- Sending a new code **retires the previous one** for good.
|
|
277
|
+
- At most **5 codes per address per 10 minutes**. A request over the limit sends nothing and the last code stays valid.
|
|
278
|
+
- Only an **HMAC** of each code is stored (`email_sign_in_codes`), keyed by `codeSecret`, so a database read alone can't brute-force the million possible codes. Rows are kept after use, expiry or replacement.
|
|
279
|
+
- Like the Twilio handler, it never provisions, reports `otpSent` for an unknown address without sending, and accepts a [dev-OTP](#developer-otp--for-devs-whose-phones-cant-receive-sms) code first, sending nothing to an enrolled user.
|
|
280
|
+
|
|
281
|
+
The router takes one `otpHandler`. To send email codes yourself and SMS through
|
|
282
|
+
Twilio, pick the handler by identifier:
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
const email = emailCodeHandler({ codeSecret, sendCode });
|
|
286
|
+
const sms = twilioVerifyHandler({ client });
|
|
287
|
+
const pick = (identifier: string) => (identifier.includes('@') ? email : sms);
|
|
288
|
+
|
|
289
|
+
const otpHandler: MethodHandler = {
|
|
290
|
+
initiate: (ctx) => pick(ctx.identifier).initiate(ctx),
|
|
291
|
+
complete: (ctx) => pick(ctx.identifier).complete(ctx),
|
|
292
|
+
};
|
|
293
|
+
```
|
|
294
|
+
|
|
255
295
|
## HTTP transport — `@smplcty/auth/http`
|
|
256
296
|
|
|
257
297
|
The pieces above are primitives. **`@smplcty/auth/http`** wires them into
|
|
@@ -487,7 +527,8 @@ The library ships its schema as schema-flow YAML inside the package at `@smplcty
|
|
|
487
527
|
│ ├── user_communication_methods.yaml # + audit; qualifier (optional consumer-defined label)
|
|
488
528
|
│ ├── auth_domains.yaml # + audit; tenant's IdP(s), 1:N, resolved by id (display_name = button)
|
|
489
529
|
│ ├── sessions.yaml # PK = token hash; last_seen_at; geo
|
|
490
|
-
│
|
|
530
|
+
│ ├── dev_otp_enrollments.yaml
|
|
531
|
+
│ └── email_sign_in_codes.yaml # emailCodeHandler's codes: HMAC only, never deleted
|
|
491
532
|
├── functions/
|
|
492
533
|
│ ├── resolve_session.yaml # pure resolver (SECURITY DEFINER)
|
|
493
534
|
│ └── current_user_id.yaml # reads app.actor_id
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { MethodHandler } from '../types.js';
|
|
2
|
+
/** How long an emailed code stays valid. */
|
|
3
|
+
export declare const EMAIL_CODE_TTL_MINUTES = 10;
|
|
4
|
+
/** Wrong guesses a code survives; the next guess fails even if it is right. */
|
|
5
|
+
export declare const EMAIL_CODE_MAX_FAILED_ATTEMPTS = 5;
|
|
6
|
+
/** Codes sent to one address per {@link EMAIL_CODE_TTL_MINUTES}; further requests send nothing. */
|
|
7
|
+
export declare const EMAIL_CODE_MAX_SENDS = 5;
|
|
8
|
+
/** The message the app delivers: the address and the code to put in it. */
|
|
9
|
+
export interface EmailCode {
|
|
10
|
+
to: string;
|
|
11
|
+
code: string;
|
|
12
|
+
}
|
|
13
|
+
export interface EmailCodeHandlerOptions {
|
|
14
|
+
/** Keys the stored code hashes. At least 32 bytes, kept out of the database. */
|
|
15
|
+
codeSecret: string | Buffer;
|
|
16
|
+
/** Delivers the code, e.g. through Amazon SES. The app owns the message. */
|
|
17
|
+
sendCode: (message: EmailCode) => Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The user-bound emailed-code method handler (subpath `@smplcty/auth/email-code`).
|
|
21
|
+
* The library issues, stores and checks the code; the app delivers it through
|
|
22
|
+
* `sendCode`, so auth depends on no mail provider.
|
|
23
|
+
*
|
|
24
|
+
* A code is six random digits, valid for {@link EMAIL_CODE_TTL_MINUTES} minutes,
|
|
25
|
+
* single-use, and dead after {@link EMAIL_CODE_MAX_FAILED_ATTEMPTS} wrong guesses.
|
|
26
|
+
* Sending a new code retires the previous one. At most
|
|
27
|
+
* {@link EMAIL_CODE_MAX_SENDS} are sent to an address per
|
|
28
|
+
* {@link EMAIL_CODE_TTL_MINUTES} minutes. Only an HMAC of each code is stored,
|
|
29
|
+
* in `email_sign_in_codes`, and rows are never deleted.
|
|
30
|
+
*
|
|
31
|
+
* Like the Twilio handler it never provisions, reports `otpSent` for an unknown
|
|
32
|
+
* address or a refused send without sending, and accepts a dev-OTP code first.
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { emailCodeHandler } from '@smplcty/auth/email-code';
|
|
36
|
+
*
|
|
37
|
+
* const handler = emailCodeHandler({
|
|
38
|
+
* codeSecret: process.env.EMAIL_CODE_SECRET!,
|
|
39
|
+
* sendCode: ({ to, code }) => sendSignInEmail(to, code),
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function emailCodeHandler(options: EmailCodeHandlerOptions): MethodHandler;
|
|
44
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/methods/email-code/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAEV,aAAa,EAId,MAAM,aAAa,CAAC;AAErB,4CAA4C;AAC5C,eAAO,MAAM,sBAAsB,KAAK,CAAC;AACzC,+EAA+E;AAC/E,eAAO,MAAM,8BAA8B,IAAI,CAAC;AAChD,mGAAmG;AACnG,eAAO,MAAM,oBAAoB,IAAI,CAAC;AAItC,2EAA2E;AAC3E,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,uBAAuB;IACtC,gFAAgF;IAChF,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,4EAA4E;IAC5E,QAAQ,EAAE,CAAC,OAAO,EAAE,SAAS,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACjD;AAmED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,uBAAuB,GAAG,aAAa,CA4DhF"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { createHmac, randomInt, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { InvalidInputError } from '../../errors.js';
|
|
3
|
+
import { findUserByCommunicationMethod } from '../../find-user-by-communication-method.js';
|
|
4
|
+
import { isDevOtpEnrolled, verifyDevOtp } from '../../dev-otp.js';
|
|
5
|
+
import { VerificationFailedError } from '../errors.js';
|
|
6
|
+
/** How long an emailed code stays valid. */
|
|
7
|
+
export const EMAIL_CODE_TTL_MINUTES = 10;
|
|
8
|
+
/** Wrong guesses a code survives; the next guess fails even if it is right. */
|
|
9
|
+
export const EMAIL_CODE_MAX_FAILED_ATTEMPTS = 5;
|
|
10
|
+
/** Codes sent to one address per {@link EMAIL_CODE_TTL_MINUTES}; further requests send nothing. */
|
|
11
|
+
export const EMAIL_CODE_MAX_SENDS = 5;
|
|
12
|
+
const MIN_SECRET_BYTES = 32;
|
|
13
|
+
// Issues a code only while the address is under the send limit, retiring the
|
|
14
|
+
// previous live code in the same statement so a refused send keeps it alive.
|
|
15
|
+
const ISSUE_CODE = `
|
|
16
|
+
WITH allowed AS (
|
|
17
|
+
SELECT count(*) < $4 AS ok
|
|
18
|
+
FROM email_sign_in_codes
|
|
19
|
+
WHERE user_communication_method_id = $1
|
|
20
|
+
AND created_at > now() - make_interval(mins => $3)
|
|
21
|
+
),
|
|
22
|
+
superseded AS (
|
|
23
|
+
UPDATE email_sign_in_codes
|
|
24
|
+
SET superseded_at = now()
|
|
25
|
+
WHERE user_communication_method_id = $1
|
|
26
|
+
AND used_at IS NULL
|
|
27
|
+
AND superseded_at IS NULL
|
|
28
|
+
AND (SELECT ok FROM allowed)
|
|
29
|
+
)
|
|
30
|
+
INSERT INTO email_sign_in_codes (user_communication_method_id, code_hash, expires_at)
|
|
31
|
+
SELECT $1, $2, now() + make_interval(mins => $3)
|
|
32
|
+
WHERE (SELECT ok FROM allowed)
|
|
33
|
+
RETURNING email_sign_in_code_id
|
|
34
|
+
`;
|
|
35
|
+
const LIVE_CODE = `
|
|
36
|
+
WHERE user_communication_method_id = $1
|
|
37
|
+
AND used_at IS NULL
|
|
38
|
+
AND superseded_at IS NULL
|
|
39
|
+
AND expires_at > now()
|
|
40
|
+
AND failed_attempts < $2
|
|
41
|
+
`;
|
|
42
|
+
const SELECT_LIVE_CODE = `
|
|
43
|
+
SELECT email_sign_in_code_id AS "id", code_hash AS "codeHash"
|
|
44
|
+
FROM email_sign_in_codes
|
|
45
|
+
${LIVE_CODE}
|
|
46
|
+
ORDER BY created_at DESC
|
|
47
|
+
LIMIT 1
|
|
48
|
+
`;
|
|
49
|
+
// Re-checks liveness so two concurrent correct guesses cannot both use the code.
|
|
50
|
+
const USE_CODE = `
|
|
51
|
+
UPDATE email_sign_in_codes
|
|
52
|
+
SET used_at = now()
|
|
53
|
+
${LIVE_CODE}
|
|
54
|
+
AND email_sign_in_code_id = $3
|
|
55
|
+
RETURNING email_sign_in_code_id
|
|
56
|
+
`;
|
|
57
|
+
const RECORD_FAILED_ATTEMPT = `
|
|
58
|
+
UPDATE email_sign_in_codes
|
|
59
|
+
SET failed_attempts = failed_attempts + 1
|
|
60
|
+
WHERE email_sign_in_code_id = $1
|
|
61
|
+
`;
|
|
62
|
+
function requireIdentifier(identifier) {
|
|
63
|
+
if (typeof identifier !== 'string' || identifier.length === 0) {
|
|
64
|
+
throw new InvalidInputError('the email code handler requires a non-empty identifier');
|
|
65
|
+
}
|
|
66
|
+
return identifier;
|
|
67
|
+
}
|
|
68
|
+
async function findUser(db, identifier) {
|
|
69
|
+
return findUserByCommunicationMethod(db, { channel: 'email', code: identifier });
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The user-bound emailed-code method handler (subpath `@smplcty/auth/email-code`).
|
|
73
|
+
* The library issues, stores and checks the code; the app delivers it through
|
|
74
|
+
* `sendCode`, so auth depends on no mail provider.
|
|
75
|
+
*
|
|
76
|
+
* A code is six random digits, valid for {@link EMAIL_CODE_TTL_MINUTES} minutes,
|
|
77
|
+
* single-use, and dead after {@link EMAIL_CODE_MAX_FAILED_ATTEMPTS} wrong guesses.
|
|
78
|
+
* Sending a new code retires the previous one. At most
|
|
79
|
+
* {@link EMAIL_CODE_MAX_SENDS} are sent to an address per
|
|
80
|
+
* {@link EMAIL_CODE_TTL_MINUTES} minutes. Only an HMAC of each code is stored,
|
|
81
|
+
* in `email_sign_in_codes`, and rows are never deleted.
|
|
82
|
+
*
|
|
83
|
+
* Like the Twilio handler it never provisions, reports `otpSent` for an unknown
|
|
84
|
+
* address or a refused send without sending, and accepts a dev-OTP code first.
|
|
85
|
+
*
|
|
86
|
+
* ```ts
|
|
87
|
+
* import { emailCodeHandler } from '@smplcty/auth/email-code';
|
|
88
|
+
*
|
|
89
|
+
* const handler = emailCodeHandler({
|
|
90
|
+
* codeSecret: process.env.EMAIL_CODE_SECRET!,
|
|
91
|
+
* sendCode: ({ to, code }) => sendSignInEmail(to, code),
|
|
92
|
+
* });
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export function emailCodeHandler(options) {
|
|
96
|
+
const { codeSecret, sendCode } = options;
|
|
97
|
+
if (Buffer.byteLength(codeSecret) < MIN_SECRET_BYTES) {
|
|
98
|
+
throw new InvalidInputError(`codeSecret must be at least ${MIN_SECRET_BYTES} bytes`);
|
|
99
|
+
}
|
|
100
|
+
const hashCode = (userCommunicationMethodId, code) => createHmac('sha256', codeSecret).update(`${userCommunicationMethodId}:${code}`).digest();
|
|
101
|
+
return {
|
|
102
|
+
async initiate(ctx) {
|
|
103
|
+
const identifier = requireIdentifier(ctx.identifier);
|
|
104
|
+
const user = await findUser(ctx.db, identifier);
|
|
105
|
+
if (!user || (await isDevOtpEnrolled(ctx.db, user.userCommunicationMethodId))) {
|
|
106
|
+
return { otpSent: true };
|
|
107
|
+
}
|
|
108
|
+
const code = String(randomInt(0, 1_000_000)).padStart(6, '0');
|
|
109
|
+
const { rowCount } = await ctx.db.query(ISSUE_CODE, [
|
|
110
|
+
user.userCommunicationMethodId,
|
|
111
|
+
hashCode(user.userCommunicationMethodId, code).toString('hex'),
|
|
112
|
+
EMAIL_CODE_TTL_MINUTES,
|
|
113
|
+
EMAIL_CODE_MAX_SENDS,
|
|
114
|
+
]);
|
|
115
|
+
if (rowCount) {
|
|
116
|
+
await sendCode({ to: identifier, code });
|
|
117
|
+
}
|
|
118
|
+
return { otpSent: true };
|
|
119
|
+
},
|
|
120
|
+
async complete(ctx) {
|
|
121
|
+
const identifier = requireIdentifier(ctx.identifier);
|
|
122
|
+
const user = await findUser(ctx.db, identifier);
|
|
123
|
+
if (!user) {
|
|
124
|
+
throw new VerificationFailedError();
|
|
125
|
+
}
|
|
126
|
+
const ucmId = user.userCommunicationMethodId;
|
|
127
|
+
if (await verifyDevOtp(ctx.db, ucmId, ctx.credential)) {
|
|
128
|
+
return user;
|
|
129
|
+
}
|
|
130
|
+
const { rows } = await ctx.db.query(SELECT_LIVE_CODE, [
|
|
131
|
+
ucmId,
|
|
132
|
+
EMAIL_CODE_MAX_FAILED_ATTEMPTS,
|
|
133
|
+
]);
|
|
134
|
+
const live = rows[0];
|
|
135
|
+
if (!live) {
|
|
136
|
+
throw new VerificationFailedError();
|
|
137
|
+
}
|
|
138
|
+
if (!timingSafeEqual(Buffer.from(live.codeHash, 'hex'), hashCode(ucmId, ctx.credential))) {
|
|
139
|
+
await ctx.db.query(RECORD_FAILED_ATTEMPT, [live.id]);
|
|
140
|
+
throw new VerificationFailedError();
|
|
141
|
+
}
|
|
142
|
+
const { rowCount } = await ctx.db.query(USE_CODE, [ucmId, EMAIL_CODE_MAX_FAILED_ATTEMPTS, live.id]);
|
|
143
|
+
if (!rowCount) {
|
|
144
|
+
throw new VerificationFailedError();
|
|
145
|
+
}
|
|
146
|
+
return user;
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
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CAAC,EAAE,CAAC;gBACzF,MAAM,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,qBAAqB,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;gBACrD,MAAM,IAAI,uBAAuB,EAAE,CAAC;YACtC,CAAC;YACD,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,8BAA8B,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;YACpG,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACd,MAAM,IAAI,uBAAuB,EAAE,CAAC;YACtC,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smplcty/auth",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.1.0",
|
|
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",
|
|
@@ -22,6 +22,11 @@
|
|
|
22
22
|
"import": "./dist/methods/twilio/index.js",
|
|
23
23
|
"default": "./dist/methods/twilio/index.js"
|
|
24
24
|
},
|
|
25
|
+
"./email-code": {
|
|
26
|
+
"types": "./dist/methods/email-code/index.d.ts",
|
|
27
|
+
"import": "./dist/methods/email-code/index.js",
|
|
28
|
+
"default": "./dist/methods/email-code/index.js"
|
|
29
|
+
},
|
|
25
30
|
"./flat-tenant": {
|
|
26
31
|
"types": "./dist/scope/flat-tenant.d.ts",
|
|
27
32
|
"import": "./dist/scope/flat-tenant.js",
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
table: email_sign_in_codes
|
|
2
|
+
# A one-time sign-in code emailed by emailCodeHandler. Rows are kept after use,
|
|
3
|
+
# expiry or replacement; they are the record of every code issued.
|
|
4
|
+
# Credentials: app_rls gets nothing; only the pre-auth machinery touches them.
|
|
5
|
+
grants:
|
|
6
|
+
- to: app_privileged
|
|
7
|
+
privileges: [SELECT, INSERT, UPDATE]
|
|
8
|
+
columns:
|
|
9
|
+
- name: email_sign_in_code_id
|
|
10
|
+
type: bigserial
|
|
11
|
+
primary_key: true
|
|
12
|
+
- name: user_communication_method_id
|
|
13
|
+
type: bigint
|
|
14
|
+
nullable: false
|
|
15
|
+
references:
|
|
16
|
+
table: user_communication_methods
|
|
17
|
+
column: user_communication_method_id
|
|
18
|
+
# HMAC-SHA256 of the code under the app's codeSecret, so a database read
|
|
19
|
+
# alone cannot brute-force the million possible codes.
|
|
20
|
+
- name: code_hash
|
|
21
|
+
type: text
|
|
22
|
+
nullable: false
|
|
23
|
+
- name: created_at
|
|
24
|
+
type: timestamptz
|
|
25
|
+
nullable: false
|
|
26
|
+
default: now()
|
|
27
|
+
- name: expires_at
|
|
28
|
+
type: timestamptz
|
|
29
|
+
nullable: false
|
|
30
|
+
- name: failed_attempts
|
|
31
|
+
type: integer
|
|
32
|
+
nullable: false
|
|
33
|
+
default: 0
|
|
34
|
+
- name: used_at
|
|
35
|
+
type: timestamptz
|
|
36
|
+
- name: superseded_at
|
|
37
|
+
type: timestamptz
|
|
38
|
+
indexes:
|
|
39
|
+
- columns: [user_communication_method_id, created_at]
|