From 3ac9541463360baa640cd5ece3a7dcb2a8e28678 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Tue, 20 Sep 2022 14:05:27 -0400 Subject: [PATCH] remove immediate create; streamline controller account infrastructure (#50) --- controller/account.go | 47 ------------------------------------------- controller/config.go | 1 - controller/util.go | 7 +++++++ 3 files changed, 7 insertions(+), 48 deletions(-) diff --git a/controller/account.go b/controller/account.go index 23a4a942..9d0c03dc 100644 --- a/controller/account.go +++ b/controller/account.go @@ -1,8 +1,6 @@ package controller import ( - "crypto/sha512" - "encoding/hex" "github.com/go-openapi/runtime/middleware" "github.com/openziti-test-kitchen/zrok/controller/store" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" @@ -20,45 +18,6 @@ func newCreateAccountHandler(cfg *Config) *createAccountHandler { 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") - } - - token := createToken() - a := &store.Account{ - Email: params.Body.Email, - Password: hashPassword(params.Body.Password), - Token: token, - } - tx, err := str.Begin() - if err != nil { - logrus.Errorf("error starting transaction: %v", err) - return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) - } - defer func() { _ = tx.Rollback() }() - id, err := str.CreateAccount(a, tx) - if err != nil { - logrus.Errorf("error creating account: %v", err) - return identity.NewCreateAccountBadRequest().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) - } - if err := tx.Commit(); err != nil { - logrus.Errorf("error comitting: %v", err) - } - - logrus.Infof("account created with id = '%v'", id) - return identity.NewCreateAccountCreated().WithPayload(&rest_model_zrok.AccountResponse{Token: token}) -} - -func (self *createAccountHandler) handleVerifiedCreate(params identity.CreateAccountParams) middleware.Responder { if params.Body == nil || params.Body.Email == "" { logrus.Errorf("missing email") return identity.NewCreateAccountBadRequest().WithPayload("missing email") @@ -89,9 +48,3 @@ func (self *createAccountHandler) handleVerifiedCreate(params identity.CreateAcc } return identity.NewCreateAccountCreated() } - -func hashPassword(raw string) string { - hash := sha512.New() - hash.Write([]byte(raw)) - return hex.EncodeToString(hash.Sum(nil)) -} diff --git a/controller/config.go b/controller/config.go index df8ae2e2..adf3bf60 100644 --- a/controller/config.go +++ b/controller/config.go @@ -33,7 +33,6 @@ type EmailConfig struct { } type RegistrationConfig struct { - ImmediateCreate bool EmailFrom string RegistrationUrlTemplate string } diff --git a/controller/util.go b/controller/util.go index a85e0124..02e22fc8 100644 --- a/controller/util.go +++ b/controller/util.go @@ -2,6 +2,7 @@ package controller import ( "crypto/rand" + "crypto/sha512" "crypto/x509" "encoding/hex" errors2 "github.com/go-openapi/errors" @@ -53,3 +54,9 @@ func createServiceName() (string, error) { } return hex.EncodeToString(bytes), nil } + +func hashPassword(raw string) string { + hash := sha512.New() + hash.Write([]byte(raw)) + return hex.EncodeToString(hash.Sum(nil)) +}