remove immediate create; streamline controller account infrastructure (#50)

This commit is contained in:
Michael Quigley 2022-09-20 14:05:27 -04:00
parent a6e203b28e
commit 3ac9541463
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 7 additions and 48 deletions

View File

@ -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))
}

View File

@ -33,7 +33,6 @@ type EmailConfig struct {
}
type RegistrationConfig struct {
ImmediateCreate bool
EmailFrom string
RegistrationUrlTemplate string
}

View File

@ -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))
}