mirror of
https://github.com/zitadel/zitadel.git
synced 2025-03-01 01:27:24 +00:00
use global id generator (#142)
* use global id generator * remove duplicate `UserRemoved` Co-authored-by: Livio Amstutz <livio.a@gmail.com>
This commit is contained in:
parent
2758bf30b1
commit
e3f9e9c05e
@ -3,8 +3,7 @@ package spooler
|
|||||||
import (
|
import (
|
||||||
"github.com/caos/logging"
|
"github.com/caos/logging"
|
||||||
"github.com/caos/zitadel/internal/eventstore"
|
"github.com/caos/zitadel/internal/eventstore"
|
||||||
"github.com/sony/sonyflake"
|
"github.com/caos/zitadel/internal/id"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@ -15,12 +14,12 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) New() *Spooler {
|
func (c *Config) New() *Spooler {
|
||||||
lockID, err := sonyflake.NewSonyflake(sonyflake.Settings{}).NextID()
|
lockID, err := id.SonyFlakeGenerator.Next()
|
||||||
logging.Log("SPOOL-bdO56").OnError(err).Panic("unable to generate lockID")
|
logging.Log("SPOOL-bdO56").OnError(err).Panic("unable to generate lockID")
|
||||||
|
|
||||||
return &Spooler{
|
return &Spooler{
|
||||||
handlers: c.ViewHandlers,
|
handlers: c.ViewHandlers,
|
||||||
lockID: strconv.FormatUint(lockID, 10),
|
lockID: lockID,
|
||||||
eventstore: c.Eventstore,
|
eventstore: c.Eventstore,
|
||||||
locker: c.Locker,
|
locker: c.Locker,
|
||||||
queue: make(chan *spooledHandler),
|
queue: make(chan *spooledHandler),
|
||||||
|
@ -2,17 +2,17 @@ package eventsourcing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/caos/zitadel/internal/errors"
|
"github.com/caos/zitadel/internal/errors"
|
||||||
"github.com/caos/zitadel/internal/eventstore"
|
"github.com/caos/zitadel/internal/eventstore"
|
||||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||||
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
||||||
|
"github.com/caos/zitadel/internal/id"
|
||||||
org_model "github.com/caos/zitadel/internal/org/model"
|
org_model "github.com/caos/zitadel/internal/org/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OrgEventstore struct {
|
type OrgEventstore struct {
|
||||||
eventstore.Eventstore
|
eventstore.Eventstore
|
||||||
|
idGenerator id.Generator
|
||||||
}
|
}
|
||||||
|
|
||||||
type OrgConfig struct {
|
type OrgConfig struct {
|
||||||
@ -20,18 +20,21 @@ type OrgConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func StartOrg(conf OrgConfig) *OrgEventstore {
|
func StartOrg(conf OrgConfig) *OrgEventstore {
|
||||||
return &OrgEventstore{Eventstore: conf.Eventstore}
|
return &OrgEventstore{
|
||||||
|
Eventstore: conf.Eventstore,
|
||||||
|
idGenerator: id.SonyFlakeGenerator,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (es *OrgEventstore) PrepareCreateOrg(ctx context.Context, orgModel *org_model.Org) (*Org, []*es_models.Aggregate, error) {
|
func (es *OrgEventstore) PrepareCreateOrg(ctx context.Context, orgModel *org_model.Org) (*Org, []*es_models.Aggregate, error) {
|
||||||
if orgModel == nil || !orgModel.IsValid() {
|
if orgModel == nil || !orgModel.IsValid() {
|
||||||
return nil, nil, errors.ThrowInvalidArgument(nil, "EVENT-OeLSk", "org not valid")
|
return nil, nil, errors.ThrowInvalidArgument(nil, "EVENT-OeLSk", "org not valid")
|
||||||
}
|
}
|
||||||
id, err := idGenerator.NextID()
|
id, err := es.idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, errors.ThrowInternal(err, "EVENT-OwciI", "id gen failed")
|
return nil, nil, errors.ThrowInternal(err, "EVENT-OwciI", "id gen failed")
|
||||||
}
|
}
|
||||||
orgModel.AggregateID = strconv.FormatUint(id, 10)
|
orgModel.AggregateID = id
|
||||||
org := OrgFromModel(orgModel)
|
org := OrgFromModel(orgModel)
|
||||||
|
|
||||||
aggregates, err := orgCreatedAggregates(ctx, es.AggregateCreator(), org)
|
aggregates, err := orgCreatedAggregates(ctx, es.AggregateCreator(), org)
|
||||||
|
@ -2,15 +2,11 @@ package eventsourcing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/caos/zitadel/internal/errors"
|
"github.com/caos/zitadel/internal/errors"
|
||||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||||
org_model "github.com/caos/zitadel/internal/org/model"
|
org_model "github.com/caos/zitadel/internal/org/model"
|
||||||
"github.com/sony/sonyflake"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var idGenerator = sonyflake.NewSonyflake(sonyflake.Settings{})
|
|
||||||
|
|
||||||
func OrgByIDQuery(id string, latestSequence uint64) (*es_models.SearchQuery, error) {
|
func OrgByIDQuery(id string, latestSequence uint64) (*es_models.SearchQuery, error) {
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dke74", "id should be filled")
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dke74", "id should be filled")
|
||||||
|
@ -4,15 +4,14 @@ import (
|
|||||||
"github.com/caos/zitadel/internal/cache/config"
|
"github.com/caos/zitadel/internal/cache/config"
|
||||||
sd "github.com/caos/zitadel/internal/config/systemdefaults"
|
sd "github.com/caos/zitadel/internal/config/systemdefaults"
|
||||||
es_int "github.com/caos/zitadel/internal/eventstore"
|
es_int "github.com/caos/zitadel/internal/eventstore"
|
||||||
|
"github.com/caos/zitadel/internal/id"
|
||||||
"github.com/caos/zitadel/internal/policy"
|
"github.com/caos/zitadel/internal/policy"
|
||||||
"github.com/sony/sonyflake"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var idGenerator = sonyflake.NewSonyflake(sonyflake.Settings{})
|
|
||||||
|
|
||||||
type PolicyEventstore struct {
|
type PolicyEventstore struct {
|
||||||
es_int.Eventstore
|
es_int.Eventstore
|
||||||
policyCache *PolicyCache
|
policyCache *PolicyCache
|
||||||
|
idGenerator id.Generator
|
||||||
passwordAgePolicyDefault policy.PasswordAgePolicyDefault
|
passwordAgePolicyDefault policy.PasswordAgePolicyDefault
|
||||||
passwordComplexityPolicyDefault policy.PasswordComplexityPolicyDefault
|
passwordComplexityPolicyDefault policy.PasswordComplexityPolicyDefault
|
||||||
passwordLockoutPolicyDefault policy.PasswordLockoutPolicyDefault
|
passwordLockoutPolicyDefault policy.PasswordLockoutPolicyDefault
|
||||||
@ -31,6 +30,7 @@ func StartPolicy(conf PolicyConfig, systemDefaults sd.SystemDefaults) (*PolicyEv
|
|||||||
return &PolicyEventstore{
|
return &PolicyEventstore{
|
||||||
Eventstore: conf.Eventstore,
|
Eventstore: conf.Eventstore,
|
||||||
policyCache: policyCache,
|
policyCache: policyCache,
|
||||||
|
idGenerator: id.SonyFlakeGenerator,
|
||||||
passwordAgePolicyDefault: systemDefaults.DefaultPolicies.Age,
|
passwordAgePolicyDefault: systemDefaults.DefaultPolicies.Age,
|
||||||
passwordComplexityPolicyDefault: systemDefaults.DefaultPolicies.Complexity,
|
passwordComplexityPolicyDefault: systemDefaults.DefaultPolicies.Complexity,
|
||||||
passwordLockoutPolicyDefault: systemDefaults.DefaultPolicies.Lockout,
|
passwordLockoutPolicyDefault: systemDefaults.DefaultPolicies.Lockout,
|
||||||
|
@ -2,8 +2,6 @@ package eventsourcing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/caos/zitadel/internal/api/auth"
|
"github.com/caos/zitadel/internal/api/auth"
|
||||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||||
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
||||||
@ -39,11 +37,11 @@ func (es *PolicyEventstore) CreatePasswordAgePolicy(ctx context.Context, policy
|
|||||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-yDJ5I", "Policy allready exists")
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-yDJ5I", "Policy allready exists")
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := idGenerator.NextID()
|
id, err := es.idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
policy.AggregateID = strconv.FormatUint(id, 10)
|
policy.AggregateID = id
|
||||||
|
|
||||||
repoPolicy := PasswordAgePolicyFromModel(policy)
|
repoPolicy := PasswordAgePolicyFromModel(policy)
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package eventsourcing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/caos/zitadel/internal/id"
|
||||||
|
|
||||||
mock_cache "github.com/caos/zitadel/internal/cache/mock"
|
mock_cache "github.com/caos/zitadel/internal/cache/mock"
|
||||||
"github.com/caos/zitadel/internal/eventstore/mock"
|
"github.com/caos/zitadel/internal/eventstore/mock"
|
||||||
@ -14,6 +15,7 @@ func GetMockedEventstoreAge(ctrl *gomock.Controller, mockEs *mock.MockEventstore
|
|||||||
return &PolicyEventstore{
|
return &PolicyEventstore{
|
||||||
Eventstore: mockEs,
|
Eventstore: mockEs,
|
||||||
policyCache: GetMockCacheAge(ctrl),
|
policyCache: GetMockCacheAge(ctrl),
|
||||||
|
idGenerator: GetSonyFlacke(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,6 +26,10 @@ func GetMockCacheAge(ctrl *gomock.Controller) *PolicyCache {
|
|||||||
return &PolicyCache{policyCache: mockCache}
|
return &PolicyCache{policyCache: mockCache}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetSonyFlacke() id.Generator {
|
||||||
|
return id.SonyFlakeGenerator
|
||||||
|
}
|
||||||
|
|
||||||
func GetMockGetPasswordAgePolicyOK(ctrl *gomock.Controller) *PolicyEventstore {
|
func GetMockGetPasswordAgePolicyOK(ctrl *gomock.Controller) *PolicyEventstore {
|
||||||
data, _ := json.Marshal(model.PasswordAgePolicy{Description: "Name"})
|
data, _ := json.Marshal(model.PasswordAgePolicy{Description: "Name"})
|
||||||
events := []*es_models.Event{
|
events := []*es_models.Event{
|
||||||
|
@ -2,8 +2,6 @@ package eventsourcing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/caos/zitadel/internal/api/auth"
|
"github.com/caos/zitadel/internal/api/auth"
|
||||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||||
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
||||||
@ -42,11 +40,11 @@ func (es *PolicyEventstore) CreatePasswordComplexityPolicy(ctx context.Context,
|
|||||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-yDJ5I", "Policy allready exists")
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-yDJ5I", "Policy allready exists")
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := idGenerator.NextID()
|
id, err := es.idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
policy.AggregateID = strconv.FormatUint(id, 10)
|
policy.AggregateID = id
|
||||||
|
|
||||||
repoPolicy := PasswordComplexityPolicyFromModel(policy)
|
repoPolicy := PasswordComplexityPolicyFromModel(policy)
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ func GetMockedEventstoreComplexity(ctrl *gomock.Controller, mockEs *mock.MockEve
|
|||||||
return &PolicyEventstore{
|
return &PolicyEventstore{
|
||||||
Eventstore: mockEs,
|
Eventstore: mockEs,
|
||||||
policyCache: GetMockCacheComplexity(ctrl),
|
policyCache: GetMockCacheComplexity(ctrl),
|
||||||
|
idGenerator: GetSonyFlacke(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,6 @@ package eventsourcing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/caos/zitadel/internal/api/auth"
|
"github.com/caos/zitadel/internal/api/auth"
|
||||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||||
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
||||||
@ -39,11 +37,11 @@ func (es *PolicyEventstore) CreatePasswordLockoutPolicy(ctx context.Context, pol
|
|||||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-yDJ5I", "Policy allready exists")
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-yDJ5I", "Policy allready exists")
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := idGenerator.NextID()
|
id, err := es.idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
policy.AggregateID = strconv.FormatUint(id, 10)
|
policy.AggregateID = id
|
||||||
|
|
||||||
repoPolicy := PasswordLockoutPolicyFromModel(policy)
|
repoPolicy := PasswordLockoutPolicyFromModel(policy)
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ func GetMockedEventstoreLockout(ctrl *gomock.Controller, mockEs *mock.MockEvents
|
|||||||
return &PolicyEventstore{
|
return &PolicyEventstore{
|
||||||
Eventstore: mockEs,
|
Eventstore: mockEs,
|
||||||
policyCache: GetMockCacheLockout(ctrl),
|
policyCache: GetMockCacheLockout(ctrl),
|
||||||
|
idGenerator: GetSonyFlacke(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,25 +2,22 @@ package eventsourcing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
sd "github.com/caos/zitadel/internal/config/systemdefaults"
|
|
||||||
"github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/sony/sonyflake"
|
|
||||||
|
|
||||||
"github.com/caos/zitadel/internal/cache/config"
|
"github.com/caos/zitadel/internal/cache/config"
|
||||||
|
sd "github.com/caos/zitadel/internal/config/systemdefaults"
|
||||||
"github.com/caos/zitadel/internal/crypto"
|
"github.com/caos/zitadel/internal/crypto"
|
||||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||||
es_int "github.com/caos/zitadel/internal/eventstore"
|
es_int "github.com/caos/zitadel/internal/eventstore"
|
||||||
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
||||||
|
"github.com/caos/zitadel/internal/id"
|
||||||
proj_model "github.com/caos/zitadel/internal/project/model"
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
||||||
|
"github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProjectEventstore struct {
|
type ProjectEventstore struct {
|
||||||
es_int.Eventstore
|
es_int.Eventstore
|
||||||
projectCache *ProjectCache
|
projectCache *ProjectCache
|
||||||
pwGenerator crypto.Generator
|
pwGenerator crypto.Generator
|
||||||
idGenerator *sonyflake.Sonyflake
|
idGenerator id.Generator
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProjectConfig struct {
|
type ProjectConfig struct {
|
||||||
@ -35,12 +32,11 @@ func StartProject(conf ProjectConfig, systemDefaults sd.SystemDefaults) (*Projec
|
|||||||
}
|
}
|
||||||
passwordAlg := crypto.NewBCrypt(systemDefaults.SecretGenerators.PasswordSaltCost)
|
passwordAlg := crypto.NewBCrypt(systemDefaults.SecretGenerators.PasswordSaltCost)
|
||||||
pwGenerator := crypto.NewHashGenerator(systemDefaults.SecretGenerators.ClientSecretGenerator, passwordAlg)
|
pwGenerator := crypto.NewHashGenerator(systemDefaults.SecretGenerators.ClientSecretGenerator, passwordAlg)
|
||||||
idGenerator := sonyflake.NewSonyflake(sonyflake.Settings{})
|
|
||||||
return &ProjectEventstore{
|
return &ProjectEventstore{
|
||||||
Eventstore: conf.Eventstore,
|
Eventstore: conf.Eventstore,
|
||||||
projectCache: projectCache,
|
projectCache: projectCache,
|
||||||
pwGenerator: pwGenerator,
|
pwGenerator: pwGenerator,
|
||||||
idGenerator: idGenerator,
|
idGenerator: id.SonyFlakeGenerator,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,11 +59,11 @@ func (es *ProjectEventstore) CreateProject(ctx context.Context, project *proj_mo
|
|||||||
if !project.IsValid() {
|
if !project.IsValid() {
|
||||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "Name is required")
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "Name is required")
|
||||||
}
|
}
|
||||||
id, err := es.idGenerator.NextID()
|
id, err := es.idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
project.AggregateID = strconv.FormatUint(id, 10)
|
project.AggregateID = id
|
||||||
project.State = proj_model.PROJECTSTATE_ACTIVE
|
project.State = proj_model.PROJECTSTATE_ACTIVE
|
||||||
repoProject := model.ProjectFromModel(project)
|
repoProject := model.ProjectFromModel(project)
|
||||||
|
|
||||||
@ -331,16 +327,16 @@ func (es *ProjectEventstore) AddApplication(ctx context.Context, app *proj_model
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
id, err := es.idGenerator.NextID()
|
id, err := es.idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
app.AppID = strconv.FormatUint(id, 10)
|
app.AppID = id
|
||||||
|
|
||||||
var stringPw string
|
var stringPw string
|
||||||
var cryptoPw *crypto.CryptoValue
|
var cryptoPw *crypto.CryptoValue
|
||||||
if app.OIDCConfig != nil {
|
if app.OIDCConfig != nil {
|
||||||
app.OIDCConfig.AppID = strconv.FormatUint(id, 10)
|
app.OIDCConfig.AppID = id
|
||||||
stringPw, cryptoPw, err = generateNewClientSecret(es.pwGenerator)
|
stringPw, cryptoPw, err = generateNewClientSecret(es.pwGenerator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -567,11 +563,11 @@ func (es *ProjectEventstore) AddProjectGrant(ctx context.Context, grant *proj_mo
|
|||||||
if !existing.ContainsRoles(grant.RoleKeys) {
|
if !existing.ContainsRoles(grant.RoleKeys) {
|
||||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-di83d", "One role doesnt exist in Project")
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-di83d", "One role doesnt exist in Project")
|
||||||
}
|
}
|
||||||
id, err := es.idGenerator.NextID()
|
id, err := es.idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
grant.GrantID = strconv.FormatUint(id, 10)
|
grant.GrantID = id
|
||||||
|
|
||||||
repoProject := model.ProjectFromModel(existing)
|
repoProject := model.ProjectFromModel(existing)
|
||||||
repoGrant := model.GrantFromModel(grant)
|
repoGrant := model.GrantFromModel(grant)
|
||||||
|
@ -6,10 +6,10 @@ import (
|
|||||||
"github.com/caos/zitadel/internal/crypto"
|
"github.com/caos/zitadel/internal/crypto"
|
||||||
"github.com/caos/zitadel/internal/eventstore/mock"
|
"github.com/caos/zitadel/internal/eventstore/mock"
|
||||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||||
|
"github.com/caos/zitadel/internal/id"
|
||||||
proj_model "github.com/caos/zitadel/internal/project/model"
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
||||||
"github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
"github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/sony/sonyflake"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *ProjectEventstore {
|
func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *ProjectEventstore {
|
||||||
@ -35,8 +35,8 @@ func GetMockCache(ctrl *gomock.Controller) *ProjectCache {
|
|||||||
return &ProjectCache{projectCache: mockCache}
|
return &ProjectCache{projectCache: mockCache}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSonyFlacke() *sonyflake.Sonyflake {
|
func GetSonyFlacke() id.Generator {
|
||||||
return sonyflake.NewSonyflake(sonyflake.Settings{})
|
return id.SonyFlakeGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMockPwGenerator(ctrl *gomock.Controller) crypto.Generator {
|
func GetMockPwGenerator(ctrl *gomock.Controller) crypto.Generator {
|
||||||
|
@ -5,14 +5,14 @@ import (
|
|||||||
"github.com/caos/logging"
|
"github.com/caos/logging"
|
||||||
"github.com/caos/zitadel/internal/crypto"
|
"github.com/caos/zitadel/internal/crypto"
|
||||||
"github.com/caos/zitadel/internal/errors"
|
"github.com/caos/zitadel/internal/errors"
|
||||||
|
"github.com/caos/zitadel/internal/id"
|
||||||
"github.com/caos/zitadel/internal/project/model"
|
"github.com/caos/zitadel/internal/project/model"
|
||||||
"github.com/sony/sonyflake"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
//ClientID random_number@projectname (eg. 495894098234@zitadel)
|
//ClientID random_number@projectname (eg. 495894098234@zitadel)
|
||||||
func generateNewClientID(idGenerator *sonyflake.Sonyflake, project *model.Project) (string, error) {
|
func generateNewClientID(idGenerator id.Generator, project *model.Project) (string, error) {
|
||||||
rndID, err := idGenerator.NextID()
|
rndID, err := idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,8 @@ package eventsourcing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strconv"
|
"github.com/caos/zitadel/internal/id"
|
||||||
|
|
||||||
"github.com/pquerna/otp/totp"
|
"github.com/pquerna/otp/totp"
|
||||||
"github.com/sony/sonyflake"
|
|
||||||
|
|
||||||
req_model "github.com/caos/zitadel/internal/auth_request/model"
|
req_model "github.com/caos/zitadel/internal/auth_request/model"
|
||||||
"github.com/caos/zitadel/internal/cache/config"
|
"github.com/caos/zitadel/internal/cache/config"
|
||||||
@ -23,7 +21,7 @@ import (
|
|||||||
type UserEventstore struct {
|
type UserEventstore struct {
|
||||||
es_int.Eventstore
|
es_int.Eventstore
|
||||||
userCache *UserCache
|
userCache *UserCache
|
||||||
idGenerator *sonyflake.Sonyflake
|
idGenerator id.Generator
|
||||||
PasswordAlg crypto.HashAlgorithm
|
PasswordAlg crypto.HashAlgorithm
|
||||||
InitializeUserCode crypto.Generator
|
InitializeUserCode crypto.Generator
|
||||||
EmailVerificationCode crypto.Generator
|
EmailVerificationCode crypto.Generator
|
||||||
@ -44,7 +42,6 @@ func StartUser(conf UserConfig, systemDefaults sd.SystemDefaults) (*UserEventsto
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
idGenerator := sonyflake.NewSonyflake(sonyflake.Settings{})
|
|
||||||
aesCrypto, err := crypto.NewAESCrypto(systemDefaults.UserVerificationKey)
|
aesCrypto, err := crypto.NewAESCrypto(systemDefaults.UserVerificationKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -59,7 +56,7 @@ func StartUser(conf UserConfig, systemDefaults sd.SystemDefaults) (*UserEventsto
|
|||||||
return &UserEventstore{
|
return &UserEventstore{
|
||||||
Eventstore: conf.Eventstore,
|
Eventstore: conf.Eventstore,
|
||||||
userCache: userCache,
|
userCache: userCache,
|
||||||
idGenerator: idGenerator,
|
idGenerator: id.SonyFlakeGenerator,
|
||||||
InitializeUserCode: initCodeGen,
|
InitializeUserCode: initCodeGen,
|
||||||
EmailVerificationCode: emailVerificationCode,
|
EmailVerificationCode: emailVerificationCode,
|
||||||
PhoneVerificationCode: phoneVerificationCode,
|
PhoneVerificationCode: phoneVerificationCode,
|
||||||
@ -95,11 +92,13 @@ func (es *UserEventstore) PrepareCreateUser(ctx context.Context, user *usr_model
|
|||||||
if !user.IsValid() {
|
if !user.IsValid() {
|
||||||
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "User is invalid")
|
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "User is invalid")
|
||||||
}
|
}
|
||||||
id, err := es.idGenerator.NextID()
|
|
||||||
|
//TODO: Check Uniqueness
|
||||||
|
id, err := es.idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
user.AggregateID = strconv.FormatUint(id, 10)
|
user.AggregateID = id
|
||||||
|
|
||||||
err = user.HashPasswordIfExisting(es.PasswordAlg, true)
|
err = user.HashPasswordIfExisting(es.PasswordAlg, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -143,11 +142,12 @@ func (es *UserEventstore) PrepareRegisterUser(ctx context.Context, user *usr_mod
|
|||||||
if !user.IsValid() || user.Password == nil || user.SecretString == "" {
|
if !user.IsValid() || user.Password == nil || user.SecretString == "" {
|
||||||
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "user is invalid")
|
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "user is invalid")
|
||||||
}
|
}
|
||||||
id, err := es.idGenerator.NextID()
|
//TODO: Check Uniqueness
|
||||||
|
id, err := es.idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
user.AggregateID = strconv.FormatUint(id, 10)
|
user.AggregateID = id
|
||||||
|
|
||||||
err = user.HashPasswordIfExisting(es.PasswordAlg, false)
|
err = user.HashPasswordIfExisting(es.PasswordAlg, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -2,17 +2,16 @@ package eventsourcing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/caos/zitadel/internal/id"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/mock/gomock"
|
|
||||||
"github.com/sony/sonyflake"
|
|
||||||
|
|
||||||
mock_cache "github.com/caos/zitadel/internal/cache/mock"
|
mock_cache "github.com/caos/zitadel/internal/cache/mock"
|
||||||
"github.com/caos/zitadel/internal/crypto"
|
"github.com/caos/zitadel/internal/crypto"
|
||||||
"github.com/caos/zitadel/internal/eventstore/mock"
|
"github.com/caos/zitadel/internal/eventstore/mock"
|
||||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||||
global_model "github.com/caos/zitadel/internal/model"
|
global_model "github.com/caos/zitadel/internal/model"
|
||||||
"github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
"github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
||||||
|
"github.com/golang/mock/gomock"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *UserEventstore {
|
func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *UserEventstore {
|
||||||
@ -52,8 +51,8 @@ func GetMockCache(ctrl *gomock.Controller) *UserCache {
|
|||||||
return &UserCache{userCache: mockCache}
|
return &UserCache{userCache: mockCache}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSonyFlacke() *sonyflake.Sonyflake {
|
func GetSonyFlacke() id.Generator {
|
||||||
return sonyflake.NewSonyflake(sonyflake.Settings{})
|
return id.SonyFlakeGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMockPwGenerator(ctrl *gomock.Controller) crypto.Generator {
|
func GetMockPwGenerator(ctrl *gomock.Controller) crypto.Generator {
|
||||||
|
@ -11,7 +11,6 @@ const (
|
|||||||
UserRegistered models.EventType = "user.selfregistered"
|
UserRegistered models.EventType = "user.selfregistered"
|
||||||
InitializedUserCodeAdded models.EventType = "user.initialization.code.added"
|
InitializedUserCodeAdded models.EventType = "user.initialization.code.added"
|
||||||
InitializedUserCodeSent models.EventType = "user.initialization.code.sent"
|
InitializedUserCodeSent models.EventType = "user.initialization.code.sent"
|
||||||
UserRemoved models.EventType = "user.removed"
|
|
||||||
|
|
||||||
UserUserNameReserved models.EventType = "user.username.reserved"
|
UserUserNameReserved models.EventType = "user.username.reserved"
|
||||||
UserUserNameReleased models.EventType = "user.username.released"
|
UserUserNameReleased models.EventType = "user.username.released"
|
||||||
|
@ -7,16 +7,15 @@ import (
|
|||||||
es_int "github.com/caos/zitadel/internal/eventstore"
|
es_int "github.com/caos/zitadel/internal/eventstore"
|
||||||
"github.com/caos/zitadel/internal/eventstore/models"
|
"github.com/caos/zitadel/internal/eventstore/models"
|
||||||
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
||||||
|
"github.com/caos/zitadel/internal/id"
|
||||||
grant_model "github.com/caos/zitadel/internal/usergrant/model"
|
grant_model "github.com/caos/zitadel/internal/usergrant/model"
|
||||||
"github.com/caos/zitadel/internal/usergrant/repository/eventsourcing/model"
|
"github.com/caos/zitadel/internal/usergrant/repository/eventsourcing/model"
|
||||||
"github.com/sony/sonyflake"
|
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserGrantEventStore struct {
|
type UserGrantEventStore struct {
|
||||||
es_int.Eventstore
|
es_int.Eventstore
|
||||||
userGrantCache *UserGrantCache
|
userGrantCache *UserGrantCache
|
||||||
idGenerator *sonyflake.Sonyflake
|
idGenerator id.Generator
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserGrantConfig struct {
|
type UserGrantConfig struct {
|
||||||
@ -29,11 +28,10 @@ func StartUserGrant(conf UserGrantConfig) (*UserGrantEventStore, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
idGenerator := sonyflake.NewSonyflake(sonyflake.Settings{})
|
|
||||||
return &UserGrantEventStore{
|
return &UserGrantEventStore{
|
||||||
Eventstore: conf.Eventstore,
|
Eventstore: conf.Eventstore,
|
||||||
userGrantCache: userGrantCache,
|
userGrantCache: userGrantCache,
|
||||||
idGenerator: idGenerator,
|
idGenerator: id.SonyFlakeGenerator,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,11 +57,12 @@ func (es *UserGrantEventStore) AddUserGrant(ctx context.Context, grant *grant_mo
|
|||||||
if grant == nil || !grant.IsValid() {
|
if grant == nil || !grant.IsValid() {
|
||||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-sdiw3", "User grant invalid")
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-sdiw3", "User grant invalid")
|
||||||
}
|
}
|
||||||
id, err := es.idGenerator.NextID()
|
//TODO: Check Uniqueness
|
||||||
|
id, err := es.idGenerator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
grant.AggregateID = strconv.FormatUint(id, 10)
|
grant.AggregateID = id
|
||||||
|
|
||||||
repoGrant := model.UserGrantFromModel(grant)
|
repoGrant := model.UserGrantFromModel(grant)
|
||||||
|
|
||||||
|
@ -5,9 +5,9 @@ import (
|
|||||||
mock_cache "github.com/caos/zitadel/internal/cache/mock"
|
mock_cache "github.com/caos/zitadel/internal/cache/mock"
|
||||||
"github.com/caos/zitadel/internal/eventstore/mock"
|
"github.com/caos/zitadel/internal/eventstore/mock"
|
||||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||||
|
"github.com/caos/zitadel/internal/id"
|
||||||
"github.com/caos/zitadel/internal/usergrant/repository/eventsourcing/model"
|
"github.com/caos/zitadel/internal/usergrant/repository/eventsourcing/model"
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/sony/sonyflake"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *UserGrantEventStore {
|
func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *UserGrantEventStore {
|
||||||
@ -25,8 +25,8 @@ func GetMockCache(ctrl *gomock.Controller) *UserGrantCache {
|
|||||||
return &UserGrantCache{userGrantCache: mockCache}
|
return &UserGrantCache{userGrantCache: mockCache}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSonyFlacke() *sonyflake.Sonyflake {
|
func GetSonyFlacke() id.Generator {
|
||||||
return sonyflake.NewSonyflake(sonyflake.Settings{})
|
return id.SonyFlakeGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMockUserGrantByIDOK(ctrl *gomock.Controller) *UserGrantEventStore {
|
func GetMockUserGrantByIDOK(ctrl *gomock.Controller) *UserGrantEventStore {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user