@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,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,CAAC;AAE7E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAiC,EACjC,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAA0D;IAElG,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;YAChE,MAAM,OAAO,GAAG,MAAM,WAAW;iBAC9B,WAAW,CAAC,SAAS,CAAC;iBACtB,GAAG,CAAC,EAAE,WAAW,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;iBACnC,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,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAW,CAAC;YACxE,MAAM,gBAAgB,CAAC,WAAW,EAAE;gBAClC,SAAS;gBACT,MAAM,EAAE,kBAAkB;gBAC1B,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE;gBACzC,GAAG;aACJ,CAAC,CAAC;YACH,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAW,CAAC;QACrC,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,KAAK,kBAAkB;YAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;QACjH,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
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 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,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,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC;IAC1F,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClD,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,SAAS,GAAG,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,MAAM,SAAS,GAAG,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,IAAI,CAAC;QACH,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;YACzD,MAAM,WAAW;iBACd,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC;iBACvG,OAAO,EAAE,CAAC;YACb,MAAM,WAAW;iBACd,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;iBAC9F,OAAO,EAAE,CAAC;YACb,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;QAC5G,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,UAAU,GAAG,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC;QACpD,IAAI,UAAU,KAAK,sBAAsB;YAAE,OAAO,EAAE,OAAO,EAAE,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"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
/**
|
|
3
|
+
* A new session token (256 random bits, base64url) for a cookie, and the hash to store instead: a leaked table gives
|
|
4
|
+
* no sessions. A plain SHA-256 is enough for tokens this random.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const { token, hash } = createSessionToken();
|
|
8
|
+
*/
|
|
9
|
+
export function createSessionToken() {
|
|
10
|
+
const token = randomBytes(32).toString("base64url");
|
|
11
|
+
return { token, hash: hashSessionToken(token) };
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The stored form of a session token.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* hashSessionToken(cookie); // compare with browser_session.token_hash
|
|
18
|
+
*/
|
|
19
|
+
export function hashSessionToken(token) {
|
|
20
|
+
return createHash("sha256").update(token).digest("base64url");
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/session-token/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB;IAChC,MAAM,KAAK,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IACpD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;AAClD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAChE,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MemoryDatabase } from "@sharadox/database-web-library/memory-database/type";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* A migrated account database in memory, for this library's tests (and applications' own): Postgres 18 through
|
|
5
|
+
* PGlite, so every constraint and trigger runs as in production.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const memory = await createTestAccountDatabase();
|
|
9
|
+
* // … test …
|
|
10
|
+
* await memory.close();
|
|
11
|
+
*/
|
|
12
|
+
export declare function createTestAccountDatabase(): Promise<MemoryDatabase<AccountDatabase>>;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/test-database/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qDAAqD,CAAC;AAC1F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAG3F;;;;;;;;GAQG;AACH,wBAAsB,yBAAyB,IAAI,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC,CAAC,CAI1F"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createMemoryDatabase } from "@sharadox/database-web-library/memory-database";
|
|
2
|
+
import { migrateAccount } from "@sharadox/account-web-library/account-migration";
|
|
3
|
+
/**
|
|
4
|
+
* A migrated account database in memory, for this library's tests (and applications' own): Postgres 18 through
|
|
5
|
+
* PGlite, so every constraint and trigger runs as in production.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const memory = await createTestAccountDatabase();
|
|
9
|
+
* // … test …
|
|
10
|
+
* await memory.close();
|
|
11
|
+
*/
|
|
12
|
+
export async function createTestAccountDatabase() {
|
|
13
|
+
const memory = await createMemoryDatabase();
|
|
14
|
+
await migrateAccount(memory.database);
|
|
15
|
+
return memory;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/test-database/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,gDAAgD,CAAC;AAGtF,OAAO,EAAE,cAAc,EAAE,MAAM,iDAAiD,CAAC;AAEjF;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB;IAC7C,MAAM,MAAM,GAAG,MAAM,oBAAoB,EAAmB,CAAC;IAC7D,MAAM,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACtC,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* Whether a username can be taken: `available`, `taken` (in any case: `aBc` is taken when `abc` exists) or `invalid`
|
|
5
|
+
* (outside the rules). Rate limit callers: it tells whether an account exists.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await usernameAvailability(database, "Luis-Pires"); // "available"
|
|
9
|
+
*/
|
|
10
|
+
export declare function usernameAvailability(database: Kysely<AccountDatabase>, username: string): Promise<"available" | "taken" | "invalid">;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/username-availability/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAG3F;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,WAAW,GAAG,OAAO,GAAG,SAAS,CAAC,CAS5C"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
import { checkUsername } from "@sharadox/account-web-library/username";
|
|
3
|
+
/**
|
|
4
|
+
* Whether a username can be taken: `available`, `taken` (in any case: `aBc` is taken when `abc` exists) or `invalid`
|
|
5
|
+
* (outside the rules). Rate limit callers: it tells whether an account exists.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await usernameAvailability(database, "Luis-Pires"); // "available"
|
|
9
|
+
*/
|
|
10
|
+
export async function usernameAvailability(database, username) {
|
|
11
|
+
const check = checkUsername(username);
|
|
12
|
+
if (!check.valid)
|
|
13
|
+
return "invalid";
|
|
14
|
+
const found = await database
|
|
15
|
+
.selectFrom("account")
|
|
16
|
+
.select("id")
|
|
17
|
+
.where(sql `lower(username)`, "=", check.key)
|
|
18
|
+
.executeTakeFirst();
|
|
19
|
+
return found ? "taken" : "available";
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/username-availability/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAe,MAAM,QAAQ,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AAEvE;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,QAAiC,EACjC,QAAgB;IAEhB,MAAM,KAAK,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;IACtC,IAAI,CAAC,KAAK,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IACnC,MAAM,KAAK,GAAG,MAAM,QAAQ;SACzB,UAAU,CAAC,SAAS,CAAC;SACrB,MAAM,CAAC,IAAI,CAAC;SACZ,KAAK,CAAC,GAAG,CAAQ,iBAAiB,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC;SACnD,gBAAgB,EAAE,CAAC;IACtB,OAAO,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
import type { UsernameChangeResult } from "@sharadox/account-web-library/username-change/type";
|
|
4
|
+
/**
|
|
5
|
+
* Changes an account's username whenever its owner wants; the old one is free for anyone at once. Accounts are
|
|
6
|
+
* referenced by id, so nothing breaks; the audit log keeps the history. Changing only the case is allowed.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* await changeUsername(database, { accountId, username: "Luis-Pires" }); // { changed: true }
|
|
10
|
+
*/
|
|
11
|
+
export declare function changeUsername(database: Kysely<AccountDatabase>, { accountId, username }: {
|
|
12
|
+
accountId: string;
|
|
13
|
+
username: string;
|
|
14
|
+
}): Promise<UsernameChangeResult>;
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/username-change/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oDAAoD,CAAC;AAE/F;;;;;;GAMG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC/D,OAAO,CAAC,oBAAoB,CAAC,CAwB/B"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
|
|
2
|
+
import { databaseError } from "@sharadox/account-web-library/database-error";
|
|
3
|
+
import { checkUsername } from "@sharadox/account-web-library/username";
|
|
4
|
+
/**
|
|
5
|
+
* Changes an account's username whenever its owner wants; the old one is free for anyone at once. Accounts are
|
|
6
|
+
* referenced by id, so nothing breaks; the audit log keeps the history. Changing only the case is allowed.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* await changeUsername(database, { accountId, username: "Luis-Pires" }); // { changed: true }
|
|
10
|
+
*/
|
|
11
|
+
export async function changeUsername(database, { accountId, username }) {
|
|
12
|
+
if (!checkUsername(username).valid)
|
|
13
|
+
return { changed: false, reason: "username-invalid" };
|
|
14
|
+
try {
|
|
15
|
+
return await database.transaction().execute(async (transaction) => {
|
|
16
|
+
const current = await transaction
|
|
17
|
+
.selectFrom("account")
|
|
18
|
+
.select("username")
|
|
19
|
+
.where("id", "=", accountId)
|
|
20
|
+
.forUpdate()
|
|
21
|
+
.executeTakeFirst();
|
|
22
|
+
if (!current)
|
|
23
|
+
return { changed: false, reason: "no-account" };
|
|
24
|
+
await transaction.updateTable("account").set({ username }).where("id", "=", accountId).execute();
|
|
25
|
+
await recordAuditEvent(transaction, {
|
|
26
|
+
accountId,
|
|
27
|
+
action: "account.username-changed",
|
|
28
|
+
detail: { from: current.username, to: username },
|
|
29
|
+
});
|
|
30
|
+
return { changed: true };
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (databaseError(error)?.constraint === "account_username_key")
|
|
35
|
+
return { changed: false, reason: "username-taken" };
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/username-change/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AAGvE;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,QAAiC,EACjC,EAAE,SAAS,EAAE,QAAQ,EAA2C;IAEhE,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,KAAK;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC;IAC1F,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAiC,EAAE;YAC/F,MAAM,OAAO,GAAG,MAAM,WAAW;iBAC9B,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,UAAU,CAAC;iBAClB,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;iBAC3B,SAAS,EAAE;iBACX,gBAAgB,EAAE,CAAC;YACtB,IAAI,CAAC,OAAO;gBAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;YAC9D,MAAM,WAAW,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC,OAAO,EAAE,CAAC;YACjG,MAAM,gBAAgB,CAAC,WAAW,EAAE;gBAClC,SAAS;gBACT,MAAM,EAAE,0BAA0B;gBAClC,MAAM,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE;aACjD,CAAC,CAAC;YACH,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,KAAK,sBAAsB;YAC7D,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,CAAC;QACtD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/username-change/type.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,oBAAoB,GAC9B;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,kBAAkB,GAAG,gBAAgB,GAAG,YAAY,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/username-change/type.ts"],"names":[],"mappings":""}
|