zrok/controller/account.go

74 lines
2.6 KiB
Go
Raw Normal View History

2022-07-25 23:05:44 +02:00
package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
2022-07-25 23:05:44 +02:00
"github.com/sirupsen/logrus"
)
type createAccountHandler struct {
cfg *Config
}
func newCreateAccountHandler(cfg *Config) *createAccountHandler {
return &createAccountHandler{cfg: cfg}
}
func (self *createAccountHandler) Handle(params identity.CreateAccountParams) middleware.Responder {
if params.Body == nil || params.Body.Email == "" {
logrus.Errorf("missing email")
return identity.NewCreateAccountBadRequest().WithPayload("missing email")
}
logrus.Infof("received account request for email '%v'", params.Body.Email)
2022-09-14 20:08:12 +02:00
token := createToken()
2022-09-19 22:26:54 +02:00
ar := &store.AccountRequest{
Token: token,
Email: params.Body.Email,
SourceAddress: params.HTTPRequest.RemoteAddr,
}
2022-09-19 22:26:54 +02:00
tx, err := str.Begin()
if err != nil {
logrus.Error(err)
2022-09-26 22:35:06 +02:00
return identity.NewCreateAccountInternalServerError()
2022-09-19 22:26:54 +02:00
}
defer func() { _ = tx.Rollback() }()
if _, err := str.FindAccountWithEmail(params.Body.Email, tx); err == nil {
logrus.Errorf("found account for '%v', cannot process account request", params.Body.Email)
return identity.NewCreateAccountBadRequest()
} else {
logrus.Infof("no account found for '%v': %v", params.Body.Email, err)
}
if oldAr, err := str.FindAccountRequestWithEmail(params.Body.Email, tx); err == nil {
logrus.Warnf("found previous account request for '%v', removing", params.Body.Email)
if err := str.DeleteAccountRequest(oldAr.Id, tx); err != nil {
logrus.Errorf("error deleteing previous account request for '%v': %v", params.Body.Email, err)
2022-09-26 22:35:06 +02:00
return identity.NewCreateAccountInternalServerError()
}
} else {
logrus.Warnf("error finding previous account request for '%v': %v", params.Body.Email, err)
}
2022-09-19 22:26:54 +02:00
if _, err := str.CreateAccountRequest(ar, tx); err != nil {
2022-09-20 22:27:18 +02:00
logrus.Errorf("error creating account request for '%v': %v", params.Body.Email, err)
2022-09-26 22:35:06 +02:00
return identity.NewCreateAccountInternalServerError()
2022-09-19 22:26:54 +02:00
}
if err := tx.Commit(); err != nil {
2022-09-20 22:27:18 +02:00
logrus.Errorf("error committing account request for '%v': %v", params.Body.Email, err)
2022-09-26 22:35:06 +02:00
return identity.NewCreateAccountInternalServerError()
2022-09-19 22:26:54 +02:00
}
if err := sendVerificationEmail(params.Body.Email, token, self.cfg); err != nil {
2022-09-20 22:27:18 +02:00
logrus.Errorf("error sending verification email for '%v': %v", params.Body.Email, err)
2022-09-26 22:35:06 +02:00
return identity.NewCreateAccountInternalServerError()
}
logrus.Infof("account request for '%v' has registration token '%v'", params.Body.Email, ar.Token)
return identity.NewCreateAccountCreated()
}