fix: fix email validating in shell input

fix: test cases

fix feedback

fix: validate email with custom validator in shell input
This commit is contained in:
Darko Djalevski 2021-03-27 15:55:59 +01:00 committed by Darko Djalevski
parent ab9c53f1b0
commit 2def328f6a
3 changed files with 40 additions and 2 deletions

View file

@ -25,6 +25,7 @@ import (
"golang.org/x/crypto/ssh/terminal"
"moul.io/sshportal/pkg/crypto"
"moul.io/sshportal/pkg/dbmodels"
"moul.io/sshportal/pkg/utils"
)
var banner = `
@ -1623,9 +1624,11 @@ GLOBAL OPTIONS:
return err
}
// FIXME: validate email
email := c.Args().First()
valid := utils.ValidateEmail(email)
if !valid {
return errors.New("invalid email")
}
name := strings.Split(email, "@")[0]
if c.String("name") != "" {
name = c.String("name")

View file

@ -0,0 +1,13 @@
package utils
import "regexp"
var emailRegex = 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])?)*$")
// ValidateEmail validates email.
func ValidateEmail(e string) bool {
if len(e) < 3 && len(e) > 254 {
return false
}
return emailRegex.MatchString(e)
}

View file

@ -0,0 +1,22 @@
package utils
import (
"testing"
)
func TestValidateEmail(t *testing.T) {
goodEmail := "goodemail@email.com"
badEmail := "b@2323.22"
got := ValidateEmail(goodEmail)
if got == false {
t.Errorf("got1= %v; want true", got)
}
got2 := ValidateEmail(badEmail)
if got2 == false {
t.Errorf("got2= %v; want false", got2)
}
}