mirror of
https://github.com/openziti/zrok.git
synced 2025-06-21 18:21:34 +02:00
implement verify endpoint (#50)
This commit is contained in:
parent
3d9cc7d1b3
commit
08aba670ab
@ -44,10 +44,10 @@ func (self *createAccountHandler) handleDirectCreate(params identity.CreateAccou
|
||||
logrus.Errorf("error starting transaction: %v", err)
|
||||
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
id, err := str.CreateAccount(a, tx)
|
||||
if err != nil {
|
||||
logrus.Errorf("error creating account: %v", err)
|
||||
_ = tx.Rollback()
|
||||
return identity.NewCreateAccountBadRequest().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
|
@ -27,6 +27,7 @@ func Run(cfg *Config) error {
|
||||
api.IdentityEnableHandler = newEnableHandler(cfg)
|
||||
api.IdentityDisableHandler = newDisableHandler(cfg)
|
||||
api.IdentityLoginHandler = identity.LoginHandlerFunc(loginHandler)
|
||||
api.IdentityVerifyHandler = newVerifyHandler(cfg)
|
||||
api.MetadataOverviewHandler = metadata.OverviewHandlerFunc(overviewHandler)
|
||||
api.MetadataVersionHandler = metadata.VersionHandlerFunc(versionHandler)
|
||||
api.TunnelTunnelHandler = newTunnelHandler(cfg)
|
||||
|
@ -1,75 +1,31 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/openziti-test-kitchen/zrok/controller/email_ui"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/go-openapi/runtime/middleware"
|
||||
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
||||
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/wneessen/go-mail"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
type verificationEmail struct {
|
||||
EmailAddress string
|
||||
VerifyUrl string
|
||||
type verifyHandler struct {
|
||||
cfg *Config
|
||||
}
|
||||
|
||||
func sendVerificationEmail(emailAddress, token string, cfg *Config) error {
|
||||
emailData := &verificationEmail{
|
||||
EmailAddress: emailAddress,
|
||||
VerifyUrl: cfg.Registration.RegistrationUrlTemplate + "/" + token,
|
||||
}
|
||||
|
||||
plainBody, err := mergeTemplate(emailData, "verify.gotext")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
htmlBody, err := mergeTemplate(emailData, "verify.gohtml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg := mail.NewMsg()
|
||||
if err := msg.From("ziggy@zrok.io"); err != nil {
|
||||
return errors.Wrap(err, "failed to set from address in verification email")
|
||||
}
|
||||
if err := msg.To(emailAddress); err != nil {
|
||||
return errors.Wrap(err, "failed to sent to address in verification email")
|
||||
}
|
||||
msg.Subject("Welcome to zrok!")
|
||||
msg.SetDate()
|
||||
msg.SetMessageID()
|
||||
msg.SetBulk()
|
||||
msg.SetImportance(mail.ImportanceHigh)
|
||||
msg.SetBodyString(mail.TypeTextPlain, plainBody)
|
||||
msg.SetBodyString(mail.TypeTextHTML, htmlBody)
|
||||
|
||||
client, err := mail.NewClient(cfg.Email.Host,
|
||||
mail.WithPort(cfg.Email.Port),
|
||||
mail.WithSMTPAuth(mail.SMTPAuthPlain),
|
||||
mail.WithUsername(cfg.Email.Username),
|
||||
mail.WithPassword(cfg.Email.Password),
|
||||
mail.WithTLSPolicy(mail.TLSMandatory),
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error creating verification email client")
|
||||
}
|
||||
if err := client.DialAndSend(msg); err != nil {
|
||||
return errors.Wrap(err, "error sending verification email")
|
||||
}
|
||||
|
||||
logrus.Infof("verification email sent to '%v'", emailAddress)
|
||||
return nil
|
||||
func newVerifyHandler(cfg *Config) *verifyHandler {
|
||||
return &verifyHandler{cfg: cfg}
|
||||
}
|
||||
|
||||
func mergeTemplate(emailData *verificationEmail, filename string) (string, error) {
|
||||
t, err := template.ParseFS(email_ui.FS, filename)
|
||||
func (self *verifyHandler) Handle(params identity.VerifyParams) middleware.Responder {
|
||||
logrus.Infof("received verify request for token '%v'", params.Body.Token)
|
||||
tx, err := str.Begin()
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "error parsing verification email template '%v'", filename)
|
||||
logrus.Errorf("error starting transaction: %v", err)
|
||||
return identity.NewVerifyInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
if err := t.Execute(buf, emailData); err != nil {
|
||||
return "", errors.Wrapf(err, "error executing verification email template '%v'", filename)
|
||||
ar, err := str.FindAccountRequestWithToken(params.Body.Token, tx)
|
||||
if err != nil {
|
||||
logrus.Errorf("error finding account with token '%v': %v", params.Body.Token, err)
|
||||
return identity.NewVerifyNotFound()
|
||||
}
|
||||
return buf.String(), nil
|
||||
return identity.NewVerifyOK().WithPayload(&rest_model_zrok.VerifyResponse{Email: ar.Email})
|
||||
}
|
||||
|
75
controller/verify_email.go
Normal file
75
controller/verify_email.go
Normal file
@ -0,0 +1,75 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/openziti-test-kitchen/zrok/controller/email_ui"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/wneessen/go-mail"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
type verificationEmail struct {
|
||||
EmailAddress string
|
||||
VerifyUrl string
|
||||
}
|
||||
|
||||
func sendVerificationEmail(emailAddress, token string, cfg *Config) error {
|
||||
emailData := &verificationEmail{
|
||||
EmailAddress: emailAddress,
|
||||
VerifyUrl: cfg.Registration.RegistrationUrlTemplate + "/" + token,
|
||||
}
|
||||
|
||||
plainBody, err := mergeTemplate(emailData, "verify.gotext")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
htmlBody, err := mergeTemplate(emailData, "verify.gohtml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg := mail.NewMsg()
|
||||
if err := msg.From("ziggy@zrok.io"); err != nil {
|
||||
return errors.Wrap(err, "failed to set from address in verification email")
|
||||
}
|
||||
if err := msg.To(emailAddress); err != nil {
|
||||
return errors.Wrap(err, "failed to sent to address in verification email")
|
||||
}
|
||||
msg.Subject("Welcome to zrok!")
|
||||
msg.SetDate()
|
||||
msg.SetMessageID()
|
||||
msg.SetBulk()
|
||||
msg.SetImportance(mail.ImportanceHigh)
|
||||
msg.SetBodyString(mail.TypeTextPlain, plainBody)
|
||||
msg.SetBodyString(mail.TypeTextHTML, htmlBody)
|
||||
|
||||
client, err := mail.NewClient(cfg.Email.Host,
|
||||
mail.WithPort(cfg.Email.Port),
|
||||
mail.WithSMTPAuth(mail.SMTPAuthPlain),
|
||||
mail.WithUsername(cfg.Email.Username),
|
||||
mail.WithPassword(cfg.Email.Password),
|
||||
mail.WithTLSPolicy(mail.TLSMandatory),
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error creating verification email client")
|
||||
}
|
||||
if err := client.DialAndSend(msg); err != nil {
|
||||
return errors.Wrap(err, "error sending verification email")
|
||||
}
|
||||
|
||||
logrus.Infof("verification email sent to '%v'", emailAddress)
|
||||
return nil
|
||||
}
|
||||
|
||||
func mergeTemplate(emailData *verificationEmail, filename string) (string, error) {
|
||||
t, err := template.ParseFS(email_ui.FS, filename)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "error parsing verification email template '%v'", filename)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
if err := t.Execute(buf, emailData); err != nil {
|
||||
return "", errors.Wrapf(err, "error executing verification email template '%v'", filename)
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user