@sharadox/account-web-library 0.1.0-development.12 → 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 CHANGED
@@ -168,6 +168,17 @@ points to any number of account sessions, so the selector has no limit and the c
168
168
  - `pruneSessions(database, { idleSeconds?, now? })` → `{ accountSessions, browserSessions }`, for a daily job.
169
169
  - `createSessionToken()`, `hashSessionToken(token)` (`session-token`).
170
170
 
171
+ ### `email-channel`: `createEmailChannel({ sender, from, design, logos?, now? })` → `ContactChannel`
172
+
173
+ Sends codes by email through email-web-library's sender, in the shared email layout.
174
+
175
+ - English, or European Portuguese for any `pt` reader.
176
+ - The code is in the body only, never in the subject (lock screens show subjects).
177
+ - No link to click: nothing for a phishing email to imitate.
178
+
179
+ A `ContactChannel` (`contact-channel/type`) is `{ kind, send({ contact, code, purpose, language, expiresAt }) }`:
180
+ phone, WhatsApp, Messenger and Instagram channels will have the same shape.
181
+
171
182
  ### `contact-kind`: `normalizeContact({ kind, value })`, `CONTACT_KINDS`
172
183
 
173
184
  `email` in this version; unknown kinds and invalid values throw.
@@ -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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -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 @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AAC,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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/email-channel/type.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sharadox/account-web-library",
3
- "version": "0.1.0-development.12",
3
+ "version": "0.1.0-development.15",
4
4
  "description": "Passwordless accounts for web applications: one-time codes, two-factor, roles, bans and OpenID Connect for people and agents.",
5
5
  "private": false,
6
6
  "license": "SEE LICENSE IN LICENCA.md",
@@ -41,6 +41,7 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@sharadox/database-web-library": "0.1.0-development.6",
44
+ "@sharadox/email-web-library": "^0.7.1",
44
45
  "@sharadox/web-library": "^0.11.1",
45
46
  "kysely": "^0.28.20"
46
47
  },