Accounts 1.0.0 backend: OIDC provider, sign-in steps, account and admin API
oidc-provider 9.13.3 with PKCE, ES256, crosscode.identity/1 claims and back-channel logout; clients are the linked products (Connect). Sign-in steps (password, sign-up, email check, 2FA and its setup, privacy notice, news question, password reset) survive to the product. Account page and admin API, church profile, export and deletion fan-out, Hub v2 client, jobs. End-to-end tests with a test product on openid-client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* Email (SMTP, from the admin's settings or the server's .env) and WhatsApp (WAWP, as Cross
|
||||
* Events). In tests a `MemoryMessenger` keeps what would have been sent.
|
||||
*/
|
||||
import nodemailer from 'nodemailer';
|
||||
import type { Ctx } from '../context.js';
|
||||
|
||||
export type Email = { to: string; subject: string; text: string };
|
||||
export type WhatsApp = { to: string; text: string };
|
||||
|
||||
export interface Messenger {
|
||||
email(msg: Email): Promise<void>;
|
||||
whatsapp(msg: WhatsApp): Promise<void>;
|
||||
/** Whether WhatsApp is set up, so it's offered as a 2FA method. */
|
||||
whatsappReady(): Promise<boolean>;
|
||||
}
|
||||
|
||||
/** 0821234567 / +27 82 123 4567 → 27821234567 (South African mobiles; others as given, digits only). */
|
||||
export function waNumber(raw: string): string | null {
|
||||
let d = String(raw || '').replace(/\D/g, '');
|
||||
if (d.startsWith('0') && d.length === 10) d = `27${d.slice(1)}`;
|
||||
return d.length >= 10 && d.length <= 15 ? d : null;
|
||||
}
|
||||
|
||||
export class LiveMessenger implements Messenger {
|
||||
constructor(private readonly ctx: Pick<Ctx, 'settings' | 'config'>) {}
|
||||
|
||||
private async smtp() {
|
||||
const s = this.ctx.settings;
|
||||
const host = (await s.get('smtp_host')) || this.ctx.config.smtp?.host;
|
||||
if (!host) return null;
|
||||
const port = Number((await s.get('smtp_port')) || this.ctx.config.smtp?.port || 587);
|
||||
return {
|
||||
transport: nodemailer.createTransport({
|
||||
host,
|
||||
port,
|
||||
secure: (await s.get('smtp_secure')) === 'true' || (this.ctx.config.smtp?.secure ?? port === 465),
|
||||
auth: { user: (await s.get('smtp_user')) || this.ctx.config.smtp?.user || '', pass: (await s.get('smtp_pass')) || this.ctx.config.smtp?.pass || '' },
|
||||
}),
|
||||
from: (await s.get('smtp_from')) || this.ctx.config.smtp?.from || '',
|
||||
};
|
||||
}
|
||||
|
||||
async email(msg: Email): Promise<void> {
|
||||
const smtp = await this.smtp();
|
||||
if (!smtp) {
|
||||
if (this.ctx.config.production) throw new Error('Email isn\'t set up (Admin → Email and WhatsApp).');
|
||||
console.log(`[email] (no SMTP) to ${msg.to}: ${msg.subject}\n${msg.text}`);
|
||||
return;
|
||||
}
|
||||
const church = (await this.ctx.settings.json<{ name?: string }>('church_profile', {})).name;
|
||||
await smtp.transport.sendMail({ from: church ? `"${church.replace(/"/g, '')}" <${smtp.from}>` : smtp.from, to: msg.to, subject: msg.subject, text: msg.text });
|
||||
}
|
||||
|
||||
async whatsappReady(): Promise<boolean> {
|
||||
return !!((await this.ctx.settings.get('wawp_access_token')) && (await this.ctx.settings.get('wawp_instance_id')));
|
||||
}
|
||||
|
||||
async whatsapp(msg: WhatsApp): Promise<void> {
|
||||
const token = await this.ctx.settings.get('wawp_access_token');
|
||||
const instance = await this.ctx.settings.get('wawp_instance_id');
|
||||
const number = waNumber(msg.to);
|
||||
if (!token || !instance) throw new Error('WhatsApp isn\'t set up.');
|
||||
if (!number) throw new Error('That isn\'t a WhatsApp number.');
|
||||
const res = await fetch('https://api.wawp.net/v2/send/text', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ access_token: token, instance_id: instance, chatId: `${number}@c.us`, message: msg.text }),
|
||||
signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
if (!res.ok) throw new Error(`WhatsApp couldn't send the message (${res.status}).`);
|
||||
}
|
||||
}
|
||||
|
||||
export class MemoryMessenger implements Messenger {
|
||||
emails: Email[] = [];
|
||||
whatsapps: WhatsApp[] = [];
|
||||
whatsappOn = false;
|
||||
async email(msg: Email) { this.emails.push(msg); }
|
||||
async whatsapp(msg: WhatsApp) { this.whatsapps.push(msg); }
|
||||
async whatsappReady() { return this.whatsappOn; }
|
||||
/** The last 6-digit code sent to `to` (tests). */
|
||||
lastCode(to: string): string | null {
|
||||
const all = [...this.emails.filter((e) => e.to === to).map((e) => e.text), ...this.whatsapps.filter((w) => w.to === to).map((w) => w.text)];
|
||||
const m = /\b(\d{6})\b/.exec(all[all.length - 1] ?? '');
|
||||
return m ? m[1]! : null;
|
||||
}
|
||||
/** The last link sent to `to` (tests). */
|
||||
lastLink(to: string): string | null {
|
||||
const e = this.emails.filter((x) => x.to === to).at(-1);
|
||||
const m = /(https?:\/\/\S+)/.exec(e?.text ?? '');
|
||||
return m ? m[1]! : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user