mirror of
https://github.com/openziti/zrok.git
synced 2025-01-13 17:38:14 +01:00
fix authentication transaction; record created identities (#10)
This commit is contained in:
parent
cb1a88e339
commit
1292c5d702
@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"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/rest_model_zrok"
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
|
||||||
"github.com/openziti/edge/rest_management_api_client"
|
"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()))
|
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{
|
resp := identity.NewEnableCreated().WithPayload(&rest_model_zrok.EnableResponse{
|
||||||
Identity: ident.Payload.Data.ID,
|
Identity: ident.Payload.Data.ID,
|
||||||
})
|
})
|
||||||
|
@ -11,12 +11,12 @@ type Identity struct {
|
|||||||
ZitiId string
|
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 (?, ?)")
|
stmt, err := tx.Prepare("insert into identities (account_id, ziti_id) values (?, ?)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.Wrap(err, "error preparing identities insert statement")
|
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 {
|
if err != nil {
|
||||||
return 0, errors.Wrap(err, "error executing identities insert statement")
|
return 0, errors.Wrap(err, "error executing identities insert statement")
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ func ZrokAuthenticate(token string) (*rest_model_zrok.Principal, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer func() { _ = tx.Rollback() }()
|
||||||
if a, err := str.FindAccountWithToken(token, tx); err == nil {
|
if a, err := str.FindAccountWithToken(token, tx); err == nil {
|
||||||
principal := rest_model_zrok.Principal{
|
principal := rest_model_zrok.Principal{
|
||||||
ID: int64(a.Id),
|
ID: int64(a.Id),
|
||||||
|
Loading…
Reference in New Issue
Block a user