All checks were successful
Build pull request / Build pull request (pull_request) Successful in 3m44s
registrationEntry accepts an https url, or http when the host is localhost, *.localhost, 127.0.0.1 or ::1. A test pins both sides, including a look-alike host. The two tests that pass config.json as a ClientConfig now cast it, so tsc is clean for them. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
32 lines
1.3 KiB
TypeScript
32 lines
1.3 KiB
TypeScript
import type { ClientConfig } from '../../hooks/useClientConfig';
|
|
import { TRAINING_NOTICE_VERSION, trainingNoticeText } from '../../utils/trainingNotice';
|
|
|
|
export type RegistrationEntry = { url: string; notice: string; noticeVersion: number };
|
|
|
|
// Plain http is for a developer's own machine only; a stranger's sign-up page is https.
|
|
const isLocalHost = (hostname: string): boolean =>
|
|
hostname === 'localhost' ||
|
|
hostname.endsWith('.localhost') ||
|
|
hostname === '127.0.0.1' ||
|
|
hostname === '[::1]';
|
|
|
|
/**
|
|
* The sign-up entry the sign-in page may show: the account service's registration page, when the
|
|
* switch is on and the configured url is an https address (http only for localhost). Anything else is no entry. The
|
|
* consent sentence and its version are trainingNotice.ts's, the ones a room shows.
|
|
*/
|
|
export const registrationEntry = (config: ClientConfig): RegistrationEntry | undefined => {
|
|
const { enabled, url } = config.registration ?? {};
|
|
if (enabled !== true || typeof url !== 'string') return undefined;
|
|
try {
|
|
const { protocol, hostname } = new URL(url);
|
|
if (protocol !== 'https:' && !(protocol === 'http:' && isLocalHost(hostname))) return undefined;
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
return {
|
|
url,
|
|
notice: trainingNoticeText(config.previewDemo),
|
|
noticeVersion: TRAINING_NOTICE_VERSION,
|
|
};
|
|
};
|