regToken, accountToken (#820, #834)

This commit is contained in:
Michael Quigley
2025-02-04 11:52:46 -05:00
parent 7685c06e45
commit 47421e65bd
42 changed files with 482 additions and 407 deletions

View File

@ -33,7 +33,7 @@ func (h *inviteHandler) Handle(params account.InviteParams) middleware.Responder
return account.NewInviteBadRequest()
}
logrus.Infof("received account request for email '%v'", params.Body.Email)
var token string
var regToken string
tx, err := str.Begin()
if err != nil {
@ -55,13 +55,13 @@ func (h *inviteHandler) Handle(params account.InviteParams) middleware.Responder
logrus.Infof("using invite token '%v' to process invite request for '%v'", inviteToken.Token, params.Body.Email)
}
token, err = CreateToken()
regToken, err = CreateToken()
if err != nil {
logrus.Error(err)
return account.NewInviteInternalServerError()
}
ar := &store.AccountRequest{
Token: token,
Token: regToken,
Email: params.Body.Email,
SourceAddress: params.HTTPRequest.RemoteAddr,
}
@ -94,7 +94,7 @@ func (h *inviteHandler) Handle(params account.InviteParams) middleware.Responder
}
if cfg.Email != nil && cfg.Registration != nil {
if err := sendVerificationEmail(params.Body.Email, token); err != nil {
if err := sendVerificationEmail(params.Body.Email, regToken); err != nil {
logrus.Errorf("error sending verification email for '%v': %v", params.Body.Email, err)
return account.NewInviteInternalServerError()
}

View File

@ -19,63 +19,63 @@ func newRegisterHandler(cfg *config.Config) *registerHandler {
}
}
func (h *registerHandler) Handle(params account.RegisterParams) middleware.Responder {
if params.Body.Token == "" || params.Body.Password == "" {
if params.Body.RegToken == "" || params.Body.Password == "" {
logrus.Error("missing token or password")
return account.NewRegisterNotFound()
}
logrus.Infof("received register request for token '%v'", params.Body.Token)
logrus.Infof("received register request for registration token '%v'", params.Body.RegToken)
tx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction for token '%v': %v", params.Body.Token, err)
logrus.Errorf("error starting transaction for registration token '%v': %v", params.Body.RegToken, err)
return account.NewRegisterInternalServerError()
}
defer func() { _ = tx.Rollback() }()
ar, err := str.FindAccountRequestWithToken(params.Body.Token, tx)
ar, err := str.FindAccountRequestWithToken(params.Body.RegToken, tx)
if err != nil {
logrus.Errorf("error finding account request with token '%v': %v", params.Body.Token, err)
logrus.Errorf("error finding account request with registration token '%v': %v", params.Body.RegToken, err)
return account.NewRegisterNotFound()
}
token, err := CreateToken()
accountToken, err := CreateToken()
if err != nil {
logrus.Errorf("error creating token for request '%v' (%v): %v", params.Body.Token, ar.Email, err)
logrus.Errorf("error creating account token for request '%v' (%v): %v", params.Body.RegToken, ar.Email, err)
return account.NewRegisterInternalServerError()
}
if err := validatePassword(h.cfg, params.Body.Password); err != nil {
logrus.Errorf("password not valid for request '%v', (%v): %v", params.Body.Token, ar.Email, err)
logrus.Errorf("password not valid for request '%v', (%v): %v", params.Body.RegToken, ar.Email, err)
return account.NewRegisterUnprocessableEntity().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
hpwd, err := HashPassword(params.Body.Password)
if err != nil {
logrus.Errorf("error hashing password for request '%v' (%v): %v", params.Body.Token, ar.Email, err)
logrus.Errorf("error hashing password for request '%v' (%v): %v", params.Body.RegToken, ar.Email, err)
return account.NewRegisterInternalServerError()
}
a := &store.Account{
Email: ar.Email,
Salt: hpwd.Salt,
Password: hpwd.Password,
Token: token,
Token: accountToken,
}
if _, err := str.CreateAccount(a, tx); err != nil {
logrus.Errorf("error creating account for request '%v' (%v): %v", params.Body.Token, ar.Email, err)
logrus.Errorf("error creating account for request '%v' (%v): %v", params.Body.RegToken, ar.Email, err)
return account.NewRegisterInternalServerError()
}
if err := str.DeleteAccountRequest(ar.Id, tx); err != nil {
logrus.Errorf("error deleteing account request '%v' (%v): %v", params.Body.Token, ar.Email, err)
logrus.Errorf("error deleteing account request '%v' (%v): %v", params.Body.RegToken, ar.Email, err)
return account.NewRegisterInternalServerError()
}
if err := tx.Commit(); err != nil {
logrus.Errorf("error committing '%v' (%v): %v", params.Body.Token, ar.Email, err)
logrus.Errorf("error committing '%v' (%v): %v", params.Body.RegToken, ar.Email, err)
return account.NewRegisterInternalServerError()
}
logrus.Infof("created account '%v'", a.Email)
return account.NewRegisterOK().WithPayload(&account.RegisterOKBody{Token: a.Token})
return account.NewRegisterOK().WithPayload(&account.RegisterOKBody{AccountToken: a.Token})
}

View File

@ -17,10 +17,10 @@ type verificationEmail struct {
Version string
}
func sendVerificationEmail(emailAddress, token string) error {
func sendVerificationEmail(emailAddress, regToken string) error {
emailData := &verificationEmail{
EmailAddress: emailAddress,
VerifyUrl: cfg.Registration.RegistrationUrlTemplate + "/" + token,
VerifyUrl: cfg.Registration.RegistrationUrlTemplate + "/" + regToken,
Version: build.String(),
}