Skip to content

Commit

Permalink
Merge pull request #262 from moul/fix/email-validator
Browse files Browse the repository at this point in the history
feat: New Email validator
  • Loading branch information
moul authored Mar 28, 2021
2 parents ab9c53f + 2def328 commit 79cbaa3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/bastion/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down Expand Up @@ -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")
Expand Down
13 changes: 13 additions & 0 deletions pkg/utils/emailvalidator.go
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions pkg/utils/emailvalidator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package utils

import (
"testing"
)

func TestValidateEmail(t *testing.T) {

goodEmail := "[email protected]"
badEmail := "[email protected]"

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)
}

}

0 comments on commit 79cbaa3

Please sign in to comment.