F1: roles count only for accounts of a staff collection, in every exported predicate and access builder (isAdmin, isEditor, hasMinimumRole, adminOnly, adminOrEditor, requireRole, adminOrSelf, adminOrSelfIn, the Field variants, the roles field) and first in userMeetsRole. An account of another auth collection with its own roles: ['admin'] gets nothing. Staff collections: admin.user, access.authCollection, access.staffCollections. Predicates take req.payload as an optional last argument and otherwise use the staff collections of the config ipalKit built in this process. F2: every endpoint @clocklimited/payload-2fa registers answers accounts of the 2FA collection only (setup-totp wrote the secret into the 2FA collection under the caller's id, whatever collection the caller came from); remove and the admin reset/remove endpoints also need the second factor. F3: per-entity opt-out honoured, operation by operation: the package's custom.totp.disableAccessWrapper and ipal-kit's custom.ipal.secondFactor (passed on to the package); the package's global disableAccessWrapper too. F4: a no-user auth strategy on the 2FA collection guards the live config at the start of each request, so entities from plugins listed after ipalKit stay guarded after a dev hot reload and with disableOnInit. Tests through HTTP for all four; the 2FA tests now run on payload-2fa 3.0.0-beta.7 (what client sites use), a second file on beta.1 (alias), and the HTTP files can run against Postgres (IPAL_TEST_POSTGRES_URI). Docs, agent guide, skills and changelog updated; the not-yet-enrolled limitation is stated plainly. Co-Authored-By: Claude Fable 5.2 <[email protected]>
80 lines
3.1 KiB
TypeScript
80 lines
3.1 KiB
TypeScript
/**
|
|
* The documented non-interactive paths keep working with 2FA on (real HTTP, see securityHttp.test.ts):
|
|
* an API key of a staff account, and the bearer token of GET /api/ipal/doctor.
|
|
*/
|
|
import type { Payload } from 'payload'
|
|
|
|
import { payloadTotp } from '@clocklimited/payload-2fa'
|
|
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
|
import os from 'os'
|
|
import path from 'path'
|
|
import { getPayload } from 'payload'
|
|
import { afterAll, beforeAll, expect, test, vi } from 'vitest'
|
|
|
|
import { ipalKit } from '../src/plugin.js'
|
|
import { buildTestConfig } from './helpers.js'
|
|
import { testDatabase } from './http/db.js'
|
|
import { startPayloadServer, type TestServer } from './http/server.js'
|
|
|
|
vi.mock('server-only', () => ({}))
|
|
vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock)
|
|
|
|
const API_KEY = 'b7f0c3a2-5d1e-4f6a-9c8b-0e1d2f3a4b5c'
|
|
const DOCTOR_TOKEN = 'doctor-token-0123456789-0123456789-abcdef'
|
|
let server: TestServer
|
|
let payload: Payload
|
|
|
|
beforeAll(async () => {
|
|
process.env.IPAL_DOCTOR_TOKEN = DOCTOR_TOKEN
|
|
const config = buildTestConfig({
|
|
collections: [
|
|
// The project's own staff collection, with API keys.
|
|
{ slug: 'users', auth: { useAPIKey: true }, fields: [] },
|
|
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } },
|
|
],
|
|
db: testDatabase('sechttp_tok'),
|
|
editor: lexicalEditor(),
|
|
plugins: [
|
|
ipalKit({
|
|
access: { authCollection: 'users' },
|
|
forms: {},
|
|
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] },
|
|
twoFactor: { issuer: 'Example', plugin: payloadTotp },
|
|
}),
|
|
],
|
|
secret: 'integration-test-secret-0123456789',
|
|
telemetry: false,
|
|
})
|
|
payload = await getPayload({ config })
|
|
server = await startPayloadServer(config)
|
|
await payload.create({
|
|
collection: 'users' as never,
|
|
data: { apiKey: API_KEY, email: '[email protected]', enableAPIKey: true, password: 'a-long-test-password-1', roles: ['admin'], totpSecret: 'JBSWY3DPEHPK3PXP' } as never,
|
|
})
|
|
}, 120_000)
|
|
|
|
afterAll(async () => {
|
|
delete process.env.IPAL_DOCTOR_TOKEN
|
|
await server?.close()
|
|
await payload?.destroy?.()
|
|
})
|
|
|
|
const get = (pathname: string, authorization?: string) =>
|
|
fetch(`${server.url}/api${pathname}`, { headers: authorization ? { authorization } : {} })
|
|
|
|
test('an API key of an admin account reads Site integrations and form submissions without a TOTP code', async () => {
|
|
const key = `users API-Key ${API_KEY}`
|
|
expect((await get('/globals/site-integrations', key)).status).toBe(200)
|
|
expect((await get('/form-submissions', key)).status).toBe(200)
|
|
expect((await get('/users', key)).status).toBe(200)
|
|
expect((await get('/globals/site-integrations', 'users API-Key wrong-key')).status).toBe(403)
|
|
})
|
|
|
|
test('GET /api/ipal/doctor answers its bearer token, and nobody else', async () => {
|
|
const ok = await get('/ipal/doctor', `Bearer ${DOCTOR_TOKEN}`)
|
|
expect(ok.status).toBe(200)
|
|
expect(await ok.json()).toMatchObject({ usersWithout2fa: 0 })
|
|
expect((await get('/ipal/doctor')).status).toBe(401)
|
|
expect((await get('/ipal/doctor', 'Bearer not-the-token-0123456789-0123456789')).status).toBe(401)
|
|
})
|