From 643bc4279c1f0c2b44444f3682a87c68634bb715 Mon Sep 17 00:00:00 2001 From: Roland Shoemaker Date: Sat, 6 Jun 2015 06:37:29 -0700 Subject: [PATCH] Move email validation to seperate function --- ra/registration-authority.go | 52 +++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/ra/registration-authority.go b/ra/registration-authority.go index ee9b8f8cf..28e33bc91 100644 --- a/ra/registration-authority.go +++ b/ra/registration-authority.go @@ -49,6 +49,27 @@ func lastPathSegment(url core.AcmeURL) string { return allButLastPathSegment.ReplaceAllString(url.Path, "") } +func validateEmail(address string) (err error) { + _, err = mail.ParseAddress(address) + if err != nil { + err = core.MalformedRequestError(err.Error()) + return + } + splitEmail := strings.SplitN(address, "@", -1) + domain := strings.ToLower(splitEmail[len(splitEmail)-1]) + var mx []*net.MX + mx, err = net.LookupMX(domain) + if err != nil { + err = core.InternalServerError(err.Error()) + return + } + if len(mx) == 0 { + err = core.MalformedRequestError(fmt.Sprintf("No MX record for domain %s", domain)) + return + } + return +} + type certificateRequestEvent struct { ID string `json:",omitempty"` Requester int64 `json:",omitempty"` @@ -76,30 +97,18 @@ func (ra *RegistrationAuthorityImpl) NewRegistration(init core.Registration) (re reg.MergeUpdate(init) for _, contact := range reg.Contact { - // If contact email provided check MX records exist for the domain - if !strings.HasPrefix(contact.Scheme, "mailto") && !strings.HasPrefix(contact.Scheme, "tel") { + switch contact.Scheme { + case "tel": + continue + case "mailto": + err = validateEmail(contact.Opaque) + if err != nil { + return + } + default: err = core.MalformedRequestError(fmt.Sprintf("Contact method %s is not supported", contact.Scheme)) return } - if contact.Scheme == "mailto" { - _, err = mail.ParseAddress(contact.Opaque) - if err != nil { - err = core.MalformedRequestError(err.Error()) - return - } - splitEmail := strings.SplitN(contact.Opaque, "@", -1) - domain := strings.ToLower(splitEmail[len(splitEmail)-1]) - var mx []*net.MX - mx, err = net.LookupMX(domain) - if err != nil { - err = core.InternalServerError(err.Error()) - return - } - if len(mx) == 0 { - err = core.MalformedRequestError(fmt.Sprintf("No MX record for domain %s", domain)) - return - } - } } // Store the authorization object, then return it @@ -107,6 +116,7 @@ func (ra *RegistrationAuthorityImpl) NewRegistration(init core.Registration) (re if err != nil { err = core.InternalServerError(err.Error()) } + return }