github.com/greenpau/go-authcrunch@v1.1.4/pkg/util/validate/login_hint.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 validate
    16  
    17  import (
    18  	"github.com/greenpau/go-authcrunch/pkg/errors"
    19  	"net/mail"
    20  	"regexp"
    21  )
    22  
    23  // LoginHint verifies if the provided login_hint argument is valid
    24  func LoginHint(loginHint string, validators []string) error {
    25  
    26  	for _, validator := range validators {
    27  		switch validator {
    28  		case "email":
    29  			if _, err := mail.ParseAddress(loginHint); err == nil {
    30  				return nil
    31  			}
    32  		case "phone":
    33  			regex, _ := regexp.Compile("^[0-9\\-+\\s]+$")
    34  			if match := regex.MatchString(loginHint); match == true {
    35  				return nil
    36  			}
    37  		case "alphanumeric":
    38  			regex, _ := regexp.Compile("^[a-zA-Z0-9\\-._!~*'()]+$")
    39  			if match := regex.MatchString(loginHint); match == true {
    40  				return nil
    41  			}
    42  		}
    43  	}
    44  	return errors.ErrInvalidLoginHint
    45  }