auth

Main authentication module for my projects
git clone git://git.finwo.net/app/auth
Log | Files | Refs

hosts.ts (1521B)


      1 export function normalizeHostValue(raw: string) {
      2   return raw.trim().toLowerCase();
      3 }
      4 
      5 export function hostFormatError(raw: string): string | null {
      6   const value = raw.trim();
      7   const host = normalizeHostValue(value);
      8 
      9   if (!host) return 'Hostname is required.';
     10   if (/\s/.test(value)) return 'Hostname must not contain spaces.';
     11   if (host.includes('://') || /^[a-z][a-z0-9+.-]*:\/\//i.test(value)) {
     12     return 'Hostname must not include a scheme such as https://.';
     13   }
     14   if (host.includes('@')) return 'Hostname must not include @.';
     15   if (host.includes('/')) return 'Hostname must not include a path.';
     16   if (host.includes('*')) return 'Wildcard hostnames are not supported.';
     17   if (host.startsWith('[')) {
     18     if (!host.endsWith(']')) return 'Enter a valid IPv6 hostname.';
     19   } else if (host.includes(':')) {
     20     return 'Hostname must not include a port.';
     21   }
     22   if (host.length > 253) return 'Hostname must be 253 characters or fewer.';
     23   if (host.startsWith('.') || host.endsWith('.')) {
     24     return 'Hostname must not start or end with a dot.';
     25   }
     26 
     27   if (!host.startsWith('[')) {
     28     const labels = host.split('.');
     29     if (labels.length < 2) return 'Enter a complete hostname such as login.acme.test.';
     30     for (const label of labels) {
     31       if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(label)) {
     32         return 'Hostname labels must use lowercase letters, numbers and hyphens.';
     33       }
     34       if (label.length > 63) return 'Each hostname label must be 63 characters or fewer.';
     35     }
     36   }
     37 
     38   return null;
     39 }