feat: add admin authenticator 2FA and configurable login email alerts
This commit is contained in:
1 parent
bee3b990f9
commit
52cc865140
21 files changed
+1961
-20
No files matched your search
@@ -48,7 +48,11 @@ test('admin creates and edits recipes with recoverable drafts and image upload',
|
||||
await page
|
||||
.getByRole('textbox', { name: 'Rezepttitel', exact: true })
|
||||
.fill('Browser-Küchenrezept verbessert');
|
||||
const saved = page.waitForResponse(
|
||||
(r) => r.url().includes('/api/recipes/') && r.request().method() === 'PUT',
|
||||
);
|
||||
await page.getByRole('button', { name: 'Rezept speichern' }).click();
|
||||
expect((await saved).status()).toBe(200);
|
||||
await expect(page.getByRole('status')).toContainText('erfolgreich gespeichert');
|
||||
await page.goto('/admin');
|
||||
await expect(
|
||||
@@ -109,6 +113,7 @@ test('JSON and XML import with a real preview, duplicate prevention and export',
|
||||
await expect(page.locator('.import-preview')).toContainText('200 g Pasta');
|
||||
await page.getByRole('button', { name: '1 Rezept(e) jetzt importieren' }).click();
|
||||
await expect(page.getByRole('heading', { name: recipe.title })).toBeVisible();
|
||||
await page.reload();
|
||||
}
|
||||
await page.getByRole('button', { name: 'Importieren', exact: true }).click();
|
||||
await page.getByLabel('Rezeptdaten').fill(exportRecipes([pasta], 'json'));
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { SMTPServer } from 'smtp-server';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import postgres from 'postgres';
|
||||
|
||||
test('encrypted SMTP credentials, real TLS delivery and optional login alerts', async ({
|
||||
request,
|
||||
}) => {
|
||||
const messages: string[] = [];
|
||||
const recipients: string[] = [];
|
||||
const server = new SMTPServer({
|
||||
secure: true,
|
||||
key: readFileSync(join(process.env.TEST_TLS_DIR!, 'key.pem')),
|
||||
cert: readFileSync(join(process.env.TEST_TLS_DIR!, 'cert.pem')),
|
||||
onAuth(auth, _session, done) {
|
||||
if (auth.username === 'testmailer' && auth.password === 'smtp-test-password')
|
||||
done(null, { user: 'testmailer' });
|
||||
else done(new Error('Invalid credentials'));
|
||||
},
|
||||
onData(stream, session, done) {
|
||||
let message = '';
|
||||
stream.on('data', (chunk) => {
|
||||
message += chunk.toString();
|
||||
});
|
||||
stream.on('end', () => {
|
||||
messages.push(message);
|
||||
recipients.push(...session.envelope.rcptTo.map((r) => r.address));
|
||||
done();
|
||||
});
|
||||
},
|
||||
});
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.on('error', reject);
|
||||
server.listen(15440, '127.0.0.1', resolve);
|
||||
});
|
||||
const db = postgres(process.env.TEST_DATABASE_URL!, { max: 1 });
|
||||
const password = process.env.TEST_PASSWORD!;
|
||||
const headers = {
|
||||
origin: 'http://localhost:3100',
|
||||
'x-forwarded-for': '1.2.3.4, 2001:db8::123',
|
||||
'user-agent': 'HamHam-TestBrowser',
|
||||
};
|
||||
const mail = {
|
||||
host: '127.0.0.1',
|
||||
port: 15440,
|
||||
secure: true,
|
||||
username: 'testmailer',
|
||||
password: 'smtp-test-password',
|
||||
from: 'hamham@example.org',
|
||||
};
|
||||
try {
|
||||
expect(
|
||||
(
|
||||
await request.post('/api/auth/login', {
|
||||
headers,
|
||||
data: { username: 'testadmin', password },
|
||||
})
|
||||
).status(),
|
||||
).toBe(200);
|
||||
expect(
|
||||
(
|
||||
await request.put('/api/settings/mail', {
|
||||
headers,
|
||||
data: { password, email: 'admin@example.org', loginNotifications: true, mail },
|
||||
})
|
||||
).status(),
|
||||
).toBe(200);
|
||||
expect((await request.post('/api/settings/mail/test', { headers })).status()).toBe(200);
|
||||
expect(messages).toHaveLength(1);
|
||||
expect(recipients).toEqual(['admin@example.org']);
|
||||
await request.post('/api/auth/logout', { headers });
|
||||
expect(
|
||||
(
|
||||
await request.post('/api/auth/login', {
|
||||
headers,
|
||||
data: { username: 'testadmin', password },
|
||||
})
|
||||
).status(),
|
||||
).toBe(200);
|
||||
await expect
|
||||
.poll(
|
||||
async () => (await (await request.get('/api/auth/security')).json()).events[0].mail_status,
|
||||
)
|
||||
.toBe('sent');
|
||||
expect(messages).toHaveLength(2);
|
||||
expect(messages[1]).toContain('HamHam-TestBrowser');
|
||||
expect(messages[1]).toContain('2001:db8::123');
|
||||
expect(messages[1]).not.toContain('1.2.3.4');
|
||||
expect(messages[1]).not.toContain(password);
|
||||
expect(
|
||||
(
|
||||
await request.put('/api/settings/mail', {
|
||||
headers,
|
||||
data: {
|
||||
password,
|
||||
email: 'admin@example.org',
|
||||
loginNotifications: false,
|
||||
mail: { ...mail, password: '' },
|
||||
},
|
||||
})
|
||||
).status(),
|
||||
).toBe(200);
|
||||
expect((await request.post('/api/settings/mail/test', { headers })).status()).toBe(200);
|
||||
expect(messages).toHaveLength(3); // Blank password preserved the encrypted credential.
|
||||
await request.post('/api/auth/logout', { headers });
|
||||
await request.post('/api/auth/login', { headers, data: { username: 'testadmin', password } });
|
||||
expect((await (await request.get('/api/auth/security')).json()).events[0].mail_status).toBe(
|
||||
'disabled',
|
||||
);
|
||||
expect(messages).toHaveLength(3);
|
||||
} finally {
|
||||
await db`UPDATE admin_security SET login_notifications = false`;
|
||||
await db`DELETE FROM mail_config`;
|
||||
await db.end();
|
||||
await new Promise<void>((resolve) => server.close(resolve));
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,203 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { authenticator } from '../../src/lib/auth-crypto';
|
||||
import postgres from 'postgres';
|
||||
|
||||
const headers = { origin: 'http://localhost:3100' };
|
||||
test('authenticator enrollment, challenge isolation, replay protection, recovery and private mail settings', async ({
|
||||
page,
|
||||
playwright,
|
||||
}) => {
|
||||
const other = await playwright.request.newContext({ baseURL: 'http://localhost:3100' });
|
||||
const db = postgres(process.env.TEST_DATABASE_URL!, { max: 1 });
|
||||
const password = process.env.TEST_PASSWORD!;
|
||||
await other.post('/api/auth/login', { headers, data: { username: 'testadmin', password } });
|
||||
await page.goto('/admin');
|
||||
await page.getByLabel('Benutzername', { exact: true }).fill('testadmin');
|
||||
await page.getByLabel('Passwort', { exact: true }).fill(password);
|
||||
await page.getByRole('button', { name: 'Anmelden', exact: true }).click();
|
||||
await page.getByRole('button', { name: 'Einstellungen', exact: true }).click();
|
||||
await expect(page.getByRole('heading', { name: 'Mail & Benachrichtigungen' })).toBeVisible();
|
||||
await page.getByLabel('Aktuelles Passwort für 2FA').fill(password);
|
||||
await page.getByRole('button', { name: '2FA einrichten', exact: true }).click();
|
||||
await expect(
|
||||
page.getByRole('img', { name: 'QR-Code für die HamHam-Authenticator-Einrichtung' }),
|
||||
).toBeVisible();
|
||||
const secret = await page.locator('.authenticator-setup code').textContent();
|
||||
const oldCode = authenticator(secret!).generate({ timestamp: Date.now() - 30000 });
|
||||
await page
|
||||
.getByLabel('Authenticator- oder Wiederherstellungscode', { exact: true })
|
||||
.fill(oldCode);
|
||||
await page.getByRole('button', { name: '2FA aktivieren', exact: true }).click();
|
||||
await expect(page.locator('.recovery-codes li')).toHaveCount(10);
|
||||
const recovery = await page.locator('.recovery-codes li code').allTextContents();
|
||||
try {
|
||||
expect((await other.get('/api/auth/security')).status()).toBe(401); // Enrollment revoked the old session.
|
||||
// Password alone never grants an admin session when 2FA is enabled.
|
||||
const login = await other.post('/api/auth/login', {
|
||||
headers,
|
||||
data: { username: 'testadmin', password },
|
||||
});
|
||||
expect(await login.json()).toEqual({ twoFactorRequired: true });
|
||||
expect(login.headers()['set-cookie']).not.toContain('hamham_session');
|
||||
expect((await other.get('/api/export')).status()).toBe(401);
|
||||
expect((await other.get('/api/auth/security')).status()).toBe(401);
|
||||
expect(
|
||||
(
|
||||
await other.post('/api/auth/verify', {
|
||||
headers: { origin: 'https://foreign.example' },
|
||||
data: { code: recovery[0] },
|
||||
})
|
||||
).status(),
|
||||
).toBe(403);
|
||||
expect(
|
||||
(await other.post('/api/auth/verify', { headers, data: { code: 'invalid' } })).status(),
|
||||
).toBe(401);
|
||||
const code = authenticator(secret!).generate();
|
||||
expect((await other.post('/api/auth/verify', { headers, data: { code } })).status()).toBe(200);
|
||||
expect((await other.post('/api/auth/verify', { headers, data: { code } })).status()).toBe(401);
|
||||
await other.post('/api/auth/logout', { headers });
|
||||
await other.post('/api/auth/login', { headers, data: { username: 'testadmin', password } });
|
||||
expect((await other.post('/api/auth/verify', { headers, data: { code } })).status()).toBe(401);
|
||||
expect(
|
||||
(await other.post('/api/auth/verify', { headers, data: { code: recovery[0] } })).status(),
|
||||
).toBe(200);
|
||||
const security = await (await other.get('/api/auth/security')).json();
|
||||
expect(security.recoveryRemaining).toBe(9);
|
||||
expect(JSON.stringify(security)).not.toContain(secret!);
|
||||
expect(security.events[0].factor).toContain('Wiederherstellung');
|
||||
const mail = {
|
||||
host: '127.0.0.1',
|
||||
port: 1,
|
||||
secure: true,
|
||||
username: 'test',
|
||||
password: 'smtp-secret-for-test',
|
||||
from: 'hamham@example.org',
|
||||
};
|
||||
expect(
|
||||
(
|
||||
await other.put('/api/settings/mail', {
|
||||
headers,
|
||||
data: {
|
||||
email: 'admin@example.org',
|
||||
loginNotifications: true,
|
||||
mail,
|
||||
password,
|
||||
code: recovery[1],
|
||||
},
|
||||
})
|
||||
).status(),
|
||||
).toBe(200);
|
||||
const settings = await (await other.get('/api/auth/security')).json();
|
||||
expect(settings.mail.passwordConfigured).toBe(true);
|
||||
expect(JSON.stringify(settings)).not.toContain(mail.password);
|
||||
expect(JSON.stringify(await (await other.get('/api/settings')).json())).not.toContain(
|
||||
mail.password,
|
||||
);
|
||||
const [stored] = await db`SELECT data FROM mail_config WHERE id = 1`;
|
||||
expect(stored.data.password).not.toContain(mail.password);
|
||||
expect((await other.post('/api/settings/mail/test', { headers })).status()).toBe(502);
|
||||
await other.post('/api/auth/logout', { headers });
|
||||
// A challenge cannot be retried indefinitely, even with a valid recovery code afterwards.
|
||||
await other.post('/api/auth/login', { headers, data: { username: 'testadmin', password } });
|
||||
for (let n = 0; n < 5; n++)
|
||||
expect(
|
||||
(await other.post('/api/auth/verify', { headers, data: { code: 'invalid' } })).status(),
|
||||
).toBe(401);
|
||||
expect(
|
||||
(await other.post('/api/auth/verify', { headers, data: { code: recovery[2] } })).status(),
|
||||
).toBe(401);
|
||||
await other.post('/api/auth/logout', { headers });
|
||||
// Real browser login via the second screen, without asking for the password again.
|
||||
await page.getByRole('button', { name: 'Abmelden', exact: true }).click();
|
||||
await page.getByLabel('Benutzername', { exact: true }).fill('testadmin');
|
||||
await page.getByLabel('Passwort', { exact: true }).fill(password);
|
||||
await page.getByRole('button', { name: 'Anmelden', exact: true }).click();
|
||||
await expect(page.getByLabel('Passwort', { exact: true })).toHaveCount(0);
|
||||
await page
|
||||
.getByLabel('Authenticator- oder Wiederherstellungscode', { exact: true })
|
||||
.fill(recovery[2]);
|
||||
await page.getByRole('button', { name: 'Anmelden', exact: true }).click();
|
||||
await expect(page.getByRole('heading', { name: 'Hallo, testadmin.' })).toBeVisible();
|
||||
await page.getByRole('button', { name: 'Einstellungen', exact: true }).click();
|
||||
// Failed notification delivery must not undo a successful authenticated login.
|
||||
await expect
|
||||
.poll(
|
||||
async () =>
|
||||
(await (await page.request.get('/api/auth/security')).json()).events[0].mail_status,
|
||||
)
|
||||
.toBe('failed');
|
||||
await page.screenshot({
|
||||
path: test.info().outputPath('security-settings.png'),
|
||||
fullPage: true,
|
||||
});
|
||||
await page.setViewportSize({ width: 390, height: 844 });
|
||||
expect(
|
||||
await page.evaluate(
|
||||
() => document.documentElement.scrollWidth <= document.documentElement.clientWidth,
|
||||
),
|
||||
).toBe(true);
|
||||
await page.screenshot({
|
||||
path: test.info().outputPath('security-settings-mobile.png'),
|
||||
fullPage: true,
|
||||
});
|
||||
await db`UPDATE admin_security SET login_notifications = false`;
|
||||
// Expired challenges cannot consume an otherwise valid recovery code.
|
||||
await other.post('/api/auth/login', { headers, data: { username: 'testadmin', password } });
|
||||
await db`UPDATE login_challenges SET expires = now() - interval '1 second'`;
|
||||
expect(
|
||||
(await other.post('/api/auth/verify', { headers, data: { code: recovery[4] } })).status(),
|
||||
).toBe(401);
|
||||
// Two independent challenges racing for one recovery code yield only one session.
|
||||
const concurrent = await playwright.request.newContext({ baseURL: 'http://localhost:3100' });
|
||||
try {
|
||||
for (const client of [other, concurrent]) {
|
||||
expect(
|
||||
(
|
||||
await client.post('/api/auth/login', {
|
||||
headers,
|
||||
data: { username: 'testadmin', password },
|
||||
})
|
||||
).status(),
|
||||
).toBe(200);
|
||||
}
|
||||
const results = await Promise.all(
|
||||
[other, concurrent].map((client) =>
|
||||
client.post('/api/auth/verify', { headers, data: { code: recovery[4] } }),
|
||||
),
|
||||
);
|
||||
expect(results.map((result) => result.status()).sort()).toEqual([200, 401]);
|
||||
// Regeneration revokes other sessions and replaces all previous codes.
|
||||
const regenerated = await page.request.post('/api/auth/2fa/recovery', {
|
||||
headers,
|
||||
data: { password, code: recovery[5] },
|
||||
});
|
||||
expect(regenerated.status()).toBe(200);
|
||||
const newCodes = (await regenerated.json()).codes as string[];
|
||||
expect(newCodes).toHaveLength(10);
|
||||
for (const client of [other, concurrent])
|
||||
expect((await client.get('/api/auth/security')).status()).toBe(401);
|
||||
await other.post('/api/auth/login', { headers, data: { username: 'testadmin', password } });
|
||||
expect(
|
||||
(await other.post('/api/auth/verify', { headers, data: { code: recovery[6] } })).status(),
|
||||
).toBe(401);
|
||||
recovery[3] = newCodes[0];
|
||||
} finally {
|
||||
await concurrent.dispose();
|
||||
}
|
||||
expect(
|
||||
(
|
||||
await page.request.post('/api/auth/2fa/disable', {
|
||||
headers,
|
||||
data: { password, code: recovery[3] },
|
||||
})
|
||||
).status(),
|
||||
).toBe(200);
|
||||
expect((await (await page.request.get('/api/auth/security')).json()).enabled).toBe(false);
|
||||
} finally {
|
||||
await db`DELETE FROM admin_security WHERE admin_id = (SELECT id FROM admins WHERE username = 'testadmin')`;
|
||||
await db`DELETE FROM login_challenges`;
|
||||
await db`DELETE FROM mail_config`;
|
||||
await db.end();
|
||||
await other.dispose();
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user