diff --git a/CHANGELOG.md b/CHANGELOG.md index d1241af..a77fce1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ Each package and app has its own section and its own tags (`contracts@1.0.0`, `accounts@1.0.0`, …). +## accounts@1.6.0 - 2026-11-20 + +- **Accounts 1.6.0:** a "New sign-in" email (and WhatsApp for a verified number) after every + sign-in, as Cross Events' own sign-in did; Sign-in settings → `signin_notices` turns it off. + ## events@3.6.0, accounts@1.5.0, contracts@1.14.0 - 2026-11-20 A nightly check that each site's copies of people match their church accounts, for changes that diff --git a/apps/accounts/CHANGELOG.md b/apps/accounts/CHANGELOG.md index 7303d34..e047ea6 100644 --- a/apps/accounts/CHANGELOG.md +++ b/apps/accounts/CHANGELOG.md @@ -1,5 +1,18 @@ # Changelog — Accounts +## [1.6.0] - 2026-11-20 + +### Added + +- **"New sign-in" notices**, as Cross Events' own sign-in sends (`src/accounts/signInNotice.ts`): + after each completed sign-in (the interaction reaching done, so not the silent ones a second + product gets from an existing session), an email with when, the site, rough location and device + (`newSignInEmail` in `src/lib/emails.ts`), and a WhatsApp message when the person has a verified + number and WhatsApp is set up. Location from ip-api.com in production only, never for private + addresses. Fire-and-forget: never holds up or fails a sign-in. Admins can turn it off: + `signin_notices` (Sign-in settings, on by default). In Accounts mode Cross Events no longer sends + its own, so before this nobody was told. + ## [1.5.0] - 2026-11-20 Needs contracts 1.14.0. diff --git a/apps/accounts/WHATS-NEW.md b/apps/accounts/WHATS-NEW.md index 2c9f973..17d0aa0 100644 --- a/apps/accounts/WHATS-NEW.md +++ b/apps/accounts/WHATS-NEW.md @@ -3,6 +3,13 @@ What each version changes for the church, in plain language (read by the Hub like Cross Events' WHATS-NEW.md; a section shows once its version is tagged). +## 1.6.0 + +- **You're told about each sign-in.** After someone signs in, they get an email saying when, roughly + where, on what device and to which site, with what to do if it wasn't them, as Cross Events + does for its own sign-in. People with a confirmed WhatsApp number get it on WhatsApp too. + Admins can turn this off under Sign-in settings. + ## 1.5.0 - Your church's sites can now check every night that the details they keep for each person match diff --git a/apps/accounts/backend/package.json b/apps/accounts/backend/package.json index 7acd4c4..0b9edf7 100644 --- a/apps/accounts/backend/package.json +++ b/apps/accounts/backend/package.json @@ -1,6 +1,6 @@ { "name": "@crosscode/accounts-backend", - "version": "1.5.0", + "version": "1.6.0", "description": "Cross Code Accounts: the church's sign-in (OpenID Connect provider), account pages API and admin API", "private": true, "type": "module", diff --git a/apps/accounts/backend/src/accounts/signInNotice.ts b/apps/accounts/backend/src/accounts/signInNotice.ts new file mode 100644 index 0000000..bdd5538 --- /dev/null +++ b/apps/accounts/backend/src/accounts/signInNotice.ts @@ -0,0 +1,58 @@ +/** + * "New sign-in" notices (1.6.0), as Cross Events sends for its own sign-in: after each completed + * sign-in, an email to the person, and a WhatsApp message too when they have a verified number and + * WhatsApp is set up. Admins can turn them off (Sign-in settings, `signin_notices`). Never holds up + * or fails the sign-in. + * + * The rough location comes from the IP (ip-api.com, as Cross Events), in production only, and never + * for private addresses. + */ +import type { Ctx } from '../context.js'; +import { newSignInEmail } from '../lib/emails.js'; +import { productName } from '../oidc/clients.js'; + +const PRIVATE_IP = /^(::1$|127\.|10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.|fc|fd|fe80|::ffff:(127|10|192\.168)\.)/i; + +/** "Chrome on Windows" from a user agent; the raw string when it can't tell. */ +export function describeDevice(ua: string | null | undefined): string { + const s = String(ua || ''); + if (!s) return 'Unknown device'; + const browser = /Edg\//.test(s) ? 'Edge' : /OPR\/|Opera/.test(s) ? 'Opera' : /Firefox\//.test(s) ? 'Firefox' + : /SamsungBrowser\//.test(s) ? 'Samsung Internet' : /Chrome\//.test(s) ? 'Chrome' : /Safari\//.test(s) ? 'Safari' : null; + const os = /iPhone|iPad|iPod/.test(s) ? 'iPhone or iPad' : /Android/.test(s) ? 'Android' : /Windows/.test(s) ? 'Windows' + : /Mac OS X|Macintosh/.test(s) ? 'a Mac' : /CrOS/.test(s) ? 'a Chromebook' : /Linux/.test(s) ? 'Linux' : null; + if (browser && os) return `${browser} on ${os}`; + return browser || os || s.slice(0, 120); +} + +async function roughLocation(ctx: Ctx, ip: string | null): Promise { + if (!ip || PRIVATE_IP.test(ip) || !ctx.config.production) return 'Unknown'; + try { + const res = await fetch(`http://ip-api.com/json/${encodeURIComponent(ip.replace(/^::ffff:/, ''))}?fields=status,city,regionName,country`, { signal: AbortSignal.timeout(3000) }); + const geo = (await res.json()) as { status?: string; city?: string; regionName?: string; country?: string }; + const parts = geo.status === 'success' ? [geo.city, geo.regionName, geo.country].filter(Boolean) : []; + return parts.length ? parts.join(', ') : 'Unknown'; + } catch { + return 'Unknown'; + } +} + +export async function sendSignInNotice(ctx: Ctx, o: { accountId: string; ip: string | null; userAgent: string | null; clientId: string | null }): Promise<'sent' | 'off' | 'failed'> { + try { + if (!(await ctx.settings.bool('signin_notices', true))) return 'off'; + const a = await ctx.db.account.findUnique({ where: { id: o.accountId } }); + if (!a || a.status !== 'active') return 'off'; + const link = o.clientId ? await ctx.db.productLink.findUnique({ where: { installId: o.clientId } }) : null; + const site = link ? productName(link.product) : o.clientId === 'accounts-self' ? 'Your account page' : null; + const when = ctx.now().toLocaleString('en-ZA', { timeZone: ctx.config.timezone, dateStyle: 'medium', timeStyle: 'short' }); + const msg = await newSignInEmail(ctx, { name: a.name, when, location: await roughLocation(ctx, o.ip), device: describeDevice(o.userAgent), site }); + await ctx.messenger.email({ to: a.email, subject: msg.subject, text: msg.text, html: msg.html }); + if (a.phone && a.phoneVerified && (await ctx.messenger.whatsappReady())) { + await ctx.messenger.whatsapp({ to: a.phone, text: msg.whatsapp }).catch((e: Error) => console.warn('[sign-in notice] WhatsApp:', e.message)); + } + return 'sent'; + } catch (e) { + console.warn('[sign-in notice] not sent:', (e as Error).message); + return 'failed'; + } +} diff --git a/apps/accounts/backend/src/interaction/routes.ts b/apps/accounts/backend/src/interaction/routes.ts index 2712921..f4908ed 100644 --- a/apps/accounts/backend/src/interaction/routes.ts +++ b/apps/accounts/backend/src/interaction/routes.ts @@ -8,7 +8,8 @@ import type Provider from 'oidc-provider'; import { decodeState, encodeState, safeReturnPath } from '@crosscode/contracts'; import type { Ctx } from '../context.js'; import { ApiError, handle, limit } from '../http.js'; -import { loginEvent } from '../lib/audit.js'; +import { clientIp, loginEvent } from '../lib/audit.js'; +import { sendSignInNotice } from '../accounts/signInNotice.js'; import { resetPasswordEmail } from '../lib/emails.js'; import { hashToken, randomToken } from '../lib/crypto.js'; import { hashPassword, passwordProblem } from '../lib/passwords.js'; @@ -44,6 +45,8 @@ export function interactionRouter(ctx: Ctx, provider: Provider) { const clientId = String(details.params.client_id ?? '') || null; await loginEvent(ctx, pending.accountId, 'sign_in', req, { clientId, detail: { amr: pending.amr } }); await signedInThrough(ctx, clientId, pending.accountId); + // 1.6.0: "New sign-in" email (and WhatsApp), as Cross Events; never waited for. + if (pending.accountId) void sendSignInNotice(ctx, { accountId: pending.accountId, ip: clientIp(req), userAgent: req.get('user-agent') ?? null, clientId }); await ctx.db.pendingSignIn.deleteMany({ where: { uid: pending.uid } }); return { stage: 'done', redirectTo, ...extra }; } diff --git a/apps/accounts/backend/src/lib/emails.ts b/apps/accounts/backend/src/lib/emails.ts index 384e8c2..d03db7b 100644 --- a/apps/accounts/backend/src/lib/emails.ts +++ b/apps/accounts/backend/src/lib/emails.ts @@ -206,3 +206,20 @@ export async function testEmail(ctx: Ctx, o: { from?: string } = {}): Promise { + const b = await brand(ctx); + const row = (k: string, v: string) => `${esc(k)}${esc(v)}`; + const body = title('New sign-in', `Someone signed in to your ${b.name} account`) + + hello(o.name) + + `` + + row('When', o.when) + (o.site ? row('Signed in to', o.site) : '') + row('Where (roughly)', o.location) + row('Device', o.device) + + '
' + + callout(`Not you? Change your password straight away from your account page, and let the church office know${contactLine(b)}.`, 'warning') + + small('If this was you, there\'s nothing to do.'); + const lines = [`When: ${o.when}`, ...(o.site ? [`Signed in to: ${o.site}`] : []), `Where (roughly): ${o.location}`, `Device: ${o.device}`]; + const text = `Hello ${o.name || 'there'},\n\nSomeone signed in to your ${b.name} account.\n\n${lines.join('\n')}\n\nIf this wasn't you, change your password straight away and let the church office know${b.email ? ` (${b.email})` : ''}.\nIf it was you, there's nothing to do.`; + const whatsapp = `🔐 *New sign-in*\n\nHello ${o.name || 'there'}, someone signed in to your *${b.name}* account.\n\n${lines.join('\n')}\n\nNot you? Change your password straight away and let the church office know.`; + return { subject: `New sign-in to your ${b.name} account`, text, html: layout(b, body, `New sign-in: ${o.when}, ${o.device}.`), whatsapp }; +} diff --git a/apps/accounts/backend/src/lib/settings.ts b/apps/accounts/backend/src/lib/settings.ts index 73c78d9..e917f98 100644 --- a/apps/accounts/backend/src/lib/settings.ts +++ b/apps/accounts/backend/src/lib/settings.ts @@ -9,7 +9,7 @@ export const SECRET_SETTINGS = new Set(['smtp_pass', 'wawp_access_token', 'hub_i /** The settings an admin may read and change through the generic settings API. */ export const ADMIN_SETTINGS = new Set([ - 'signup_enabled', 'two_factor_required_for_admins', 'session_days', + 'signup_enabled', 'two_factor_required_for_admins', 'session_days', 'signin_notices', 'smtp_host', 'smtp_port', 'smtp_user', 'smtp_pass', 'smtp_from', 'smtp_secure', 'wawp_access_token', 'wawp_instance_id', ]); diff --git a/apps/accounts/backend/tests/signInNotice.test.ts b/apps/accounts/backend/tests/signInNotice.test.ts new file mode 100644 index 0000000..e27ff3c --- /dev/null +++ b/apps/accounts/backend/tests/signInNotice.test.ts @@ -0,0 +1,50 @@ +// 1.6.0: a "New sign-in" email (and WhatsApp) after each sign-in, as Cross Events sends. +import assert from 'node:assert/strict'; +import { after, before, describe, test } from 'node:test'; +import { startHarness, type Harness } from './support/harness.js'; +import { describeDevice, sendSignInNotice } from '../src/accounts/signInNotice.js'; +import type { MemoryMessenger } from '../src/lib/messages.js'; + +let h: Harness; +before(async () => { h = await startHarness(); }); +after(async () => { await h.close(); }); + +const CHROME_WIN = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0 Safari/537.36'; + +describe('sign-in notices', () => { + test('an email with when, where, the device and the site; WhatsApp too for a verified number', async () => { + const m = h.ctx.messenger as MemoryMessenger; + const a = await h.makeAccount({ email: 'notice.person@hope.test' }); + assert.equal(await sendSignInNotice(h.ctx, { accountId: a.id as string, ip: '127.0.0.1', userAgent: CHROME_WIN, clientId: 'accounts-self' }), 'sent'); + const mail = m.emails.filter((e) => e.to === 'notice.person@hope.test').at(-1)!; + assert.match(mail.subject, /^New sign-in to your .+ account$/); + assert.match(mail.text, /Device: Chrome on Windows/); + assert.match(mail.text, /Signed in to: Your account page/); + assert.match(mail.html ?? '', /Not you\?/); + + await h.db.account.update({ where: { id: a.id as string }, data: { phone: '+27821234567', phoneVerified: true } }); + m.whatsappOn = true; + const before = m.whatsapps.length; + await sendSignInNotice(h.ctx, { accountId: a.id as string, ip: null, userAgent: null, clientId: null }); + assert.equal(m.whatsapps.length, before + 1); + assert.match(m.whatsapps.at(-1)!.text, /New sign-in/); + m.whatsappOn = false; + }); + + test('admins can switch them off; a failing email never throws', async () => { + const m = h.ctx.messenger as MemoryMessenger; + const a = await h.makeAccount({ email: 'notice.off@hope.test' }); + await h.ctx.settings.set('signin_notices', 'false'); + assert.equal(await sendSignInNotice(h.ctx, { accountId: a.id as string, ip: null, userAgent: null, clientId: null }), 'off'); + await h.ctx.settings.set('signin_notices', null); + m.emailOn = false; + assert.equal(await sendSignInNotice(h.ctx, { accountId: a.id as string, ip: null, userAgent: null, clientId: null }), 'failed'); + m.emailOn = true; + }); + + test('device names', () => { + assert.equal(describeDevice(CHROME_WIN), 'Chrome on Windows'); + assert.equal(describeDevice('Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Version/17.0 Mobile/15E148 Safari/604.1'), 'Safari on iPhone or iPad'); + assert.equal(describeDevice(null), 'Unknown device'); + }); +}); diff --git a/apps/accounts/frontend/package.json b/apps/accounts/frontend/package.json index a362374..b2a9303 100644 --- a/apps/accounts/frontend/package.json +++ b/apps/accounts/frontend/package.json @@ -1,6 +1,6 @@ { "name": "@crosscode/accounts-frontend", - "version": "1.5.0", + "version": "1.6.0", "private": true, "description": "Cross Code Accounts: the church's sign-in pages, the account page and admin", "scripts": { diff --git a/apps/accounts/frontend/src/app/admin/Admin.tsx b/apps/accounts/frontend/src/app/admin/Admin.tsx index 97cbeb7..52bcea2 100644 --- a/apps/accounts/frontend/src/app/admin/Admin.tsx +++ b/apps/accounts/frontend/src/app/admin/Admin.tsx @@ -57,7 +57,7 @@ export function Admin({ section = "people" }: { section?: AdminSection }) { {section === "deletions" && } {section === "church" && } {section === "linked" && } - {section === "signin" && } + {section === "signin" && } {section === "messages" && } {section === "notice" && } {section === "backups" && } @@ -252,6 +252,7 @@ function Linked({ flash }: { flash: Flash }) { const SETTING_LABELS: Record = { signup_enabled: ["Members can create their own account", "bool"], two_factor_required_for_admins: ["Admins must use two-step sign-in", "bool"], + signin_notices: ["Tell people by email (and WhatsApp) each time they sign in", "bool"], session_days: ['Days "Keep me signed in" lasts', "number"], smtp_host: ["SMTP server", "text"], smtp_port: ["SMTP port", "number"], smtp_user: ["SMTP user name", "text"], smtp_pass: ["SMTP password", "secret"], smtp_from: ["Send from (email address)", "text"], smtp_secure: ["Use TLS from the start (port 465)", "bool"], @@ -266,7 +267,7 @@ function Settings({ flash, keys }: { flash: Flash; keys: string[] }) { return () => { alive = false; }; }, []); if (!values) return

Loading…

; - const defaults: Record = { signup_enabled: true, two_factor_required_for_admins: true }; + const defaults: Record = { signup_enabled: true, two_factor_required_for_admins: true, signin_notices: true }; return (
{ diff --git a/docs/VERSIONS.md b/docs/VERSIONS.md index 8433721..ad56379 100644 --- a/docs/VERSIONS.md +++ b/docs/VERSIONS.md @@ -11,7 +11,7 @@ separately below; the two often differ. | What | Repo | Latest | Tag | Released | |------|------|--------|-----|----------| | Cross Events | `crosscode-platform` (`apps/events`) | **3.6.0** | `events@3.6.0` | 2026-11-20 | -| Accounts | `crosscode-platform` (`apps/accounts`) | **1.5.0** | `accounts@1.5.0` | 2026-11-20 | +| Accounts | `crosscode-platform` (`apps/accounts`) | **1.6.0** | `accounts@1.6.0` | 2026-11-20 | | Server agent | `crosscode-platform` (`apps/agent`) | **1.3.1** | `agent@1.3.1` | 2026-11-20 | | Contracts | `crosscode-platform` (`packages/contracts`) | **1.14.0** | `contracts@1.14.0` | 2026-11-20 | | Cross Code Hub | `events-hub` | **2.9.0** | `v2.9.0` | 2026-11-20 | @@ -37,6 +37,9 @@ need their own tags until something outside this repo depends on them. ## What each latest release was for +- **Accounts 1.6.0:** a "New sign-in" email (and WhatsApp) after every sign-in, as Cross Events' + own sign-in sends; admins can turn it off. No migration. + - **Events 3.6.0 + Accounts 1.5.0 + contracts 1.14.0:** a nightly people check (Events asks Accounts about everyone with an account, 500 at a time, `people.check`): out-of-date copies are fixed, deleted accounts handled as `user.deleted`, and it stops if most people are unknown. Update diff --git a/package-lock.json b/package-lock.json index 3d6dd46..8305831 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,7 +46,7 @@ }, "apps/accounts/backend": { "name": "@crosscode/accounts-backend", - "version": "1.5.0", + "version": "1.6.0", "hasInstallScript": true, "dependencies": { "@crosscode/contracts": "*", @@ -83,7 +83,7 @@ }, "apps/accounts/frontend": { "name": "@crosscode/accounts-frontend", - "version": "1.5.0", + "version": "1.6.0", "dependencies": { "@crosscode/contracts": "*", "@crosscode/ui": "*",