mirror of
https://github.com/openziti/zrok.git
synced 2024-11-22 08:03:49 +01:00
new endpoint handler and wiring for account creation (#734)
This commit is contained in:
parent
1095c7b42b
commit
d036198332
@ -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()
|
||||
|
59
controller/createAccount.go
Normal file
59
controller/createAccount.go
Normal 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})
|
||||
}
|
Loading…
Reference in New Issue
Block a user