diff --git a/controller/account.go b/controller/account.go index 723a6786..a2879c39 100644 --- a/controller/account.go +++ b/controller/account.go @@ -63,6 +63,19 @@ func (self *createAccountHandler) handleDirectCreate(params identity.CreateAccou } func (self *createAccountHandler) handleVerifiedCreate(params identity.CreateAccountParams) middleware.Responder { + if params.Body == nil || params.Body.Email == "" { + logrus.Errorf("missing email") + return identity.NewCreateAccountBadRequest().WithPayload("missing email") + } + token, err := generateApiToken() + if err != nil { + logrus.Errorf("error generating api token: %v", err) + return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + if err := sendVerificationEmail(params.Body.Email, token, self.cfg); err != nil { + logrus.Error(err) + return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } return identity.NewCreateAccountCreated() } diff --git a/controller/config.go b/controller/config.go index 1317307a..df8ae2e2 100644 --- a/controller/config.go +++ b/controller/config.go @@ -27,7 +27,7 @@ type ProxyConfig struct { type EmailConfig struct { Host string - Port uint16 + Port int Username string Password string } diff --git a/controller/verify.go b/controller/verify.go index 898569ac..e50cf108 100644 --- a/controller/verify.go +++ b/controller/verify.go @@ -29,9 +29,12 @@ func sendVerificationEmail(emailAddress, token string, cfg *Config) error { return errors.Wrap(err, "error executing email verification template") } + subject := "Subject: Welcome to zrok!\n" + mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" + msg := []byte(subject + mime + buf.String()) auth := smtp.PlainAuth("", cfg.Email.Username, cfg.Email.Password, cfg.Email.Host) to := []string{emailAddress} - err = smtp.SendMail(fmt.Sprintf("%v:%d", cfg.Email.Host, cfg.Email.Port), auth, cfg.Registration.EmailFrom, to, buf.Bytes()) + err = smtp.SendMail(fmt.Sprintf("%v:%d", cfg.Email.Host, cfg.Email.Port), auth, cfg.Registration.EmailFrom, to, msg) if err != nil { return errors.Wrap(err, "error sending email verification") }