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:
Fabi
2020-05-28 13:28:36 +02:00
committed by GitHub
parent 2758bf30b1
commit e3f9e9c05e
18 changed files with 69 additions and 76 deletions

View File

@@ -2,10 +2,8 @@ package eventsourcing
import (
"context"
"strconv"
"github.com/caos/zitadel/internal/id"
"github.com/pquerna/otp/totp"
"github.com/sony/sonyflake"
req_model "github.com/caos/zitadel/internal/auth_request/model"
"github.com/caos/zitadel/internal/cache/config"
@@ -23,7 +21,7 @@ import (
type UserEventstore struct {
es_int.Eventstore
userCache *UserCache
idGenerator *sonyflake.Sonyflake
idGenerator id.Generator
PasswordAlg crypto.HashAlgorithm
InitializeUserCode crypto.Generator
EmailVerificationCode crypto.Generator
@@ -44,7 +42,6 @@ func StartUser(conf UserConfig, systemDefaults sd.SystemDefaults) (*UserEventsto
if err != nil {
return nil, err
}
idGenerator := sonyflake.NewSonyflake(sonyflake.Settings{})
aesCrypto, err := crypto.NewAESCrypto(systemDefaults.UserVerificationKey)
if err != nil {
return nil, err
@@ -59,7 +56,7 @@ func StartUser(conf UserConfig, systemDefaults sd.SystemDefaults) (*UserEventsto
return &UserEventstore{
Eventstore: conf.Eventstore,
userCache: userCache,
idGenerator: idGenerator,
idGenerator: id.SonyFlakeGenerator,
InitializeUserCode: initCodeGen,
EmailVerificationCode: emailVerificationCode,
PhoneVerificationCode: phoneVerificationCode,
@@ -95,11 +92,13 @@ func (es *UserEventstore) PrepareCreateUser(ctx context.Context, user *usr_model
if !user.IsValid() {
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 {
return nil, nil, err
}
user.AggregateID = strconv.FormatUint(id, 10)
user.AggregateID = id
err = user.HashPasswordIfExisting(es.PasswordAlg, true)
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 == "" {
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 {
return nil, nil, err
}
user.AggregateID = strconv.FormatUint(id, 10)
user.AggregateID = id
err = user.HashPasswordIfExisting(es.PasswordAlg, false)
if err != nil {

View File

@@ -2,17 +2,16 @@ package eventsourcing
import (
"encoding/json"
"github.com/caos/zitadel/internal/id"
"time"
"github.com/golang/mock/gomock"
"github.com/sony/sonyflake"
mock_cache "github.com/caos/zitadel/internal/cache/mock"
"github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/eventstore/mock"
es_models "github.com/caos/zitadel/internal/eventstore/models"
global_model "github.com/caos/zitadel/internal/model"
"github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
"github.com/golang/mock/gomock"
)
func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *UserEventstore {
@@ -52,8 +51,8 @@ func GetMockCache(ctrl *gomock.Controller) *UserCache {
return &UserCache{userCache: mockCache}
}
func GetSonyFlacke() *sonyflake.Sonyflake {
return sonyflake.NewSonyflake(sonyflake.Settings{})
func GetSonyFlacke() id.Generator {
return id.SonyFlakeGenerator
}
func GetMockPwGenerator(ctrl *gomock.Controller) crypto.Generator {

View File

@@ -11,7 +11,6 @@ const (
UserRegistered models.EventType = "user.selfregistered"
InitializedUserCodeAdded models.EventType = "user.initialization.code.added"
InitializedUserCodeSent models.EventType = "user.initialization.code.sent"
UserRemoved models.EventType = "user.removed"
UserUserNameReserved models.EventType = "user.username.reserved"
UserUserNameReleased models.EventType = "user.username.released"