IAPL rename, scripted: preferred names in src, bin, scripts, services, tests; bin files and skill folders moved
Replaced by an explicit list of forms only (ten exported names, runtime env names, project file names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are not touched. package.json keeps the five old bin names beside the five new ones. The old names stop working in this commit and come back as aliases in the next; docs follow. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
414a4702df
commit
0a4f001797
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* The rules behind test/securityHttp.test.ts, one by one: who is staff (userMeetsRole), when the second
|
||||
* factor counts as passed, and what ipalKit does with a 2FA plugin it does not recognise.
|
||||
* factor counts as passed, and what iapl does with a 2FA plugin it does not recognise.
|
||||
*/
|
||||
import type { Access, Config } from 'payload'
|
||||
|
||||
@@ -23,7 +23,7 @@ import {
|
||||
} from '../src/modules/security/totp/secondFactor.js'
|
||||
import { buildTwoFactorResetEndpoint, resolveRequireSetup, strictExempt } from '../src/modules/security/totp/setupRequired.js'
|
||||
import { buildVerifyLimit, clearVerifyLimits, verifyLimitKey } from '../src/modules/security/totp/verifyLimit.js'
|
||||
import { ipalKit } from '../src/plugin.js'
|
||||
import { iapl } from '../src/plugin.js'
|
||||
import { withTotpParts } from './helpers.js'
|
||||
|
||||
const i18n: I18nConfig = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }
|
||||
@@ -47,7 +47,7 @@ const twoAuth = (): Config =>
|
||||
|
||||
describe('userMeetsRole: who is staff', () => {
|
||||
test('with roles: the role decides; an account of another auth collection never passes', async () => {
|
||||
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(twoAuth())
|
||||
const config = await iapl({ access: { authCollection: 'users' }, i18n, twoFactor: false })(twoAuth())
|
||||
const payload = payloadOf(config)
|
||||
expect(userMeetsRole({ payload, user: { collection: 'users', roles: ['admin'] } }, 'admin')).toBe(true)
|
||||
expect(userMeetsRole({ payload, user: { collection: 'users', roles: ['user'] } }, 'editor')).toBe(false)
|
||||
@@ -61,7 +61,7 @@ describe('userMeetsRole: who is staff', () => {
|
||||
})
|
||||
|
||||
test('without `access`: every account of the panel collection passes, other auth collections do not', async () => {
|
||||
const config = await ipalKit({ i18n, twoFactor: false })({ ...twoAuth(), admin: { user: 'users' } } as Config)
|
||||
const config = await iapl({ i18n, twoFactor: false })({ ...twoAuth(), admin: { user: 'users' } } as Config)
|
||||
const payload = payloadOf(config)
|
||||
expect(staffCollectionSlugs(payload)).toEqual(['users'])
|
||||
expect(userMeetsRole({ payload, user: { collection: 'users' } }, 'admin')).toBe(true)
|
||||
@@ -69,7 +69,7 @@ describe('userMeetsRole: who is staff', () => {
|
||||
})
|
||||
|
||||
test('access.staffCollections names more staff collections, on purpose', async () => {
|
||||
const config = await ipalKit({ access: { authCollection: 'users', staffCollections: ['partners'] }, i18n, twoFactor: false })(twoAuth())
|
||||
const config = await iapl({ access: { authCollection: 'users', staffCollections: ['partners'] }, i18n, twoFactor: false })(twoAuth())
|
||||
const payload = payloadOf(config)
|
||||
expect(staffCollectionSlugs(payload)).toEqual(['users', 'partners'])
|
||||
expect(userMeetsRole({ payload, user: { collection: 'partners' } }, 'admin')).toBe(true) // no roles field there: all equal
|
||||
@@ -77,8 +77,8 @@ describe('userMeetsRole: who is staff', () => {
|
||||
expect(userMeetsRole({ payload, user: { collection: 'customers' } }, 'admin')).toBe(false)
|
||||
})
|
||||
|
||||
test('without a payload: the staff collections of the config ipalKit built last; only a held role counts', async () => {
|
||||
await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })({ ...twoAuth(), admin: { user: 'users' } } as Config)
|
||||
test('without a payload: the staff collections of the config iapl built last; only a held role counts', async () => {
|
||||
await iapl({ access: { authCollection: 'users' }, i18n, twoFactor: false })({ ...twoAuth(), admin: { user: 'users' } } as Config)
|
||||
expect(userMeetsRole({ user: { collection: 'users' } }, 'user')).toBe(false)
|
||||
expect(userMeetsRole({ user: { collection: 'users', roles: ['admin'] } }, 'admin')).toBe(true)
|
||||
expect(userMeetsRole({ user: { collection: 'customers', roles: ['admin'] } }, 'admin')).toBe(false)
|
||||
@@ -190,12 +190,12 @@ describe('secondFactorPassed', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('ipalKit and the 2FA plugin', () => {
|
||||
describe('iapl and the 2FA plugin', () => {
|
||||
const passwordOnly = { id: 1, _strategy: 'local-jwt', collection: 'users', hasTotp: true, roles: ['admin'] }
|
||||
const verified = { ...passwordOnly, _strategy: 'totp' }
|
||||
|
||||
test('every collection and global in the config is guarded, whenever it was added', async () => {
|
||||
const config = await ipalKit({
|
||||
const config = await iapl({
|
||||
access: { authCollection: 'users' }, consentLog: true, cookieScanner: { crawl: true }, forms: {}, i18n, stats: true,
|
||||
twoFactor: { issuer: 'A', plugin: fakeTotp as never },
|
||||
})({ collections: [{ slug: 'pages', fields: [] }], globals: [{ slug: 'own', fields: [] }], secret: 'x' } as unknown as Config)
|
||||
@@ -258,15 +258,15 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
test('a 2FA plugin without the parts the guard relies on stops the start', async () => {
|
||||
const unknown = () => (c: Config) => c
|
||||
const run = (guard?: false) =>
|
||||
ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: unknown as never, ...(guard === false ? { guard } : {}) } })({ collections: [], secret: 'x' } as unknown as Config)
|
||||
iapl({ i18n, twoFactor: { issuer: 'A', plugin: unknown as never, ...(guard === false ? { guard } : {}) } })({ collections: [], secret: 'x' } as unknown as Config)
|
||||
await expect(run()).rejects.toThrow(/did not add the `totpSecret` field, the `hasTotp` field, the `totp` auth strategy/)
|
||||
// twoFactor.guard: false = the package alone, as before
|
||||
const config = await run(false)
|
||||
expect((config.custom as { ipal?: { twoFactor?: unknown } } | undefined)?.ipal?.twoFactor).toBeUndefined()
|
||||
})
|
||||
|
||||
test('per-entity opt-out: the package key and ipal-kit\'s key, operation by operation', async () => {
|
||||
const config = await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never } })({
|
||||
test('per-entity opt-out: the package key and IAPL\'s key, operation by operation', async () => {
|
||||
const config = await iapl({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never } })({
|
||||
collections: [
|
||||
{ slug: 'a', custom: { totp: { disableAccessWrapper: { read: true } } }, fields: [] },
|
||||
{ slug: 'b', custom: { ipal: { secondFactor: false } }, fields: [] },
|
||||
@@ -288,7 +288,7 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
const g = config.globals!.find((x) => x.slug === 'g')!.access
|
||||
expect(call(g, 'read')).toBe(true)
|
||||
expect(call(g, 'update')).toBe(false)
|
||||
// ipal-kit's key reaches the package in its own key, so the package leaves the operation alone too
|
||||
// IAPL's key reaches the package in its own key, so the package leaves the operation alone too
|
||||
expect((config.globals!.find((x) => x.slug === 'g')!.custom as { totp: unknown }).totp).toEqual({ disableAccessWrapper: { read: true } })
|
||||
})
|
||||
|
||||
@@ -338,9 +338,9 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
return last
|
||||
}
|
||||
clearVerifyLimits()
|
||||
expect(await call(await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: withEndpoint as never } })(base()), 6)).toBe(429)
|
||||
expect(await call(await iapl({ i18n, twoFactor: { issuer: 'A', plugin: withEndpoint as never } })(base()), 6)).toBe(429)
|
||||
clearVerifyLimits()
|
||||
expect(await call(await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: withEndpoint as never, verifyLimit: false } })(base()), 8)).toBe(200)
|
||||
expect(await call(await iapl({ i18n, twoFactor: { issuer: 'A', plugin: withEndpoint as never, verifyLimit: false } })(base()), 8)).toBe(200)
|
||||
})
|
||||
|
||||
// M1 (R3-2): the count is also kept in the database, so a restart or a second instance does not reset it.
|
||||
@@ -475,7 +475,7 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
...withTotpParts(c, o.collection),
|
||||
endpoints: [...(c.endpoints ?? []), ...['/verify-totp', '/setup-totp', '/remove-totp'].map((path) => ({ handler, method: 'post' as const, path }))],
|
||||
})
|
||||
const build = (twoFactor: Record<string, unknown> = {}) => ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: withEndpoints as never, ...twoFactor } as never })({ collections: [], endpoints: [], secret: 'x' } as unknown as Config)
|
||||
const build = (twoFactor: Record<string, unknown> = {}) => iapl({ i18n, twoFactor: { issuer: 'A', plugin: withEndpoints as never, ...twoFactor } as never })({ collections: [], endpoints: [], secret: 'x' } as unknown as Config)
|
||||
const call = async (config: Config, path: string, headers: Record<string, string>, user: Record<string, unknown>) =>
|
||||
(await config.endpoints!.find((e) => e.path === path)!.handler({ headers: new Headers(headers), i18n: undefined, payload: { config, logger: { warn } }, url: 'https://site.example/api' + path, user: { id: 5, collection: 'users', ...user } } as never)).status
|
||||
const elsewhere = { host: 'site.example', origin: 'https://blog.site.example' }
|
||||
@@ -507,7 +507,7 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
})
|
||||
|
||||
test('staff collections outside the 2FA collection are named in a warning at start', async () => {
|
||||
const config = await ipalKit({ access: { authCollection: 'users', staffCollections: ['partners'] }, i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never } })({
|
||||
const config = await iapl({ access: { authCollection: 'users', staffCollections: ['partners'] }, i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never } })({
|
||||
...twoAuth(),
|
||||
admin: { user: 'users' },
|
||||
} as Config)
|
||||
@@ -528,7 +528,7 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
|
||||
test('setup required: on by default, with the field, the hook, the reset endpoint and the notices; every part can be changed', async () => {
|
||||
const base = () => ({ collections: [], secret: 'x' }) as unknown as Config
|
||||
const on = await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never } })(base())
|
||||
const on = await iapl({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never } })(base())
|
||||
const guard = (c: Config) => (c.custom as { ipal: { twoFactor: Record<string, unknown> } }).ipal.twoFactor
|
||||
expect(guard(on)).toMatchObject({ collection: 'users', requireSetup: true, setupWindowMs: 72 * 3_600_000 })
|
||||
const users = on.collections!.find((c) => c.slug === 'users')!
|
||||
@@ -537,10 +537,10 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
expect(on.endpoints!.some((e) => e.path === '/ipal/two-factor/reset')).toBe(true)
|
||||
expect(JSON.stringify(on.admin?.components?.beforeDashboard)).toContain('TwoFactorAlert')
|
||||
|
||||
const custom = await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: { windowHours: 24 } } })(base())
|
||||
const custom = await iapl({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: { windowHours: 24 } } })(base())
|
||||
expect(guard(custom).setupWindowMs).toBe(24 * 3_600_000)
|
||||
|
||||
const off = await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: false } })(base())
|
||||
const off = await iapl({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: false } })(base())
|
||||
expect(guard(off)).toMatchObject({ requireSetup: false })
|
||||
// the data field stays whenever 2FA is on: the schema must not depend on the switch (SQL would drop the column)
|
||||
const offUsers = off.collections!.find((c) => c.slug === 'users')!
|
||||
@@ -548,7 +548,7 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
expect(offUsers.fields.some((f) => 'name' in f && f.name === 'totpSetupStatus')).toBe(false)
|
||||
expect(offUsers.hooks?.beforeChange?.length ?? 0).toBe(0) // no stamping while the mode is off
|
||||
expect(JSON.stringify(off.admin?.components?.beforeDashboard ?? [])).not.toContain('TwoFactorAlert')
|
||||
const guardOff = await ipalKit({ i18n, twoFactor: { guard: false, issuer: 'A', plugin: fakeTotp as never } })(base())
|
||||
const guardOff = await iapl({ i18n, twoFactor: { guard: false, issuer: 'A', plugin: fakeTotp as never } })(base())
|
||||
expect(guardOff.collections!.find((c) => c.slug === 'users')!.fields.some((f) => 'name' in f && f.name === 'totpSetupFrom')).toBe(true)
|
||||
|
||||
// each part of the mode has its own switch
|
||||
@@ -559,28 +559,28 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
})
|
||||
// fakeTotp registers no provider, so the setup notice is checked with the real package in test/safeBack.test.ts
|
||||
expect(has(on)).toMatchObject({ dashboard: true, status: true })
|
||||
const noDash = await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: { dashboardNotice: false } } })(base())
|
||||
const noDash = await iapl({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: { dashboardNotice: false } } })(base())
|
||||
expect(has(noDash)).toMatchObject({ dashboard: false, status: true })
|
||||
const noStatus = await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: { statusField: false } } })(base())
|
||||
const noStatus = await iapl({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: { statusField: false } } })(base())
|
||||
expect(has(noStatus)).toMatchObject({ dashboard: true, status: false })
|
||||
expect(guard(noStatus)).toMatchObject({ requireSetup: true }) // the lock itself is unchanged
|
||||
expect(off.endpoints!.some((e) => e.path === '/ipal/two-factor/reset')).toBe(true) // an admin can still reset a lost phone
|
||||
|
||||
// 2FA off: nothing of this exists
|
||||
const none = await ipalKit({ i18n, twoFactor: false })(base())
|
||||
const none = await iapl({ i18n, twoFactor: false })(base())
|
||||
expect(none.endpoints!.some((e) => e.path === '/ipal/two-factor/reset')).toBe(false)
|
||||
expect(none.collections!.find((c) => c.slug === 'users')!.fields.some((f) => 'name' in f && f.name === 'totpSetupFrom')).toBe(false)
|
||||
})
|
||||
|
||||
test.each([['24'], [-1], [Number.NaN], [Number.POSITIVE_INFINITY], [true], [null]])('requireSetup.windowHours %s stops the start (a typo must not mean "no limit")', async (windowHours) => {
|
||||
await expect(Promise.resolve().then(() => ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: { windowHours: windowHours as never } } })({ collections: [], secret: 'x' } as unknown as Config))).rejects.toThrow(/windowHours must be a number/)
|
||||
// also when ipal-kit's own check is off: a wrong value is never silently accepted
|
||||
await expect(Promise.resolve().then(() => ipalKit({ i18n, twoFactor: { guard: false, issuer: 'A', plugin: fakeTotp as never, requireSetup: { windowHours: windowHours as never } } })({ collections: [], secret: 'x' } as unknown as Config))).rejects.toThrow(/windowHours/)
|
||||
await expect(Promise.resolve().then(() => iapl({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: { windowHours: windowHours as never } } })({ collections: [], secret: 'x' } as unknown as Config))).rejects.toThrow(/windowHours must be a number/)
|
||||
// also when IAPL's own check is off: a wrong value is never silently accepted
|
||||
await expect(Promise.resolve().then(() => iapl({ i18n, twoFactor: { guard: false, issuer: 'A', plugin: fakeTotp as never, requireSetup: { windowHours: windowHours as never } } })({ collections: [], secret: 'x' } as unknown as Config))).rejects.toThrow(/windowHours/)
|
||||
expect(() => resolveRequireSetup({ windowHours: windowHours as never })).toThrow(/windowHours/)
|
||||
})
|
||||
|
||||
test('requireSetup: null is the default, like leaving it out', async () => {
|
||||
const config = await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: null as never } })({ collections: [], secret: 'x' } as unknown as Config)
|
||||
const config = await iapl({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: null as never } })({ collections: [], secret: 'x' } as unknown as Config)
|
||||
expect((config.custom as { ipal: { twoFactor: Record<string, unknown> } }).ipal.twoFactor).toMatchObject({ requireSetup: true, setupWindowMs: 72 * 3_600_000 })
|
||||
expect(JSON.stringify(config.admin?.components?.beforeDashboard)).toContain('TwoFactorAlert')
|
||||
expect(resolveRequireSetup(null as never)).toEqual({ requireSetup: true, setupWindowMs: 72 * 3_600_000 })
|
||||
@@ -599,7 +599,7 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
|
||||
test('an async function stops the start', async () => {
|
||||
expect(() => resolveRequireSetup({ exempt: (async () => false) as never })).toThrow(/must not be async/)
|
||||
await expect(Promise.resolve().then(() => ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: { exempt: (async () => true) as never } } })({ collections: [], secret: 'x' } as unknown as Config))).rejects.toThrow(/must not be async/)
|
||||
await expect(Promise.resolve().then(() => iapl({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never, requireSetup: { exempt: (async () => true) as never } } })({ collections: [], secret: 'x' } as unknown as Config))).rejects.toThrow(/must not be async/)
|
||||
expect(() => resolveRequireSetup({ exempt: 'yes' as never })).toThrow(/must be a function/)
|
||||
})
|
||||
|
||||
@@ -740,7 +740,7 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
})
|
||||
|
||||
test('twoFactor: false — no guard, a password is enough (as documented)', async () => {
|
||||
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })({ collections: [], secret: 'x' } as unknown as Config)
|
||||
const config = await iapl({ access: { authCollection: 'users' }, i18n, twoFactor: false })({ collections: [], secret: 'x' } as unknown as Config)
|
||||
const payload = payloadOf(config)
|
||||
const read = config.globals!.find((g) => g.slug === 'site-integrations')!.access!.read as (a: unknown) => unknown
|
||||
expect(read({ req: { payload, user: { collection: 'users', roles: ['admin'] } } })).toBe(true)
|
||||
|
||||
Reference in New Issue
Block a user