A locked staff account answers its login like a wrong password (accounts.lockedAnswer)
Payload checks the lock before the password, so after five wrong passwords
a staff address answered "This user is locked…" to any password while an
address with no account always answered "The email or password provided is
incorrect.": anybody could find out which addresses have a staff account
(IAWL idea 6, owner's Q1). A config-level afterError hook now answers
LockedAuth on a staff collection's login exactly like AuthenticationError:
REST status and body, GraphQL message and extensions.name. The lock is
unchanged; visitor collections keep Payload's answer.
accounts: { lockedAnswer: 'payload' } keeps Payload's message; any other
value stops the start. Building blocks lockedLooksWrong, withLockedAnswer,
graphqlLoginName.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
fc70a34f3e
commit
15b5c6258d
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* A locked staff account answers its login exactly like a wrong password (accounts.lockedAnswer, 1.17.2; IAWL
|
||||
* idea 6). Shared by lockedAnswer.test.ts (default, 2FA on), lockedAnswer2fa-off.test.ts (2FA off) and
|
||||
* lockedAnswerPayload.test.ts (`lockedAnswer: 'payload'`): one Payload per file.
|
||||
* Database: SQLite in memory, or IPAL_TEST_POSTGRES_URI / IPAL_TEST_MONGO_URI (test/http/db.ts).
|
||||
*/
|
||||
import type { Payload } from 'payload'
|
||||
|
||||
import { payloadTotp } from '@clocklimited/payload-2fa'
|
||||
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
||||
import { getPayload } from 'payload'
|
||||
import { afterAll, beforeAll } from 'vitest'
|
||||
|
||||
import { iapl } from '../src/plugin.js'
|
||||
import { buildTestConfig } from './helpers.js'
|
||||
import { testDatabase } from './http/db.js'
|
||||
import { startPayloadServer, type TestServer } from './http/server.js'
|
||||
|
||||
export const PASSWORD = 'a-long-test-password-1'
|
||||
|
||||
export type Answer = { body: string; status: number }
|
||||
|
||||
export function lockedAnswerSite({ lockedAnswer, twoFactor = true }: { lockedAnswer?: 'payload'; twoFactor?: boolean } = {}) {
|
||||
const state = {} as { payload: Payload; server: TestServer }
|
||||
beforeAll(async () => {
|
||||
const config = buildTestConfig({
|
||||
admin: { user: 'users' },
|
||||
collections: [
|
||||
// a visitor collection with Payload's own lock: not staff, not changed
|
||||
{ slug: 'customers', access: { create: () => true }, auth: { maxLoginAttempts: 3 }, fields: [] },
|
||||
{ slug: 'media', fields: [], upload: { staticDir: '/tmp/iapl-locked-answer-media' } },
|
||||
],
|
||||
db: testDatabase('lockedanswer'),
|
||||
editor: lexicalEditor(),
|
||||
plugins: [
|
||||
iapl({
|
||||
access: { authCollection: 'users' },
|
||||
...(lockedAnswer ? { accounts: { lockedAnswer } } : {}),
|
||||
i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] },
|
||||
twoFactor: twoFactor ? { issuer: 'Example', plugin: payloadTotp } : false,
|
||||
}),
|
||||
],
|
||||
secret: 'integration-test-secret-0123456789',
|
||||
telemetry: false,
|
||||
})
|
||||
state.payload = await getPayload({ config })
|
||||
state.server = await startPayloadServer(config)
|
||||
await state.payload.create({ collection: 'users' as never, data: { email: '[email protected]', password: PASSWORD, roles: ['admin'] } as never })
|
||||
await state.payload.create({ collection: 'customers' as never, data: { email: '[email protected]', password: PASSWORD } as never })
|
||||
}, 120_000)
|
||||
afterAll(async () => {
|
||||
await state.server?.close()
|
||||
await state.payload?.destroy?.()
|
||||
})
|
||||
|
||||
const restLogin = async (collection: string, email: string, password: string): Promise<Answer> => {
|
||||
const res = await fetch(`${state.server.url}/api/${collection}/login`, {
|
||||
body: JSON.stringify({ email, password }),
|
||||
headers: { 'content-type': 'application/json' },
|
||||
method: 'POST',
|
||||
})
|
||||
return { body: await res.text(), status: res.status }
|
||||
}
|
||||
const gqlLogin = async (mutation: string, email: string, password: string) => {
|
||||
const res = await fetch(`${state.server.url}/api/graphql`, {
|
||||
body: JSON.stringify({ query: `mutation { ${mutation}(email: ${JSON.stringify(email)}, password: ${JSON.stringify(password)}) { token } }` }),
|
||||
headers: { 'content-type': 'application/json' },
|
||||
method: 'POST',
|
||||
})
|
||||
const body = (await res.json()) as { errors?: { extensions?: { name?: string; statusCode?: number }; message: string }[] }
|
||||
const e = body.errors?.[0]
|
||||
return { name: e?.extensions?.name, message: e?.message, status: res.status, statusCode: e?.extensions?.statusCode }
|
||||
}
|
||||
/** Locks `email` with wrong passwords (Payload's default: 5). */
|
||||
const lock = async (collection: string, email: string, times = 5) => {
|
||||
for (let i = 0; i < times; i++) {await restLogin(collection, email, 'a-wrong-password')}
|
||||
}
|
||||
return { gqlLogin, lock, restLogin, state }
|
||||
}
|
||||
Reference in New Issue
Block a user