Sign-in look: the logo follows IAWL's rules; no plate by default; the brand fields are admin-only
Bitmaps only (JPEG, PNG, WebP, AVIF, GIF; SVG with svgLogo: true), the file fetched and checked by its bytes before it is printed (checkLogo; 512 KB; ten-minute memory; an unreachable address is believed), served from a generated size at least twice the shown width. logoPlate replaces logoMadeFor: no plate unless asked. With the look on, the name, logo and icon of Site settings are changed by admins only (brandAccess: 'editor' gives them back). Docs, skills, changelog draft, tests and pictures follow. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
3ceeab8469
commit
939ab679c7
+3
-1
@@ -74,6 +74,7 @@ export {
|
||||
ADMIN_LANGUAGE_PATH,
|
||||
adminPathFromReferer,
|
||||
buildAdminLanguageEndpoint,
|
||||
checkLogoFile,
|
||||
fitLogo,
|
||||
isHexColour,
|
||||
isSvgUpload,
|
||||
@@ -81,6 +82,7 @@ export {
|
||||
languageLinks,
|
||||
loadSignInBrand,
|
||||
SIGN_IN_LOGO_BOX,
|
||||
SIGN_IN_LOGO_TYPES,
|
||||
SIGN_IN_LOOK,
|
||||
SIGN_IN_NAME_MAX,
|
||||
SIGN_IN_STYLE_ID,
|
||||
@@ -89,7 +91,7 @@ export {
|
||||
signInName,
|
||||
signInProviderActive,
|
||||
} from './modules/branding/signIn/index.js'
|
||||
export type { LanguageLink, SignInBrand, SignInLogo, SignInLookProps } from './modules/branding/signIn/index.js'
|
||||
export type { LanguageLink, LogoCheck, SignInBrand, SignInLogo, SignInLookProps } from './modules/branding/signIn/index.js'
|
||||
export { resolveSignInLook } from './modules/branding/signIn/options.js'
|
||||
export type { ResolvedSignInLook, SignInLookOptions } from './modules/branding/signIn/options.js'
|
||||
export {
|
||||
|
||||
@@ -21,7 +21,7 @@ export type SignInPanelProps = {
|
||||
|
||||
/**
|
||||
* The dark panel of the sign-in screens with its stylesheet: the label "Administration panel", the client's
|
||||
* logo (on a white plate unless it was made for dark grounds) or the site's name, and the panel's languages
|
||||
* logo (straight on the panel; on a white plate with `logoPlate`) or the site's name, and the panel's languages
|
||||
* as links. A Server Component; it reads Site settings with the `payload` Payload hands it and never throws.
|
||||
*
|
||||
* The stylesheet hides it wherever the page is not one of Payload's minimal screens, so the same element can
|
||||
@@ -33,7 +33,7 @@ export async function SignInPanel(props: SignInPanelProps = {}): Promise<React.R
|
||||
const text = (key: 'signInLanguage' | 'signInPanelLabel') => (i18n ? iaplT(i18n.t, key) : settingsTexts.en[key])
|
||||
const brand = await loadSignInBrand({ locale: props.locale, look, payload, user: props.user })
|
||||
const languages = look.languages === false ? [] : languageLinks(payload?.config, i18n?.language)
|
||||
const plate = brand.logo && look.logoMadeFor !== 'dark'
|
||||
const plate = brand.logo && look.logoPlate === true
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { Payload } from 'payload'
|
||||
import { getFromImportMap } from 'payload/shared'
|
||||
|
||||
import { secondFactorPassed } from '../../security/totp/secondFactor.js'
|
||||
import { publicImageDataUri, siteOrigin } from '../publicImage.js'
|
||||
import { PUBLIC_IMAGE_MAX_BYTES, rasterImageType, siteOrigin } from '../publicImage.js'
|
||||
|
||||
/**
|
||||
* What the sign-in panel shows, read from Site settings: the client's logo or, without one, the site's name.
|
||||
@@ -12,8 +12,9 @@ import { publicImageDataUri, siteOrigin } from '../publicImage.js'
|
||||
* What may reach a page that anyone can open, before signing in:
|
||||
* - the name as plain text (React escapes it), one line of at most `SIGN_IN_NAME_MAX` characters: no markup,
|
||||
* no link, nothing an address inside it could turn into;
|
||||
* - the logo as an `<img>`: a picture never runs script there, an SVG included. `svgLogo: false` leaves SVG
|
||||
* out all the same (the name is shown);
|
||||
* - the logo as an `<img>`, and only a bitmap: JPEG, PNG, WebP, AVIF or GIF, as the upload says AND as the
|
||||
* file's first bytes say (the file is looked at before it is printed; a deleted, broken or renamed file
|
||||
* gives the name, never a broken picture). No SVG unless the project asks (`svgLogo: true`);
|
||||
* - no account, no e-mail address, no hint whether an account exists: the forms are Payload's own.
|
||||
*/
|
||||
|
||||
@@ -30,26 +31,33 @@ export const SIGN_IN_NAME_MAX = 80
|
||||
export type SignInLookProps = {
|
||||
/** Hex colour of the button, the links and the focus ring. */
|
||||
accent?: string
|
||||
/** `false`: the logo's file is not fetched and looked at before it is printed. */
|
||||
checkLogo?: false
|
||||
/** Upload field of the logo. Default `'logo'`. */
|
||||
imageField?: string
|
||||
/** `whiteLabel.inlineImages: false`: never fetch the logo on the server for the 2FA screens. */
|
||||
inlineImages?: false
|
||||
/** `false`: no language links on the panel. */
|
||||
languages?: false
|
||||
/** `'dark'`: the logo was drawn for dark grounds, so it gets no white plate. */
|
||||
logoMadeFor?: 'dark' | 'light'
|
||||
/** `true`: the logo stands on a white plate (a logo drawn for light grounds). Default: no plate. */
|
||||
logoPlate?: true
|
||||
/** Field of the site's name. Default `'siteName'`. */
|
||||
nameField?: string
|
||||
/** Slug of the settings global. Default `'site-settings'`. */
|
||||
settingsSlug?: string
|
||||
/** `false`: an SVG logo is not shown on the sign-in screens (the name is). */
|
||||
svgLogo?: false
|
||||
/** `true`: an SVG logo is shown too (as a picture). Default: bitmaps only, an SVG logo gives the name. */
|
||||
svgLogo?: true
|
||||
}
|
||||
|
||||
export type SignInLogo = { height?: number; src: string; width?: number }
|
||||
export type SignInBrand = { logo: null | SignInLogo; name: string }
|
||||
|
||||
type MediaLike = { filename?: unknown; height?: unknown; mimeType?: unknown; url?: unknown; width?: unknown }
|
||||
type MediaLike = { filename?: unknown; height?: unknown; mimeType?: unknown; sizes?: unknown; url?: unknown; width?: unknown }
|
||||
|
||||
/** The picture types a logo may have on the sign-in screens: the bitmaps a browser shows. */
|
||||
export const SIGN_IN_LOGO_TYPES = ['image/jpeg', 'image/png', 'image/webp', 'image/avif', 'image/gif'] as const
|
||||
|
||||
const usableAddress = (url: unknown): url is string => typeof url === 'string' && /^(?:https?:\/\/|\/(?!\/))/i.test(url.trim())
|
||||
|
||||
/** A localized text field may arrive as a string or a { locale: value } map. */
|
||||
function pickText(value: unknown, locale: string | undefined): string {
|
||||
@@ -82,17 +90,98 @@ export function fitLogo(width: unknown, height: unknown, box: { height: number;
|
||||
return { height: Math.max(1, Math.round(height * scale)), width: Math.max(1, Math.round(width * scale)) }
|
||||
}
|
||||
|
||||
/** The logo of an upload document, or null when there is nothing to show. */
|
||||
/**
|
||||
* The address to serve for a logo shown `shownWidth` wide: the smallest generated size (the upload's
|
||||
* `sizes`) that is at least twice that width, has the picture's own proportions (a cropped size is another
|
||||
* picture) and an allowed type; else the upload itself.
|
||||
*/
|
||||
function servedAddress(m: MediaLike, shownWidth: number): string {
|
||||
const full = (m.url as string).trim()
|
||||
if (typeof m.width !== 'number' || typeof m.height !== 'number' || !m.sizes || typeof m.sizes !== 'object') {return full}
|
||||
let best: { url: string; width: number } | null = null
|
||||
for (const size of Object.values(m.sizes as Record<string, MediaLike | null>)) {
|
||||
if (!size || typeof size.width !== 'number' || typeof size.height !== 'number' || !usableAddress(size.url)) {continue}
|
||||
if (size.width < 2 * shownWidth || size.width >= m.width || size.height < 1) {continue}
|
||||
if (Math.abs(size.width / size.height - m.width / m.height) > 0.02) {continue}
|
||||
if (typeof size.mimeType === 'string' && !(SIGN_IN_LOGO_TYPES as readonly string[]).includes(size.mimeType.toLowerCase())) {continue}
|
||||
if (!best || size.width < best.width) {best = { url: size.url.trim(), width: size.width }}
|
||||
}
|
||||
return best?.url ?? full
|
||||
}
|
||||
|
||||
/**
|
||||
* The logo of an upload document, or null when there is nothing to show: no address, an address that is not
|
||||
* http(s) or site-relative, or a type that is not one of `SIGN_IN_LOGO_TYPES` (SVG only with `svgLogo: true`).
|
||||
*/
|
||||
export function signInLogo(media: unknown, look: Pick<SignInLookProps, 'svgLogo'> = {}): null | SignInLogo {
|
||||
if (!media || typeof media !== 'object') {return null}
|
||||
const m = media as MediaLike
|
||||
if (typeof m.url !== 'string' || !m.url.trim()) {return null}
|
||||
// An address a browser would not load as a picture of the site: only http(s) and site-relative ones.
|
||||
if (!/^(?:https?:\/\/|\/(?!\/))/i.test(m.url.trim())) {return null}
|
||||
if (look.svgLogo === false && isSvgUpload(m)) {return null}
|
||||
return { src: m.url.trim(), ...(fitLogo(m.width, m.height) ?? {}) }
|
||||
if (!usableAddress(m.url)) {return null}
|
||||
if (isSvgUpload(m)) {return look.svgLogo === true ? { src: m.url.trim(), ...(fitLogo(m.width, m.height) ?? {}) } : null}
|
||||
if (typeof m.mimeType !== 'string' || !(SIGN_IN_LOGO_TYPES as readonly string[]).includes(m.mimeType.toLowerCase())) {return null}
|
||||
const fit = fitLogo(m.width, m.height)
|
||||
return { src: fit ? servedAddress(m, fit.width) : m.url.trim(), ...(fit ?? {}) }
|
||||
}
|
||||
|
||||
export type LogoCheck = 'bad' | 'unknown' | { bytes: Buffer; type: string }
|
||||
|
||||
/**
|
||||
* Looks at the logo's file the way an anonymous visitor gets it (no cookies, at most 512 KB):
|
||||
* - the picture itself when it answers and its first bytes are one of the allowed bitmaps (an SVG, allowed
|
||||
* by `svgLogo: true`, is taken by its `image/svg+xml` type);
|
||||
* - `'bad'` when the server ANSWERS and it is no such picture: 404, another type, too large, broken bytes;
|
||||
* - `'unknown'` when nothing answers (the site cannot reach its own address from inside its container, a
|
||||
* timeout): the upload document is then believed, so a network quirk never takes the logo away.
|
||||
*/
|
||||
export async function checkLogoFile(url: string, base: null | string | undefined, allowSvg = false): Promise<LogoCheck> {
|
||||
let target: URL
|
||||
try {
|
||||
target = new URL(url, base || undefined)
|
||||
} catch {
|
||||
return 'unknown' // a site-relative address and no origin to resolve it against
|
||||
}
|
||||
if (target.protocol !== 'http:' && target.protocol !== 'https:') {return 'bad'}
|
||||
let res: Response
|
||||
try {
|
||||
res = await fetch(target, { credentials: 'omit', redirect: 'follow', signal: AbortSignal.timeout(4000) })
|
||||
} catch {
|
||||
return 'unknown'
|
||||
}
|
||||
try {
|
||||
if (!res.ok) {return res.status >= 500 ? 'unknown' : 'bad'}
|
||||
const declared = Number(res.headers.get('content-length'))
|
||||
if (Number.isFinite(declared) && declared > PUBLIC_IMAGE_MAX_BYTES) {return 'bad'}
|
||||
const bytes = Buffer.from(await res.arrayBuffer())
|
||||
if (!bytes.length || bytes.length > PUBLIC_IMAGE_MAX_BYTES) {return 'bad'}
|
||||
const declaredType = (res.headers.get('content-type') ?? '').split(';')[0].trim().toLowerCase()
|
||||
if (allowSvg && declaredType === 'image/svg+xml') {return { type: declaredType, bytes }}
|
||||
const type = rasterImageType(bytes)
|
||||
return type && (SIGN_IN_LOGO_TYPES as readonly string[]).includes(type) ? { type, bytes } : 'bad'
|
||||
} catch {
|
||||
return 'unknown'
|
||||
} finally {
|
||||
await res.body?.cancel().catch(() => undefined)
|
||||
}
|
||||
}
|
||||
|
||||
/** What the last look at a logo's file said, kept for ten minutes per address: one fetch, not one per page view. */
|
||||
const CHECKED = new Map<string, { at: number; result: LogoCheck }>()
|
||||
const CHECK_TTL_MS = 10 * 60_000
|
||||
|
||||
async function checkedLogo(url: string, base: null | string | undefined, allowSvg: boolean): Promise<LogoCheck> {
|
||||
const key = `${base ?? ''}|${url}|${allowSvg ? 1 : 0}`
|
||||
const hit = CHECKED.get(key)
|
||||
if (hit && Date.now() - hit.at < CHECK_TTL_MS) {return hit.result}
|
||||
const result = await checkLogoFile(url, base, allowSvg)
|
||||
if (CHECKED.size > 50) {CHECKED.clear()}
|
||||
// "nothing answered" is asked again soon; an answer is kept
|
||||
CHECKED.set(key, { at: result === 'unknown' ? Date.now() - CHECK_TTL_MS + 30_000 : Date.now(), result })
|
||||
return result
|
||||
}
|
||||
|
||||
/** For tests: forget what was checked. */
|
||||
export const forgetCheckedLogos = (): void => CHECKED.clear()
|
||||
|
||||
/** The host of the site, shown when Site settings has no name. */
|
||||
function siteHost(serverURL: null | string | undefined): string {
|
||||
try {
|
||||
@@ -124,11 +213,17 @@ export async function loadSignInBrand({ locale, look, payload, user }: LoadArgs)
|
||||
const name = signInName(settings[look.nameField ?? 'siteName'], code) || siteHost(origin)
|
||||
let logo = signInLogo(settings[look.imageField ?? 'logo'], look)
|
||||
// A session that still owes its 2FA code (or has not set 2FA up) is refused `media` like every other
|
||||
// collection: give it the picture itself, fetched as an anonymous visitor would get it. When that fails,
|
||||
// the name is shown instead of a broken picture.
|
||||
if (logo && user && !secondFactorPassed({ payload, user })) {
|
||||
const data = look.inlineImages === false ? null : await publicImageDataUri(logo.src, origin)
|
||||
logo = data ? { ...logo, src: data } : null
|
||||
// collection: it gets the picture itself (the one just looked at), or the name.
|
||||
const pending = Boolean(user && !secondFactorPassed({ payload, user }))
|
||||
if (logo && (look.checkLogo !== false || pending) && !(pending && look.inlineImages === false)) {
|
||||
const checked = await checkedLogo(logo.src, origin, look.svgLogo === true)
|
||||
if (checked === 'bad') {
|
||||
logo = null
|
||||
} else if (pending) {
|
||||
logo = checked === 'unknown' ? null : { ...logo, src: `data:${checked.type};base64,${checked.bytes.toString('base64')}` }
|
||||
}
|
||||
} else if (logo && pending) {
|
||||
logo = null
|
||||
}
|
||||
return { name, logo }
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
export {
|
||||
ADMIN_LANGUAGE_PATH,
|
||||
checkLogoFile,
|
||||
fitLogo,
|
||||
isSvgUpload,
|
||||
LANGUAGE_NAMES,
|
||||
languageLinks,
|
||||
loadSignInBrand,
|
||||
SIGN_IN_LOGO_BOX,
|
||||
SIGN_IN_LOGO_TYPES,
|
||||
SIGN_IN_LOOK,
|
||||
SIGN_IN_NAME_MAX,
|
||||
signInLogo,
|
||||
signInName,
|
||||
signInProviderActive,
|
||||
} from './brand.js'
|
||||
export type { LanguageLink, SignInBrand, SignInLogo, SignInLookProps } from './brand.js'
|
||||
export type { LanguageLink, LogoCheck, SignInBrand, SignInLogo, SignInLookProps } from './brand.js'
|
||||
export { isHexColour, SIGN_IN_STYLE_ID, signInCss } from './css.js'
|
||||
export { adminPathFromReferer, buildAdminLanguageEndpoint } from './languageEndpoint.js'
|
||||
|
||||
@@ -16,12 +16,23 @@ export type SignInLookOptions = {
|
||||
* without touching its import map; the project starter turns it on.
|
||||
*/
|
||||
allScreens?: boolean
|
||||
/**
|
||||
* `false`: the logo's file is not fetched and looked at before it is printed (its type by its first bytes,
|
||||
* at most 512 KB; a deleted or broken file gives the site's name). Default: checked, remembered ten minutes.
|
||||
*/
|
||||
checkLogo?: false
|
||||
/** `false`: no language links on the panel. Default: shown when the panel has more than one language. */
|
||||
languages?: false
|
||||
/** `'dark'`: the logo was made for dark grounds (a white logo) and gets no white plate. Default `'light'`. */
|
||||
logoMadeFor?: 'dark' | 'light'
|
||||
/** `false`: an SVG logo is not shown on the sign-in screens; the site's name is. Default: shown as a picture. */
|
||||
svgLogo?: false
|
||||
/**
|
||||
* `true`: the logo stands on a white plate — for a logo drawn for light grounds (dark letters). Default:
|
||||
* no plate, the logo straight on the dark panel, so the panel's logo should be one made for dark grounds.
|
||||
*/
|
||||
logoPlate?: boolean
|
||||
/**
|
||||
* `true`: an SVG logo is shown too (as a picture, where it cannot run script). Default: only JPEG, PNG,
|
||||
* WebP, AVIF and GIF are shown; with an SVG logo the sign-in screens show the site's name.
|
||||
*/
|
||||
svgLogo?: boolean
|
||||
}
|
||||
|
||||
export type ResolvedSignInLook = { allScreens: boolean; props: SignInLookProps }
|
||||
@@ -42,10 +53,11 @@ export function resolveSignInLook(whiteLabel: boolean | undefined | WhiteLabelFi
|
||||
...(wl.logoField ? { imageField: wl.logoField } : {}),
|
||||
...(wl.inlineImages === false ? { inlineImages: false as const } : {}),
|
||||
...(signIn.languages === false ? { languages: false as const } : {}),
|
||||
...(signIn.logoMadeFor === 'dark' ? { logoMadeFor: 'dark' as const } : {}),
|
||||
...(signIn.checkLogo === false ? { checkLogo: false as const } : {}),
|
||||
...(signIn.logoPlate === true ? { logoPlate: true as const } : {}),
|
||||
...(wl.nameField ? { nameField: wl.nameField } : {}),
|
||||
...(wl.settingsSlug ? { settingsSlug: wl.settingsSlug } : {}),
|
||||
...(signIn.svgLogo === false ? { svgLogo: false as const } : {}),
|
||||
...(signIn.svgLogo === true ? { svgLogo: true as const } : {}),
|
||||
}
|
||||
return { allScreens: signIn.allScreens === true, props }
|
||||
}
|
||||
|
||||
+5
-3
@@ -773,9 +773,11 @@ export const iapl = (options: IaplOptions): Plugin => {
|
||||
: pluginGlobals
|
||||
// The project's globalOverrides win (access, admin, hooks merged; a function = full control).
|
||||
config.globals = [...(config.globals ?? []), ...applyGlobalOverrides(withRevalidate, options.globalOverrides)]
|
||||
// `whiteLabel.brandAccess: 'admin'`: the brand the sign-in screens show is changed by admins only.
|
||||
if (options.whiteLabel && options.whiteLabel !== true && options.whiteLabel.brandAccess === 'admin') {
|
||||
const wl = options.whiteLabel
|
||||
// The brand the sign-in screens show (name, logo, icon) is changed by admins only — with the sign-in look,
|
||||
// unless the project says `brandAccess: 'editor'`; without the look only when it says `'admin'`.
|
||||
const brandWl = options.whiteLabel === true ? {} : options.whiteLabel
|
||||
if (brandWl && (brandWl.brandAccess === 'admin' || (brandWl.brandAccess !== 'editor' && resolveSignInLook(brandWl)))) {
|
||||
const wl = brandWl
|
||||
config.globals = lockBrandFields(config.globals, wl.settingsSlug ?? 'site-settings', [wl.nameField ?? 'siteName', wl.logoField ?? 'logo', wl.iconField ?? 'favicon'])
|
||||
}
|
||||
|
||||
|
||||
+4
-2
@@ -447,8 +447,10 @@ export type IaplOptions = {
|
||||
| {
|
||||
/**
|
||||
* Who may change the brand the sign-in screens show (the name, logo and
|
||||
* icon fields of Site settings). Default `'editor'`: whoever may edit Site
|
||||
* settings, as before. `'admin'`: field-level access, admins only.
|
||||
* icon fields of Site settings). Default with the sign-in look: `'admin'`
|
||||
* (field-level access, admins only; no schema change). `'editor'`: whoever
|
||||
* may edit Site settings, as before 1.18. With `signIn: false` the default
|
||||
* is `'editor'`.
|
||||
*/
|
||||
brandAccess?: 'admin' | 'editor'
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user