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"
|
2022-07-26 21:16:02 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
|
2022-07-25 23:05:44 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-09-12 20:35:11 +02:00
|
|
|
type createAccountHandler struct {
|
|
|
|
cfg *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCreateAccountHandler(cfg *Config) *createAccountHandler {
|
|
|
|
return &createAccountHandler{cfg: cfg}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *createAccountHandler) Handle(params identity.CreateAccountParams) middleware.Responder {
|
2022-09-12 21:28:59 +02:00
|
|
|
if params.Body == nil || params.Body.Email == "" {
|
|
|
|
logrus.Errorf("missing email")
|
|
|
|
return identity.NewCreateAccountBadRequest().WithPayload("missing email")
|
|
|
|
}
|
2022-09-20 22:01:46 +02:00
|
|
|
logrus.Infof("received account request for email '%v'", params.Body.Email)
|
2022-09-20 20:23:01 +02:00
|
|
|
|
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-20 20:23:01 +02:00
|
|
|
|
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() }()
|
2022-09-20 20:23:01 +02:00
|
|
|
|
|
|
|
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()
|
2022-09-20 20:23:01 +02:00
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
2022-09-20 20:23:01 +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()
|
2022-09-20 20:23:01 +02:00
|
|
|
}
|
|
|
|
|
2022-09-20 22:38:20 +02:00
|
|
|
logrus.Infof("account request for '%v' has registration token '%v'", params.Body.Email, ar.Token)
|
|
|
|
|
2022-09-12 20:35:11 +02:00
|
|
|
return identity.NewCreateAccountCreated()
|
|
|
|
}
|