fix authentication transaction; record created identities (#10)

This commit is contained in:
Michael Quigley 2022-07-29 15:54:13 -04:00
parent cb1a88e339
commit 1292c5d702
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 21 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"github.com/go-openapi/runtime/middleware"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
"github.com/openziti/edge/rest_management_api_client"
@ -34,6 +35,23 @@ func enableHandler(_ identity.EnableParams, principal *rest_model_zrok.Principal
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
tx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
iid, err := str.CreateIdentity(int(principal.ID), &store.Identity{ZitiId: ident.Payload.Data.ID}, tx)
if err != nil {
logrus.Errorf("error storing created identity: %v", err)
_ = tx.Rollback()
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
if err := tx.Commit(); err != nil {
logrus.Errorf("error committing: %v", err)
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
logrus.Infof("recorded identity '%v' with id '%v' for '%v'", ident.Payload.Data.ID, iid, principal.Username)
resp := identity.NewEnableCreated().WithPayload(&rest_model_zrok.EnableResponse{
Identity: ident.Payload.Data.ID,
})

View File

@ -11,12 +11,12 @@ type Identity struct {
ZitiId string
}
func (self *Store) CreateIdentity(accountId int, svc *Service, tx *sqlx.Tx) (int, error) {
func (self *Store) CreateIdentity(accountId int, i *Identity, tx *sqlx.Tx) (int, error) {
stmt, err := tx.Prepare("insert into identities (account_id, ziti_id) values (?, ?)")
if err != nil {
return 0, errors.Wrap(err, "error preparing identities insert statement")
}
res, err := stmt.Exec(accountId, svc.ZitiId)
res, err := stmt.Exec(accountId, i.ZitiId)
if err != nil {
return 0, errors.Wrap(err, "error executing identities insert statement")
}

View File

@ -18,6 +18,7 @@ func ZrokAuthenticate(token string) (*rest_model_zrok.Principal, error) {
if err != nil {
return nil, err
}
defer func() { _ = tx.Rollback() }()
if a, err := str.FindAccountWithToken(token, tx); err == nil {
principal := rest_model_zrok.Principal{
ID: int64(a.Id),