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"
|
2023-01-10 16:53:41 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
2022-11-30 17:43:00 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/account"
|
2023-01-10 16:53:41 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/admin"
|
2022-11-29 19:23:23 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/util"
|
2022-07-25 23:05:44 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-11-30 17:43:00 +01:00
|
|
|
type inviteHandler struct {
|
2023-01-09 18:00:37 +01:00
|
|
|
cfg *Config
|
2022-09-12 20:35:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-09 18:00:37 +01:00
|
|
|
func newInviteHandler(cfg *Config) *inviteHandler {
|
|
|
|
return &inviteHandler{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
2022-09-12 20:35:11 +02:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:43:00 +01:00
|
|
|
func (self *inviteHandler) Handle(params account.InviteParams) middleware.Responder {
|
2022-09-12 21:28:59 +02:00
|
|
|
if params.Body == nil || params.Body.Email == "" {
|
|
|
|
logrus.Errorf("missing email")
|
2022-11-30 17:43:00 +01:00
|
|
|
return account.NewInviteBadRequest()
|
2022-09-12 21:28:59 +02:00
|
|
|
}
|
2022-11-29 19:23:23 +01:00
|
|
|
if !util.IsValidEmail(params.Body.Email) {
|
|
|
|
logrus.Errorf("'%v' is not a valid email address", params.Body.Email)
|
2022-11-30 17:43:00 +01:00
|
|
|
return account.NewInviteBadRequest()
|
2022-11-29 19:23:23 +01:00
|
|
|
}
|
2022-09-20 22:01:46 +02:00
|
|
|
logrus.Infof("received account request for email '%v'", params.Body.Email)
|
2023-01-09 18:00:37 +01:00
|
|
|
var token string
|
2022-09-20 20:23:01 +02:00
|
|
|
|
2023-01-09 18:00:37 +01:00
|
|
|
tx, err := str.Begin()
|
2022-10-18 21:21:53 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return account.NewInviteInternalServerError()
|
2022-10-18 21:21:53 +02:00
|
|
|
}
|
2023-01-09 18:00:37 +01:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
|
|
|
if self.cfg.Registration.TokenStrategy == "store" {
|
2023-01-09 20:23:02 +01:00
|
|
|
invite, err := str.GetInviteByToken(params.Body.Token, tx)
|
2023-01-09 18:00:37 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2023-01-09 20:23:02 +01:00
|
|
|
return account.NewInviteBadRequest()
|
2023-01-09 18:00:37 +01:00
|
|
|
}
|
2023-01-09 20:23:02 +01:00
|
|
|
if err := str.DeleteInvite(invite.Id, tx); err != nil {
|
2023-01-09 18:00:37 +01:00
|
|
|
logrus.Error(err)
|
|
|
|
return account.NewInviteInternalServerError()
|
|
|
|
}
|
|
|
|
}
|
2023-01-09 20:23:02 +01:00
|
|
|
|
|
|
|
token, err = createToken()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return account.NewInviteInternalServerError()
|
|
|
|
}
|
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
|
|
|
|
|
|
|
if _, err := str.FindAccountWithEmail(params.Body.Email, tx); err == nil {
|
|
|
|
logrus.Errorf("found account for '%v', cannot process account request", params.Body.Email)
|
2022-11-30 17:43:00 +01:00
|
|
|
return account.NewInviteBadRequest()
|
2022-09-20 20:23:01 +02:00
|
|
|
} 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-11-30 17:43:00 +01:00
|
|
|
return account.NewInviteInternalServerError()
|
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-11-30 17:43:00 +01:00
|
|
|
return account.NewInviteInternalServerError()
|
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-11-30 17:43:00 +01:00
|
|
|
return account.NewInviteInternalServerError()
|
2022-09-19 22:26:54 +02:00
|
|
|
}
|
2022-09-20 20:23:01 +02:00
|
|
|
|
2022-12-07 18:24:00 +01:00
|
|
|
if cfg.Email != nil && cfg.Registration != nil {
|
|
|
|
if err := sendVerificationEmail(params.Body.Email, token); err != nil {
|
|
|
|
logrus.Errorf("error sending verification email for '%v': %v", params.Body.Email, err)
|
|
|
|
return account.NewInviteInternalServerError()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("'email' and 'registration' configuration missing; skipping registration email")
|
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-11-30 18:46:19 +01:00
|
|
|
return account.NewInviteCreated()
|
2022-09-12 20:35:11 +02:00
|
|
|
}
|
2023-01-09 18:00:37 +01:00
|
|
|
|
|
|
|
type inviteGenerateHandler struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInviteGenerateHandler() *inviteGenerateHandler {
|
|
|
|
return &inviteGenerateHandler{}
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:53:41 +01:00
|
|
|
func (handler *inviteGenerateHandler) Handle(params admin.InviteGenerateParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
|
|
|
if !principal.Admin {
|
|
|
|
logrus.Errorf("invalid admin principal")
|
2023-01-10 17:05:57 +01:00
|
|
|
return admin.NewInviteGenerateUnauthorized()
|
2023-01-10 16:53:41 +01:00
|
|
|
}
|
|
|
|
|
2023-01-09 18:00:37 +01:00
|
|
|
if params.Body == nil || len(params.Body.Tokens) == 0 {
|
|
|
|
logrus.Error("missing tokens")
|
2023-01-10 16:53:41 +01:00
|
|
|
return admin.NewInviteGenerateBadRequest()
|
2023-01-09 18:00:37 +01:00
|
|
|
}
|
|
|
|
logrus.Infof("received invite generate request with %d tokens", len(params.Body.Tokens))
|
|
|
|
|
|
|
|
invites := make([]*store.Invite, len(params.Body.Tokens))
|
|
|
|
for i, token := range params.Body.Tokens {
|
|
|
|
invites[i] = &store.Invite{
|
2023-01-09 20:23:02 +01:00
|
|
|
Token: token,
|
2023-01-09 18:00:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2023-01-10 16:53:41 +01:00
|
|
|
return admin.NewInviteGenerateInternalServerError()
|
2023-01-09 18:00:37 +01:00
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
|
|
|
if err := str.CreateInvites(invites, tx); err != nil {
|
|
|
|
logrus.Error(err)
|
2023-01-10 16:53:41 +01:00
|
|
|
return admin.NewInviteGenerateInternalServerError()
|
2023-01-09 18:00:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
logrus.Errorf("error committing inviteGenerate request: %v", err)
|
|
|
|
return account.NewInviteInternalServerError()
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:53:41 +01:00
|
|
|
return admin.NewInviteGenerateCreated()
|
2023-01-09 18:00:37 +01:00
|
|
|
}
|