ensure supplied email addresses are valid at both the CLI and the API (#108)

This commit is contained in:
Michael Quigley 2022-11-29 13:23:23 -05:00
parent 39302cd4d5
commit 65d1539182
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 23 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/identity"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/util"
"github.com/openziti-test-kitchen/zrok/zrokdir"
"github.com/openziti/foundation/v2/term"
"github.com/spf13/cobra"
@ -33,6 +34,9 @@ func (cmd *inviteCommand) run(_ *cobra.Command, _ []string) {
if err != nil {
panic(err)
}
if !util.IsValidEmail(email) {
showError(fmt.Sprintf("'%v' is not a valid email address", email), nil)
}
confirm, err := term.Prompt("Confirm Email: ")
if err != nil {
panic(err)

View File

@ -1,9 +1,12 @@
package controller
import (
"fmt"
"github.com/go-openapi/runtime/middleware"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
"github.com/openziti-test-kitchen/zrok/util"
"github.com/sirupsen/logrus"
)
@ -19,6 +22,10 @@ func (self *createAccountHandler) Handle(params identity.CreateAccountParams) mi
logrus.Errorf("missing email")
return identity.NewCreateAccountBadRequest().WithPayload("missing email")
}
if !util.IsValidEmail(params.Body.Email) {
logrus.Errorf("'%v' is not a valid email address", params.Body.Email)
return identity.NewCreateAccountBadRequest().WithPayload(rest_model_zrok.ErrorMessage(fmt.Sprintf("'%v' is not a valid email address", params.Body.Email)))
}
logrus.Infof("received account request for email '%v'", params.Body.Email)
token, err := createToken()

12
util/email.go Normal file
View File

@ -0,0 +1,12 @@
package util
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])?)*$")
func IsValidEmail(e string) bool {
if len(e) < 3 && len(e) > 254 {
return false
}
return emailRegex.MatchString(e)
}