2FA back link: review fixes — views guarded by object, client views, a loud stale import map
SB-1: the 2FA package replaces a view that already stands under its keys, so a key that existed before the factory ran was wrongly treated as the project's own and left unguarded (also with a double registration, or ipalKit over its own output). Views are now compared by object, right after the factory. SB-2: SafeTotpView renders through Payload's RenderServerComponent: a client component set as a 2FA view gets only client props plus the checked back. SB-3: a production build with the previous import map shows an empty 2FA screen. twoFactor.importMapCheck (on, production only) reads the committed map at start: an error in the log and a message on the 2FA screens. Docs name the real symptom. SB-4: enabled: false restores admin.dependencies; wrapTotpViews keeps exportName. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
93e75426ab
commit
ad51456eaa
@@ -75,42 +75,60 @@ export const safeAdminBack: SafeBackRule = (back, { adminRoute, origin }) => {
|
||||
/** The package's views that take `back` — matched by the route the package gives them. */
|
||||
const TOTP_VIEW_PATH = /^\/?(?:setup|verify)-totp$/
|
||||
|
||||
type ViewComponent = { clientProps?: object; path: string; serverProps?: object } | string
|
||||
type ViewLike = { Component?: unknown; path?: string }
|
||||
/** Import-map path of the wrapper ipalKit registers. */
|
||||
export const SAFE_TOTP_VIEW = '@intecion/ipal-kit/admin#SafeTotpView'
|
||||
|
||||
/** Key of the import-map entry that keeps the package's own view importable. */
|
||||
export const totpViewDependencyKey = (path: string) => `ipal-totp-view:${path}`
|
||||
/** A view component as it stands in a Payload config. */
|
||||
export type TotpViewComponent = { clientProps?: object; exportName?: string; path: string; serverProps?: object } | string
|
||||
type ViewLike = { Component?: unknown; path?: string }
|
||||
type Dependency = { exportName?: string; path: string; type: 'component' }
|
||||
|
||||
/** `path#export` of a component — the key it has in the import map. */
|
||||
export const totpViewImportKey = (component: TotpViewComponent): string => {
|
||||
const [path, hash] = (typeof component === 'string' ? component : component.path).split('#')
|
||||
const exportName = (typeof component === 'object' ? component.exportName : undefined) ?? hash ?? 'default'
|
||||
return `${path}#${exportName}`
|
||||
}
|
||||
|
||||
/** Key of the `admin.dependencies` entry that keeps a wrapped view importable. */
|
||||
export const totpViewDependencyKey = (component: TotpViewComponent) => `ipal-totp-view:${totpViewImportKey(component)}`
|
||||
|
||||
/**
|
||||
* Puts ipal-kit's wrapper in front of the 2FA views the package added (`added` = their keys):
|
||||
* the wrapper cleans `back` and renders the package's view, which it finds in the import map
|
||||
* (`admin.dependencies` keeps it there). ipal-kit still never imports the optional package.
|
||||
* Puts ipal-kit's wrapper in front of the 2FA views for which `added(key)` is true and whose
|
||||
* route is `setup-totp` / `verify-totp`: the wrapper cleans `back` and renders the original
|
||||
* view, which it finds in the import map (`admin.dependencies` keeps it there). ipal-kit still
|
||||
* never imports the optional package. The original's `serverProps` stay server props; its
|
||||
* `clientProps` travel as `ipalTotpClientProps`, so the wrapper can hand them to a client view.
|
||||
*/
|
||||
export function wrapTotpViews<V extends ViewLike>(
|
||||
views: Record<string, V>,
|
||||
added: (key: string) => boolean,
|
||||
rule: SafeBackRule | undefined,
|
||||
wrapper = '@intecion/ipal-kit/admin#SafeTotpView',
|
||||
): { dependencies: Record<string, { path: string; type: 'component' }>; views: Record<string, V> } {
|
||||
const dependencies: Record<string, { path: string; type: 'component' }> = {}
|
||||
wrapper = SAFE_TOTP_VIEW,
|
||||
): { dependencies: Record<string, Dependency>; views: Record<string, V> } {
|
||||
const dependencies: Record<string, Dependency> = {}
|
||||
const out: Record<string, V> = {}
|
||||
for (const [key, view] of Object.entries(views)) {
|
||||
const component = view?.Component as undefined | ViewComponent
|
||||
const component = view?.Component as TotpViewComponent | undefined
|
||||
const path = typeof component === 'string' ? component : component?.path
|
||||
if (!added(key) || !view || typeof view.path !== 'string' || !TOTP_VIEW_PATH.test(view.path) || !path || path === wrapper) {
|
||||
if (!added(key) || !view || typeof view.path !== 'string' || !TOTP_VIEW_PATH.test(view.path) || !component || !path || path === wrapper) {
|
||||
out[key] = view
|
||||
continue
|
||||
}
|
||||
dependencies[totpViewDependencyKey(path)] = { type: 'component', path }
|
||||
const own = typeof component === 'object' ? component : undefined
|
||||
const original = own?.exportName ? { exportName: own.exportName, path } : path
|
||||
dependencies[totpViewDependencyKey(component)] = { type: 'component', ...(own?.exportName ? { exportName: own.exportName } : {}), path }
|
||||
out[key] = {
|
||||
...view,
|
||||
Component: {
|
||||
...(own?.clientProps ? { clientProps: own.clientProps } : {}),
|
||||
path: wrapper,
|
||||
serverProps: { ...own?.serverProps, ipalSafeBack: rule, ipalTotpView: path },
|
||||
serverProps: { ...own?.serverProps, ipalSafeBack: rule, ipalTotpClientProps: own?.clientProps, ipalTotpView: original },
|
||||
},
|
||||
}
|
||||
}
|
||||
return { dependencies, views: out }
|
||||
}
|
||||
|
||||
/** Keys whose view object is not the one in `before` — what a plugin added or replaced. */
|
||||
export const changedViewKeys = (before: Record<string, unknown>, after: Record<string, unknown>): Set<string> =>
|
||||
new Set(Object.keys(after).filter((key) => after[key] !== before[key]))
|
||||
|
||||
Reference in New Issue
Block a user