mirror of
https://github.com/moul/sshportal.git
synced 2025-11-08 14:30:32 +08:00
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:
parent
ab9c53f1b0
commit
2def328f6a
3 changed files with 40 additions and 2 deletions
|
|
@ -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")
|
||||
|
|
|
|||
13
pkg/utils/emailvalidator.go
Normal file
13
pkg/utils/emailvalidator.go
Normal 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)
|
||||
}
|
||||
22
pkg/utils/emailvalidator_test.go
Normal file
22
pkg/utils/emailvalidator_test.go
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue