MCPcopy Index your code
hub / github.com/tinyauthapp/tinyauth / GetCookieDomain

Function GetCookieDomain

internal/utils/app_utils.go:18–50  ·  view source on GitHub ↗

Get cookie domain parses a hostname and returns the upper domain (e.g. sub1.sub2.domain.com -> sub2.domain.com)

(u string)

Source from the content-addressed store, hash-verified

16
17// Get cookie domain parses a hostname and returns the upper domain (e.g. sub1.sub2.domain.com -> sub2.domain.com)
18func GetCookieDomain(u string) (string, error) {
19 parsed, err := url.Parse(u)
20 if err != nil {
21 return "", err
22 }
23
24 host := parsed.Hostname()
25
26 if netIP := net.ParseIP(host); netIP != nil {
27 return "", errors.New("IP addresses not allowed")
28 }
29
30 parts := strings.Split(host, ".")
31
32 if len(parts) == 2 {
33 tlog.App.Warn().Msgf("Running on the root domain, cookies will be set for .%v", host)
34 return host, nil
35 }
36
37 if len(parts) < 3 {
38 return "", errors.New("invalid app url, must be at least second level domain")
39 }
40
41 domain := strings.Join(parts[1:], ".")
42
43 _, err = publicsuffix.DomainFromListWithOptions(publicsuffix.DefaultList, domain, nil)
44
45 if err != nil {
46 return "", errors.New("domain in public suffix list, cannot set cookies")
47 }
48
49 return domain, nil
50}
51
52func ParseFileToLine(content string) string {
53 lines := strings.Split(content, "\n")

Callers 2

TestGetRootDomainFunction · 0.92
SetupMethod · 0.92

Calls

no outgoing calls

Tested by 1

TestGetRootDomainFunction · 0.74