fix: commandside queries (#1313)

* fix: move user by id to query side

* fix: move get passwordless to query side

# Conflicts:
#	internal/user/repository/eventsourcing/eventstore.go

* fix: move get passwordless to query side

* remove user eventstore

* remove unused models

* org changes

* org changes

* fix: move org queries to query side

* fix: remove org eventstore

* fix: remove org eventstore

* fix: remove org eventstore

* remove project from es v1

* project cleanup

* project cleanup

* fix: remove org eventstore

* fix: remove iam eventstore

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
This commit is contained in:
Fabi
2021-02-22 14:08:47 +01:00
committed by GitHub
parent 2ba56595b1
commit 428ef4acdb
106 changed files with 2301 additions and 2799 deletions

View File

@@ -21,22 +21,6 @@ type OTPVerified struct {
UserAgentID string `json:"userAgentID,omitempty"`
}
func OTPFromModel(otp *model.OTP) *OTP {
return &OTP{
ObjectRoot: otp.ObjectRoot,
Secret: otp.Secret,
State: int32(otp.State),
}
}
func OTPToModel(otp *OTP) *model.OTP {
return &model.OTP{
ObjectRoot: otp.ObjectRoot,
Secret: otp.Secret,
State: model.MFAState(otp.State),
}
}
func (u *Human) appendOTPAddedEvent(event *es_models.Event) error {
u.OTP = &OTP{
State: int32(model.MFAStateNotReady),

View File

@@ -24,42 +24,6 @@ type User struct {
*Machine
}
func UserFromModel(user *model.User) *User {
var human *Human
if user.Human != nil {
human = HumanFromModel(user.Human)
}
var machine *Machine
if user.Machine != nil {
machine = MachineFromModel(user.Machine)
}
return &User{
ObjectRoot: user.ObjectRoot,
State: int32(user.State),
UserName: user.UserName,
Human: human,
Machine: machine,
}
}
func UserToModel(user *User) *model.User {
var human *model.Human
if user.Human != nil {
human = HumanToModel(user.Human)
}
var machine *model.Machine
if user.Machine != nil {
machine = MachineToModel(user.Machine)
}
return &model.User{
ObjectRoot: user.ObjectRoot,
State: model.UserState(user.State),
UserName: user.UserName,
Human: human,
Machine: machine,
}
}
func (u *User) AppendEvents(events ...*es_models.Event) error {
for _, event := range events {
if err := u.AppendEvent(event); err != nil {

View File

@@ -37,113 +37,6 @@ type InitUserCode struct {
Expiry time.Duration `json:"expiry,omitempty"`
}
func HumanFromModel(user *model.Human) *Human {
human := new(Human)
if user.Password != nil {
human.Password = PasswordFromModel(user.Password)
}
if user.Profile != nil {
human.Profile = ProfileFromModel(user.Profile)
}
if user.Email != nil {
human.Email = EmailFromModel(user.Email)
}
if user.Phone != nil {
human.Phone = PhoneFromModel(user.Phone)
}
if user.Address != nil {
human.Address = AddressFromModel(user.Address)
}
if user.OTP != nil {
human.OTP = OTPFromModel(user.OTP)
}
if user.ExternalIDPs != nil {
human.ExternalIDPs = ExternalIDPsFromModel(user.ExternalIDPs)
}
if user.U2FTokens != nil {
human.U2FTokens = WebAuthNsFromModel(user.U2FTokens)
}
if user.PasswordlessTokens != nil {
human.PasswordlessTokens = WebAuthNsFromModel(user.PasswordlessTokens)
}
if user.U2FLogins != nil {
human.U2FLogins = WebAuthNLoginsFromModel(user.U2FLogins)
}
if user.PasswordlessLogins != nil {
human.PasswordlessLogins = WebAuthNLoginsFromModel(user.PasswordlessLogins)
}
return human
}
func HumanToModel(user *Human) *model.Human {
human := new(model.Human)
if user.Password != nil {
human.Password = PasswordToModel(user.Password)
}
if user.Profile != nil {
human.Profile = ProfileToModel(user.Profile)
}
if user.Email != nil {
human.Email = EmailToModel(user.Email)
}
if user.Phone != nil {
human.Phone = PhoneToModel(user.Phone)
}
if user.Address != nil {
human.Address = AddressToModel(user.Address)
}
if user.ExternalIDPs != nil {
human.ExternalIDPs = ExternalIDPsToModel(user.ExternalIDPs)
}
if user.InitCode != nil {
human.InitCode = InitCodeToModel(user.InitCode)
}
if user.EmailCode != nil {
human.EmailCode = EmailCodeToModel(user.EmailCode)
}
if user.PhoneCode != nil {
human.PhoneCode = PhoneCodeToModel(user.PhoneCode)
}
if user.PasswordCode != nil {
human.PasswordCode = PasswordCodeToModel(user.PasswordCode)
}
if user.OTP != nil {
human.OTP = OTPToModel(user.OTP)
}
if user.U2FTokens != nil {
human.U2FTokens = WebAuthNsToModel(user.U2FTokens)
}
if user.PasswordlessTokens != nil {
human.PasswordlessTokens = WebAuthNsToModel(user.PasswordlessTokens)
}
if user.U2FLogins != nil {
human.U2FLogins = WebAuthNLoginsToModel(user.U2FLogins)
}
if user.PasswordlessLogins != nil {
human.PasswordlessLogins = WebAuthNLoginsToModel(user.PasswordlessLogins)
}
return human
}
func InitCodeFromModel(code *model.InitUserCode) *InitUserCode {
if code == nil {
return nil
}
return &InitUserCode{
ObjectRoot: code.ObjectRoot,
Expiry: code.Expiry,
Code: code.Code,
}
}
func InitCodeToModel(code *InitUserCode) *model.InitUserCode {
return &model.InitUserCode{
ObjectRoot: code.ObjectRoot,
Expiry: code.Expiry,
Code: code.Code,
}
}
func (p *Human) AppendEvents(events ...*es_models.Event) error {
for _, event := range events {
if err := p.AppendEvent(event); err != nil {

View File

@@ -1,44 +0,0 @@
package model
import (
"encoding/json"
"testing"
"time"
es_models "github.com/caos/zitadel/internal/eventstore/models"
)
func TestAppendInitUserCodeEvent(t *testing.T) {
type args struct {
user *Human
code *InitUserCode
event *es_models.Event
}
tests := []struct {
name string
args args
result *Human
}{
{
name: "append init user code event",
args: args{
user: &Human{},
code: &InitUserCode{Expiry: time.Hour * 30},
event: &es_models.Event{},
},
result: &Human{InitCode: &InitUserCode{Expiry: time.Hour * 30}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.code != nil {
data, _ := json.Marshal(tt.args.code)
tt.args.event.Data = data
}
tt.args.user.appendInitUsercodeCreatedEvent(tt.args.event)
if tt.args.user.InitCode.Expiry != tt.result.InitCode.Expiry {
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result, tt.args.user)
}
})
}
}

View File

@@ -5,12 +5,9 @@ import (
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/models"
es_models "github.com/caos/zitadel/internal/eventstore/models"
key_model "github.com/caos/zitadel/internal/key/model"
"github.com/caos/zitadel/internal/user/model"
)
type Machine struct {
@@ -46,28 +43,6 @@ func (sa *Machine) setData(event *models.Event) error {
return nil
}
func (sa *Machine) Changes(updatedAccount *Machine) map[string]interface{} {
changes := make(map[string]interface{})
if updatedAccount.Description != "" && updatedAccount.Description != sa.Description {
changes["description"] = updatedAccount.Description
}
return changes
}
func MachineFromModel(machine *model.Machine) *Machine {
return &Machine{
Description: machine.Description,
Name: machine.Name,
}
}
func MachineToModel(machine *Machine) *model.Machine {
return &model.Machine{
Description: machine.Description,
Name: machine.Name,
}
}
type MachineKey struct {
es_models.ObjectRoot `json:"-"`
KeyID string `json:"keyId,omitempty"`
@@ -100,35 +75,3 @@ func (key *MachineKey) AppendEvent(event *es_models.Event) (err error) {
}
return err
}
func MachineKeyFromModel(machine *model.MachineKey) *MachineKey {
return &MachineKey{
ObjectRoot: machine.ObjectRoot,
ExpirationDate: machine.ExpirationDate,
KeyID: machine.KeyID,
Type: int32(machine.Type),
}
}
func MachineKeyToModel(machine *MachineKey) *model.MachineKey {
return &model.MachineKey{
ObjectRoot: machine.ObjectRoot,
ExpirationDate: machine.ExpirationDate,
KeyID: machine.KeyID,
PrivateKey: machine.privateKey,
Type: key_model.AuthNKeyType(machine.Type),
}
}
func (key *MachineKey) GenerateMachineKeyPair(keySize int, alg crypto.EncryptionAlgorithm) error {
privateKey, publicKey, err := crypto.GenerateKeyPair(keySize)
if err != nil {
return err
}
key.PublicKey, err = crypto.PublicKeyToBytes(publicKey)
if err != nil {
return err
}
key.privateKey = crypto.PrivateKeyToBytes(privateKey)
return nil
}

View File

@@ -1,115 +0,0 @@
package model
import (
"testing"
"github.com/caos/zitadel/internal/user/model"
)
func TestAppendDeactivatedEvent(t *testing.T) {
type args struct {
user *User
}
tests := []struct {
name string
args args
result *User
}{
{
name: "append deactivate event",
args: args{
user: &User{},
},
result: &User{State: int32(model.UserStateInactive)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.user.appendDeactivatedEvent()
if tt.args.user.State != tt.result.State {
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result, tt.args.user)
}
})
}
}
func TestAppendReactivatedEvent(t *testing.T) {
type args struct {
user *User
}
tests := []struct {
name string
args args
result *User
}{
{
name: "append reactivate event",
args: args{
user: &User{},
},
result: &User{State: int32(model.UserStateActive)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.user.appendReactivatedEvent()
if tt.args.user.State != tt.result.State {
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result, tt.args.user)
}
})
}
}
func TestAppendLockEvent(t *testing.T) {
type args struct {
user *User
}
tests := []struct {
name string
args args
result *User
}{
{
name: "append lock event",
args: args{
user: &User{},
},
result: &User{State: int32(model.UserStateLocked)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.user.appendLockedEvent()
if tt.args.user.State != tt.result.State {
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result, tt.args.user)
}
})
}
}
func TestAppendUnlockEvent(t *testing.T) {
type args struct {
user *User
}
tests := []struct {
name string
args args
result *User
}{
{
name: "append unlock event",
args: args{
user: &User{},
},
result: &User{State: int32(model.UserStateActive)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.user.appendUnlockedEvent()
if tt.args.user.State != tt.result.State {
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result, tt.args.user)
}
})
}
}

View File

@@ -62,97 +62,6 @@ func GetWebauthn(webauthnTokens []*WebAuthNToken, id string) (int, *WebAuthNToke
return -1, nil
}
func WebAuthNsToModel(u2fs []*WebAuthNToken) []*model.WebAuthNToken {
convertedIDPs := make([]*model.WebAuthNToken, len(u2fs))
for i, m := range u2fs {
convertedIDPs[i] = WebAuthNToModel(m)
}
return convertedIDPs
}
func WebAuthNsFromModel(u2fs []*model.WebAuthNToken) []*WebAuthNToken {
convertedIDPs := make([]*WebAuthNToken, len(u2fs))
for i, m := range u2fs {
convertedIDPs[i] = WebAuthNFromModel(m)
}
return convertedIDPs
}
func WebAuthNFromModel(webAuthN *model.WebAuthNToken) *WebAuthNToken {
return &WebAuthNToken{
ObjectRoot: webAuthN.ObjectRoot,
WebauthNTokenID: webAuthN.WebAuthNTokenID,
Challenge: webAuthN.Challenge,
State: int32(webAuthN.State),
KeyID: webAuthN.KeyID,
PublicKey: webAuthN.PublicKey,
AAGUID: webAuthN.AAGUID,
SignCount: webAuthN.SignCount,
AttestationType: webAuthN.AttestationType,
WebAuthNTokenName: webAuthN.WebAuthNTokenName,
}
}
func WebAuthNToModel(webAuthN *WebAuthNToken) *model.WebAuthNToken {
return &model.WebAuthNToken{
ObjectRoot: webAuthN.ObjectRoot,
WebAuthNTokenID: webAuthN.WebauthNTokenID,
Challenge: webAuthN.Challenge,
State: model.MFAState(webAuthN.State),
KeyID: webAuthN.KeyID,
PublicKey: webAuthN.PublicKey,
AAGUID: webAuthN.AAGUID,
SignCount: webAuthN.SignCount,
AttestationType: webAuthN.AttestationType,
WebAuthNTokenName: webAuthN.WebAuthNTokenName,
}
}
func WebAuthNVerifyFromModel(webAuthN *model.WebAuthNToken, userAgentID string) *WebAuthNVerify {
return &WebAuthNVerify{
WebAuthNTokenID: webAuthN.WebAuthNTokenID,
KeyID: webAuthN.KeyID,
PublicKey: webAuthN.PublicKey,
AAGUID: webAuthN.AAGUID,
SignCount: webAuthN.SignCount,
AttestationType: webAuthN.AttestationType,
WebAuthNTokenName: webAuthN.WebAuthNTokenName,
UserAgentID: userAgentID,
}
}
func WebAuthNLoginsToModel(u2fs []*WebAuthNLogin) []*model.WebAuthNLogin {
convertedIDPs := make([]*model.WebAuthNLogin, len(u2fs))
for i, m := range u2fs {
convertedIDPs[i] = WebAuthNLoginToModel(m)
}
return convertedIDPs
}
func WebAuthNLoginsFromModel(u2fs []*model.WebAuthNLogin) []*WebAuthNLogin {
convertedIDPs := make([]*WebAuthNLogin, len(u2fs))
for i, m := range u2fs {
convertedIDPs[i] = WebAuthNLoginFromModel(m)
}
return convertedIDPs
}
func WebAuthNLoginFromModel(webAuthN *model.WebAuthNLogin) *WebAuthNLogin {
return &WebAuthNLogin{
ObjectRoot: webAuthN.ObjectRoot,
Challenge: webAuthN.Challenge,
AuthRequest: AuthRequestFromModel(webAuthN.AuthRequest),
}
}
func WebAuthNLoginToModel(webAuthN *WebAuthNLogin) *model.WebAuthNLogin {
return &model.WebAuthNLogin{
ObjectRoot: webAuthN.ObjectRoot,
Challenge: webAuthN.Challenge,
AuthRequest: AuthRequestToModel(webAuthN.AuthRequest),
}
}
func (w *WebAuthNVerify) SetData(event *es_models.Event) error {
if err := json.Unmarshal(event.Data, w); err != nil {
logging.Log("EVEN-G342rf").WithError(err).Error("could not unmarshal event data")