github.com/letsencrypt/boulder@v0.20251208.0/iana/iana.go (about)

     1  package iana
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/weppos/publicsuffix-go/publicsuffix"
     7  )
     8  
     9  // ExtractSuffix returns the public suffix of the domain using only the "ICANN"
    10  // section of the Public Suffix List database.
    11  // If the domain does not end in a suffix that belongs to an IANA-assigned
    12  // domain, ExtractSuffix returns an error.
    13  func ExtractSuffix(name string) (string, error) {
    14  	if name == "" {
    15  		return "", fmt.Errorf("Blank name argument passed to ExtractSuffix")
    16  	}
    17  
    18  	rule := publicsuffix.DefaultList.Find(name, &publicsuffix.FindOptions{IgnorePrivate: true, DefaultRule: nil})
    19  	if rule == nil {
    20  		return "", fmt.Errorf("Domain %s has no IANA TLD", name)
    21  	}
    22  
    23  	suffix := rule.Decompose(name)[1]
    24  
    25  	// If the TLD is empty, it means name is actually a suffix.
    26  	// In fact, decompose returns an array of empty strings in this case.
    27  	if suffix == "" {
    28  		suffix = name
    29  	}
    30  
    31  	return suffix, nil
    32  }