@smplcty/auth 0.1.0 → 0.2.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 +92 -0
- package/dist/dev-otp.d.ts +67 -0
- package/dist/dev-otp.d.ts.map +1 -0
- package/dist/dev-otp.js +140 -0
- package/dist/dev-otp.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +12 -9
- package/schema/tables/dev_otp_enrollments.yaml +37 -0
package/README.md
CHANGED
|
@@ -171,6 +171,98 @@ await revokeSession(db, sessionId); // idempotent — no error if already revoke
|
|
|
171
171
|
|
|
172
172
|
Hard-deletes the session row.
|
|
173
173
|
|
|
174
|
+
## Developer OTP — for devs whose phones can't receive SMS
|
|
175
|
+
|
|
176
|
+
Twilio's Verify service doesn't deliver SMS to every carrier reliably (especially overseas, certain pre-paid carriers, and some VoIP numbers). Real-world projects need a way for developers to sign in even when SMS delivery is broken.
|
|
177
|
+
|
|
178
|
+
`@smplcty/auth` ships per-developer TOTP enrollment for exactly this case. Each enrolled dev has their own time-based one-time password secret stored in `dev_otp_enrollments`, scanned into a standard authenticator app (1Password, Authy, Google Authenticator, etc.). The sign-in-verify handler tries the dev OTP first; if it doesn't match, it falls through to Twilio.
|
|
179
|
+
|
|
180
|
+
### Verifying a dev OTP
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
import { verifyDevOtp } from '@smplcty/auth';
|
|
184
|
+
|
|
185
|
+
// In your sign-in-verify handler, AFTER looking up the user:
|
|
186
|
+
const ok = await verifyDevOtp(db, userCommunicationMethodId, submittedCode);
|
|
187
|
+
if (ok) {
|
|
188
|
+
// Log an audit event with phone, IP, timestamp.
|
|
189
|
+
return createSession(db, { userCommunicationMethodId, ttl: '30 days', ip, geo });
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Fall through to Twilio Verify
|
|
193
|
+
const verified = await verifyVerificationCode(to, submittedCode);
|
|
194
|
+
if (verified) {
|
|
195
|
+
return createSession(db, { ... });
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return { statusCode: 400, body: 'Invalid code' };
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
`verifyDevOtp` returns `false` (does not throw) when:
|
|
202
|
+
- The user has no row in `dev_otp_enrollments`
|
|
203
|
+
- The submitted code doesn't match a TOTP for the stored secret within the ±30s tolerance window
|
|
204
|
+
- The stored secret is malformed (caught and treated as a non-match)
|
|
205
|
+
|
|
206
|
+
On success it updates the enrollment row's `last_used_at` and `used_count`, giving you a built-in audit signal that the dev OTP path was taken.
|
|
207
|
+
|
|
208
|
+
### Enrolling a dev
|
|
209
|
+
|
|
210
|
+
There's no shipped CLI — at the team sizes this library is designed for, manual enrollment via SQL is fine and explicit. Here's the recipe:
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
// One-off enrollment script: scripts/enroll-dev.mts
|
|
214
|
+
import { generateDevOtpSecret, getDevOtpEnrollmentUri } from '@smplcty/auth';
|
|
215
|
+
import qrcode from 'qrcode'; // pnpm add -D qrcode
|
|
216
|
+
|
|
217
|
+
const phone = process.argv[2]; // e.g. '+15558675309'
|
|
218
|
+
const label = process.argv[3]; // e.g. 'sam@salez1.com'
|
|
219
|
+
const issuer = 'Salez1';
|
|
220
|
+
|
|
221
|
+
const secret = generateDevOtpSecret();
|
|
222
|
+
const uri = getDevOtpEnrollmentUri({ secret, label, issuer });
|
|
223
|
+
|
|
224
|
+
console.log('\nScan this QR code with your authenticator app:\n');
|
|
225
|
+
console.log(await qrcode.toString(uri, { type: 'terminal', small: true }));
|
|
226
|
+
console.log(`\nManual entry secret: ${secret}\n`);
|
|
227
|
+
console.log('After scanning, run this SQL against your database:');
|
|
228
|
+
console.log(`
|
|
229
|
+
INSERT INTO dev_otp_enrollments (user_communication_method_id, totp_secret, label)
|
|
230
|
+
SELECT ucm.user_communication_method_id, '${secret}', '${label.replace(/'/g, "''")}'
|
|
231
|
+
FROM user_communication_methods ucm
|
|
232
|
+
JOIN communication_channels cc ON cc.communication_channel_id = ucm.communication_channel_id
|
|
233
|
+
WHERE cc.name = 'phone' AND ucm.code = '${phone}';
|
|
234
|
+
`);
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Run it with `pnpm tsx scripts/enroll-dev.mts +15558675309 'Sam (iPhone)'`. The script generates a fresh secret, prints a scannable QR code, and gives you the SQL to run against your database.
|
|
238
|
+
|
|
239
|
+
### Revoking a dev's enrollment
|
|
240
|
+
|
|
241
|
+
```sql
|
|
242
|
+
DELETE FROM dev_otp_enrollments
|
|
243
|
+
WHERE user_communication_method_id = (
|
|
244
|
+
SELECT user_communication_method_id
|
|
245
|
+
FROM user_communication_methods ucm
|
|
246
|
+
JOIN communication_channels cc ON cc.communication_channel_id = ucm.communication_channel_id
|
|
247
|
+
WHERE cc.name = 'phone' AND ucm.code = '+15558675309'
|
|
248
|
+
);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
The next sign-in-verify call for that user will fall through to Twilio as if they were never enrolled.
|
|
252
|
+
|
|
253
|
+
### Why per-dev TOTP instead of a shared bypass code?
|
|
254
|
+
|
|
255
|
+
Earlier versions of this codebase used a `DEV_PHONE_NUMBERS` + `DEV_VERIFICATION_CODE` env var pair where any dev phone, when paired with the magic env var code, bypassed Twilio. That design has several problems: a single static secret shared across all devs, no per-dev revocation, no audit trail, and the bypass mechanism baked into the source code as a recipe for "how to sign in without OTP." Per-dev TOTP fixes all of these:
|
|
256
|
+
|
|
257
|
+
| Concern | Shared bypass code | Per-dev TOTP |
|
|
258
|
+
|---|---|---|
|
|
259
|
+
| Secret leak blast radius | Every dev account compromised | One dev account |
|
|
260
|
+
| Per-dev revocation | Rotate the shared secret + everyone re-syncs | Delete one row |
|
|
261
|
+
| Audit trail | None | `last_used_at` + `used_count` per enrollment |
|
|
262
|
+
| Brute-force resistance | 6-digit space, no rotation | 6-digit space, rotates every 30s |
|
|
263
|
+
| Source-code visible | Shared secret + bypass logic | Just the verification code path; secrets are per-dev in the DB |
|
|
264
|
+
| Possession factor | Just env var knowledge | Authenticator app on a specific device |
|
|
265
|
+
|
|
174
266
|
## Typed roles
|
|
175
267
|
|
|
176
268
|
By default `roleName` is typed as `string`, which works for any consumer. To get autocomplete and typo detection for your specific role names, write a thin wrapper in your application code:
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { Queryable } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Verify a TOTP code against a developer's enrolled secret.
|
|
4
|
+
*
|
|
5
|
+
* Returns `true` if the user has a row in `dev_otp_enrollments` AND
|
|
6
|
+
* the submitted code matches a valid TOTP for the stored secret within
|
|
7
|
+
* the configured ±30s tolerance window. On a successful verification,
|
|
8
|
+
* the enrollment row's `last_used_at` and `used_count` are updated.
|
|
9
|
+
*
|
|
10
|
+
* Returns `false` if no enrollment exists for the user, or if the code
|
|
11
|
+
* doesn't match. **Does not throw on these cases** — falling through to
|
|
12
|
+
* Twilio verification (or any other verification path) is the caller's
|
|
13
|
+
* responsibility.
|
|
14
|
+
*
|
|
15
|
+
* Use this in a sign-in-verify handler to give devs whose phones can't
|
|
16
|
+
* receive Twilio SMS a way to sign in via an authenticator app:
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* const ok = await verifyDevOtp(db, ucmId, submittedCode);
|
|
20
|
+
* if (ok) {
|
|
21
|
+
* // log audit event with phone, IP, timestamp
|
|
22
|
+
* return createSession(db, { ... });
|
|
23
|
+
* }
|
|
24
|
+
* // fall through to Twilio verification
|
|
25
|
+
* const verified = await verifyVerificationCode(to, submittedCode);
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @throws {InvalidInputError} If `userCommunicationMethodId` is not a
|
|
29
|
+
* positive integer or `code` is not a non-empty string.
|
|
30
|
+
*/
|
|
31
|
+
export declare function verifyDevOtp(db: Queryable, userCommunicationMethodId: number, code: string): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Generate a fresh base32-encoded TOTP secret suitable for storing in
|
|
34
|
+
* the `dev_otp_enrollments.totp_secret` column.
|
|
35
|
+
*
|
|
36
|
+
* Use this from a one-off enrollment script:
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { generateDevOtpSecret, getDevOtpEnrollmentUri } from '@smplcty/auth';
|
|
40
|
+
*
|
|
41
|
+
* const secret = generateDevOtpSecret();
|
|
42
|
+
* const uri = getDevOtpEnrollmentUri({
|
|
43
|
+
* secret,
|
|
44
|
+
* label: 'sam@salez1.com',
|
|
45
|
+
* issuer: 'Salez1',
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* // INSERT secret into dev_otp_enrollments via SQL,
|
|
49
|
+
* // print uri as a QR code with the qrcode package,
|
|
50
|
+
* // and have the dev scan it with their authenticator app.
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function generateDevOtpSecret(): string;
|
|
54
|
+
/**
|
|
55
|
+
* Build an `otpauth://` URI suitable for QR-code encoding. Authenticator
|
|
56
|
+
* apps scan this URI and add the secret to their list of accounts.
|
|
57
|
+
*
|
|
58
|
+
* The label is what the user sees in their authenticator app — typically
|
|
59
|
+
* the user's email address or phone number. The issuer is the application
|
|
60
|
+
* name (e.g., "Salez1"). Both are URI-encoded by the underlying library.
|
|
61
|
+
*/
|
|
62
|
+
export declare function getDevOtpEnrollmentUri(input: {
|
|
63
|
+
secret: string;
|
|
64
|
+
label: string;
|
|
65
|
+
issuer: string;
|
|
66
|
+
}): string;
|
|
67
|
+
//# sourceMappingURL=dev-otp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dev-otp.d.ts","sourceRoot":"","sources":["../src/dev-otp.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAqC5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,YAAY,CAChC,EAAE,EAAE,SAAS,EACb,yBAAyB,EAAE,MAAM,EACjC,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,OAAO,CAAC,CA2ClB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,CAE7C;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE;IAC5C,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB,GAAG,MAAM,CAeT"}
|
package/dist/dev-otp.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { generateSecret, generateURI, verifySync } from 'otplib';
|
|
2
|
+
import { InvalidInputError } from './errors.js';
|
|
3
|
+
// Accept codes within a ±30-second window — i.e. the current code,
|
|
4
|
+
// the previous code (just rolled), and the next code (about to roll).
|
|
5
|
+
// This handles small clock drift between the dev's authenticator app
|
|
6
|
+
// and the database server. Larger windows trade security for
|
|
7
|
+
// convenience and are not worth it.
|
|
8
|
+
const EPOCH_TOLERANCE_SECONDS = 30;
|
|
9
|
+
const SELECT_ENROLLMENT = `
|
|
10
|
+
SELECT
|
|
11
|
+
user_communication_method_id AS "userCommunicationMethodId",
|
|
12
|
+
totp_secret AS "totpSecret",
|
|
13
|
+
label AS "label",
|
|
14
|
+
enrolled_at AS "enrolledAt",
|
|
15
|
+
last_used_at AS "lastUsedAt",
|
|
16
|
+
used_count AS "usedCount"
|
|
17
|
+
FROM dev_otp_enrollments
|
|
18
|
+
WHERE user_communication_method_id = $1
|
|
19
|
+
`;
|
|
20
|
+
const RECORD_USE = `
|
|
21
|
+
UPDATE dev_otp_enrollments
|
|
22
|
+
SET last_used_at = now(),
|
|
23
|
+
used_count = used_count + 1
|
|
24
|
+
WHERE user_communication_method_id = $1
|
|
25
|
+
`;
|
|
26
|
+
/**
|
|
27
|
+
* Verify a TOTP code against a developer's enrolled secret.
|
|
28
|
+
*
|
|
29
|
+
* Returns `true` if the user has a row in `dev_otp_enrollments` AND
|
|
30
|
+
* the submitted code matches a valid TOTP for the stored secret within
|
|
31
|
+
* the configured ±30s tolerance window. On a successful verification,
|
|
32
|
+
* the enrollment row's `last_used_at` and `used_count` are updated.
|
|
33
|
+
*
|
|
34
|
+
* Returns `false` if no enrollment exists for the user, or if the code
|
|
35
|
+
* doesn't match. **Does not throw on these cases** — falling through to
|
|
36
|
+
* Twilio verification (or any other verification path) is the caller's
|
|
37
|
+
* responsibility.
|
|
38
|
+
*
|
|
39
|
+
* Use this in a sign-in-verify handler to give devs whose phones can't
|
|
40
|
+
* receive Twilio SMS a way to sign in via an authenticator app:
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* const ok = await verifyDevOtp(db, ucmId, submittedCode);
|
|
44
|
+
* if (ok) {
|
|
45
|
+
* // log audit event with phone, IP, timestamp
|
|
46
|
+
* return createSession(db, { ... });
|
|
47
|
+
* }
|
|
48
|
+
* // fall through to Twilio verification
|
|
49
|
+
* const verified = await verifyVerificationCode(to, submittedCode);
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @throws {InvalidInputError} If `userCommunicationMethodId` is not a
|
|
53
|
+
* positive integer or `code` is not a non-empty string.
|
|
54
|
+
*/
|
|
55
|
+
export async function verifyDevOtp(db, userCommunicationMethodId, code) {
|
|
56
|
+
if (!Number.isInteger(userCommunicationMethodId) ||
|
|
57
|
+
userCommunicationMethodId <= 0) {
|
|
58
|
+
throw new InvalidInputError('userCommunicationMethodId must be a positive integer');
|
|
59
|
+
}
|
|
60
|
+
if (typeof code !== 'string' || code.length === 0) {
|
|
61
|
+
throw new InvalidInputError('code must be a non-empty string');
|
|
62
|
+
}
|
|
63
|
+
const { rows } = await db.query(SELECT_ENROLLMENT, [
|
|
64
|
+
userCommunicationMethodId,
|
|
65
|
+
]);
|
|
66
|
+
const enrollment = rows[0];
|
|
67
|
+
if (!enrollment) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
let valid = false;
|
|
71
|
+
try {
|
|
72
|
+
const result = verifySync({
|
|
73
|
+
secret: enrollment.totpSecret,
|
|
74
|
+
token: code,
|
|
75
|
+
epochTolerance: EPOCH_TOLERANCE_SECONDS,
|
|
76
|
+
});
|
|
77
|
+
valid = result.valid;
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
// otplib throws on malformed secrets or non-numeric tokens — treat
|
|
81
|
+
// as a non-match rather than crashing the request. A malformed
|
|
82
|
+
// secret in the DB is a configuration error worth noticing through
|
|
83
|
+
// the consumer's audit pipeline.
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (!valid) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
await db.query(RECORD_USE, [userCommunicationMethodId]);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Generate a fresh base32-encoded TOTP secret suitable for storing in
|
|
94
|
+
* the `dev_otp_enrollments.totp_secret` column.
|
|
95
|
+
*
|
|
96
|
+
* Use this from a one-off enrollment script:
|
|
97
|
+
*
|
|
98
|
+
* ```ts
|
|
99
|
+
* import { generateDevOtpSecret, getDevOtpEnrollmentUri } from '@smplcty/auth';
|
|
100
|
+
*
|
|
101
|
+
* const secret = generateDevOtpSecret();
|
|
102
|
+
* const uri = getDevOtpEnrollmentUri({
|
|
103
|
+
* secret,
|
|
104
|
+
* label: 'sam@salez1.com',
|
|
105
|
+
* issuer: 'Salez1',
|
|
106
|
+
* });
|
|
107
|
+
*
|
|
108
|
+
* // INSERT secret into dev_otp_enrollments via SQL,
|
|
109
|
+
* // print uri as a QR code with the qrcode package,
|
|
110
|
+
* // and have the dev scan it with their authenticator app.
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export function generateDevOtpSecret() {
|
|
114
|
+
return generateSecret();
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Build an `otpauth://` URI suitable for QR-code encoding. Authenticator
|
|
118
|
+
* apps scan this URI and add the secret to their list of accounts.
|
|
119
|
+
*
|
|
120
|
+
* The label is what the user sees in their authenticator app — typically
|
|
121
|
+
* the user's email address or phone number. The issuer is the application
|
|
122
|
+
* name (e.g., "Salez1"). Both are URI-encoded by the underlying library.
|
|
123
|
+
*/
|
|
124
|
+
export function getDevOtpEnrollmentUri(input) {
|
|
125
|
+
if (typeof input?.secret !== 'string' || input.secret.length === 0) {
|
|
126
|
+
throw new InvalidInputError('secret must be a non-empty string');
|
|
127
|
+
}
|
|
128
|
+
if (typeof input?.label !== 'string' || input.label.length === 0) {
|
|
129
|
+
throw new InvalidInputError('label must be a non-empty string');
|
|
130
|
+
}
|
|
131
|
+
if (typeof input?.issuer !== 'string' || input.issuer.length === 0) {
|
|
132
|
+
throw new InvalidInputError('issuer must be a non-empty string');
|
|
133
|
+
}
|
|
134
|
+
return generateURI({
|
|
135
|
+
issuer: input.issuer,
|
|
136
|
+
label: input.label,
|
|
137
|
+
secret: input.secret,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=dev-otp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dev-otp.js","sourceRoot":"","sources":["../src/dev-otp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,mEAAmE;AACnE,sEAAsE;AACtE,qEAAqE;AACrE,6DAA6D;AAC7D,oCAAoC;AACpC,MAAM,uBAAuB,GAAG,EAAE,CAAC;AAEnC,MAAM,iBAAiB,GAAG;;;;;;;;;;CAUzB,CAAC;AAEF,MAAM,UAAU,GAAG;;;;;CAKlB,CAAC;AAWF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,C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;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAItC;IACC,IAAI,OAAO,KAAK,EAAE,MAAM,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnE,MAAM,IAAI,iBAAiB,CAAC,mCAAmC,CAAC,CAAC;IACnE,CAAC;IACD,IAAI,OAAO,KAAK,EAAE,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjE,MAAM,IAAI,iBAAiB,CAAC,kCAAkC,CAAC,CAAC;IAClE,CAAC;IACD,IAAI,OAAO,KAAK,EAAE,MAAM,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnE,MAAM,IAAI,iBAAiB,CAAC,mCAAmC,CAAC,CAAC;IACnE,CAAC;IACD,OAAO,WAAW,CAAC;QACjB,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,MAAM,EAAE,KAAK,CAAC,MAAM;KACrB,CAAC,CAAC;AACL,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -7,4 +7,5 @@ export { findUserByCommunicationMethod } from './find-user-by-communication-meth
|
|
|
7
7
|
export { withSession } from './with-session.js';
|
|
8
8
|
export { withTransaction } from './with-transaction.js';
|
|
9
9
|
export { setSessionId, setRoleName, setTenantIds, setAllTenants, setSessionContext, SESSION_VAR_NAMES, } from './set-helpers.js';
|
|
10
|
+
export { verifyDevOtp, generateDevOtpSecret, getDevOtpEnrollmentUri, } from './dev-otp.js';
|
|
10
11
|
//# 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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D,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAGxD,OAAO,EACL,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EACL,YAAY,EACZ,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -10,4 +10,6 @@ export { withSession } from './with-session.js';
|
|
|
10
10
|
export { withTransaction } from './with-transaction.js';
|
|
11
11
|
// Low-level helpers — must be called inside an open transaction.
|
|
12
12
|
export { setSessionId, setRoleName, setTenantIds, setAllTenants, setSessionContext, SESSION_VAR_NAMES, } from './set-helpers.js';
|
|
13
|
+
// Developer OTP — for devs whose phones can't receive Twilio SMS.
|
|
14
|
+
export { verifyDevOtp, generateDevOtpSecret, getDevOtpEnrollmentUri, } from './dev-otp.js';
|
|
13
15
|
//# 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,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAgBrB,oBAAoB;AACpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAEvF,wBAAwB;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,iEAAiE;AACjE,OAAO,EACL,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,SAAS;AACT,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAgBrB,oBAAoB;AACpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAEvF,wBAAwB;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,iEAAiE;AACjE,OAAO,EACL,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAE1B,kEAAkE;AAClE,OAAO,EACL,YAAY,EACZ,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,cAAc,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smplcty/auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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",
|
|
@@ -45,6 +45,14 @@
|
|
|
45
45
|
"engines": {
|
|
46
46
|
"node": ">=20"
|
|
47
47
|
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsc -p tsconfig.build.json",
|
|
50
|
+
"clean": "rm -rf dist",
|
|
51
|
+
"lint": "eslint src tests",
|
|
52
|
+
"typecheck": "tsc --noEmit",
|
|
53
|
+
"test": "vitest run",
|
|
54
|
+
"test:watch": "vitest"
|
|
55
|
+
},
|
|
48
56
|
"peerDependencies": {
|
|
49
57
|
"pg": ">=8.0.0"
|
|
50
58
|
},
|
|
@@ -57,12 +65,7 @@
|
|
|
57
65
|
"typescript": "^6.0.2",
|
|
58
66
|
"vitest": "^4.1.4"
|
|
59
67
|
},
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"clean": "rm -rf dist",
|
|
63
|
-
"lint": "eslint src tests",
|
|
64
|
-
"typecheck": "tsc --noEmit",
|
|
65
|
-
"test": "vitest run",
|
|
66
|
-
"test:watch": "vitest"
|
|
68
|
+
"dependencies": {
|
|
69
|
+
"otplib": "^13.4.0"
|
|
67
70
|
}
|
|
68
|
-
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
table: dev_otp_enrollments
|
|
2
|
+
columns:
|
|
3
|
+
# The user_communication_method this enrollment is for. There can only
|
|
4
|
+
# be one dev OTP enrollment per communication method (PK + FK).
|
|
5
|
+
- name: user_communication_method_id
|
|
6
|
+
type: integer
|
|
7
|
+
primary_key: true
|
|
8
|
+
references:
|
|
9
|
+
table: user_communication_methods
|
|
10
|
+
column: user_communication_method_id
|
|
11
|
+
|
|
12
|
+
# Base32-encoded TOTP shared secret. Compatible with any standard TOTP
|
|
13
|
+
# authenticator (1Password, Authy, Google Authenticator, etc.).
|
|
14
|
+
# Recommend storing encrypted at rest if your Postgres has pgcrypto;
|
|
15
|
+
# the library reads/writes the raw value.
|
|
16
|
+
- name: totp_secret
|
|
17
|
+
type: text
|
|
18
|
+
nullable: false
|
|
19
|
+
|
|
20
|
+
# Human label for the enrollment — useful when an admin needs to
|
|
21
|
+
# know whose phone this is. Examples: "Sam's iPhone", "Mark's 1Password vault".
|
|
22
|
+
- name: label
|
|
23
|
+
type: text
|
|
24
|
+
nullable: false
|
|
25
|
+
|
|
26
|
+
- name: enrolled_at
|
|
27
|
+
type: timestamptz
|
|
28
|
+
nullable: false
|
|
29
|
+
default: now()
|
|
30
|
+
|
|
31
|
+
- name: last_used_at
|
|
32
|
+
type: timestamptz
|
|
33
|
+
|
|
34
|
+
- name: used_count
|
|
35
|
+
type: integer
|
|
36
|
+
nullable: false
|
|
37
|
+
default: 0
|