mirror of
https://github.com/openziti/zrok.git
synced 2025-06-27 21:21:48 +02:00
remove immediate create; streamline controller account infrastructure (#50)
This commit is contained in:
parent
a6e203b28e
commit
3ac9541463
@ -1,8 +1,6 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha512"
|
|
||||||
"encoding/hex"
|
|
||||||
"github.com/go-openapi/runtime/middleware"
|
"github.com/go-openapi/runtime/middleware"
|
||||||
"github.com/openziti-test-kitchen/zrok/controller/store"
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
"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 {
|
func (self *createAccountHandler) Handle(params identity.CreateAccountParams) middleware.Responder {
|
||||||
logrus.Infof("received account request for email '%v'", params.Body.Email)
|
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 == "" {
|
if params.Body == nil || params.Body.Email == "" {
|
||||||
logrus.Errorf("missing email")
|
logrus.Errorf("missing email")
|
||||||
return identity.NewCreateAccountBadRequest().WithPayload("missing email")
|
return identity.NewCreateAccountBadRequest().WithPayload("missing email")
|
||||||
@ -89,9 +48,3 @@ func (self *createAccountHandler) handleVerifiedCreate(params identity.CreateAcc
|
|||||||
}
|
}
|
||||||
return identity.NewCreateAccountCreated()
|
return identity.NewCreateAccountCreated()
|
||||||
}
|
}
|
||||||
|
|
||||||
func hashPassword(raw string) string {
|
|
||||||
hash := sha512.New()
|
|
||||||
hash.Write([]byte(raw))
|
|
||||||
return hex.EncodeToString(hash.Sum(nil))
|
|
||||||
}
|
|
||||||
|
@ -33,7 +33,6 @@ type EmailConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type RegistrationConfig struct {
|
type RegistrationConfig struct {
|
||||||
ImmediateCreate bool
|
|
||||||
EmailFrom string
|
EmailFrom string
|
||||||
RegistrationUrlTemplate string
|
RegistrationUrlTemplate string
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"crypto/sha512"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
errors2 "github.com/go-openapi/errors"
|
errors2 "github.com/go-openapi/errors"
|
||||||
@ -53,3 +54,9 @@ func createServiceName() (string, error) {
|
|||||||
}
|
}
|
||||||
return hex.EncodeToString(bytes), nil
|
return hex.EncodeToString(bytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hashPassword(raw string) string {
|
||||||
|
hash := sha512.New()
|
||||||
|
hash.Write([]byte(raw))
|
||||||
|
return hex.EncodeToString(hash.Sum(nil))
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user