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,35 @@
|
||||
/** A locked staff account answers like a wrong password, REST and GraphQL (lockedAnswerSuite.ts). 2FA on. */
|
||||
import { describe, expect, test, vi } from 'vitest'
|
||||
|
||||
import { lockedAnswerSite, PASSWORD } from './lockedAnswerSuite.js'
|
||||
|
||||
vi.mock('server-only', () => ({}))
|
||||
vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock)
|
||||
|
||||
const { gqlLogin, lock, restLogin, state } = lockedAnswerSite()
|
||||
|
||||
describe('accounts.lockedAnswer (default): a locked staff account looks like a wrong password', () => {
|
||||
test('REST: the locked owner, with any password, gets exactly what an invented address gets', async () => {
|
||||
await lock('users', '[email protected]')
|
||||
const invented = await restLogin('users', '[email protected]', 'a-wrong-password')
|
||||
expect(invented.status).toBe(401)
|
||||
expect(await restLogin('users', '[email protected]', 'a-wrong-password')).toEqual(invented)
|
||||
expect(await restLogin('users', '[email protected]', PASSWORD)).toEqual(invented)
|
||||
expect(invented.body).not.toMatch(/locked/i)
|
||||
// the lock itself is still there
|
||||
const stored = (await state.payload.find({ collection: 'users' as never, overrideAccess: true, showHiddenFields: true, where: { email: { equals: '[email protected]' } } })).docs[0] as { lockUntil?: string }
|
||||
expect(new Date(String(stored.lockUntil)).getTime()).toBeGreaterThan(Date.now())
|
||||
})
|
||||
|
||||
test('GraphQL: the same message, status and error name', async () => {
|
||||
const invented = await gqlLogin('loginUser', '[email protected]', 'a-wrong-password')
|
||||
expect(invented.name).toBe('AuthenticationError')
|
||||
expect(await gqlLogin('loginUser', '[email protected]', PASSWORD)).toEqual(invented)
|
||||
})
|
||||
|
||||
test('a visitor collection keeps Payload\'s answer (not staff)', async () => {
|
||||
await lock('customers', '[email protected]', 3)
|
||||
expect((await restLogin('customers', '[email protected]', PASSWORD)).body).toMatch(/locked/i)
|
||||
expect((await gqlLogin('loginCustomer', '[email protected]', PASSWORD)).name).toBe('LockedAuth')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user