Get cookie domain parses a hostname and returns the upper domain (e.g. sub1.sub2.domain.com -> sub2.domain.com)
(u string)
| 16 | |
| 17 | // Get cookie domain parses a hostname and returns the upper domain (e.g. sub1.sub2.domain.com -> sub2.domain.com) |
| 18 | func 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 | |
| 52 | func ParseFileToLine(content string) string { |
| 53 | lines := strings.Split(content, "\n") |
no outgoing calls