@sharadox/account-web-library 0.1.0-development.0 → 0.1.0-development.12
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 -3
- package/distribution/account-database/type.d.ts +94 -0
- package/distribution/account-database/type.d.ts.map +1 -0
- package/distribution/account-database/type.js +2 -0
- package/distribution/account-database/type.js.map +1 -0
- package/distribution/account-migration/index.d.ts +18 -0
- package/distribution/account-migration/index.d.ts.map +1 -0
- package/distribution/account-migration/index.js +146 -0
- package/distribution/account-migration/index.js.map +1 -0
- package/distribution/account-session-revocation/index.d.ts +14 -0
- package/distribution/account-session-revocation/index.d.ts.map +1 -0
- package/distribution/account-session-revocation/index.js +25 -0
- package/distribution/account-session-revocation/index.js.map +1 -0
- package/distribution/account-sign-in/index.d.ts +15 -0
- package/distribution/account-sign-in/index.d.ts.map +1 -0
- package/distribution/account-sign-in/index.js +76 -0
- package/distribution/account-sign-in/index.js.map +1 -0
- package/distribution/account-sign-in/type.d.ts +20 -0
- package/distribution/account-sign-in/type.d.ts.map +1 -0
- package/distribution/account-sign-in/type.js +2 -0
- package/distribution/account-sign-in/type.js.map +1 -0
- package/distribution/account-sign-out/index.d.ts +16 -0
- package/distribution/account-sign-out/index.d.ts.map +1 -0
- package/distribution/account-sign-out/index.js +34 -0
- package/distribution/account-sign-out/index.js.map +1 -0
- package/distribution/audit-event/index.d.ts +12 -0
- package/distribution/audit-event/index.d.ts.map +1 -0
- package/distribution/audit-event/index.js +25 -0
- package/distribution/audit-event/index.js.map +1 -0
- package/distribution/audit-event/type.d.ts +15 -0
- package/distribution/audit-event/type.d.ts.map +1 -0
- package/distribution/audit-event/type.js +2 -0
- package/distribution/audit-event/type.js.map +1 -0
- package/distribution/browser-session-read/index.d.ts +17 -0
- package/distribution/browser-session-read/index.d.ts.map +1 -0
- package/distribution/browser-session-read/index.js +48 -0
- package/distribution/browser-session-read/index.js.map +1 -0
- package/distribution/browser-session-read/type.d.ts +14 -0
- package/distribution/browser-session-read/type.d.ts.map +1 -0
- package/distribution/browser-session-read/type.js +2 -0
- package/distribution/browser-session-read/type.js.map +1 -0
- 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/distribution/contact-addition/index.d.ts +13 -0
- package/distribution/contact-addition/index.d.ts.map +1 -0
- package/distribution/contact-addition/index.js +48 -0
- package/distribution/contact-addition/index.js.map +1 -0
- package/distribution/contact-addition/type.d.ts +15 -0
- package/distribution/contact-addition/type.d.ts.map +1 -0
- package/distribution/contact-addition/type.js +2 -0
- package/distribution/contact-addition/type.js.map +1 -0
- package/distribution/contact-kind/index.d.ts +12 -0
- package/distribution/contact-kind/index.d.ts.map +1 -0
- package/distribution/contact-kind/index.js +18 -0
- package/distribution/contact-kind/index.js.map +1 -0
- package/distribution/contact-kind/type.d.ts +6 -0
- package/distribution/contact-kind/type.d.ts.map +1 -0
- package/distribution/contact-kind/type.js +2 -0
- package/distribution/contact-kind/type.js.map +1 -0
- package/distribution/contact-lookup/index.d.ts +16 -0
- package/distribution/contact-lookup/index.d.ts.map +1 -0
- package/distribution/contact-lookup/index.js +20 -0
- package/distribution/contact-lookup/index.js.map +1 -0
- package/distribution/contact-removal/index.d.ts +19 -0
- package/distribution/contact-removal/index.d.ts.map +1 -0
- package/distribution/contact-removal/index.js +35 -0
- package/distribution/contact-removal/index.js.map +1 -0
- package/distribution/contact-verification/index.d.ts +20 -0
- package/distribution/contact-verification/index.d.ts.map +1 -0
- package/distribution/contact-verification/index.js +37 -0
- package/distribution/contact-verification/index.js.map +1 -0
- package/distribution/database-error/index.d.ts +12 -0
- package/distribution/database-error/index.d.ts.map +1 -0
- package/distribution/database-error/index.js +14 -0
- package/distribution/database-error/index.js.map +1 -0
- package/distribution/person-account/index.d.ts +14 -0
- package/distribution/person-account/index.d.ts.map +1 -0
- package/distribution/person-account/index.js +45 -0
- package/distribution/person-account/index.js.map +1 -0
- package/distribution/person-account/type.d.ts +17 -0
- package/distribution/person-account/type.d.ts.map +1 -0
- package/distribution/person-account/type.js +2 -0
- package/distribution/person-account/type.js.map +1 -0
- package/distribution/session-prune/index.d.ts +17 -0
- package/distribution/session-prune/index.d.ts.map +1 -0
- package/distribution/session-prune/index.js +30 -0
- package/distribution/session-prune/index.js.map +1 -0
- package/distribution/session-token/index.d.ts +19 -0
- package/distribution/session-token/index.d.ts.map +1 -0
- package/distribution/session-token/index.js +22 -0
- package/distribution/session-token/index.js.map +1 -0
- package/distribution/test-database/index.d.ts +13 -0
- package/distribution/test-database/index.d.ts.map +1 -0
- package/distribution/test-database/index.js +17 -0
- package/distribution/test-database/index.js.map +1 -0
- package/distribution/username-availability/index.d.ts +11 -0
- package/distribution/username-availability/index.d.ts.map +1 -0
- package/distribution/username-availability/index.js +21 -0
- package/distribution/username-availability/index.js.map +1 -0
- package/distribution/username-change/index.d.ts +15 -0
- package/distribution/username-change/index.d.ts.map +1 -0
- package/distribution/username-change/index.js +39 -0
- package/distribution/username-change/index.js.map +1 -0
- package/distribution/username-change/type.d.ts +7 -0
- package/distribution/username-change/type.d.ts.map +1 -0
- package/distribution/username-change/type.js +2 -0
- package/distribution/username-change/type.js.map +1 -0
- package/package.json +18 -17
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
import type { AuditEvent } from "@sharadox/account-web-library/audit-event/type";
|
|
4
|
+
/**
|
|
5
|
+
* Adds a line to the append-only audit log (the database refuses changes to it). Pass a transaction to record it
|
|
6
|
+
* together with the change it describes. Returns the event's UUIDv7.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* await recordAuditEvent(transaction, { accountId, action: "contact.removed", detail: { contactId } });
|
|
10
|
+
*/
|
|
11
|
+
export declare function recordAuditEvent(database: Kysely<AccountDatabase>, { accountId, actorAccountId, application, action, detail, now }: AuditEvent): Promise<string>;
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/audit-event/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,UAAU,EAAE,MAAM,gDAAgD,CAAC;AAEjF;;;;;;GAMG;AACH,wBAAsB,gBAAgB,CACpC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAgB,EAAE,cAAqB,EAAE,WAAkB,EAAE,MAAM,EAAE,MAAW,EAAE,GAAgB,EAAE,EAAE,UAAU,GACjH,OAAO,CAAC,MAAM,CAAC,CAejB"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
|
|
2
|
+
/**
|
|
3
|
+
* Adds a line to the append-only audit log (the database refuses changes to it). Pass a transaction to record it
|
|
4
|
+
* together with the change it describes. Returns the event's UUIDv7.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* await recordAuditEvent(transaction, { accountId, action: "contact.removed", detail: { contactId } });
|
|
8
|
+
*/
|
|
9
|
+
export async function recordAuditEvent(database, { accountId = null, actorAccountId = null, application = null, action, detail = {}, now = Date.now() }) {
|
|
10
|
+
const id = uuidVersion7({ now });
|
|
11
|
+
await database
|
|
12
|
+
.insertInto("audit_event")
|
|
13
|
+
.values({
|
|
14
|
+
id,
|
|
15
|
+
at: new Date(now),
|
|
16
|
+
account_id: accountId,
|
|
17
|
+
actor_account_id: actorAccountId,
|
|
18
|
+
application,
|
|
19
|
+
action,
|
|
20
|
+
detail,
|
|
21
|
+
})
|
|
22
|
+
.execute();
|
|
23
|
+
return id;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/audit-event/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAI/E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,QAAiC,EACjC,EAAE,SAAS,GAAG,IAAI,EAAE,cAAc,GAAG,IAAI,EAAE,WAAW,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,EAAE,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAc;IAElH,MAAM,EAAE,GAAG,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACjC,MAAM,QAAQ;SACX,UAAU,CAAC,aAAa,CAAC;SACzB,MAAM,CAAC;QACN,EAAE;QACF,EAAE,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC;QACjB,UAAU,EAAE,SAAS;QACrB,gBAAgB,EAAE,cAAc;QAChC,WAAW;QACX,MAAM;QACN,MAAM;KACP,CAAC;SACD,OAAO,EAAE,CAAC;IACb,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** What happened, to which account, done by whom, in which application. */
|
|
2
|
+
export interface AuditEvent {
|
|
3
|
+
/** The account it is about. */
|
|
4
|
+
accountId?: string | null;
|
|
5
|
+
/** Who did it, when not the account itself. */
|
|
6
|
+
actorAccountId?: string | null;
|
|
7
|
+
application?: string | null;
|
|
8
|
+
/** `<subject>.<verb>`: `account.created`, `contact.removed`, `role.granted`… */
|
|
9
|
+
action: string;
|
|
10
|
+
/** Ids and kinds, never contact values or secrets. */
|
|
11
|
+
detail?: Record<string, unknown>;
|
|
12
|
+
/** Milliseconds since 1970 (now by default). */
|
|
13
|
+
now?: number;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/audit-event/type.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,MAAM,WAAW,UAAU;IACzB,+BAA+B;IAC/B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,+CAA+C;IAC/C,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,gFAAgF;IAChF,MAAM,EAAE,MAAM,CAAC;IACf,sDAAsD;IACtD,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/audit-event/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
import type { BrowserSession } from "@sharadox/account-web-library/browser-session-read/type";
|
|
4
|
+
/**
|
|
5
|
+
* The accounts signed in on a browser, from its cookie's token, leaving out sessions past their end or idle longer
|
|
6
|
+
* than `idleSeconds` (7 days by default); marks the browser and those sessions as seen now. Undefined for a missing,
|
|
7
|
+
* unknown or expired token.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const session = await readBrowserSession(database, { token: cookies.get("__Host-account")?.value });
|
|
11
|
+
*/
|
|
12
|
+
export declare function readBrowserSession(database: Kysely<AccountDatabase>, { token, idleSeconds, now, }: {
|
|
13
|
+
token: string | undefined;
|
|
14
|
+
idleSeconds?: number;
|
|
15
|
+
now?: number;
|
|
16
|
+
}): Promise<BrowserSession | undefined>;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/browser-session-read/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,EAAE,cAAc,EAAE,MAAM,yDAAyD,CAAC;AAG9F;;;;;;;GAOG;AACH,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EACE,KAAK,EACL,WAAwB,EACxB,GAAgB,GACjB,EAAE;IAAE,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GACnE,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAuCrC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { hashSessionToken } from "@sharadox/account-web-library/session-token";
|
|
2
|
+
/**
|
|
3
|
+
* The accounts signed in on a browser, from its cookie's token, leaving out sessions past their end or idle longer
|
|
4
|
+
* than `idleSeconds` (7 days by default); marks the browser and those sessions as seen now. Undefined for a missing,
|
|
5
|
+
* unknown or expired token.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const session = await readBrowserSession(database, { token: cookies.get("__Host-account")?.value });
|
|
9
|
+
*/
|
|
10
|
+
export async function readBrowserSession(database, { token, idleSeconds = 7 * 86_400, now = Date.now(), }) {
|
|
11
|
+
if (!token)
|
|
12
|
+
return undefined;
|
|
13
|
+
const at = new Date(now);
|
|
14
|
+
const browser = await database
|
|
15
|
+
.selectFrom("browser_session")
|
|
16
|
+
.select("id")
|
|
17
|
+
.where("token_hash", "=", hashSessionToken(token))
|
|
18
|
+
.where("expires_at", ">", at)
|
|
19
|
+
.executeTakeFirst();
|
|
20
|
+
if (!browser)
|
|
21
|
+
return undefined;
|
|
22
|
+
const rows = await database
|
|
23
|
+
.selectFrom("account_session")
|
|
24
|
+
.innerJoin("account", "account.id", "account_session.account_id")
|
|
25
|
+
.select([
|
|
26
|
+
"account_session.id as accountSessionId",
|
|
27
|
+
"account.id as accountId",
|
|
28
|
+
"account.username as username",
|
|
29
|
+
"account_session.created_at as signedInAt",
|
|
30
|
+
"account_session.second_factor_at as secondFactorAt",
|
|
31
|
+
])
|
|
32
|
+
.where("account_session.browser_session_id", "=", browser.id)
|
|
33
|
+
.where("account_session.expires_at", ">", at)
|
|
34
|
+
.where("account_session.last_seen_at", ">", new Date(now - idleSeconds * 1000))
|
|
35
|
+
.orderBy("account_session.created_at")
|
|
36
|
+
.orderBy("account_session.id")
|
|
37
|
+
.execute();
|
|
38
|
+
await database.updateTable("browser_session").set({ last_seen_at: at }).where("id", "=", browser.id).execute();
|
|
39
|
+
if (rows.length > 0) {
|
|
40
|
+
await database
|
|
41
|
+
.updateTable("account_session")
|
|
42
|
+
.set({ last_seen_at: at })
|
|
43
|
+
.where("id", "in", rows.map(({ accountSessionId }) => accountSessionId))
|
|
44
|
+
.execute();
|
|
45
|
+
}
|
|
46
|
+
return { browserSessionId: browser.id, accounts: rows };
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/browser-session-read/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,6CAA6C,CAAC;AAE/E;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,QAAiC,EACjC,EACE,KAAK,EACL,WAAW,GAAG,CAAC,GAAG,MAAM,EACxB,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,GACkD;IAEpE,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IAC7B,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,OAAO,GAAG,MAAM,QAAQ;SAC3B,UAAU,CAAC,iBAAiB,CAAC;SAC7B,MAAM,CAAC,IAAI,CAAC;SACZ,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAC;SACjD,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,EAAE,CAAC;SAC5B,gBAAgB,EAAE,CAAC;IACtB,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,MAAM,IAAI,GAAG,MAAM,QAAQ;SACxB,UAAU,CAAC,iBAAiB,CAAC;SAC7B,SAAS,CAAC,SAAS,EAAE,YAAY,EAAE,4BAA4B,CAAC;SAChE,MAAM,CAAC;QACN,wCAAwC;QACxC,yBAAyB;QACzB,8BAA8B;QAC9B,0CAA0C;QAC1C,oDAAoD;KACrD,CAAC;SACD,KAAK,CAAC,oCAAoC,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC;SAC5D,KAAK,CAAC,4BAA4B,EAAE,GAAG,EAAE,EAAE,CAAC;SAC5C,KAAK,CAAC,8BAA8B,EAAE,GAAG,EAAE,IAAI,IAAI,CAAC,GAAG,GAAG,WAAW,GAAG,IAAI,CAAC,CAAC;SAC9E,OAAO,CAAC,4BAA4B,CAAC;SACrC,OAAO,CAAC,oBAAoB,CAAC;SAC7B,OAAO,EAAE,CAAC;IACb,MAAM,QAAQ,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;IAC/G,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpB,MAAM,QAAQ;aACX,WAAW,CAAC,iBAAiB,CAAC;aAC9B,GAAG,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,CAAC;aACzB,KAAK,CACJ,IAAI,EACJ,IAAI,EACJ,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,gBAAgB,EAAE,EAAE,EAAE,CAAC,gBAAgB,CAAC,CACrD;aACA,OAAO,EAAE,CAAC;IACf,CAAC;IACD,OAAO,EAAE,gBAAgB,EAAE,OAAO,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** An account signed in on the browser, as the account selector shows it. */
|
|
2
|
+
export interface SignedInAccount {
|
|
3
|
+
accountSessionId: string;
|
|
4
|
+
accountId: string;
|
|
5
|
+
username: string;
|
|
6
|
+
signedInAt: Date;
|
|
7
|
+
secondFactorAt: Date | null;
|
|
8
|
+
}
|
|
9
|
+
export interface BrowserSession {
|
|
10
|
+
browserSessionId: string;
|
|
11
|
+
/** In the order they signed in. */
|
|
12
|
+
accounts: SignedInAccount[];
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/browser-session-read/type.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,MAAM,WAAW,eAAe;IAC9B,gBAAgB,EAAE,MAAM,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,IAAI,CAAC;IACjB,cAAc,EAAE,IAAI,GAAG,IAAI,CAAC;CAC7B;AAED,MAAM,WAAW,cAAc;IAC7B,gBAAgB,EAAE,MAAM,CAAC;IACzB,mCAAmC;IACnC,QAAQ,EAAE,eAAe,EAAE,CAAC;CAC7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/browser-session-read/type.ts"],"names":[],"mappings":""}
|
|
@@ -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":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAG/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAC9E,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,6CAA6C,CAAC;AAEjG,oCA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,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,CAAC,cAAc,CAAC;aAC1B,SAAS,EAAE;aACX,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;aAC3B,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,OAAO,CAAC;aAC9B,SAAS,EAAE;aACX,gBAAgB,EAAE,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AC,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":""}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
import type { ContactAdditionRequest, ContactAdditionResult } from "@sharadox/account-web-library/contact-addition/type";
|
|
4
|
+
/**
|
|
5
|
+
* Adds a contact to an account, unverified until a one-time code sent to it comes back (`verifyContact`). Refuses
|
|
6
|
+
* one the account already has, or one another account has verified. Ask for a fresh code first (step-up) and tell
|
|
7
|
+
* the other contacts.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const result = await addContact(database, { accountId, contact: { kind: "email", value } });
|
|
11
|
+
*/
|
|
12
|
+
export declare function addContact(database: Kysely<AccountDatabase>, { accountId, contact, now }: ContactAdditionRequest): Promise<ContactAdditionResult>;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-addition/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F,OAAO,KAAK,EACV,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,qDAAqD,CAAC;AAI7D;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAC9B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,OAAO,EAAE,GAAgB,EAAE,EAAE,sBAAsB,GAC/D,OAAO,CAAC,qBAAqB,CAAC,CA8BhC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
|
|
2
|
+
import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
|
|
3
|
+
import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
|
|
4
|
+
import { databaseError } from "@sharadox/account-web-library/database-error";
|
|
5
|
+
/**
|
|
6
|
+
* Adds a contact to an account, unverified until a one-time code sent to it comes back (`verifyContact`). Refuses
|
|
7
|
+
* one the account already has, or one another account has verified. Ask for a fresh code first (step-up) and tell
|
|
8
|
+
* the other contacts.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const result = await addContact(database, { accountId, contact: { kind: "email", value } });
|
|
12
|
+
*/
|
|
13
|
+
export async function addContact(database, { accountId, contact, now = Date.now() }) {
|
|
14
|
+
const { kind, value } = normalizeContact(contact);
|
|
15
|
+
const contactId = uuidVersion7({ now });
|
|
16
|
+
try {
|
|
17
|
+
return await database.transaction().execute(async (transaction) => {
|
|
18
|
+
const account = await transaction
|
|
19
|
+
.selectFrom("account")
|
|
20
|
+
.select("id")
|
|
21
|
+
.where("id", "=", accountId)
|
|
22
|
+
.executeTakeFirst();
|
|
23
|
+
if (!account)
|
|
24
|
+
return { added: false, reason: "no-account" };
|
|
25
|
+
const owner = await transaction
|
|
26
|
+
.selectFrom("contact")
|
|
27
|
+
.select("account_id")
|
|
28
|
+
.where("kind", "=", kind)
|
|
29
|
+
.where("value", "=", value)
|
|
30
|
+
.where("verified_at", "is not", null)
|
|
31
|
+
.executeTakeFirst();
|
|
32
|
+
if (owner && owner.account_id !== accountId)
|
|
33
|
+
return { added: false, reason: "contact-taken" };
|
|
34
|
+
await transaction
|
|
35
|
+
.insertInto("contact")
|
|
36
|
+
.values({ id: contactId, account_id: accountId, kind, value, verified_at: null, created_at: new Date(now) })
|
|
37
|
+
.execute();
|
|
38
|
+
await recordAuditEvent(transaction, { accountId, action: "contact.added", detail: { contactId, kind }, now });
|
|
39
|
+
return { added: true, contactId };
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (databaseError(error)?.constraint === "contact_per_account")
|
|
44
|
+
return { added: false, reason: "already-added" };
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-addition/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAE/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAK7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,QAAiC,EACjC,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAA0B;IAEhE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClD,MAAM,SAAS,GAAG,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EA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K,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,KAAK,qBAAqB;YAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;QACjH,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
|
|
2
|
+
export interface ContactAdditionRequest {
|
|
3
|
+
accountId: string;
|
|
4
|
+
contact: Contact;
|
|
5
|
+
/** Milliseconds since 1970 (now by default). */
|
|
6
|
+
now?: number;
|
|
7
|
+
}
|
|
8
|
+
export type ContactAdditionResult = {
|
|
9
|
+
added: true;
|
|
10
|
+
contactId: string;
|
|
11
|
+
} | {
|
|
12
|
+
added: false;
|
|
13
|
+
reason: "already-added" | "contact-taken" | "no-account";
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/contact-addition/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,sBAAsB;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,qBAAqB,GAC/B;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,eAAe,GAAG,eAAe,GAAG,YAAY,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/contact-addition/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
|
|
2
|
+
/** The kinds of contact this version can store and send codes to. */
|
|
3
|
+
export declare const CONTACT_KINDS: string[];
|
|
4
|
+
/**
|
|
5
|
+
* A contact in the one form it is stored and compared in (an email trimmed and lowercase…). Throws for a kind this
|
|
6
|
+
* version doesn't know, or a value that isn't valid for its kind.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* normalizeContact({ kind: "email", value: " Luis@Example.com " }); // { kind: "email", value: "luis@example.com" }
|
|
10
|
+
*/
|
|
11
|
+
export declare function normalizeContact({ kind, value }: Contact): Contact;
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-kind/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAI/E,qEAAqE;AACrE,eAAO,MAAM,aAAa,UAA2B,CAAC;AAEtD;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,OAAO,GAAG,OAAO,CAIlE"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { normalizeEmail } from "@sharadox/account-web-library/email-contact";
|
|
2
|
+
const NORMALIZERS = { email: normalizeEmail };
|
|
3
|
+
/** The kinds of contact this version can store and send codes to. */
|
|
4
|
+
export const CONTACT_KINDS = Object.keys(NORMALIZERS);
|
|
5
|
+
/**
|
|
6
|
+
* A contact in the one form it is stored and compared in (an email trimmed and lowercase…). Throws for a kind this
|
|
7
|
+
* version doesn't know, or a value that isn't valid for its kind.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* normalizeContact({ kind: "email", value: " Luis@Example.com " }); // { kind: "email", value: "luis@example.com" }
|
|
11
|
+
*/
|
|
12
|
+
export function normalizeContact({ kind, value }) {
|
|
13
|
+
const normalize = NORMALIZERS[kind];
|
|
14
|
+
if (!normalize)
|
|
15
|
+
throw new Error(`unknown contact kind "${kind}"`);
|
|
16
|
+
return { kind, value: normalize(value) };
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-kind/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,6CAA6C,CAAC;AAG7E,MAAM,WAAW,GAA8C,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC;AAEzF,qEAAqE;AACrE,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,EAAE,IAAI,EAAE,KAAK,EAAW;IACvD,MAAM,SAAS,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IACpC,IAAI,CAAC,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,GAAG,CAAC,CAAC;IAClE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/contact-kind/type.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/contact-kind/type.ts"],"names":[],"mappings":""}
|