new endpoint handler and wiring for account creation (#734)

This commit is contained in:
Michael Quigley 2024-08-19 11:49:36 -04:00
parent 1095c7b42b
commit d036198332
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 60 additions and 0 deletions

View File

@ -52,6 +52,7 @@ func Run(inCfg *config.Config) error {
api.AccountResetPasswordHandler = newResetPasswordHandler(cfg)
api.AccountResetPasswordRequestHandler = newResetPasswordRequestHandler()
api.AccountVerifyHandler = newVerifyHandler()
api.AdminCreateAccountHandler = newCreateAccountHandler()
api.AdminCreateFrontendHandler = newCreateFrontendHandler()
api.AdminCreateIdentityHandler = newCreateIdentityHandler()
api.AdminDeleteFrontendHandler = newDeleteFrontendHandler()

View File

@ -0,0 +1,59 @@
package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/admin"
"github.com/sirupsen/logrus"
)
type createAccountHandler struct{}
func newCreateAccountHandler() *createAccountHandler {
return &createAccountHandler{}
}
func (h *createAccountHandler) Handle(params admin.CreateAccountParams, principal *rest_model_zrok.Principal) middleware.Responder {
if !principal.Admin {
logrus.Errorf("invalid admin principal")
return admin.NewCreateAccountUnauthorized()
}
token, err := CreateToken()
if err != nil {
logrus.Errorf("error creating token: %v", err)
return admin.NewCreateAccountInternalServerError()
}
hpwd, err := HashPassword(params.Body.Password)
if err != nil {
logrus.Errorf("error hashing password: %v", err)
return admin.NewCreateAccountInternalServerError()
}
trx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return admin.NewCreateAccountInternalServerError()
}
defer func() {
_ = trx.Rollback()
}()
a := &store.Account{
Email: params.Body.Email,
Salt: hpwd.Salt,
Password: hpwd.Password,
Token: token,
}
if _, err := str.CreateAccount(a, trx); err != nil {
logrus.Errorf("error creating account: %v", err)
return admin.NewCreateAccountInternalServerError()
}
if err := trx.Commit(); err != nil {
logrus.Errorf("error committing transaction: %v", err)
}
logrus.Infof("administratively created account '%v'", params.Body.Email)
return admin.NewCreateAccountCreated().WithPayload(&admin.CreateAccountCreatedBody{Token: token})
}