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]>
86 lines
4.1 KiB
TypeScript
86 lines
4.1 KiB
TypeScript
import type { Config } from 'payload'
|
|
|
|
import { payloadTotp } from '@clocklimited/payload-2fa'
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
import { iapl } from '../src/index.js'
|
|
import { totpRedirectTarget } from '../src/modules/security/totp/totpRedirect.js'
|
|
|
|
const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }
|
|
const fresh = { _strategy: 'local-jwt', hasTotp: false }
|
|
|
|
const base = (): Config =>
|
|
({ admin: { user: 'users' }, collections: [{ slug: 'users', auth: true, fields: [] }] }) as unknown as Config
|
|
|
|
const providerPaths = (config: Config) =>
|
|
(config.admin?.components?.providers ?? []).map((p) => (typeof p === 'object' && p ? p.path : p))
|
|
|
|
/** Follows the redirects the provider would make, the way a browser would. */
|
|
function walk(start: string, user: typeof fresh, adminRoute = '/admin') {
|
|
const visited = [start]
|
|
let url = start
|
|
for (let i = 0; i < 5; i++) {
|
|
const next = totpRedirectTarget({ adminRoute, forceSetup: true, pathname: url.split('?')[0], user })
|
|
if (!next) {return visited}
|
|
url = next
|
|
visited.push(url)
|
|
}
|
|
throw new Error(`redirect loop: ${visited.join(' → ')}`)
|
|
}
|
|
|
|
describe('2FA setup redirect (fresh admin, forceSetup)', () => {
|
|
it('lands on the setup screen once, with the page to return to — no loop, no back=null', () => {
|
|
expect(walk('/admin', fresh)).toEqual(['/admin', '/admin/setup-totp?back=%2Fadmin'])
|
|
expect(walk('/admin/collections/pages', fresh).at(-1)).toBe('/admin/setup-totp?back=%2Fadmin%2Fcollections%2Fpages')
|
|
})
|
|
|
|
it('stays on the setup and verify screens, with or without a trailing slash', () => {
|
|
for (const pathname of ['/admin/setup-totp', '/admin/setup-totp/', '/admin/verify-totp']) {
|
|
expect(totpRedirectTarget({ adminRoute: '/admin', forceSetup: true, pathname, user: fresh })).toBeNull()
|
|
}
|
|
})
|
|
|
|
it('never redirects on an unknown path (the package read a missing header as null → back=null)', () => {
|
|
expect(totpRedirectTarget({ adminRoute: '/admin', forceSetup: true, pathname: null, user: fresh })).toBeNull()
|
|
})
|
|
|
|
it('works with a custom admin route and lets logout through', () => {
|
|
expect(walk('/its', fresh, '/its')).toEqual(['/its', '/its/setup-totp?back=%2Fits'])
|
|
expect(walk('/', fresh, '/')).toEqual(['/', '/setup-totp?back=%2F'])
|
|
expect(totpRedirectTarget({ adminRoute: '/admin', forceSetup: true, pathname: '/admin/logout', user: fresh })).toBeNull()
|
|
})
|
|
|
|
it('sends a user with TOTP to verify until this session passed it; api keys pass', () => {
|
|
const user = { _strategy: 'local-jwt', hasTotp: true }
|
|
expect(walk('/admin', user)).toEqual(['/admin', '/admin/verify-totp?back=%2Fadmin'])
|
|
expect(walk('/admin', { _strategy: 'totp', hasTotp: true })).toEqual(['/admin'])
|
|
expect(walk('/admin', { _strategy: 'api-key', hasTotp: false })).toEqual(['/admin'])
|
|
})
|
|
|
|
it('leaves users alone when setup is not forced', () => {
|
|
expect(totpRedirectTarget({ adminRoute: '/admin', pathname: '/admin', user: fresh })).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('twoFactor.provider', () => {
|
|
const run = (provider?: unknown) =>
|
|
iapl({ i18n, twoFactor: { issuer: 'Test', plugin: payloadTotp, ...(provider === undefined ? {} : { provider }) } } as never)(base())
|
|
|
|
it("replaces the package's header-based provider with IAPL's by default", async () => {
|
|
const config = await run()
|
|
expect(providerPaths(config)).toContain('@intecion/ipal-kit/admin#TotpRedirectProvider')
|
|
expect(providerPaths(config)).not.toContain('@clocklimited/payload-2fa/rsc#TOTPProvider')
|
|
const ours = config.admin?.components?.providers?.find(
|
|
(p) => typeof p === 'object' && p && p.path === '@intecion/ipal-kit/admin#TotpRedirectProvider',
|
|
)
|
|
expect(ours).toMatchObject({ clientProps: { forceSetup: true } })
|
|
})
|
|
|
|
it('takes a project component, or false to keep the package provider', async () => {
|
|
expect(providerPaths(await run('@/components/MyTotp#MyTotp'))).toContain('@/components/MyTotp#MyTotp')
|
|
const kept = await run(false)
|
|
expect(providerPaths(kept)).toContain('@clocklimited/payload-2fa/rsc#TOTPProvider')
|
|
expect(providerPaths(kept)).not.toContain('@intecion/ipal-kit/admin#TotpRedirectProvider')
|
|
})
|
|
})
|