multi-path account create logic (#50, #51)

This commit is contained in:
Michael Quigley 2022-09-12 14:35:11 -04:00
parent 97481e7df7
commit 600f0396d2
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 25 additions and 4 deletions

View File

@ -10,8 +10,24 @@ import (
"github.com/sirupsen/logrus"
)
func createAccountHandler(params identity.CreateAccountParams) middleware.Responder {
type createAccountHandler struct {
cfg *Config
}
func newCreateAccountHandler(cfg *Config) *createAccountHandler {
return &createAccountHandler{cfg: cfg}
}
func (self *createAccountHandler) Handle(params identity.CreateAccountParams) middleware.Responder {
logrus.Infof("received account request for email '%v'", params.Body.Email)
if self.cfg.Registration.ImmediateCreate {
return self.handleDirectCreate(params)
} else {
return self.handleVerifiedCreate(params)
}
}
func (self *createAccountHandler) handleDirectCreate(params identity.CreateAccountParams) middleware.Responder {
if params.Body == nil || params.Body.Email == "" || params.Body.Password == "" {
logrus.Errorf("missing email or password")
return identity.NewCreateAccountBadRequest().WithPayload("missing email or password")
@ -46,6 +62,10 @@ func createAccountHandler(params identity.CreateAccountParams) middleware.Respon
return identity.NewCreateAccountCreated().WithPayload(&rest_model_zrok.AccountResponse{Token: token})
}
func (self *createAccountHandler) handleVerifiedCreate(params identity.CreateAccountParams) middleware.Responder {
return identity.NewCreateAccountCreated()
}
func hashPassword(raw string) string {
hash := sha512.New()
hash.Write([]byte(raw))

View File

@ -33,8 +33,9 @@ type EmailConfig struct {
}
type RegistrationConfig struct {
EmailFrom string
VerifyUrlTemplate string
ImmediateCreate bool
EmailFrom string
RegistrationUrlTemplate string
}
type ZitiConfig struct {

View File

@ -21,7 +21,7 @@ func Run(cfg *Config) error {
api := operations.NewZrokAPI(swaggerSpec)
api.KeyAuth = ZrokAuthenticate
api.IdentityCreateAccountHandler = identity.CreateAccountHandlerFunc(createAccountHandler)
api.IdentityCreateAccountHandler = newCreateAccountHandler(cfg)
api.IdentityEnableHandler = newEnableHandler(cfg)
api.IdentityDisableHandler = newDisableHandler(cfg)
api.IdentityLoginHandler = identity.LoginHandlerFunc(loginHandler)