@smplcty/auth 0.2.0 → 0.3.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 +60 -12
- package/dist/dev-otp.d.ts +22 -0
- package/dist/dev-otp.d.ts.map +1 -1
- package/dist/dev-otp.js +40 -5
- package/dist/dev-otp.js.map +1 -1
- package/dist/get-user-role-names.d.ts +21 -0
- package/dist/get-user-role-names.d.ts.map +1 -0
- package/dist/get-user-role-names.js +34 -0
- package/dist/get-user-role-names.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -177,25 +177,71 @@ Twilio's Verify service doesn't deliver SMS to every carrier reliably (especiall
|
|
|
177
177
|
|
|
178
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
179
|
|
|
180
|
-
###
|
|
180
|
+
### How the codes are distinguished from Twilio codes
|
|
181
|
+
|
|
182
|
+
**They aren't, by format.** A 6-digit TOTP code from an authenticator app is indistinguishable from a 6-digit SMS code from Twilio. Users type whatever they have into the same input field. The backend figures out which one was used by **trying dev OTP first, then falling through to Twilio**.
|
|
183
|
+
|
|
184
|
+
The 1-in-1,000,000 collision risk between a wrong Twilio code and the user's current TOTP is negligible, and the audit trail (`dev_otp_enrollments.last_used_at` + `used_count`) lets you tell after the fact which path succeeded for any given sign-in.
|
|
185
|
+
|
|
186
|
+
### Send side: skip Twilio for dev-enrolled users
|
|
187
|
+
|
|
188
|
+
When a user is enrolled in dev OTP, you don't need to send them an SMS at all — they'll generate their code from their authenticator app. Use `isDevOtpEnrolled` to skip the Twilio call:
|
|
181
189
|
|
|
182
190
|
```ts
|
|
183
|
-
import {
|
|
191
|
+
import { findUserByCommunicationMethod, isDevOtpEnrolled } from '@smplcty/auth';
|
|
192
|
+
import { createTwilioVerifyClient } from '@smplcty/twilio';
|
|
193
|
+
|
|
194
|
+
const twilio = createTwilioVerifyClient({ /* ... */ });
|
|
184
195
|
|
|
185
|
-
//
|
|
186
|
-
const
|
|
187
|
-
if (
|
|
188
|
-
//
|
|
189
|
-
return
|
|
196
|
+
// Sign-in send handler:
|
|
197
|
+
const lookup = await findUserByCommunicationMethod(db, { channel: 'phone', code: phone });
|
|
198
|
+
if (!lookup) {
|
|
199
|
+
// User not registered. Return success anyway to avoid the enumeration oracle.
|
|
200
|
+
return ok();
|
|
190
201
|
}
|
|
191
202
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
203
|
+
const enrolled = await isDevOtpEnrolled(db, lookup.userCommunicationMethodId);
|
|
204
|
+
if (!enrolled) {
|
|
205
|
+
// Normal user — send the SMS code.
|
|
206
|
+
await twilio.sendVerificationCode({ channel: 'sms', to: phone });
|
|
196
207
|
}
|
|
208
|
+
// Dev-enrolled user gets the same response shape with no SMS — they
|
|
209
|
+
// already know to open their authenticator app.
|
|
197
210
|
|
|
198
|
-
return
|
|
211
|
+
return ok();
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
`isDevOtpEnrolled(db, userCommunicationMethodId)` returns `boolean`. Cheap one-row lookup. Throws `InvalidInputError` if the id is malformed.
|
|
215
|
+
|
|
216
|
+
### Verify side: try dev OTP first, fall through to Twilio
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { verifyDevOtp, createSession, findUserByCommunicationMethod } from '@smplcty/auth';
|
|
220
|
+
import { createTwilioVerifyClient } from '@smplcty/twilio';
|
|
221
|
+
|
|
222
|
+
const twilio = createTwilioVerifyClient({ /* ... */ });
|
|
223
|
+
|
|
224
|
+
// Sign-in verify handler:
|
|
225
|
+
const lookup = await findUserByCommunicationMethod(db, { channel: 'phone', code: phone });
|
|
226
|
+
if (!lookup) return badRequest('Invalid code');
|
|
227
|
+
|
|
228
|
+
// 1. Try dev OTP first.
|
|
229
|
+
const devOk = await verifyDevOtp(db, lookup.userCommunicationMethodId, submittedCode);
|
|
230
|
+
if (devOk) {
|
|
231
|
+
// dev_otp_enrollments.last_used_at and used_count have been updated
|
|
232
|
+
// — that's your built-in audit signal that the dev path was taken.
|
|
233
|
+
return await createSessionResponse(db, lookup, ip, geo);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 2. Fall through to Twilio. For users without a dev enrollment this
|
|
237
|
+
// is the only path; for users with one whose code didn't match (e.g.
|
|
238
|
+
// they happened to type the SMS code), this is the fallback.
|
|
239
|
+
const twilioOk = await twilio.verifyVerificationCode({ to: phone, code: submittedCode });
|
|
240
|
+
if (twilioOk) {
|
|
241
|
+
return await createSessionResponse(db, lookup, ip, geo);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return badRequest('Invalid code');
|
|
199
245
|
```
|
|
200
246
|
|
|
201
247
|
`verifyDevOtp` returns `false` (does not throw) when:
|
|
@@ -205,6 +251,8 @@ return { statusCode: 400, body: 'Invalid code' };
|
|
|
205
251
|
|
|
206
252
|
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
253
|
|
|
254
|
+
Both `verifyDevOtp` and `isDevOtpEnrolled` return `false` regardless of whether the user is enrolled or not (just with different conditions), so neither leaks enrollment status to the caller.
|
|
255
|
+
|
|
208
256
|
### Enrolling a dev
|
|
209
257
|
|
|
210
258
|
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:
|
package/dist/dev-otp.d.ts
CHANGED
|
@@ -29,6 +29,28 @@ import type { Queryable } from './types.js';
|
|
|
29
29
|
* positive integer or `code` is not a non-empty string.
|
|
30
30
|
*/
|
|
31
31
|
export declare function verifyDevOtp(db: Queryable, userCommunicationMethodId: number, code: string): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Check whether a user_communication_method has a dev OTP enrollment.
|
|
34
|
+
*
|
|
35
|
+
* Use this on the **send** side of a sign-in flow to skip the Twilio
|
|
36
|
+
* SMS send for dev-enrolled users — they'll generate their code from
|
|
37
|
+
* an authenticator app instead, so the SMS would be wasted (and may
|
|
38
|
+
* not deliver anyway, which is the whole reason they're enrolled in
|
|
39
|
+
* dev OTP).
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const enrolled = await isDevOtpEnrolled(db, user.userCommunicationMethodId);
|
|
44
|
+
* if (!enrolled) {
|
|
45
|
+
* await twilio.sendVerificationCode({ channel: 'sms', to });
|
|
46
|
+
* }
|
|
47
|
+
* return ok(); // dev users get the same response shape; the verify
|
|
48
|
+
* // step will accept their TOTP code
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @throws {InvalidInputError} If `userCommunicationMethodId` is not a positive integer.
|
|
52
|
+
*/
|
|
53
|
+
export declare function isDevOtpEnrolled(db: Queryable, userCommunicationMethodId: number): Promise<boolean>;
|
|
32
54
|
/**
|
|
33
55
|
* Generate a fresh base32-encoded TOTP secret suitable for storing in
|
|
34
56
|
* the `dev_otp_enrollments.totp_secret` column.
|
package/dist/dev-otp.d.ts.map
CHANGED
|
@@ -1 +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;
|
|
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;AA4C5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,YAAY,CAChC,EAAE,EAAE,SAAS,EACb,yBAAyB,EAAE,MAAM,EACjC,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,OAAO,CAAC,CAwClB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,gBAAgB,CACpC,EAAE,EAAE,SAAS,EACb,yBAAyB,EAAE,MAAM,GAChC,OAAO,CAAC,OAAO,CAAC,CAalB;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
CHANGED
|
@@ -17,6 +17,12 @@ const SELECT_ENROLLMENT = `
|
|
|
17
17
|
FROM dev_otp_enrollments
|
|
18
18
|
WHERE user_communication_method_id = $1
|
|
19
19
|
`;
|
|
20
|
+
const SELECT_ENROLLMENT_EXISTS = `
|
|
21
|
+
SELECT 1
|
|
22
|
+
FROM dev_otp_enrollments
|
|
23
|
+
WHERE user_communication_method_id = $1
|
|
24
|
+
LIMIT 1
|
|
25
|
+
`;
|
|
20
26
|
const RECORD_USE = `
|
|
21
27
|
UPDATE dev_otp_enrollments
|
|
22
28
|
SET last_used_at = now(),
|
|
@@ -67,14 +73,15 @@ export async function verifyDevOtp(db, userCommunicationMethodId, code) {
|
|
|
67
73
|
if (!enrollment) {
|
|
68
74
|
return false;
|
|
69
75
|
}
|
|
70
|
-
let valid = false;
|
|
71
76
|
try {
|
|
72
77
|
const result = verifySync({
|
|
73
78
|
secret: enrollment.totpSecret,
|
|
74
79
|
token: code,
|
|
75
80
|
epochTolerance: EPOCH_TOLERANCE_SECONDS,
|
|
76
81
|
});
|
|
77
|
-
|
|
82
|
+
if (!result.valid) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
78
85
|
}
|
|
79
86
|
catch {
|
|
80
87
|
// otplib throws on malformed secrets or non-numeric tokens — treat
|
|
@@ -83,12 +90,40 @@ export async function verifyDevOtp(db, userCommunicationMethodId, code) {
|
|
|
83
90
|
// the consumer's audit pipeline.
|
|
84
91
|
return false;
|
|
85
92
|
}
|
|
86
|
-
if (!valid) {
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
93
|
await db.query(RECORD_USE, [userCommunicationMethodId]);
|
|
90
94
|
return true;
|
|
91
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Check whether a user_communication_method has a dev OTP enrollment.
|
|
98
|
+
*
|
|
99
|
+
* Use this on the **send** side of a sign-in flow to skip the Twilio
|
|
100
|
+
* SMS send for dev-enrolled users — they'll generate their code from
|
|
101
|
+
* an authenticator app instead, so the SMS would be wasted (and may
|
|
102
|
+
* not deliver anyway, which is the whole reason they're enrolled in
|
|
103
|
+
* dev OTP).
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* const enrolled = await isDevOtpEnrolled(db, user.userCommunicationMethodId);
|
|
108
|
+
* if (!enrolled) {
|
|
109
|
+
* await twilio.sendVerificationCode({ channel: 'sms', to });
|
|
110
|
+
* }
|
|
111
|
+
* return ok(); // dev users get the same response shape; the verify
|
|
112
|
+
* // step will accept their TOTP code
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @throws {InvalidInputError} If `userCommunicationMethodId` is not a positive integer.
|
|
116
|
+
*/
|
|
117
|
+
export async function isDevOtpEnrolled(db, userCommunicationMethodId) {
|
|
118
|
+
if (!Number.isInteger(userCommunicationMethodId) ||
|
|
119
|
+
userCommunicationMethodId <= 0) {
|
|
120
|
+
throw new InvalidInputError('userCommunicationMethodId must be a positive integer');
|
|
121
|
+
}
|
|
122
|
+
const { rowCount } = await db.query(SELECT_ENROLLMENT_EXISTS, [
|
|
123
|
+
userCommunicationMethodId,
|
|
124
|
+
]);
|
|
125
|
+
return (rowCount ?? 0) > 0;
|
|
126
|
+
}
|
|
92
127
|
/**
|
|
93
128
|
* Generate a fresh base32-encoded TOTP secret suitable for storing in
|
|
94
129
|
* the `dev_otp_enrollments.totp_secret` column.
|
package/dist/dev-otp.js.map
CHANGED
|
@@ -1 +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,CAChC,EAAa,EACb,yBAAiC,EACjC,IAAY;IAEZ,IACE,CAAC,MAAM,CAAC,SAAS,CAAC,yBAAyB,CAAC;QAC5C,yBAAyB,IAAI,CAAC,EAC9B,CAAC;QACD,MAAM,IAAI,iBAAiB,CACzB,sDAAsD,CACvD,CAAC;IACJ,CAAC;IACD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAClD,MAAM,IAAI,iBAAiB,CAAC,iCAAiC,CAAC,CAAC;IACjE,CAAC;IAED,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,EAAE,CAAC,KAAK,CAAgB,iBAAiB,EAAE;QAChE,yBAAyB;KAC1B,CAAC,CAAC;IACH,MAAM,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IAC3B,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,
|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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"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Queryable } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Return every distinct role name assigned to a user, in alphabetical
|
|
4
|
+
* order. Returns an empty array if the user has no roles.
|
|
5
|
+
*
|
|
6
|
+
* Use this after `createSession` when the caller needs to tell the
|
|
7
|
+
* client which roles are available (e.g. to drive UI routing on the
|
|
8
|
+
* frontend). For in-transaction session bootstrapping — where the role
|
|
9
|
+
* is already known — use `withSession` instead.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const session = await createSession(db, { ... });
|
|
14
|
+
* const roles = await getUserRoleNames(db, session.userId);
|
|
15
|
+
* return { sessionId: session.sessionId, roles };
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @throws {InvalidInputError} If `userId` is not a positive integer.
|
|
19
|
+
*/
|
|
20
|
+
export declare function getUserRoleNames(db: Queryable, userId: number): Promise<string[]>;
|
|
21
|
+
//# sourceMappingURL=get-user-role-names.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-user-role-names.d.ts","sourceRoot":"","sources":["../src/get-user-role-names.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAc5C;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,gBAAgB,CACpC,EAAE,EAAE,SAAS,EACb,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,EAAE,CAAC,CAOnB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { InvalidInputError } from './errors.js';
|
|
2
|
+
const SELECT_ROLE_NAMES = `
|
|
3
|
+
SELECT DISTINCT r.name
|
|
4
|
+
FROM user_roles ur
|
|
5
|
+
JOIN roles r ON r.role_id = ur.role_id
|
|
6
|
+
WHERE ur.user_id = $1
|
|
7
|
+
ORDER BY r.name
|
|
8
|
+
`;
|
|
9
|
+
/**
|
|
10
|
+
* Return every distinct role name assigned to a user, in alphabetical
|
|
11
|
+
* order. Returns an empty array if the user has no roles.
|
|
12
|
+
*
|
|
13
|
+
* Use this after `createSession` when the caller needs to tell the
|
|
14
|
+
* client which roles are available (e.g. to drive UI routing on the
|
|
15
|
+
* frontend). For in-transaction session bootstrapping — where the role
|
|
16
|
+
* is already known — use `withSession` instead.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const session = await createSession(db, { ... });
|
|
21
|
+
* const roles = await getUserRoleNames(db, session.userId);
|
|
22
|
+
* return { sessionId: session.sessionId, roles };
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @throws {InvalidInputError} If `userId` is not a positive integer.
|
|
26
|
+
*/
|
|
27
|
+
export async function getUserRoleNames(db, userId) {
|
|
28
|
+
if (!Number.isInteger(userId) || userId <= 0) {
|
|
29
|
+
throw new InvalidInputError('userId must be a positive integer');
|
|
30
|
+
}
|
|
31
|
+
const { rows } = await db.query(SELECT_ROLE_NAMES, [userId]);
|
|
32
|
+
return rows.map((r) => r.name);
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=get-user-role-names.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-user-role-names.js","sourceRoot":"","sources":["../src/get-user-role-names.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,MAAM,iBAAiB,GAAG;;;;;;CAMzB,CAAC;AAMF;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,EAAa,EACb,MAAc;IAEd,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,iBAAiB,CAAC,mCAAmC,CAAC,CAAC;IACnE,CAAC;IAED,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,EAAE,CAAC,KAAK,CAAc,iBAAiB,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1E,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,8 +4,9 @@ export { createSession } from './create-session.js';
|
|
|
4
4
|
export { validateSession } from './validate-session.js';
|
|
5
5
|
export { revokeSession } from './revoke-session.js';
|
|
6
6
|
export { findUserByCommunicationMethod } from './find-user-by-communication-method.js';
|
|
7
|
+
export { getUserRoleNames } from './get-user-role-names.js';
|
|
7
8
|
export { withSession } from './with-session.js';
|
|
8
9
|
export { withTransaction } from './with-transaction.js';
|
|
9
10
|
export { setSessionId, setRoleName, setTenantIds, setAllTenants, setSessionContext, SESSION_VAR_NAMES, } from './set-helpers.js';
|
|
10
|
-
export { verifyDevOtp, generateDevOtpSecret, getDevOtpEnrollmentUri, } from './dev-otp.js';
|
|
11
|
+
export { verifyDevOtp, isDevOtpEnrolled, generateDevOtpSecret, getDevOtpEnrollmentUri, } from './dev-otp.js';
|
|
11
12
|
//# 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,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAGrB,YAAY,EACV,SAAS,EACT,OAAO,EACP,cAAc,EACd,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;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAGrB,YAAY,EACV,SAAS,EACT,OAAO,EACP,cAAc,EACd,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;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAEvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAG5D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,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,gBAAgB,EAChB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -5,11 +5,12 @@ export { createSession } from './create-session.js';
|
|
|
5
5
|
export { validateSession } from './validate-session.js';
|
|
6
6
|
export { revokeSession } from './revoke-session.js';
|
|
7
7
|
export { findUserByCommunicationMethod } from './find-user-by-communication-method.js';
|
|
8
|
+
export { getUserRoleNames } from './get-user-role-names.js';
|
|
8
9
|
// Request bootstrapping
|
|
9
10
|
export { withSession } from './with-session.js';
|
|
10
11
|
export { withTransaction } from './with-transaction.js';
|
|
11
12
|
// Low-level helpers — must be called inside an open transaction.
|
|
12
13
|
export { setSessionId, setRoleName, setTenantIds, setAllTenants, setSessionContext, SESSION_VAR_NAMES, } from './set-helpers.js';
|
|
13
14
|
// Developer OTP — for devs whose phones can't receive Twilio SMS.
|
|
14
|
-
export { verifyDevOtp, generateDevOtpSecret, getDevOtpEnrollmentUri, } from './dev-otp.js';
|
|
15
|
+
export { verifyDevOtp, isDevOtpEnrolled, generateDevOtpSecret, getDevOtpEnrollmentUri, } from './dev-otp.js';
|
|
15
16
|
//# 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;AAE1B,kEAAkE;AAClE,OAAO,EACL,YAAY,EACZ,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,cAAc,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,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D,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,gBAAgB,EAChB,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.3.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",
|
|
@@ -57,12 +57,14 @@
|
|
|
57
57
|
"pg": ">=8.0.0"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
+
"@eslint/js": "^10.0.1",
|
|
60
61
|
"@smplcty/schema-flow": "^0.3.7",
|
|
61
62
|
"@types/node": "^25.5.2",
|
|
62
63
|
"@types/pg": "^8.20.0",
|
|
63
64
|
"eslint": "^10.2.0",
|
|
64
65
|
"pg": "^8.20.0",
|
|
65
66
|
"typescript": "^6.0.2",
|
|
67
|
+
"typescript-eslint": "^8.58.1",
|
|
66
68
|
"vitest": "^4.1.4"
|
|
67
69
|
},
|
|
68
70
|
"dependencies": {
|