@sharadox/account-web-library 0.1.0-code-request.8 → 0.1.0-delivery-failure.20
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 +67 -0
- package/distribution/account-database/type.d.ts +24 -0
- package/distribution/account-database/type.d.ts.map +1 -1
- package/distribution/account-migration/index.d.ts.map +1 -1
- package/distribution/account-migration/index.js +24 -0
- package/distribution/account-migration/index.js.map +1 -1
- package/distribution/account-server/index.d.ts +15 -0
- package/distribution/account-server/index.d.ts.map +1 -0
- package/distribution/account-server/index.js +23 -0
- package/distribution/account-server/index.js.map +1 -0
- package/distribution/account-server/type.d.ts +28 -0
- package/distribution/account-server/type.d.ts.map +1 -0
- package/distribution/account-server/type.js +2 -0
- package/distribution/account-server/type.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/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/challenge-issue/index.d.ts +13 -0
- package/distribution/challenge-issue/index.d.ts.map +1 -0
- package/distribution/challenge-issue/index.js +18 -0
- package/distribution/challenge-issue/index.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/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/secret-derivation/index.d.ts +10 -0
- package/distribution/secret-derivation/index.d.ts.map +1 -0
- package/distribution/secret-derivation/index.js +15 -0
- package/distribution/secret-derivation/index.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/sign-in-finish/index.d.ts +12 -0
- package/distribution/sign-in-finish/index.d.ts.map +1 -0
- package/distribution/sign-in-finish/index.js +47 -0
- package/distribution/sign-in-finish/index.js.map +1 -0
- package/distribution/sign-in-finish/type.d.ts +34 -0
- package/distribution/sign-in-finish/type.d.ts.map +1 -0
- package/distribution/sign-in-finish/type.js +2 -0
- package/distribution/sign-in-finish/type.js.map +1 -0
- package/distribution/sign-in-start/index.d.ts +19 -0
- package/distribution/sign-in-start/index.d.ts.map +1 -0
- package/distribution/sign-in-start/index.js +89 -0
- package/distribution/sign-in-start/index.js.map +1 -0
- package/distribution/sign-in-start/type.d.ts +27 -0
- package/distribution/sign-in-start/type.d.ts.map +1 -0
- package/distribution/sign-in-start/type.js +2 -0
- package/distribution/sign-in-start/type.js.map +1 -0
- package/distribution/sign-up-finish/index.d.ts +12 -0
- package/distribution/sign-up-finish/index.d.ts.map +1 -0
- package/distribution/sign-up-finish/index.js +27 -0
- package/distribution/sign-up-finish/index.js.map +1 -0
- package/distribution/sign-up-finish/type.d.ts +21 -0
- package/distribution/sign-up-finish/type.d.ts.map +1 -0
- package/distribution/sign-up-finish/type.js +2 -0
- package/distribution/sign-up-finish/type.js.map +1 -0
- package/distribution/signed-ticket/index.d.ts +19 -0
- package/distribution/signed-ticket/index.d.ts.map +1 -0
- package/distribution/signed-ticket/index.js +35 -0
- package/distribution/signed-ticket/index.js.map +1 -0
- package/distribution/signed-ticket/type.d.ts +16 -0
- package/distribution/signed-ticket/type.d.ts.map +1 -0
- package/distribution/signed-ticket/type.js +2 -0
- package/distribution/signed-ticket/type.js.map +1 -0
- package/distribution/test-server/index.d.ts +10 -0
- package/distribution/test-server/index.d.ts.map +1 -0
- package/distribution/test-server/index.js +16 -0
- package/distribution/test-server/index.js.map +1 -0
- package/package.json +5 -2
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Challenge } from "@sharadox/protection-web-library/proof-of-work/type";
|
|
2
|
+
import type { AccountServer } from "@sharadox/account-web-library/account-server/type";
|
|
3
|
+
/**
|
|
4
|
+
* A proof-of-work challenge for the sign-in form, at the server's difficulty: the browser solves it in the
|
|
5
|
+
* background (protection-web-library's `solveChallenge`) and sends the solution with the contact.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const challenge = issueChallenge(server); // give it to the page
|
|
9
|
+
*/
|
|
10
|
+
export declare function issueChallenge(server: AccountServer, { now }?: {
|
|
11
|
+
now?: number;
|
|
12
|
+
}): Challenge;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/challenge-issue/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qDAAqD,CAAC;AAErF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAGvF;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,aAAa,EAAE,EAAE,GAAgB,EAAE,GAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,SAAS,CAM5G"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createChallenge } from "@sharadox/protection-web-library/proof-of-work";
|
|
2
|
+
import { signInLimits } from "@sharadox/account-web-library/account-server";
|
|
3
|
+
import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
|
|
4
|
+
/**
|
|
5
|
+
* A proof-of-work challenge for the sign-in form, at the server's difficulty: the browser solves it in the
|
|
6
|
+
* background (protection-web-library's `solveChallenge`) and sends the solution with the contact.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const challenge = issueChallenge(server); // give it to the page
|
|
10
|
+
*/
|
|
11
|
+
export function issueChallenge(server, { now = Date.now() } = {}) {
|
|
12
|
+
return createChallenge({
|
|
13
|
+
secret: deriveSecret(server.secret, "proof-of-work"),
|
|
14
|
+
difficulty: signInLimits(server).difficulty,
|
|
15
|
+
now,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/challenge-issue/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gDAAgD,CAAC;AAEjF,OAAO,EAAE,YAAY,EAAE,MAAM,8CAA8C,CAAC;AAE5E,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAE/E;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,MAAqB,EAAE,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,KAAuB,EAAE;IAC/F,OAAO,eAAe,CAAC;QACrB,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC;QACpD,UAAU,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC,UAAU;QAC3C,GAAG;KACJ,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
/** A one-time code to deliver to a contact. */
|
|
4
|
+
export interface CodeDelivery {
|
|
5
|
+
contact: Contact;
|
|
6
|
+
code: string;
|
|
7
|
+
purpose: CodePurpose;
|
|
8
|
+
/** The reader's language (`en`, `pt-PT`…); channels fall back to English. */
|
|
9
|
+
language: string;
|
|
10
|
+
expiresAt: Date;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* How codes reach one kind of contact (email now; phone, WhatsApp, Messenger, Instagram later). Cap the sends of paid
|
|
14
|
+
* channels before calling `send` (protection-web-library's `spendFromCap`).
|
|
15
|
+
*/
|
|
16
|
+
export interface ContactChannel {
|
|
17
|
+
kind: string;
|
|
18
|
+
send(delivery: CodeDelivery): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/contact-channel/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iDAAiD,CAAC;AACnF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,+CAA+C;AAC/C,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,WAAW,CAAC;IACrB,6EAA6E;IAC7E,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,QAAQ,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/contact-channel/type.ts"],"names":[],"mappings":""}
|
|
@@ -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,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,OAAO,CAAC,OAAO,CAAC;gBACtB,EAAE;gBACF,GAAG,KAAK,CAAC,KAAK,KAAK,KAAK,EAAE;gBAC1B,EAAE;gBACF,GAAG,UAAU;gBACb,EAAE;gBACF,KAAK,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,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A separate 256-bit key for one purpose (`one-time-code`, `rate-limit`, `proof-of-work`, `sign-up-ticket`…),
|
|
3
|
+
* derived from the account server's one root secret with HKDF-SHA-256: one secret to keep, and a key leaked from one
|
|
4
|
+
* use reveals nothing about the others.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const pepper = deriveSecret(process.env.ACCOUNT_SECRET!, "one-time-code");
|
|
8
|
+
*/
|
|
9
|
+
export declare function deriveSecret(root: string, purpose: string): string;
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/secret-derivation/index.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAGlE"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { hkdfSync } from "node:crypto";
|
|
2
|
+
/**
|
|
3
|
+
* A separate 256-bit key for one purpose (`one-time-code`, `rate-limit`, `proof-of-work`, `sign-up-ticket`…),
|
|
4
|
+
* derived from the account server's one root secret with HKDF-SHA-256: one secret to keep, and a key leaked from one
|
|
5
|
+
* use reveals nothing about the others.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const pepper = deriveSecret(process.env.ACCOUNT_SECRET!, "one-time-code");
|
|
9
|
+
*/
|
|
10
|
+
export function deriveSecret(root, purpose) {
|
|
11
|
+
if (root.length < 32)
|
|
12
|
+
throw new Error("the account secret must have at least 32 characters");
|
|
13
|
+
return Buffer.from(hkdfSync("sha256", root, "", `sharadox-account:${purpose}`, 32)).toString("base64url");
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/secret-derivation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,IAAY,EAAE,OAAe;IACxD,IAAI,IAAI,CAAC,MAAM,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;IAC7F,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,EAAE,oBAAoB,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AAC5G,CAAC"}
|
|
@@ -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,12 @@
|
|
|
1
|
+
import type { AccountServer } from "@sharadox/account-web-library/account-server/type";
|
|
2
|
+
import type { SignInFinish, SignInFinishResult } from "@sharadox/account-web-library/sign-in-finish/type";
|
|
3
|
+
/**
|
|
4
|
+
* The second step: the typed code. Guesses are limited per address group (and per request by `verifyCode`). With the
|
|
5
|
+
* right code, an account with that contact is signed in on the browser (the new token goes in its cookie); a contact
|
|
6
|
+
* with no account gets a sign-up ticket for choosing a username.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const result = await finishSignIn(server, { requestId, code, address, token: cookie });
|
|
10
|
+
*/
|
|
11
|
+
export declare function finishSignIn(server: AccountServer, { requestId, code, address, token, now }: SignInFinish): Promise<SignInFinishResult>;
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/sign-in-finish/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAKvF,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,mDAAmD,CAAC;AAK1G;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,aAAa,EACrB,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,GAAgB,EAAE,EAAE,YAAY,GAClE,OAAO,CAAC,kBAAkB,CAAC,CA+B7B"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { addressGroup } from "@sharadox/protection-web-library/address-group";
|
|
2
|
+
import { limitRate } from "@sharadox/protection-web-library/rate-limit";
|
|
3
|
+
import { signIn } from "@sharadox/account-web-library/account-sign-in";
|
|
4
|
+
import { signInLimits } from "@sharadox/account-web-library/account-server";
|
|
5
|
+
import { verifyCode } from "@sharadox/account-web-library/code-verification";
|
|
6
|
+
import { findAccountByContact } from "@sharadox/account-web-library/contact-lookup";
|
|
7
|
+
import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
|
|
8
|
+
import { createTicket } from "@sharadox/account-web-library/signed-ticket";
|
|
9
|
+
/** How long a sign-up ticket lasts: time to choose a username. */
|
|
10
|
+
const SIGN_UP_SECONDS = 900;
|
|
11
|
+
/**
|
|
12
|
+
* The second step: the typed code. Guesses are limited per address group (and per request by `verifyCode`). With the
|
|
13
|
+
* right code, an account with that contact is signed in on the browser (the new token goes in its cookie); a contact
|
|
14
|
+
* with no account gets a sign-up ticket for choosing a username.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const result = await finishSignIn(server, { requestId, code, address, token: cookie });
|
|
18
|
+
*/
|
|
19
|
+
export async function finishSignIn(server, { requestId, code, address, token, now = Date.now() }) {
|
|
20
|
+
const rate = await limitRate(server.database, [{ key: `verification:${addressGroup(address)}`, rule: signInLimits(server).verificationsPerAddress }], { secret: deriveSecret(server.secret, "rate-limit"), now });
|
|
21
|
+
if (!rate.allowed)
|
|
22
|
+
return { status: "refused", reason: "rate-limit", retryAfterSeconds: rate.retryAfterSeconds };
|
|
23
|
+
const verification = await verifyCode(server.database, {
|
|
24
|
+
requestId,
|
|
25
|
+
code,
|
|
26
|
+
purpose: "sign-in",
|
|
27
|
+
pepper: deriveSecret(server.secret, "one-time-code"),
|
|
28
|
+
now,
|
|
29
|
+
});
|
|
30
|
+
if (!verification.valid)
|
|
31
|
+
return { status: "refused", reason: verification.reason };
|
|
32
|
+
const found = await findAccountByContact(server.database, verification.contact);
|
|
33
|
+
if (!found) {
|
|
34
|
+
const ticket = createTicket({ contact: verification.contact }, {
|
|
35
|
+
secret: deriveSecret(server.secret, "sign-up-ticket"),
|
|
36
|
+
purpose: "sign-up",
|
|
37
|
+
lifetimeSeconds: SIGN_UP_SECONDS,
|
|
38
|
+
now,
|
|
39
|
+
});
|
|
40
|
+
return { status: "sign-up", ticket };
|
|
41
|
+
}
|
|
42
|
+
if (!found.signIn)
|
|
43
|
+
return { status: "refused", reason: "unknown" };
|
|
44
|
+
const session = await signIn(server.database, { accountId: found.accountId, token, now });
|
|
45
|
+
return { status: "signed-in", accountId: found.accountId, token: session.token };
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/sign-in-finish/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gDAAgD,CAAC;AAC9E,OAAO,EAAE,SAAS,EAAE,MAAM,6CAA6C,CAAC;AACxE,OAAO,EAAE,MAAM,EAAE,MAAM,+CAA+C,CAAC;AACvE,OAAO,EAAE,YAAY,EAAE,MAAM,8CAA8C,CAAC;AAE5E,OAAO,EAAE,UAAU,EAAE,MAAM,iDAAiD,CAAC;AAC7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,8CAA8C,CAAC;AACpF,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAE,MAAM,6CAA6C,CAAC;AAG3E,kEAAkE;AAClE,MAAM,eAAe,GAAG,GAAG,CAAC;AAE5B;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,MAAqB,EACrB,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAgB;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KAAK,CAAC,MAAM;QAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IACnE,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;IAC1F,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC;AACnF,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { CodeVerificationResult } from "@sharadox/account-web-library/code-verification/type";
|
|
2
|
+
export interface SignInFinish {
|
|
3
|
+
requestId: string;
|
|
4
|
+
/** As typed. */
|
|
5
|
+
code: string;
|
|
6
|
+
/** The client's IP address. */
|
|
7
|
+
address: string;
|
|
8
|
+
/** The browser's current session cookie, if any: the account joins its accounts. */
|
|
9
|
+
token: string | undefined;
|
|
10
|
+
/** Milliseconds since 1970 (now by default). */
|
|
11
|
+
now?: number;
|
|
12
|
+
}
|
|
13
|
+
type Refusal = Extract<CodeVerificationResult, {
|
|
14
|
+
valid: false;
|
|
15
|
+
}>["reason"];
|
|
16
|
+
export type SignInFinishResult = {
|
|
17
|
+
status: "signed-in";
|
|
18
|
+
accountId: string;
|
|
19
|
+
token: string;
|
|
20
|
+
}
|
|
21
|
+
/** No account has this contact yet: ask for a username, then `finishSignUp` with the ticket. */
|
|
22
|
+
| {
|
|
23
|
+
status: "sign-up";
|
|
24
|
+
ticket: string;
|
|
25
|
+
} | {
|
|
26
|
+
status: "refused";
|
|
27
|
+
reason: Refusal;
|
|
28
|
+
} | {
|
|
29
|
+
status: "refused";
|
|
30
|
+
reason: "rate-limit";
|
|
31
|
+
retryAfterSeconds: number;
|
|
32
|
+
};
|
|
33
|
+
export {};
|
|
34
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/sign-in-finish/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sDAAsD,CAAC;AAEnG,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,+BAA+B;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,oFAAoF;IACpF,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,KAAK,OAAO,GAAG,OAAO,CAAC,sBAAsB,EAAE;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC,CAAC,QAAQ,CAAC,CAAC;AAE3E,MAAM,MAAM,kBAAkB,GAC1B;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE;AAC3D,gGAAgG;GAC9F;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACrC;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GACtC;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,YAAY,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/sign-in-finish/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AccountServer } from "@sharadox/account-web-library/account-server/type";
|
|
2
|
+
import type { SignInStart, SignInStartResult } from "@sharadox/account-web-library/sign-in-start/type";
|
|
3
|
+
/**
|
|
4
|
+
* The first step of signing in (and of signing up: they are the same): a code to the contact. In order, each check
|
|
5
|
+
* cheaper than what it protects:
|
|
6
|
+
* 1. a single-use proof of work;
|
|
7
|
+
* 2. a valid contact with a channel;
|
|
8
|
+
* 3. rate limits per contact, per address group (an IPv6 /64 is one visitor) and for everyone;
|
|
9
|
+
* 4. the channel's daily spending cap (past it, sending pauses: it never spends);
|
|
10
|
+
* 5. a new code, sent by `deliver` after answering; a failed send is recorded in the audit log
|
|
11
|
+
* (`code.delivery-failed`, with the error, never the code) and thrown.
|
|
12
|
+
* The answer never depends on whether an account exists, so nobody can probe for accounts.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const result = await startSignIn(server, { contact, solution, address: clientAddress(request), language });
|
|
16
|
+
* if (result.status === "sent") after(result.deliver);
|
|
17
|
+
*/
|
|
18
|
+
export declare function startSignIn(server: AccountServer, { contact, solution, address, language, now }: SignInStart): Promise<SignInStartResult>;
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/sign-in-start/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAMvF,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,kDAAkD,CAAC;AAEvG;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,WAAW,CAC/B,MAAM,EAAE,aAAa,EACrB,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAgB,EAAE,EAAE,WAAW,GACtE,OAAO,CAAC,iBAAiB,CAAC,CAiE5B"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { addressGroup } from "@sharadox/protection-web-library/address-group";
|
|
2
|
+
import { limitRate } from "@sharadox/protection-web-library/rate-limit";
|
|
3
|
+
import { redeemSolution } from "@sharadox/protection-web-library/solution-redemption";
|
|
4
|
+
import { spendFromCap } from "@sharadox/protection-web-library/spending-cap";
|
|
5
|
+
import { signInLimits } from "@sharadox/account-web-library/account-server";
|
|
6
|
+
import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
|
|
7
|
+
import { requestCode } from "@sharadox/account-web-library/code-request";
|
|
8
|
+
import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
|
|
9
|
+
import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
|
|
10
|
+
/**
|
|
11
|
+
* The first step of signing in (and of signing up: they are the same): a code to the contact. In order, each check
|
|
12
|
+
* cheaper than what it protects:
|
|
13
|
+
* 1. a single-use proof of work;
|
|
14
|
+
* 2. a valid contact with a channel;
|
|
15
|
+
* 3. rate limits per contact, per address group (an IPv6 /64 is one visitor) and for everyone;
|
|
16
|
+
* 4. the channel's daily spending cap (past it, sending pauses: it never spends);
|
|
17
|
+
* 5. a new code, sent by `deliver` after answering; a failed send is recorded in the audit log
|
|
18
|
+
* (`code.delivery-failed`, with the error, never the code) and thrown.
|
|
19
|
+
* The answer never depends on whether an account exists, so nobody can probe for accounts.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const result = await startSignIn(server, { contact, solution, address: clientAddress(request), language });
|
|
23
|
+
* if (result.status === "sent") after(result.deliver);
|
|
24
|
+
*/
|
|
25
|
+
export async function startSignIn(server, { contact, solution, address, language, now = Date.now() }) {
|
|
26
|
+
const limits = signInLimits(server);
|
|
27
|
+
const proof = await redeemSolution(server.database, solution, {
|
|
28
|
+
secret: deriveSecret(server.secret, "proof-of-work"),
|
|
29
|
+
now,
|
|
30
|
+
});
|
|
31
|
+
if (!proof.valid)
|
|
32
|
+
return { status: "refused", reason: "proof" };
|
|
33
|
+
let normalized;
|
|
34
|
+
try {
|
|
35
|
+
normalized = normalizeContact(contact);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return { status: "refused", reason: "contact" };
|
|
39
|
+
}
|
|
40
|
+
const channel = server.channels.find(({ kind }) => kind === normalized.kind);
|
|
41
|
+
if (!channel)
|
|
42
|
+
return { status: "refused", reason: "contact" };
|
|
43
|
+
const rate = await limitRate(server.database, [
|
|
44
|
+
{ key: `contact:${normalized.kind}:${normalized.value}`, rule: limits.perContact },
|
|
45
|
+
{ key: `address:${addressGroup(address)}`, rule: limits.perAddress },
|
|
46
|
+
{ key: "global:sign-in", rule: limits.global },
|
|
47
|
+
], { secret: deriveSecret(server.secret, "rate-limit"), now });
|
|
48
|
+
if (!rate.allowed)
|
|
49
|
+
return { status: "refused", reason: "rate-limit", retryAfterSeconds: rate.retryAfterSeconds };
|
|
50
|
+
const cap = await spendFromCap(server.database, { name: `${normalized.kind}-send`, limit: limits.dailySendsPerChannel, period: "day" }, { now });
|
|
51
|
+
if (!cap.allowed)
|
|
52
|
+
return { status: "refused", reason: "unavailable" };
|
|
53
|
+
const request = await requestCode(server.database, {
|
|
54
|
+
purpose: "sign-in",
|
|
55
|
+
contact: normalized,
|
|
56
|
+
pepper: deriveSecret(server.secret, "one-time-code"),
|
|
57
|
+
now,
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
status: "sent",
|
|
61
|
+
requestId: request.requestId,
|
|
62
|
+
deliver: async () => {
|
|
63
|
+
try {
|
|
64
|
+
await channel.send({
|
|
65
|
+
contact: request.contact,
|
|
66
|
+
code: request.code,
|
|
67
|
+
purpose: "sign-in",
|
|
68
|
+
language,
|
|
69
|
+
expiresAt: request.expiresAt,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
// Nobody is waiting for the answer any more: the audit log is where a failed send shows (never the code).
|
|
74
|
+
const { name, message } = error;
|
|
75
|
+
await recordAuditEvent(server.database, {
|
|
76
|
+
action: "code.delivery-failed",
|
|
77
|
+
detail: {
|
|
78
|
+
kind: request.contact.kind,
|
|
79
|
+
purpose: "sign-in",
|
|
80
|
+
requestId: request.requestId,
|
|
81
|
+
error: `${name}: ${message}`.slice(0, 500),
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/sign-in-start/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gDAAgD,CAAC;AAC9E,OAAO,EAAE,SAAS,EAAE,MAAM,6CAA6C,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAC;AACtF,OAAO,EAAE,YAAY,EAAE,MAAM,+CAA+C,CAAC;AAC7E,OAAO,EAAE,YAAY,EAAE,MAAM,8CAA8C,CAAC;AAE5E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,WAAW,EAAE,MAAM,4CAA4C,CAAC;AACzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAE9E,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAG/E;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,MAAqB,EACrB,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAe;IAEvE,MAAM,MAAM,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;IACpC,MAAM,KAAK,GAAG,MAAM,cAAc,CAAC,MAAM,CAAC,QAAQ,EAAE,QAAQ,EAAE;QAC5D,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC;QACpD,GAAG;KACJ,CAAC,CAAC;IACH,IAAI,CAAC,KAAK,CAAC,KAAK;QAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;IAChE,IAAI,UAAmB,CAAC;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