@sharadox/account-web-library 0.1.0-development.0 → 0.1.0-development.15
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 +103 -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-channel/type.d.ts +20 -0
- package/distribution/contact-channel/type.d.ts.map +1 -0
- package/distribution/contact-channel/type.js +2 -0
- package/distribution/contact-channel/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/email-channel/index.d.ts +13 -0
- package/distribution/email-channel/index.d.ts.map +1 -0
- package/distribution/email-channel/index.js +90 -0
- package/distribution/email-channel/index.js.map +1 -0
- package/distribution/email-channel/type.d.ts +17 -0
- package/distribution/email-channel/type.d.ts.map +1 -0
- package/distribution/email-channel/type.js +2 -0
- package/distribution/email-channel/type.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 +19 -17
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/contact-channel/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":""}
|
|
@@ -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 { Contact } from "@sharadox/account-web-library/contact-kind/type";
|
|
4
|
+
/**
|
|
5
|
+
* The account a verified contact belongs to (written any way: it is normalized first), or undefined. Unverified
|
|
6
|
+
* contacts never count, so claiming someone's address proves nothing.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const found = await findAccountByContact(database, { kind: "email", value: typed });
|
|
10
|
+
*/
|
|
11
|
+
export declare function findAccountByContact(database: Kysely<AccountDatabase>, contact: Contact): Promise<{
|
|
12
|
+
accountId: string;
|
|
13
|
+
contactId: string;
|
|
14
|
+
signIn: boolean;
|
|
15
|
+
} | undefined>;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-lookup/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,OAAO,EAAE,OAAO,GACf,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GAAG,SAAS,CAAC,CAUhF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
|
|
2
|
+
/**
|
|
3
|
+
* The account a verified contact belongs to (written any way: it is normalized first), or undefined. Unverified
|
|
4
|
+
* contacts never count, so claiming someone's address proves nothing.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const found = await findAccountByContact(database, { kind: "email", value: typed });
|
|
8
|
+
*/
|
|
9
|
+
export async function findAccountByContact(database, contact) {
|
|
10
|
+
const { kind, value } = normalizeContact(contact);
|
|
11
|
+
const row = await database
|
|
12
|
+
.selectFrom("contact")
|
|
13
|
+
.select(["id", "account_id", "sign_in"])
|
|
14
|
+
.where("kind", "=", kind)
|
|
15
|
+
.where("value", "=", value)
|
|
16
|
+
.where("verified_at", "is not", null)
|
|
17
|
+
.executeTakeFirst();
|
|
18
|
+
return row && { accountId: row.account_id, contactId: row.id, signIn: row.sign_in };
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-lookup/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAG9E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,QAAiC,EACjC,OAAgB;IAEhB,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClD,MAAM,GAAG,GAAG,MAAM,QAAQ;SACvB,UAAU,CAAC,SAAS,CAAC;SACrB,MAAM,CAAC,CAAC,IAAI,EAAE,YAAY,EAAE,SAAS,CAAC,CAAC;SACvC,KAAK,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC;SACxB,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC;SAC1B,KAAK,CAAC,aAAa,EAAE,QAAQ,EAAE,IAAI,CAAC;SACpC,gBAAgB,EAAE,CAAC;IACtB,OAAO,GAAG,IAAI,EAAE,SAAS,EAAE,GAAG,CAAC,UAAU,EAAE,SAAS,EAAE,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC;AACtF,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* Removes a contact from an account. Refuses to remove the last verified sign-in contact of a person (the database
|
|
5
|
+
* checks it when the change commits). Ask for a fresh code first (step-up) and tell the other contacts.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const result = await removeContact(database, { accountId, contactId });
|
|
9
|
+
*/
|
|
10
|
+
export declare function removeContact(database: Kysely<AccountDatabase>, { accountId, contactId }: {
|
|
11
|
+
accountId: string;
|
|
12
|
+
contactId: string;
|
|
13
|
+
}): Promise<{
|
|
14
|
+
removed: true;
|
|
15
|
+
} | {
|
|
16
|
+
removed: false;
|
|
17
|
+
reason: "no-contact" | "last-sign-in-contact";
|
|
18
|
+
}>;
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-removal/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACjE,OAAO,CAAC;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,YAAY,GAAG,sBAAsB,CAAA;CAAE,CAAC,CAqBhG"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
|
|
2
|
+
import { databaseError } from "@sharadox/account-web-library/database-error";
|
|
3
|
+
/**
|
|
4
|
+
* Removes a contact from an account. Refuses to remove the last verified sign-in contact of a person (the database
|
|
5
|
+
* checks it when the change commits). Ask for a fresh code first (step-up) and tell the other contacts.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const result = await removeContact(database, { accountId, contactId });
|
|
9
|
+
*/
|
|
10
|
+
export async function removeContact(database, { accountId, contactId }) {
|
|
11
|
+
try {
|
|
12
|
+
return await database.transaction().execute(async (transaction) => {
|
|
13
|
+
const removed = await transaction
|
|
14
|
+
.deleteFrom("contact")
|
|
15
|
+
.where("id", "=", contactId)
|
|
16
|
+
.where("account_id", "=", accountId)
|
|
17
|
+
.returning("kind")
|
|
18
|
+
.executeTakeFirst();
|
|
19
|
+
if (!removed)
|
|
20
|
+
return { removed: false, reason: "no-contact" };
|
|
21
|
+
await recordAuditEvent(transaction, {
|
|
22
|
+
accountId,
|
|
23
|
+
action: "contact.removed",
|
|
24
|
+
detail: { contactId, kind: removed.kind },
|
|
25
|
+
});
|
|
26
|
+
return { removed: true };
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (databaseError(error)?.code === "23514")
|
|
31
|
+
return { removed: false, reason: "last-sign-in-contact" };
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-removal/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAiC,EACjC,EAAE,SAAS,EAAE,SAAS,EAA4C;IAElE,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;YAChE,MAAM,OAAO,GAAG,MAAM,WAAW;iBAC9B,UAAU,CAAC,SAAS,CAAC;iBACrB,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;iBAC3B,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;iBACnC,SAAS,CAAC,MAAM,CAAC;iBACjB,gBAAgB,EAAE,CAAC;YACtB,IAAI,CAAC,OAAO;gBAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAW,CAAC;YACvE,MAAM,gBAAgB,CAAC,WAAW,EAAE;gBAClC,SAAS;gBACT,MAAM,EAAE,iBAAiB;gBACzB,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE;aAC1C,CAAC,CAAC;YACH,OAAO,EAAE,OAAO,EAAE,IAAI,EAAW,CAAC;QACpC,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,IAAI,KAAK,OAAO;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,sBAAsB,EAAE,CAAC;QACtG,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* Marks an account's contact verified, once the one-time code sent to it came back. Refuses when another account
|
|
5
|
+
* verified the same contact first (a verified contact belongs to one account only).
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await verifyContact(database, { accountId, contactId }); // { verified: true }
|
|
9
|
+
*/
|
|
10
|
+
export declare function verifyContact(database: Kysely<AccountDatabase>, { accountId, contactId, now }: {
|
|
11
|
+
accountId: string;
|
|
12
|
+
contactId: string;
|
|
13
|
+
now?: number;
|
|
14
|
+
}): Promise<{
|
|
15
|
+
verified: true;
|
|
16
|
+
} | {
|
|
17
|
+
verified: false;
|
|
18
|
+
reason: "no-contact" | "contact-taken";
|
|
19
|
+
}>;
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-verification/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,SAAS,EAAE,GAAgB,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GACjG,OAAO,CAAC;IAAE,QAAQ,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,QAAQ,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,YAAY,GAAG,eAAe,CAAA;CAAE,CAAC,CAuB3F"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
|
|
2
|
+
import { databaseError } from "@sharadox/account-web-library/database-error";
|
|
3
|
+
/**
|
|
4
|
+
* Marks an account's contact verified, once the one-time code sent to it came back. Refuses when another account
|
|
5
|
+
* verified the same contact first (a verified contact belongs to one account only).
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await verifyContact(database, { accountId, contactId }); // { verified: true }
|
|
9
|
+
*/
|
|
10
|
+
export async function verifyContact(database, { accountId, contactId, now = Date.now() }) {
|
|
11
|
+
try {
|
|
12
|
+
return await database.transaction().execute(async (transaction) => {
|
|
13
|
+
const updated = await transaction
|
|
14
|
+
.updateTable("contact")
|
|
15
|
+
.set({ verified_at: new Date(now) })
|
|
16
|
+
.where("id", "=", contactId)
|
|
17
|
+
.where("account_id", "=", accountId)
|
|
18
|
+
.returning("kind")
|
|
19
|
+
.executeTakeFirst();
|
|
20
|
+
if (!updated)
|
|
21
|
+
return { verified: false, reason: "no-contact" };
|
|
22
|
+
await recordAuditEvent(transaction, {
|
|
23
|
+
accountId,
|
|
24
|
+
action: "contact.verified",
|
|
25
|
+
detail: { contactId, kind: updated.kind },
|
|
26
|
+
now,
|
|
27
|
+
});
|
|
28
|
+
return { verified: true };
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (databaseError(error)?.constraint === "contact_verified")
|
|
33
|
+
return { verified: false, reason: "contact-taken" };
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-verification/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAA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` check…) and the constraint that failed, from an error thrown by the
|
|
3
|
+
* driver (Neon's or PGlite's); undefined for any other error.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* if (databaseError(error)?.constraint === "account_username_key") return { reason: "username-taken" };
|
|
7
|
+
*/
|
|
8
|
+
export declare function databaseError(error: unknown): {
|
|
9
|
+
code: string;
|
|
10
|
+
constraint: string | undefined;
|
|
11
|
+
} | undefined;
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/database-error/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,GAAG,SAAS,CAK1G"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Postgres's error code (`23505` unique, `23514` check…) and the constraint that failed, from an error thrown by the
|
|
3
|
+
* driver (Neon's or PGlite's); undefined for any other error.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* if (databaseError(error)?.constraint === "account_username_key") return { reason: "username-taken" };
|
|
7
|
+
*/
|
|
8
|
+
export function databaseError(error) {
|
|
9
|
+
if (typeof error !== "object" || error === null || typeof error.code !== "string")
|
|
10
|
+
return undefined;
|
|
11
|
+
const { code, constraint } = error;
|
|
12
|
+
return { code, constraint };
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/database-error/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,OAAQ,KAA4B,CAAC,IAAI,KAAK,QAAQ;QACvG,OAAO,SAAS,CAAC;IACnB,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,KAA8C,CAAC;IAC5E,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ContactChannel } from "@sharadox/account-web-library/contact-channel/type";
|
|
2
|
+
import type { EmailChannelOptions } from "@sharadox/account-web-library/email-channel/type";
|
|
3
|
+
/**
|
|
4
|
+
* Sends one-time codes by email, in the shared Sharadox email layout (email-web-library), in English or European
|
|
5
|
+
* Portuguese (any `pt` reader). The code is in the body only, never the subject (lock screens show subjects), with
|
|
6
|
+
* no link to click: nothing to imitate in a phishing email.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const channel = createEmailChannel({ sender: createAmazonSesSender(configuration), from, design });
|
|
10
|
+
* await channel.send({ contact, code, purpose: "sign-in", language: "pt-PT", expiresAt });
|
|
11
|
+
*/
|
|
12
|
+
export declare function createEmailChannel({ sender, from, design, logos, now, }: EmailChannelOptions): ContactChannel;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/email-channel/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAgB,cAAc,EAAE,MAAM,oDAAoD,CAAC;AACvG,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kDAAkD,CAAC;AAsD5F;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,EACjC,MAAM,EACN,IAAI,EACJ,MAAM,EACN,KAAU,EACV,GAAc,GACf,EAAE,mBAAmB,GAAG,cAAc,CA2CtC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { emailLayout } from "@sharadox/email-web-library/email-layout";
|
|
2
|
+
const ENGLISH = {
|
|
3
|
+
language: "en",
|
|
4
|
+
subject: {
|
|
5
|
+
"sign-in": (company) => `Your ${company} sign-in code`,
|
|
6
|
+
"contact-verification": (company) => `Confirm your address for ${company}`,
|
|
7
|
+
"step-up": (company) => `Your ${company} confirmation code`,
|
|
8
|
+
},
|
|
9
|
+
eyebrow: { "sign-in": "Sign in", "contact-verification": "Confirm address", "step-up": "Confirmation" },
|
|
10
|
+
heading: {
|
|
11
|
+
"sign-in": "Your sign-in code",
|
|
12
|
+
"contact-verification": "Confirm this address",
|
|
13
|
+
"step-up": "Confirm it's you",
|
|
14
|
+
},
|
|
15
|
+
label: "Code",
|
|
16
|
+
lifetime: (minutes) => `It works once, for ${minutes} ${minutes === 1 ? "minute" : "minutes"}.`,
|
|
17
|
+
ignore: "If you didn't ask for it, you can ignore this email: without the code, nobody gets in.",
|
|
18
|
+
never: "We will never ask you for this code.",
|
|
19
|
+
closing: "Best regards,",
|
|
20
|
+
};
|
|
21
|
+
const PORTUGUESE = {
|
|
22
|
+
language: "pt-PT",
|
|
23
|
+
subject: {
|
|
24
|
+
"sign-in": (company) => `O seu código para entrar em ${company}`,
|
|
25
|
+
"contact-verification": (company) => `Confirme o seu endereço em ${company}`,
|
|
26
|
+
"step-up": (company) => `O seu código de confirmação de ${company}`,
|
|
27
|
+
},
|
|
28
|
+
eyebrow: { "sign-in": "Entrar", "contact-verification": "Confirmar endereço", "step-up": "Confirmação" },
|
|
29
|
+
heading: {
|
|
30
|
+
"sign-in": "O seu código para entrar",
|
|
31
|
+
"contact-verification": "Confirme este endereço",
|
|
32
|
+
"step-up": "Confirme que é você",
|
|
33
|
+
},
|
|
34
|
+
label: "Código",
|
|
35
|
+
lifetime: (minutes) => `Funciona uma vez, durante ${minutes} ${minutes === 1 ? "minuto" : "minutos"}.`,
|
|
36
|
+
ignore: "Se não o pediu, pode ignorar este email: sem o código, ninguém entra.",
|
|
37
|
+
never: "Nunca lhe pediremos este código.",
|
|
38
|
+
closing: "Com os melhores cumprimentos,",
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Sends one-time codes by email, in the shared Sharadox email layout (email-web-library), in English or European
|
|
42
|
+
* Portuguese (any `pt` reader). The code is in the body only, never the subject (lock screens show subjects), with
|
|
43
|
+
* no link to click: nothing to imitate in a phishing email.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const channel = createEmailChannel({ sender: createAmazonSesSender(configuration), from, design });
|
|
47
|
+
* await channel.send({ contact, code, purpose: "sign-in", language: "pt-PT", expiresAt });
|
|
48
|
+
*/
|
|
49
|
+
export function createEmailChannel({ sender, from, design, logos = [], now = Date.now, }) {
|
|
50
|
+
const carried = { light: logos.some(({ id }) => id === "logo"), dark: logos.some(({ id }) => id === "logo-dark") };
|
|
51
|
+
return {
|
|
52
|
+
kind: "email",
|
|
53
|
+
async send({ contact, code, purpose, language, expiresAt }) {
|
|
54
|
+
if (contact.kind !== "email")
|
|
55
|
+
throw new Error(`the email channel can't send to a "${contact.kind}" contact`);
|
|
56
|
+
const words = language.toLowerCase().startsWith("pt") ? PORTUGUESE : ENGLISH;
|
|
57
|
+
const shown = code.replace(/(.{4})(?=.)/g, "$1 ");
|
|
58
|
+
const minutes = Math.max(1, Math.ceil((expiresAt.getTime() - now()) / 60_000));
|
|
59
|
+
const paragraphs = [words.lifetime(minutes), words.ignore, words.never];
|
|
60
|
+
const html = emailLayout({
|
|
61
|
+
language: words.language,
|
|
62
|
+
preview: words.heading[purpose],
|
|
63
|
+
eyebrow: words.eyebrow[purpose],
|
|
64
|
+
heading: words.heading[purpose],
|
|
65
|
+
rows: [{ label: words.label, value: shown }],
|
|
66
|
+
paragraphs,
|
|
67
|
+
closing: words.closing,
|
|
68
|
+
}, design, carried);
|
|
69
|
+
const text = [
|
|
70
|
+
words.heading[purpose],
|
|
71
|
+
"",
|
|
72
|
+
`${words.label}: ${shown}`,
|
|
73
|
+
"",
|
|
74
|
+
...paragraphs,
|
|
75
|
+
"",
|
|
76
|
+
words.closing,
|
|
77
|
+
design.name,
|
|
78
|
+
].join("\n");
|
|
79
|
+
await sender.send({
|
|
80
|
+
from,
|
|
81
|
+
to: contact.value,
|
|
82
|
+
subject: words.subject[purpose](design.name),
|
|
83
|
+
html,
|
|
84
|
+
text,
|
|
85
|
+
...(logos.length > 0 ? { inlineImages: logos } : {}),
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/email-channel/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0CAA0C,CAAC;AAiBvE,MAAM,OAAO,GAAU;IACrB,QAAQ,EAAE,IAAI;IACd,OAAO,EAAE;QACP,SAAS,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,QAAQ,OAAO,eAAe;QACtD,sBAAsB,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,4BAA4B,OAAO,EAAE;QAC1E,SAAS,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,QAAQ,OAAO,oBAAoB;KAC5D;IACD,OAAO,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,SAAS,EAAE,cAAc,EAAE;IACvG,OAAO,EAAE;QACP,SAAS,EAAE,mBAAmB;QAC9B,sBAAsB,EAAE,sBAAsB;QAC9C,SAAS,EAAE,kBAAkB;KAC9B;IACD,KAAK,EAAE,MAAM;IACb,QAAQ,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,sBAAsB,OAAO,IAAI,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,GAAG;IAC/F,MAAM,EAAE,wFAAwF;IAChG,KAAK,EAAE,sCAAsC;IAC7C,OAAO,EAAE,eAAe;CACzB,CAAC;AAEF,MAAM,UAAU,GAAU;IACxB,QAAQ,EAAE,OAAO;IACjB,OA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,CAAC,OAAO;gBACb,MAAM,CAAC,IAAI;aACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACb,MAAM,MAAM,CAAC,IAAI,CAAC;gBAChB,IAAI;gBACJ,EAAE,EAAE,OAAO,CAAC,KAAK;gBACjB,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC;gBAC5C,IAAI;gBACJ,IAAI;gBACJ,GAAG,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aACrD,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { EmailAddress } from "@sharadox/email-web-library/email-address/type";
|
|
2
|
+
import type { EmailDesign } from "@sharadox/email-web-library/email-layout/type";
|
|
3
|
+
import type { InlineImage } from "@sharadox/email-web-library/email-message/type";
|
|
4
|
+
import type { EmailSender } from "@sharadox/email-web-library/email-sender/type";
|
|
5
|
+
export interface EmailChannelOptions {
|
|
6
|
+
/** From email-web-library (`createAmazonSesSender`…). */
|
|
7
|
+
sender: EmailSender;
|
|
8
|
+
/** The sending address, for example `"Luis Pires Decor" <account@web.luispiresdecor.com>`. */
|
|
9
|
+
from: EmailAddress;
|
|
10
|
+
/** The company's name, logo size and signature (email-web-library's layout). */
|
|
11
|
+
design: EmailDesign;
|
|
12
|
+
/** The inline images "logo" and "logo-dark", when the design shows a logo. */
|
|
13
|
+
logos?: InlineImage[];
|
|
14
|
+
/** The clock, for how long the code still works (Date.now by default). */
|
|
15
|
+
now?: () => number;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/email-channel/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gDAAgD,CAAC;AACnF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+CAA+C,CAAC;AACjF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gDAAgD,CAAC;AAClF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+CAA+C,CAAC;AAEjF,MAAM,WAAW,mBAAmB;IAClC,yDAAyD;IACzD,MAAM,EAAE,WAAW,CAAC;IACpB,8FAA8F;IAC9F,IAAI,EAAE,YAAY,CAAC;IACnB,gFAAgF;IAChF,MAAM,EAAE,WAAW,CAAC;IACpB,8EAA8E;IAC9E,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;IACtB,0EAA0E;IAC1E,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/email-channel/type.ts"],"names":[],"mappings":""}
|
|
@@ -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 { PersonAccountRequest, PersonAccountResult } from "@sharadox/account-web-library/person-account/type";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a person's account with the contact their one-time code just proved, as its verified sign-in contact, and
|
|
6
|
+
* records it — all or nothing. Refuses a username outside the rules or taken in any case, and a contact another
|
|
7
|
+
* account has verified.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const result = await createPersonAccount(database, { username: "Luis-Pires", contact: { kind: "email", value } });
|
|
11
|
+
* if (!result.created) return explain(result.reason);
|
|
12
|
+
*/
|
|
13
|
+
export declare function createPersonAccount(database: Kysely<AccountDatabase>, { username, contact, now }: PersonAccountRequest): Promise<PersonAccountResult>;
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/person-account/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,mDAAmD,CAAC;AAGnH;;;;;;;;GAQG;AACH,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAgB,EAAE,EAAE,oBAAoB,GAC5D,OAAO,CAAC,mBAAmB,CAAC,CAyB9B"}
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
import { checkUsername } from "@sharadox/account-web-library/username";
|
|
6
|
+
/**
|
|
7
|
+
* Creates a person's account with the contact their one-time code just proved, as its verified sign-in contact, and
|
|
8
|
+
* records it — all or nothing. Refuses a username outside the rules or taken in any case, and a contact another
|
|
9
|
+
* account has verified.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const result = await createPersonAccount(database, { username: "Luis-Pires", contact: { kind: "email", value } });
|
|
13
|
+
* if (!result.created) return explain(result.reason);
|
|
14
|
+
*/
|
|
15
|
+
export async function createPersonAccount(database, { username, contact, now = Date.now() }) {
|
|
16
|
+
if (!checkUsername(username).valid)
|
|
17
|
+
return { created: false, reason: "username-invalid" };
|
|
18
|
+
const { kind, value } = normalizeContact(contact);
|
|
19
|
+
const at = new Date(now);
|
|
20
|
+
const accountId = uuidVersion7({ now });
|
|
21
|
+
const contactId = uuidVersion7({ now });
|
|
22
|
+
try {
|
|
23
|
+
await database.transaction().execute(async (transaction) => {
|
|
24
|
+
await transaction
|
|
25
|
+
.insertInto("account")
|
|
26
|
+
.values({ id: accountId, kind: "person", username, owner_id: null, created_at: at, last_active_at: at })
|
|
27
|
+
.execute();
|
|
28
|
+
await transaction
|
|
29
|
+
.insertInto("contact")
|
|
30
|
+
.values({ id: contactId, account_id: accountId, kind, value, verified_at: at, created_at: at })
|
|
31
|
+
.execute();
|
|
32
|
+
await recordAuditEvent(transaction, { accountId, action: "account.created", detail: { contactId }, now });
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
const constraint = databaseError(error)?.constraint;
|
|
37
|
+
if (constraint === "account_username_key")
|
|
38
|
+
return { created: false, reason: "username-taken" };
|
|
39
|
+
if (constraint === "contact_verified")
|
|
40
|
+
return { created: false, reason: "contact-taken" };
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
return { created: true, accountId, contactId };
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/person-account/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAE/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AAEvE;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,QAAiC,EACjC,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAwB;IAE7D,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,KAAK;QAAE,OAAO,EAAE,O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,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,CAAC;QAC/F,IAAI,UAAU,KAAK,kBAAkB;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;QAC1F,MAAM,KAAK,CAAC;IACd,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
|
|
2
|
+
export interface PersonAccountRequest {
|
|
3
|
+
username: string;
|
|
4
|
+
/** Already proved by a one-time code: it is stored verified. */
|
|
5
|
+
contact: Contact;
|
|
6
|
+
/** Milliseconds since 1970 (now by default). */
|
|
7
|
+
now?: number;
|
|
8
|
+
}
|
|
9
|
+
export type PersonAccountResult = {
|
|
10
|
+
created: true;
|
|
11
|
+
accountId: string;
|
|
12
|
+
contactId: string;
|
|
13
|
+
} | {
|
|
14
|
+
created: false;
|
|
15
|
+
reason: "username-invalid" | "username-taken" | "contact-taken";
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/person-account/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,gEAAgE;IAChE,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,mBAAmB,GAC3B;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACvD;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,kBAAkB,GAAG,gBAAgB,GAAG,eAAe,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/person-account/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
|
+
/**
|
|
4
|
+
* Deletes account sessions past their end or idle longer than `idleSeconds` (7 days by default, as
|
|
5
|
+
* `readBrowserSession`), then browser sessions that ended or hold no account; from a daily job. Returns the counts.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await pruneSessions(database);
|
|
9
|
+
*/
|
|
10
|
+
export declare function pruneSessions(database: Kysely<AccountDatabase>, { idleSeconds, now }?: {
|
|
11
|
+
idleSeconds?: number;
|
|
12
|
+
now?: number;
|
|
13
|
+
}): Promise<{
|
|
14
|
+
accountSessions: number;
|
|
15
|
+
browserSessions: number;
|
|
16
|
+
}>;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/session-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,WAAwB,EAAE,GAAgB,EAAE,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAO,GAC1F,OAAO,CAAC;IAAE,eAAe,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,CAAC,CA4B/D"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
/**
|
|
3
|
+
* Deletes account sessions past their end or idle longer than `idleSeconds` (7 days by default, as
|
|
4
|
+
* `readBrowserSession`), then browser sessions that ended or hold no account; from a daily job. Returns the counts.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* await pruneSessions(database);
|
|
8
|
+
*/
|
|
9
|
+
export async function pruneSessions(database, { idleSeconds = 7 * 86_400, now = Date.now() } = {}) {
|
|
10
|
+
const at = new Date(now);
|
|
11
|
+
const accountSessions = await database
|
|
12
|
+
.deleteFrom("account_session")
|
|
13
|
+
.where((where) => where.or([where("expires_at", "<=", at), where("last_seen_at", "<=", new Date(now - idleSeconds * 1000))]))
|
|
14
|
+
.executeTakeFirst();
|
|
15
|
+
const browserSessions = await database
|
|
16
|
+
.deleteFrom("browser_session")
|
|
17
|
+
.where((where) => where.or([
|
|
18
|
+
where("expires_at", "<=", at),
|
|
19
|
+
where.not(where.exists(where
|
|
20
|
+
.selectFrom("account_session")
|
|
21
|
+
.select(sql `1`.as("one"))
|
|
22
|
+
.whereRef("account_session.browser_session_id", "=", "browser_session.id"))),
|
|
23
|
+
]))
|
|
24
|
+
.executeTakeFirst();
|
|
25
|
+
return {
|
|
26
|
+
accountSessions: Number(accountSessions.numDeletedRows),
|
|
27
|
+
browserSessions: Number(browserSessions.numDeletedRows),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/session-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAe,MAAM,QAAQ,CAAC;AAG1C;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAiC,EACjC,EAAE,WAAW,GAAG,CAAC,GAAG,MAAM,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,KAA6C,EAAE;IAE3F,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,eAAe,GAAG,MAAM,QAAQ;SACnC,UAAU,CAAC,iBAAiB,CAAC;SAC7B,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CACf,KAAK,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC,cAAc,EAAE,IAAI,EAAE,IAAI,IAAI,CAAC,GAAG,GAAG,WAAW,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAC3G;SACA,gBAAgB,EAAE,CAAC;IACtB,MAAM,eAAe,GAAG,MAAM,QAAQ;SACnC,UAAU,CAAC,iBAAiB,CAAC;SAC7B,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CACf,KAAK,CAAC,EAAE,CAAC;QACP,KAAK,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,CAAC;QAC7B,KAAK,CAAC,GAAG,CACP,KAAK,CAAC,MAAM,CACV,KAAK;aACF,UAAU,CAAC,iBAAiB,CAAC;aAC7B,MAAM,CAAC,GAAG,CAAA,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;aACxB,QAAQ,CAAC,oCAAoC,EAAE,GAAG,EAAE,oBAAoB,CAAC,CAC7E,CACF;KACF,CAAC,CACH;SACA,gBAAgB,EAAE,CAAC;IACtB,OAAO;QACL,eAAe,EAAE,MAAM,CAAC,eAAe,CAAC,cAAc,CAAC;QACvD,eAAe,EAAE,MAAM,CAAC,eAAe,CAAC,cAAc,CAAC;KACxD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A new session token (256 random bits, base64url) for a cookie, and the hash to store instead: a leaked table gives
|
|
3
|
+
* no sessions. A plain SHA-256 is enough for tokens this random.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const { token, hash } = createSessionToken();
|
|
7
|
+
*/
|
|
8
|
+
export declare function createSessionToken(): {
|
|
9
|
+
token: string;
|
|
10
|
+
hash: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The stored form of a session token.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* hashSessionToken(cookie); // compare with browser_session.token_hash
|
|
17
|
+
*/
|
|
18
|
+
export declare function hashSessionToken(token: string): string;
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/session-token/index.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,kBAAkB,IAAI;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAGpE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEtD"}
|