@sharadox/account-web-library 0.1.0-account-schema.5 → 0.1.0-code-request.8
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 +17 -0
- package/distribution/account-database/type.d.ts +19 -0
- package/distribution/account-database/type.d.ts.map +1 -1
- package/distribution/account-migration/index.d.ts.map +1 -1
- package/distribution/account-migration/index.js +20 -0
- package/distribution/account-migration/index.js.map +1 -1
- package/distribution/code-request/index.d.ts +16 -0
- package/distribution/code-request/index.d.ts.map +1 -0
- package/distribution/code-request/index.js +52 -0
- package/distribution/code-request/index.js.map +1 -0
- package/distribution/code-request/type.d.ts +25 -0
- package/distribution/code-request/type.d.ts.map +1 -0
- package/distribution/code-request/type.js +2 -0
- package/distribution/code-request/type.js.map +1 -0
- package/distribution/code-request-prune/index.d.ts +13 -0
- package/distribution/code-request-prune/index.d.ts.map +1 -0
- package/distribution/code-request-prune/index.js +12 -0
- package/distribution/code-request-prune/index.js.map +1 -0
- package/distribution/code-verification/index.d.ts +14 -0
- package/distribution/code-verification/index.d.ts.map +1 -0
- package/distribution/code-verification/index.js +43 -0
- package/distribution/code-verification/index.js.map +1 -0
- package/distribution/code-verification/type.d.ts +22 -0
- package/distribution/code-verification/type.d.ts.map +1 -0
- package/distribution/code-verification/type.js +2 -0
- package/distribution/code-verification/type.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -134,6 +134,23 @@ Verified contacts only, normalized first.
|
|
|
134
134
|
|
|
135
135
|
Each is recorded in the audit log. Ask for a fresh code first (step-up) and tell the other contacts.
|
|
136
136
|
|
|
137
|
+
### `code-request`: `requestCode(database, { purpose, contact, accountId?, pepper, length = 8, lifetimeSeconds = 600, now? })` → `{ requestId, code, contact, expiresAt }`
|
|
138
|
+
|
|
139
|
+
Starts a one-time code for `sign-in`, `contact-verification` or `step-up`.
|
|
140
|
+
|
|
141
|
+
- Only an HMAC bound to the purpose and request is stored.
|
|
142
|
+
- A new request replaces the open one for the same contact and purpose.
|
|
143
|
+
- Send the code after answering the request, so timing reveals nothing. Rate limit and cap the sends first
|
|
144
|
+
(`limitRate`, `spendFromCap` from protection-web-library).
|
|
145
|
+
|
|
146
|
+
### `code-verification`: `verifyCode(database, { requestId, code, purpose, pepper, maximumAttempts = 5, now? })`
|
|
147
|
+
|
|
148
|
+
Returns `{ valid: true, contact, accountId }` (the account that asked, null for sign-in), or `{ valid: false, reason }`
|
|
149
|
+
with `unknown`, `used`, `replaced`, `expired`, `attempts` or `wrong`. The row is locked, so concurrent guesses can't
|
|
150
|
+
pass the limit; after the last attempt even the right code is refused.
|
|
151
|
+
|
|
152
|
+
### `code-request-prune`: `pruneCodeRequests(database, { before })` → deleted count
|
|
153
|
+
|
|
137
154
|
### `contact-kind`: `normalizeContact({ kind, value })`, `CONTACT_KINDS`
|
|
138
155
|
|
|
139
156
|
`email` in this version; unknown kinds and invalid values throw.
|
|
@@ -43,12 +43,31 @@ export interface AuditEventTable {
|
|
|
43
43
|
action: string;
|
|
44
44
|
detail: ColumnType<Record<string, unknown>, Record<string, unknown> | undefined, never>;
|
|
45
45
|
}
|
|
46
|
+
/** A one-time code sent to a contact, stored only as an HMAC bound to its purpose and request. */
|
|
47
|
+
export interface CodeRequestTable {
|
|
48
|
+
id: string;
|
|
49
|
+
/** `sign-in`, `contact-verification` or `step-up`. */
|
|
50
|
+
purpose: string;
|
|
51
|
+
/** The account asking (step-up, contact verification); null when signing in. */
|
|
52
|
+
account_id: string | null;
|
|
53
|
+
kind: string;
|
|
54
|
+
value: string;
|
|
55
|
+
code_hash: string;
|
|
56
|
+
created_at: Date;
|
|
57
|
+
expires_at: Date;
|
|
58
|
+
attempts: Generated<number>;
|
|
59
|
+
/** When the right code came back. */
|
|
60
|
+
used_at: Date | null;
|
|
61
|
+
/** When a newer request for the same contact and purpose replaced it. */
|
|
62
|
+
voided_at: Date | null;
|
|
63
|
+
}
|
|
46
64
|
/** The tables `migrateAccount` creates, for typing a Kysely instance. */
|
|
47
65
|
export interface AccountDatabase {
|
|
48
66
|
application: ApplicationTable;
|
|
49
67
|
account: AccountTable;
|
|
50
68
|
contact: ContactTable;
|
|
51
69
|
audit_event: AuditEventTable;
|
|
70
|
+
code_request: CodeRequestTable;
|
|
52
71
|
}
|
|
53
72
|
export {};
|
|
54
73
|
//# sourceMappingURL=type.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-database/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEpD,wDAAwD;AACxD,KAAK,WAAW,GAAG,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,SAAS,EAAE,IAAI,CAAC,CAAC;AAE5D,sFAAsF;AACtF,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,WAAW,CAAC;CACzB;AAED,+CAA+C;AAC/C,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC;IACzB,uDAAuD;IACvD,QAAQ,EAAE,MAAM,CAAC;IACjB,qDAAqD;IACrD,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,UAAU,EAAE,WAAW,CAAC;IACxB,cAAc,EAAE,WAAW,CAAC;CAC7B;AAED,wEAAwE;AACxE,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,2CAA2C;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,uGAAuG;IACvG,WAAW,EAAE,IAAI,GAAG,IAAI,CAAC;IACzB,sDAAsD;IACtD,OAAO,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC;IAC5B,UAAU,EAAE,WA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,GAAG,IAAI,CAAC;IACrB,yEAAyE;IACzE,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;CACxB;AAED,yEAAyE;AACzE,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,gBAAgB,CAAC;IAC9B,OAAO,EAAE,YAAY,CAAC;IACtB,OAAO,EAAE,YAAY,CAAC;IACtB,WAAW,EAAE,eAAe,CAAC;IAC7B,YAAY,EAAE,gBAAgB,CAAC;CAChC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAE/E,iHAAiH;AACjH,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAE3D;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAE/E,iHAAiH;AACjH,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAE3D;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,EAuGxC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAElF"}
|
|
@@ -89,6 +89,26 @@ export const accountMigrations = [
|
|
|
89
89
|
for each row execute function audit_event_unchangeable();
|
|
90
90
|
`,
|
|
91
91
|
},
|
|
92
|
+
{
|
|
93
|
+
name: "0002-code-request",
|
|
94
|
+
sql: `
|
|
95
|
+
create table code_request (
|
|
96
|
+
id uuid primary key check (uuid_extract_version(id) = 7),
|
|
97
|
+
purpose text not null check (purpose in ('sign-in', 'contact-verification', 'step-up')),
|
|
98
|
+
account_id uuid,
|
|
99
|
+
kind text not null,
|
|
100
|
+
value text not null,
|
|
101
|
+
code_hash text not null,
|
|
102
|
+
created_at timestamptz not null,
|
|
103
|
+
expires_at timestamptz not null,
|
|
104
|
+
attempts integer not null default 0 check (attempts >= 0),
|
|
105
|
+
used_at timestamptz,
|
|
106
|
+
voided_at timestamptz
|
|
107
|
+
);
|
|
108
|
+
create index code_request_open on code_request (kind, value, purpose) where used_at is null and voided_at is null;
|
|
109
|
+
create index code_request_expires_at on code_request (expires_at);
|
|
110
|
+
`,
|
|
111
|
+
},
|
|
92
112
|
];
|
|
93
113
|
/**
|
|
94
114
|
* Creates or updates the account tables, tracked in their own migration table. Returns the names applied.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AAGnE,iHAAiH;AACjH,MAAM,CAAC,MAAM,uBAAuB,GAAG,mBAAmB,CAAC;AAE3D;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAgB;IAC5C;QACE,IAAI,EAAE,wCAAwC;QAC9C,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA8EJ;KACF;CACF,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAS,QAAwB;IAC7D,OAAO,OAAO,CAAC,QAAQ,EAAE,iBAAiB,EAAE,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC,CAAC;AAClF,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AAGnE,iHAAiH;AACjH,MAAM,CAAC,MAAM,uBAAuB,GAAG,mBAAmB,CAAC;AAE3D;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAgB;IAC5C;QACE,IAAI,EAAE,wCAAwC;QAC9C,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA8EJ;KACF;IACD;QACE,IAAI,EAAE,mBAAmB;QACzB,GAAG,EAAE;;;;;;;;;;;;;;;;KAgBJ;KACF;CACF,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAS,QAAwB;IAC7D,OAAO,OAAO,CAAC,QAAQ,EAAE,iBAAiB,EAAE,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC,CAAC;AAClF,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
import type { CodePurpose, CodeRequest, CodeRequestOptions } from "@sharadox/account-web-library/code-request/type";
|
|
4
|
+
/** The purposes a code can have. */
|
|
5
|
+
export declare const CODE_PURPOSES: readonly CodePurpose[];
|
|
6
|
+
/**
|
|
7
|
+
* Starts a one-time code for a contact: a new code (8 digits, 10 minutes by default), stored only as an HMAC bound
|
|
8
|
+
* to its purpose and request, replacing any open request for the same contact and purpose. Returns the code to send
|
|
9
|
+
* through the contact's channel. Rate limit and cap the sends before calling it (protection-web-library).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const { requestId, code } = await requestCode(database, { purpose: "sign-in", contact, pepper });
|
|
13
|
+
* await channel.send(contact, code); // after answering the request, so timing reveals nothing
|
|
14
|
+
*/
|
|
15
|
+
export declare function requestCode(database: Kysely<AccountDatabase>, { purpose, contact, accountId, pepper, length, lifetimeSeconds, now }: CodeRequestOptions): Promise<CodeRequest>;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/code-request/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,iDAAiD,CAAC;AAIpH,oCAAoC;AACpC,eAAO,MAAM,aAAa,EAAE,SAAS,WAAW,EAAmD,CAAC;AAEpG;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAC/B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAU,EAAE,eAAqB,EAAE,GAAgB,EAAE,EAAE,kBAAkB,GAC/G,OAAO,CAAC,WAAW,CAAC,CAmCtB"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
|
|
2
|
+
import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
|
|
3
|
+
import { createOneTimeCode, hashOneTimeCode } from "@sharadox/account-web-library/one-time-code";
|
|
4
|
+
/** The purposes a code can have. */
|
|
5
|
+
export const CODE_PURPOSES = ["sign-in", "contact-verification", "step-up"];
|
|
6
|
+
/**
|
|
7
|
+
* Starts a one-time code for a contact: a new code (8 digits, 10 minutes by default), stored only as an HMAC bound
|
|
8
|
+
* to its purpose and request, replacing any open request for the same contact and purpose. Returns the code to send
|
|
9
|
+
* through the contact's channel. Rate limit and cap the sends before calling it (protection-web-library).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const { requestId, code } = await requestCode(database, { purpose: "sign-in", contact, pepper });
|
|
13
|
+
* await channel.send(contact, code); // after answering the request, so timing reveals nothing
|
|
14
|
+
*/
|
|
15
|
+
export async function requestCode(database, { purpose, contact, accountId, pepper, length = 8, lifetimeSeconds = 600, now = Date.now() }) {
|
|
16
|
+
if (!CODE_PURPOSES.includes(purpose))
|
|
17
|
+
throw new Error(`unknown code purpose "${purpose}"`);
|
|
18
|
+
const { kind, value } = normalizeContact(contact);
|
|
19
|
+
const requestId = uuidVersion7({ now });
|
|
20
|
+
const code = createOneTimeCode(length);
|
|
21
|
+
const codeHash = hashOneTimeCode(code, { pepper, purpose: `${purpose}:${requestId}` });
|
|
22
|
+
const createdAt = new Date(now);
|
|
23
|
+
const expiresAt = new Date(now + lifetimeSeconds * 1000);
|
|
24
|
+
await database.transaction().execute(async (transaction) => {
|
|
25
|
+
await transaction
|
|
26
|
+
.updateTable("code_request")
|
|
27
|
+
.set({ voided_at: createdAt })
|
|
28
|
+
.where("kind", "=", kind)
|
|
29
|
+
.where("value", "=", value)
|
|
30
|
+
.where("purpose", "=", purpose)
|
|
31
|
+
.where("used_at", "is", null)
|
|
32
|
+
.where("voided_at", "is", null)
|
|
33
|
+
.execute();
|
|
34
|
+
await transaction
|
|
35
|
+
.insertInto("code_request")
|
|
36
|
+
.values({
|
|
37
|
+
id: requestId,
|
|
38
|
+
purpose,
|
|
39
|
+
account_id: accountId ?? null,
|
|
40
|
+
kind,
|
|
41
|
+
value,
|
|
42
|
+
code_hash: codeHash,
|
|
43
|
+
created_at: createdAt,
|
|
44
|
+
expires_at: expiresAt,
|
|
45
|
+
used_at: null,
|
|
46
|
+
voided_at: null,
|
|
47
|
+
})
|
|
48
|
+
.execute();
|
|
49
|
+
});
|
|
50
|
+
return { requestId, code, contact: { kind, value }, expiresAt };
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/code-request/index.ts"],"names":[],"mappings":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GAAG,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;aAC7B,KAAK,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC;aACxB,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC;aAC1B,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,OAAO,CAAC;aAC9B,KAAK,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC;aAC5B,KAAK,CAAC,WAAW,EAAE,IAAI,EAAE,IAAI,CAAC;aAC9B,OAAO,EAAE,CAAC;QACb,MAAM,WAAW;aACd,UAAU,CAAC,cAAc,CAAC;aAC1B,MAAM,CAAC;YACN,EAAE,EAAE,SAAS;YACb,OAAO;YACP,UAAU,EAAE,SAAS,IAAI,IAAI;YAC7B,IAAI;YACJ,KAAK;YACL,SAAS,EAAE,QAAQ;YACnB,UAAU,EAAE,SAAS;YACrB,UAAU,EAAE,SAAS;YACrB,OAAO,EAAE,IAAI;YACb,SAAS,EAAE,IAAI;SAChB,CAAC;aACD,OAAO,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
|
|
2
|
+
/** What a one-time code is for; a code works only for its own purpose and request. */
|
|
3
|
+
export type CodePurpose = "sign-in" | "contact-verification" | "step-up";
|
|
4
|
+
export interface CodeRequestOptions {
|
|
5
|
+
purpose: CodePurpose;
|
|
6
|
+
contact: Contact;
|
|
7
|
+
/** The account asking (step-up, contact verification). */
|
|
8
|
+
accountId?: string;
|
|
9
|
+
/** At least 32 characters, kept outside the database. */
|
|
10
|
+
pepper: string;
|
|
11
|
+
/** Digits (8 by default). */
|
|
12
|
+
length?: number;
|
|
13
|
+
/** 600 (10 minutes) by default. */
|
|
14
|
+
lifetimeSeconds?: number;
|
|
15
|
+
/** Milliseconds since 1970 (now by default). */
|
|
16
|
+
now?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface CodeRequest {
|
|
19
|
+
requestId: string;
|
|
20
|
+
/** Send it, then forget it: only its hash is stored. */
|
|
21
|
+
code: string;
|
|
22
|
+
contact: Contact;
|
|
23
|
+
expiresAt: Date;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/code-request/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,sFAAsF;AACtF,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,sBAAsB,GAAG,SAAS,CAAC;AAEzE,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,WAAW,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,0DAA0D;IAC1D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,yDAAyD;IACzD,MAAM,EAAE,MAAM,CAAC;IACf,6BAA6B;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mCAAmC;IACnC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,IAAI,CAAC;CACjB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/code-request/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* Deletes code requests that expired before `before`, with the contacts they hold, from a daily job. Returns how
|
|
5
|
+
* many were deleted.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await pruneCodeRequests(database, { before: new Date(Date.now() - 86_400_000) });
|
|
9
|
+
*/
|
|
10
|
+
export declare function pruneCodeRequests(database: Kysely<AccountDatabase>, { before }: {
|
|
11
|
+
before: Date;
|
|
12
|
+
}): Promise<number>;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/code-request-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,MAAM,EAAE,EAAE;IAAE,MAAM,EAAE,IAAI,CAAA;CAAE,GAC3B,OAAO,CAAC,MAAM,CAAC,CAGjB"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deletes code requests that expired before `before`, with the contacts they hold, from a daily job. Returns how
|
|
3
|
+
* many were deleted.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* await pruneCodeRequests(database, { before: new Date(Date.now() - 86_400_000) });
|
|
7
|
+
*/
|
|
8
|
+
export async function pruneCodeRequests(database, { before }) {
|
|
9
|
+
const result = await database.deleteFrom("code_request").where("expires_at", "<", before).executeTakeFirst();
|
|
10
|
+
return Number(result.numDeletedRows);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/code-request-prune/index.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,QAAiC,EACjC,EAAE,MAAM,EAAoB;IAE5B,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC,gBAAgB,EAAE,CAAC;IAC7G,OAAO,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
import type { CodeVerificationOptions, CodeVerificationResult } from "@sharadox/account-web-library/code-verification/type";
|
|
4
|
+
/**
|
|
5
|
+
* Checks a typed one-time code against its request, with the row locked so concurrent guesses can't pass the
|
|
6
|
+
* attempt limit. The right code works once, before it expires, unless a newer request replaced it or 5 wrong codes
|
|
7
|
+
* came first. Returns the proved contact and the account that asked (null for sign-in).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const result = await verifyCode(database, { requestId, code: typed, purpose: "sign-in", pepper });
|
|
11
|
+
* if (result.valid) signIn(await findAccountByContact(database, result.contact));
|
|
12
|
+
*/
|
|
13
|
+
export declare function verifyCode(database: Kysely<AccountDatabase>, { requestId, code, purpose, pepper, maximumAttempts, now }: CodeVerificationOptions): Promise<CodeVerificationResult>;
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/code-verification/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EACV,uBAAuB,EACvB,sBAAsB,EACvB,MAAM,sDAAsD,CAAC;AAK9D;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAC9B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,eAAmB,EAAE,GAAgB,EAAE,EAAE,uBAAuB,GACnG,OAAO,CAAC,sBAAsB,CAAC,CAwBjC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { matchesOneTimeCode } from "@sharadox/account-web-library/one-time-code";
|
|
2
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
3
|
+
/**
|
|
4
|
+
* Checks a typed one-time code against its request, with the row locked so concurrent guesses can't pass the
|
|
5
|
+
* attempt limit. The right code works once, before it expires, unless a newer request replaced it or 5 wrong codes
|
|
6
|
+
* came first. Returns the proved contact and the account that asked (null for sign-in).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const result = await verifyCode(database, { requestId, code: typed, purpose: "sign-in", pepper });
|
|
10
|
+
* if (result.valid) signIn(await findAccountByContact(database, result.contact));
|
|
11
|
+
*/
|
|
12
|
+
export async function verifyCode(database, { requestId, code, purpose, pepper, maximumAttempts = 5, now = Date.now() }) {
|
|
13
|
+
if (!UUID.test(requestId))
|
|
14
|
+
return { valid: false, reason: "unknown" };
|
|
15
|
+
return database.transaction().execute(async (transaction) => {
|
|
16
|
+
const request = await transaction
|
|
17
|
+
.selectFrom("code_request")
|
|
18
|
+
.selectAll()
|
|
19
|
+
.where("id", "=", requestId)
|
|
20
|
+
.where("purpose", "=", purpose)
|
|
21
|
+
.forUpdate()
|
|
22
|
+
.executeTakeFirst();
|
|
23
|
+
if (!request)
|
|
24
|
+
return { valid: false, reason: "unknown" };
|
|
25
|
+
if (request.used_at)
|
|
26
|
+
return { valid: false, reason: "used" };
|
|
27
|
+
if (request.voided_at)
|
|
28
|
+
return { valid: false, reason: "replaced" };
|
|
29
|
+
if (request.expires_at.getTime() <= now)
|
|
30
|
+
return { valid: false, reason: "expired" };
|
|
31
|
+
if (request.attempts >= maximumAttempts)
|
|
32
|
+
return { valid: false, reason: "attempts" };
|
|
33
|
+
const at = transaction.updateTable("code_request").where("id", "=", requestId);
|
|
34
|
+
if (matchesOneTimeCode(code, request.code_hash, { pepper, purpose: `${purpose}:${requestId}` })) {
|
|
35
|
+
await at.set({ used_at: new Date(now) }).execute();
|
|
36
|
+
return { valid: true, contact: { kind: request.kind, value: request.value }, accountId: request.account_id };
|
|
37
|
+
}
|
|
38
|
+
const attempts = request.attempts + 1;
|
|
39
|
+
await at.set({ attempts }).execute();
|
|
40
|
+
return { valid: false, reason: attempts >= maximumAttempts ? "attempts" : "wrong" };
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/code-verification/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,kBAAkB,EAAE,MAAM,6CAA6C,CAAC;AAEjF,MAAM,IAAI,GAAG,iEAAiE,CAAC;AAE/E;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,QAAiC,EACjC,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,eAAe,GAAG,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAA2B;IAEpG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IACtE,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAmC,EAAE;QAC3F,MAAM,OAAO,GAAG,MAAM,WAAW;aAC9B,UAAU,CA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E,OAAO,CAAC,KAAK,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC;QAC/G,CAAC;QACD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;QACtC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;QACrC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,IAAI,eAAe,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;IACtF,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CodePurpose } from "@sharadox/account-web-library/code-request/type";
|
|
2
|
+
import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
|
|
3
|
+
export interface CodeVerificationOptions {
|
|
4
|
+
requestId: string;
|
|
5
|
+
/** As typed: spaces are ignored. */
|
|
6
|
+
code: string;
|
|
7
|
+
purpose: CodePurpose;
|
|
8
|
+
pepper: string;
|
|
9
|
+
/** Wrong codes allowed before the request is dead (5 by default). */
|
|
10
|
+
maximumAttempts?: number;
|
|
11
|
+
/** Milliseconds since 1970 (now by default). */
|
|
12
|
+
now?: number;
|
|
13
|
+
}
|
|
14
|
+
export type CodeVerificationResult = {
|
|
15
|
+
valid: true;
|
|
16
|
+
contact: Contact;
|
|
17
|
+
accountId: string | null;
|
|
18
|
+
} | {
|
|
19
|
+
valid: false;
|
|
20
|
+
reason: "unknown" | "used" | "replaced" | "expired" | "attempts" | "wrong";
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/code-verification/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iDAAiD,CAAC;AACnF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,uBAAuB;IACtC,SAAS,EAAE,MAAM,CAAC;IAClB,oCAAoC;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,WAAW,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,qEAAqE;IACrE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,sBAAsB,GAC9B;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC;IAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAC3D;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,SAAS,GAAG,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,UAAU,GAAG,OAAO,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/code-verification/type.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sharadox/account-web-library",
|
|
3
|
-
"version": "0.1.0-
|
|
3
|
+
"version": "0.1.0-code-request.8",
|
|
4
4
|
"description": "Passwordless accounts for web applications: one-time codes, two-factor, roles, bans and OpenID Connect for people and agents.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "SEE LICENSE IN LICENCA.md",
|