github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/validators/user_input.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package validators 16 17 import ( 18 "fmt" 19 charsetutil "github.com/greenpau/go-authcrunch/pkg/util/charset" 20 "net" 21 "regexp" 22 "strings" 23 ) 24 25 const usernameCharset = "0123456789abcdefghijklmnopqrstuvwxyz" 26 27 var emailAddrRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") 28 29 // ValidateUserInput provides generic interface to validate user input. 30 func ValidateUserInput(k, v string, opts map[string]interface{}) error { 31 if v == "" { 32 return fmt.Errorf("empty %s value", k) 33 } 34 switch k { 35 case "handle": 36 if err := ValidateUserInputHandle(v, opts); err != nil { 37 return err 38 } 39 case "secret": 40 if err := ValidateUserInputSecret(v, opts); err != nil { 41 return err 42 } 43 case "email": 44 if err := ValidateUserInputEmail(v, opts); err != nil { 45 return err 46 } 47 default: 48 return fmt.Errorf("%s validation is unsupported", k) 49 } 50 return nil 51 } 52 53 // ValidateUserInputHandle validates provided user handle. 54 func ValidateUserInputHandle(v string, opts map[string]interface{}) error { 55 if len(v) > 25 { 56 return fmt.Errorf("the handle character length should not exceed 25 characters") 57 } 58 if err := charsetutil.ContainsInvalidChars(usernameCharset, v); err != nil { 59 return fmt.Errorf("the handle %s", err.Error()) 60 } 61 return nil 62 } 63 64 // ValidateUserInputSecret validates provided user secret. 65 func ValidateUserInputSecret(v string, opts map[string]interface{}) error { 66 if len(v) > 255 { 67 return fmt.Errorf("the handle character length should not exceed 255 characters") 68 } 69 return nil 70 } 71 72 // ValidateUserInputEmail validates provided email address. 73 func ValidateUserInputEmail(v string, opts map[string]interface{}) error { 74 if len(v) < 3 && len(v) > 254 { 75 return fmt.Errorf("the length of email address is invalid") 76 } 77 if !emailAddrRegex.MatchString(v) { 78 return fmt.Errorf("the email address is invalid") 79 } 80 emailParts := strings.SplitN(v, "@", 2) 81 if len(emailParts) != 2 { 82 return fmt.Errorf("the email address is invalid") 83 } 84 if opts != nil { 85 if enabled, exists := opts["check_domain_mx"]; exists { 86 if enabled.(bool) { 87 rr, err := net.LookupMX(emailParts[1]) 88 if err != nil { 89 return fmt.Errorf("the email address domain is invalid") 90 } 91 if len(rr) < 1 { 92 return fmt.Errorf("the email address domain is misconfigured") 93 } 94 } 95 } 96 } 97 return nil 98 }