NormalizeExternalURL will format the external url.
(url string)
| 120 | |
| 121 | // NormalizeExternalURL will format the external url. |
| 122 | func NormalizeExternalURL(url string) (string, error) { |
| 123 | r := strings.TrimSpace(url) |
| 124 | r = strings.TrimSuffix(r, "/") |
| 125 | if !HasPrefixes(r, "http://", "https://") { |
| 126 | return "", errors.Errorf("%s must start with http:// or https://", url) |
| 127 | } |
| 128 | parts := strings.Split(r, ":") |
| 129 | if len(parts) > 3 { |
| 130 | return "", errors.Errorf("%s malformed", url) |
| 131 | } |
| 132 | if len(parts) == 3 { |
| 133 | port, err := strconv.Atoi(parts[2]) |
| 134 | if err != nil { |
| 135 | return "", errors.Errorf("%s has non integer port", url) |
| 136 | } |
| 137 | // The external URL is used as the redirectURL in the get token process of OAuth, and the |
| 138 | // RedirectURL needs to be consistent with the RedirectURL in the get code process. |
| 139 | // The frontend gets it through window.location.origin in the get code |
| 140 | // process, so port 80/443 need to be cropped. |
| 141 | if port == 80 || port == 443 { |
| 142 | r = strings.Join(parts[0:2], ":") |
| 143 | } |
| 144 | } |
| 145 | return r, nil |
| 146 | } |
| 147 | |
| 148 | // ValidatePhone validates the phone number. |
| 149 | func ValidatePhone(phone string) error { |