feat: split users into human and machine (#470)

* feat(management): service accounts

* chore: current go version

* init

* refactor: apis

* feat(internal): start impl of service account

* chore: start impl of machine/human users

* code compiles

* fix: tests

* fix: tests

* fix: add new event types to switches

* chore: add cases to event types

* fix(management): definitive proto messages

* fix: machine/human

* fix: add missing tables as todos

* fix: remove unused permissions

* fix: refactoring

* fix: refactor

* fix: human registered

* fix: user id

* fix: logid

* fix: proto remove //equal

* chore(management): remove no comment

* fix: human mfas

* fix: user subobjects

* chore: rename existing to better name

* fix: username in user (#634)

* fix: username in user

* fix: username

* fix remove unused code

* fix add validations

* fix: use new user in all apis

* fix: regexp for username in api

* fix: fill user data for human and machine (#638)

* fix: fill Display name grant/member handlers
fix: add description to grant/member objects in api
fix: check if user is human in login

* fix: remove description from member and grant

* chore: remove todos

* feat: machine keys

* fix: implement missing parts

* feat: machine key management view

* fix: remove keys from machine view

* fix: set default expiration date

* fix: get key by ids

* feat: add machine keys in proto

* feat: machine keys

* fix: add migration

* fix: mig

* fix: correct method name

* feat: user search

* feat: user search

* fix: log ids

* fix partial authconfig prompt, domain c perm

* membership read check

* contributor refresh trigger, observe org write

* fix: migrations

* fix(console): machine build (#660)

* frontend 1

* fix html bindings

* trailing comma

* user permissions, project deactivate

* fix(console): human view (#661)

* fix search user view, user detail form

* rm log

* feat(console): user services list and create (#663)

* fix search user view, user detail form

* rm log

* machine list

* generic table component

* create user service

* proove table for undefined values

* tmp disable user link if machine

* lint

* lint styles

* user table lint

* Update console/src/assets/i18n/de.json

Co-authored-by: Florian Forster <florian@caos.ch>

* feat(console): service user detail view, keys cr_d, fix search user autocomplete (#664)

* service users for sidenav, routing

* i18n

* back routes

* machine detail form

* update machine detail, fix svc user grants

* keys table

* add key dialog, timestamp creation

* check permission on create, delete, fix selection

* lint ts, scss

* Update console/src/assets/i18n/de.json

* Apply suggestions from code review

Co-authored-by: Florian Forster <florian@caos.ch>

* allow user grants for project.write

* management service

* fix mgmt service

* feat: Machine keys (#655)

* fix: memberships (#633)

* feat: add iam members to memberships

* fix: search project grants

* fix: rename

* feat: idp and login policy configurations (#619)

* feat: oidc config

* fix: oidc configurations

* feat: oidc idp config

* feat: add oidc config test

* fix: tests

* fix: tests

* feat: translate new events

* feat: idp eventstore

* feat: idp eventstore

* fix: tests

* feat: command side idp

* feat: query side idp

* feat: idp config on org

* fix: tests

* feat: authz idp on org

* feat: org idps

* feat: login policy

* feat: login policy

* feat: login policy

* feat: add idp func on login policy

* feat: add validation to loginpolicy and idp provider

* feat: add default login policy

* feat: login policy on org

* feat: login policy on org

* fix: id config handlers

* fix: id config handlers

* fix: create idp on org

* fix: create idp on org

* fix: not existing idp config

* fix: default login policy

* fix: add login policy on org

* fix: idp provider search on org

* fix: test

* fix: remove idp on org

* fix: test

* fix: test

* fix: remove admin idp

* fix: logo src as byte

* fix: migration

* fix: tests

* Update internal/iam/repository/eventsourcing/iam.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/iam/repository/eventsourcing/iam_test.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/iam/repository/eventsourcing/iam_test.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/iam/repository/eventsourcing/model/login_policy.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/iam/repository/eventsourcing/model/login_policy.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/org/repository/eventsourcing/org_test.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/iam/repository/eventsourcing/model/login_policy_test.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/iam/repository/eventsourcing/model/login_policy_test.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* fix: pr comments

* fix: tests

* Update types.go

* fix: merge request changes

* fix: reduce optimization

Co-authored-by: Silvan <silvan.reusser@gmail.com>
Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix: reread user mfas, preferred loginname as otp account name (#636)

* fix: reread user mfas

* fix: use preferred login name as otp account name

* fix: tests

* fix: reduce (#635)

* fix: management reduce optimization

* fix: reduce optimization

* fix: reduce optimization

* fix: merge master

* chore(deps): bump github.com/gorilla/schema from 1.1.0 to 1.2.0 (#627)

Bumps [github.com/gorilla/schema](https://github.com/gorilla/schema) from 1.1.0 to 1.2.0.
- [Release notes](https://github.com/gorilla/schema/releases)
- [Commits](https://github.com/gorilla/schema/compare/v1.1.0...v1.2.0)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore(deps): bump github.com/gorilla/mux from 1.7.4 to 1.8.0 (#624)

Bumps [github.com/gorilla/mux](https://github.com/gorilla/mux) from 1.7.4 to 1.8.0.
- [Release notes](https://github.com/gorilla/mux/releases)
- [Commits](https://github.com/gorilla/mux/compare/v1.7.4...v1.8.0)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore(deps): bump github.com/DATA-DOG/go-sqlmock from 1.4.1 to 1.5.0 (#591)

Bumps [github.com/DATA-DOG/go-sqlmock](https://github.com/DATA-DOG/go-sqlmock) from 1.4.1 to 1.5.0.
- [Release notes](https://github.com/DATA-DOG/go-sqlmock/releases)
- [Commits](https://github.com/DATA-DOG/go-sqlmock/compare/v1.4.1...v1.5.0)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore: auto assign issues and PR to ZTIADEL project board (#643)

* Create main.yml

* Update main.yml

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix(console): project grant members, update deps (#645)

* fix: searchprojectgrantmembers

* chore(deps-dev): bump @angular/cli from 10.0.6 to 10.0.7 in /console (#622)

Bumps [@angular/cli](https://github.com/angular/angular-cli) from 10.0.6 to 10.0.7.
- [Release notes](https://github.com/angular/angular-cli/releases)
- [Commits](https://github.com/angular/angular-cli/compare/v10.0.6...v10.0.7)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore(deps-dev): bump @angular-devkit/build-angular in /console (#626)

Bumps [@angular-devkit/build-angular](https://github.com/angular/angular-cli) from 0.1000.6 to 0.1000.7.
- [Release notes](https://github.com/angular/angular-cli/releases)
- [Commits](https://github.com/angular/angular-cli/commits)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Max Peintner <max@caos.ch>

* chore(deps-dev): bump @types/jasmine from 3.5.12 to 3.5.13 in /console (#623)

Bumps [@types/jasmine](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jasmine) from 3.5.12 to 3.5.13.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jasmine)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore(deps-dev): bump ts-node from 8.10.2 to 9.0.0 in /console (#629)

Bumps [ts-node](https://github.com/TypeStrong/ts-node) from 8.10.2 to 9.0.0.
- [Release notes](https://github.com/TypeStrong/ts-node/releases)
- [Commits](https://github.com/TypeStrong/ts-node/compare/v8.10.2...v9.0.0)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* update packlock

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore: delete main.yml (#648)

* fix: usergrant (#650)

* fix(console): mfa refresh after verification, member eventemitter (#651)

* refresh mfa

* fix: detail link from contributors

* lint

* feat: add domain verification notification (#649)

* fix: dont (re)generate client secret with auth type none

* fix(cors): allow Origin from request

* feat: add origin allow list and fix some core issues

* rename migration

* fix UserIDsByDomain

* feat: send email to users after domain claim

* username

* check origin on userinfo

* update oidc pkg

* fix: add migration 1.6

* change username

* change username

* remove unique email aggregate

* change username in mgmt

* search global user by login name

* fix test

* change user search in angular

* fix tests

* merge

* userview in angular

* fix merge

* Update pkg/grpc/management/proto/management.proto

Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>

* Update internal/notification/static/i18n/de.yaml

Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>

* fix

Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>

* fix: translation (#647)

* fix: translation

* fix: translation

* fix: translation

* fix: remove unused code

* fix: log err

* fix: migration numbers (#652)

* chore: issue / feature templates (#642)

* feat: machine keys

* fix: implement missing parts

* feat: machine key management view

* fix: remove keys from machine view

* feat: global org read (#657)

* fix: set default expiration date

* fix: get key by ids

* feat: add machine keys in proto

* feat: machine keys

* fix: add migration

* fix: mig

* fix: correct method name

* feat: user search

* feat: user search

* fix: log ids

* fix: migrations

* fix(console): machine build (#660)

* frontend 1

* fix html bindings

* trailing comma

* fix(console): human view (#661)

* fix search user view, user detail form

* rm log

* feat(console): user services list and create (#663)

* fix search user view, user detail form

* rm log

* machine list

* generic table component

* create user service

* proove table for undefined values

* tmp disable user link if machine

* lint

* lint styles

* user table lint

* Update console/src/assets/i18n/de.json

Co-authored-by: Florian Forster <florian@caos.ch>

* feat(console): service user detail view, keys cr_d, fix search user autocomplete (#664)

* service users for sidenav, routing

* i18n

* back routes

* machine detail form

* update machine detail, fix svc user grants

* keys table

* add key dialog, timestamp creation

* check permission on create, delete, fix selection

* lint ts, scss

* Update console/src/assets/i18n/de.json

* Apply suggestions from code review

Co-authored-by: Florian Forster <florian@caos.ch>

* refactor: protos

* fix(management): key expiration date

* fix: check if user is human

* fix: marshal key details

* fix: correct generate login names

* fix: logid

Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Max Peintner <max@caos.ch>
Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com>
Co-authored-by: Florian Forster <florian@caos.ch>

* fix: naming

* refactor: findings

* fix: username

* fix: mfa upper case

* fix: tests

* fix: add translations

* reactivatemyorg req typeö

* fix: projectType for console

* fix: user changes

* fix: translate events

* fix: event type translation

* fix: remove unused types

Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com>
Co-authored-by: Max Peintner <max@caos.ch>
Co-authored-by: Florian Forster <florian@caos.ch>
Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
Silvan
2020-08-31 17:48:01 +02:00
committed by GitHub
parent c1c85e632b
commit 5abd5b0505
207 changed files with 52837 additions and 26745 deletions

View File

@@ -2,6 +2,7 @@ package model
import (
"encoding/json"
"github.com/caos/logging"
caos_errs "github.com/caos/zitadel/internal/errors"
es_models "github.com/caos/zitadel/internal/eventstore/models"
@@ -60,7 +61,7 @@ func AddressToModel(address *Address) *model.Address {
}
}
func (u *User) appendUserAddressChangedEvent(event *es_models.Event) error {
func (u *Human) appendUserAddressChangedEvent(event *es_models.Event) error {
if u.Address == nil {
u.Address = new(Address)
}

View File

@@ -2,14 +2,15 @@ package model
import (
"encoding/json"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"testing"
es_models "github.com/caos/zitadel/internal/eventstore/models"
)
func TestAddressChanges(t *testing.T) {
type args struct {
existing *Address
new *Address
existingAddress *Address
newAddress *Address
}
type res struct {
changesLen int
@@ -22,8 +23,8 @@ func TestAddressChanges(t *testing.T) {
{
name: "all fields changed",
args: args{
existing: &Address{Country: "Country", Locality: "Locality", PostalCode: "PostalCode", Region: "Region", StreetAddress: "StreetAddress"},
new: &Address{Country: "CountryChanged", Locality: "LocalityChanged", PostalCode: "PostalCodeChanged", Region: "RegionChanged", StreetAddress: "StreetAddressChanged"},
existingAddress: &Address{Country: "Country", Locality: "Locality", PostalCode: "PostalCode", Region: "Region", StreetAddress: "StreetAddress"},
newAddress: &Address{Country: "CountryChanged", Locality: "LocalityChanged", PostalCode: "PostalCodeChanged", Region: "RegionChanged", StreetAddress: "StreetAddressChanged"},
},
res: res{
changesLen: 5,
@@ -32,8 +33,8 @@ func TestAddressChanges(t *testing.T) {
{
name: "no fields changed",
args: args{
existing: &Address{Country: "Country", Locality: "Locality", PostalCode: "PostalCode", Region: "Region", StreetAddress: "StreetAddress"},
new: &Address{Country: "Country", Locality: "Locality", PostalCode: "PostalCode", Region: "Region", StreetAddress: "StreetAddress"},
existingAddress: &Address{Country: "Country", Locality: "Locality", PostalCode: "PostalCode", Region: "Region", StreetAddress: "StreetAddress"},
newAddress: &Address{Country: "Country", Locality: "Locality", PostalCode: "PostalCode", Region: "Region", StreetAddress: "StreetAddress"},
},
res: res{
changesLen: 0,
@@ -42,7 +43,7 @@ func TestAddressChanges(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
changes := tt.args.existing.Changes(tt.args.new)
changes := tt.args.existingAddress.Changes(tt.args.newAddress)
if len(changes) != tt.res.changesLen {
t.Errorf("got wrong changes len: expected: %v, actual: %v ", tt.res.changesLen, len(changes))
}
@@ -52,23 +53,23 @@ func TestAddressChanges(t *testing.T) {
func TestAppendUserAddressChangedEvent(t *testing.T) {
type args struct {
user *User
user *Human
address *Address
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append user address event",
args: args{
user: &User{Address: &Address{Locality: "Locality", Country: "Country"}},
user: &Human{Address: &Address{Locality: "Locality", Country: "Country"}},
address: &Address{Locality: "LocalityChanged", PostalCode: "PostalCode"},
event: &es_models.Event{},
},
result: &User{Address: &Address{Locality: "LocalityChanged", Country: "Country", PostalCode: "PostalCode"}},
result: &Human{Address: &Address{Locality: "LocalityChanged", Country: "Country", PostalCode: "PostalCode"}},
},
}
for _, tt := range tests {

View File

@@ -2,12 +2,13 @@ package model
import (
"encoding/json"
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/crypto"
caos_errs "github.com/caos/zitadel/internal/errors"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/user/model"
"time"
)
type Email struct {
@@ -69,17 +70,17 @@ func EmailCodeToModel(code *EmailCode) *model.EmailCode {
}
}
func (u *User) appendUserEmailChangedEvent(event *es_models.Event) error {
func (u *Human) appendUserEmailChangedEvent(event *es_models.Event) error {
u.Email = new(Email)
return u.Email.setData(event)
}
func (u *User) appendUserEmailCodeAddedEvent(event *es_models.Event) error {
func (u *Human) appendUserEmailCodeAddedEvent(event *es_models.Event) error {
u.EmailCode = new(EmailCode)
return u.EmailCode.SetData(event)
}
func (u *User) appendUserEmailVerifiedEvent() {
func (u *Human) appendUserEmailVerifiedEvent() {
u.IsEmailVerified = true
}

View File

@@ -2,15 +2,16 @@ package model
import (
"encoding/json"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"testing"
"time"
es_models "github.com/caos/zitadel/internal/eventstore/models"
)
func TestEmailChanges(t *testing.T) {
type args struct {
existing *Email
new *Email
existingEmail *Email
new *Email
}
type res struct {
changesLen int
@@ -23,8 +24,8 @@ func TestEmailChanges(t *testing.T) {
{
name: "all fields changed",
args: args{
existing: &Email{EmailAddress: "Email", IsEmailVerified: true},
new: &Email{EmailAddress: "EmailChanged", IsEmailVerified: false},
existingEmail: &Email{EmailAddress: "Email", IsEmailVerified: true},
new: &Email{EmailAddress: "EmailChanged", IsEmailVerified: false},
},
res: res{
changesLen: 1,
@@ -33,8 +34,8 @@ func TestEmailChanges(t *testing.T) {
{
name: "no fields changed",
args: args{
existing: &Email{EmailAddress: "Email", IsEmailVerified: true},
new: &Email{EmailAddress: "Email", IsEmailVerified: false},
existingEmail: &Email{EmailAddress: "Email", IsEmailVerified: true},
new: &Email{EmailAddress: "Email", IsEmailVerified: false},
},
res: res{
changesLen: 0,
@@ -43,7 +44,7 @@ func TestEmailChanges(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
changes := tt.args.existing.Changes(tt.args.new)
changes := tt.args.existingEmail.Changes(tt.args.new)
if len(changes) != tt.res.changesLen {
t.Errorf("got wrong changes len: expected: %v, actual: %v ", tt.res.changesLen, len(changes))
}
@@ -53,23 +54,23 @@ func TestEmailChanges(t *testing.T) {
func TestAppendUserEmailChangedEvent(t *testing.T) {
type args struct {
user *User
user *Human
email *Email
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append user email event",
args: args{
user: &User{Email: &Email{EmailAddress: "EmailAddress"}},
user: &Human{Email: &Email{EmailAddress: "EmailAddress"}},
email: &Email{EmailAddress: "EmailAddressChanged"},
event: &es_models.Event{},
},
result: &User{Email: &Email{EmailAddress: "EmailAddressChanged"}},
result: &Human{Email: &Email{EmailAddress: "EmailAddressChanged"}},
},
}
for _, tt := range tests {
@@ -88,23 +89,23 @@ func TestAppendUserEmailChangedEvent(t *testing.T) {
func TestAppendUserEmailCodeAddedEvent(t *testing.T) {
type args struct {
user *User
user *Human
code *EmailCode
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append user email code added event",
args: args{
user: &User{Email: &Email{EmailAddress: "EmailAddress"}},
user: &Human{Email: &Email{EmailAddress: "EmailAddress"}},
code: &EmailCode{Expiry: time.Hour * 1},
event: &es_models.Event{},
},
result: &User{EmailCode: &EmailCode{Expiry: time.Hour * 1}},
result: &Human{EmailCode: &EmailCode{Expiry: time.Hour * 1}},
},
}
for _, tt := range tests {
@@ -123,21 +124,21 @@ func TestAppendUserEmailCodeAddedEvent(t *testing.T) {
func TestAppendUserEmailVerifiedEvent(t *testing.T) {
type args struct {
user *User
user *Human
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append user email event",
args: args{
user: &User{Email: &Email{EmailAddress: "EmailAddress"}},
user: &Human{Email: &Email{EmailAddress: "EmailAddress"}},
event: &es_models.Event{},
},
result: &User{Email: &Email{EmailAddress: "EmailAddress", IsEmailVerified: true}},
result: &Human{Email: &Email{EmailAddress: "EmailAddress", IsEmailVerified: true}},
},
}
for _, tt := range tests {

View File

@@ -2,6 +2,7 @@ package model
import (
"encoding/json"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/crypto"
caos_errs "github.com/caos/zitadel/internal/errors"
@@ -32,18 +33,18 @@ func OTPToModel(otp *OTP) *model.OTP {
}
}
func (u *User) appendOtpAddedEvent(event *es_models.Event) error {
func (u *Human) appendOTPAddedEvent(event *es_models.Event) error {
u.OTP = &OTP{
State: int32(model.MfaStateNotReady),
}
return u.OTP.setData(event)
}
func (u *User) appendOtpVerifiedEvent() {
func (u *Human) appendOTPVerifiedEvent() {
u.OTP.State = int32(model.MfaStateReady)
}
func (u *User) appendOtpRemovedEvent() {
func (u *Human) appendOTPRemovedEvent() {
u.OTP = nil
}

View File

@@ -2,31 +2,32 @@ package model
import (
"encoding/json"
"testing"
"github.com/caos/zitadel/internal/crypto"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/user/model"
"testing"
)
func TestAppendMfaOTPAddedEvent(t *testing.T) {
type args struct {
user *User
user *Human
otp *OTP
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append user otp event",
args: args{
user: &User{},
user: &Human{},
otp: &OTP{Secret: &crypto.CryptoValue{KeyID: "KeyID"}},
event: &es_models.Event{},
},
result: &User{OTP: &OTP{Secret: &crypto.CryptoValue{KeyID: "KeyID"}, State: int32(model.MfaStateNotReady)}},
result: &Human{OTP: &OTP{Secret: &crypto.CryptoValue{KeyID: "KeyID"}, State: int32(model.MfaStateNotReady)}},
},
}
for _, tt := range tests {
@@ -35,7 +36,7 @@ func TestAppendMfaOTPAddedEvent(t *testing.T) {
data, _ := json.Marshal(tt.args.otp)
tt.args.event.Data = data
}
tt.args.user.appendOtpAddedEvent(tt.args.event)
tt.args.user.appendOTPAddedEvent(tt.args.event)
if tt.args.user.OTP.State != tt.result.OTP.State {
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.OTP.State, tt.args.user.OTP.State)
}
@@ -45,23 +46,23 @@ func TestAppendMfaOTPAddedEvent(t *testing.T) {
func TestAppendMfaOTPVerifyEvent(t *testing.T) {
type args struct {
user *User
user *Human
otp *OTP
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append otp verify event",
args: args{
user: &User{OTP: &OTP{Secret: &crypto.CryptoValue{KeyID: "KeyID"}}},
user: &Human{OTP: &OTP{Secret: &crypto.CryptoValue{KeyID: "KeyID"}}},
otp: &OTP{Secret: &crypto.CryptoValue{KeyID: "KeyID"}},
event: &es_models.Event{},
},
result: &User{OTP: &OTP{Secret: &crypto.CryptoValue{KeyID: "KeyID"}, State: int32(model.MfaStateReady)}},
result: &Human{OTP: &OTP{Secret: &crypto.CryptoValue{KeyID: "KeyID"}, State: int32(model.MfaStateReady)}},
},
}
for _, tt := range tests {
@@ -70,7 +71,7 @@ func TestAppendMfaOTPVerifyEvent(t *testing.T) {
data, _ := json.Marshal(tt.args.otp)
tt.args.event.Data = data
}
tt.args.user.appendOtpVerifiedEvent()
tt.args.user.appendOTPVerifiedEvent()
if tt.args.user.OTP.State != tt.result.OTP.State {
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.OTP.State, tt.args.user.OTP.State)
}
@@ -80,27 +81,27 @@ func TestAppendMfaOTPVerifyEvent(t *testing.T) {
func TestAppendMfaOTPRemoveEvent(t *testing.T) {
type args struct {
user *User
user *Human
otp *OTP
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append otp verify event",
args: args{
user: &User{OTP: &OTP{Secret: &crypto.CryptoValue{KeyID: "KeyID"}}},
user: &Human{OTP: &OTP{Secret: &crypto.CryptoValue{KeyID: "KeyID"}}},
event: &es_models.Event{},
},
result: &User{},
result: &Human{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.user.appendOtpRemovedEvent()
tt.args.user.appendOTPRemovedEvent()
if tt.args.user.OTP != nil {
t.Errorf("got wrong result: actual: %v ", tt.result.OTP)
}

View File

@@ -2,12 +2,13 @@ package model
import (
"encoding/json"
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/crypto"
caos_errs "github.com/caos/zitadel/internal/errors"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/user/model"
"time"
)
type Password struct {
@@ -50,7 +51,7 @@ func PasswordCodeToModel(code *PasswordCode) *model.PasswordCode {
}
}
func (u *User) appendUserPasswordChangedEvent(event *es_models.Event) error {
func (u *Human) appendUserPasswordChangedEvent(event *es_models.Event) error {
u.Password = new(Password)
err := u.Password.setData(event)
if err != nil {
@@ -60,7 +61,7 @@ func (u *User) appendUserPasswordChangedEvent(event *es_models.Event) error {
return nil
}
func (u *User) appendPasswordSetRequestedEvent(event *es_models.Event) error {
func (u *Human) appendPasswordSetRequestedEvent(event *es_models.Event) error {
u.PasswordCode = new(PasswordCode)
return u.PasswordCode.SetData(event)
}

View File

@@ -2,30 +2,31 @@ package model
import (
"encoding/json"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"testing"
"time"
es_models "github.com/caos/zitadel/internal/eventstore/models"
)
func TestAppendUserPasswordChangedEvent(t *testing.T) {
type args struct {
user *User
user *Human
pw *Password
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append init user code event",
args: args{
user: &User{},
user: &Human{},
pw: &Password{ChangeRequired: true},
event: &es_models.Event{},
},
result: &User{Password: &Password{ChangeRequired: true}},
result: &Human{Password: &Password{ChangeRequired: true}},
},
}
for _, tt := range tests {
@@ -44,23 +45,23 @@ func TestAppendUserPasswordChangedEvent(t *testing.T) {
func TestAppendPasswordSetRequestedEvent(t *testing.T) {
type args struct {
user *User
user *Human
code *PasswordCode
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append user phone code added event",
args: args{
user: &User{Phone: &Phone{PhoneNumber: "PhoneNumber"}},
user: &Human{Phone: &Phone{PhoneNumber: "PhoneNumber"}},
code: &PasswordCode{Expiry: time.Hour * 1},
event: &es_models.Event{},
},
result: &User{PasswordCode: &PasswordCode{Expiry: time.Hour * 1}},
result: &Human{PasswordCode: &PasswordCode{Expiry: time.Hour * 1}},
},
}
for _, tt := range tests {

View File

@@ -2,12 +2,13 @@ package model
import (
"encoding/json"
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/crypto"
caos_errs "github.com/caos/zitadel/internal/errors"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/user/model"
"time"
)
type Phone struct {
@@ -67,21 +68,21 @@ func PhoneCodeToModel(code *PhoneCode) *model.PhoneCode {
}
}
func (u *User) appendUserPhoneChangedEvent(event *es_models.Event) error {
func (u *Human) appendUserPhoneChangedEvent(event *es_models.Event) error {
u.Phone = new(Phone)
return u.Phone.setData(event)
}
func (u *User) appendUserPhoneCodeAddedEvent(event *es_models.Event) error {
func (u *Human) appendUserPhoneCodeAddedEvent(event *es_models.Event) error {
u.PhoneCode = new(PhoneCode)
return u.PhoneCode.SetData(event)
}
func (u *User) appendUserPhoneVerifiedEvent() {
func (u *Human) appendUserPhoneVerifiedEvent() {
u.IsPhoneVerified = true
}
func (u *User) appendUserPhoneRemovedEvent() {
func (u *Human) appendUserPhoneRemovedEvent() {
u.Phone = nil
u.PhoneCode = nil
}

View File

@@ -2,15 +2,16 @@ package model
import (
"encoding/json"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"testing"
"time"
es_models "github.com/caos/zitadel/internal/eventstore/models"
)
func TestPhoneChanges(t *testing.T) {
type args struct {
existing *Phone
new *Phone
existingPhone *Phone
newPhone *Phone
}
type res struct {
changesLen int
@@ -23,8 +24,8 @@ func TestPhoneChanges(t *testing.T) {
{
name: "all fields changed",
args: args{
existing: &Phone{PhoneNumber: "Phone", IsPhoneVerified: true},
new: &Phone{PhoneNumber: "PhoneChanged", IsPhoneVerified: false},
existingPhone: &Phone{PhoneNumber: "Phone", IsPhoneVerified: true},
newPhone: &Phone{PhoneNumber: "PhoneChanged", IsPhoneVerified: false},
},
res: res{
changesLen: 1,
@@ -33,8 +34,8 @@ func TestPhoneChanges(t *testing.T) {
{
name: "no fields changed",
args: args{
existing: &Phone{PhoneNumber: "Phone", IsPhoneVerified: true},
new: &Phone{PhoneNumber: "Phone", IsPhoneVerified: false},
existingPhone: &Phone{PhoneNumber: "Phone", IsPhoneVerified: true},
newPhone: &Phone{PhoneNumber: "Phone", IsPhoneVerified: false},
},
res: res{
changesLen: 0,
@@ -43,7 +44,7 @@ func TestPhoneChanges(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
changes := tt.args.existing.Changes(tt.args.new)
changes := tt.args.existingPhone.Changes(tt.args.newPhone)
if len(changes) != tt.res.changesLen {
t.Errorf("got wrong changes len: expected: %v, actual: %v ", tt.res.changesLen, len(changes))
}
@@ -53,23 +54,23 @@ func TestPhoneChanges(t *testing.T) {
func TestAppendUserPhoneChangedEvent(t *testing.T) {
type args struct {
user *User
user *Human
phone *Phone
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append user phone event",
args: args{
user: &User{Phone: &Phone{PhoneNumber: "PhoneNumber"}},
user: &Human{Phone: &Phone{PhoneNumber: "PhoneNumber"}},
phone: &Phone{PhoneNumber: "PhoneNumberChanged"},
event: &es_models.Event{},
},
result: &User{Phone: &Phone{PhoneNumber: "PhoneNumberChanged"}},
result: &Human{Phone: &Phone{PhoneNumber: "PhoneNumberChanged"}},
},
}
for _, tt := range tests {
@@ -88,23 +89,23 @@ func TestAppendUserPhoneChangedEvent(t *testing.T) {
func TestAppendUserPhoneCodeAddedEvent(t *testing.T) {
type args struct {
user *User
user *Human
code *PhoneCode
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append user phone code added event",
args: args{
user: &User{Phone: &Phone{PhoneNumber: "PhoneNumber"}},
user: &Human{Phone: &Phone{PhoneNumber: "PhoneNumber"}},
code: &PhoneCode{Expiry: time.Hour * 1},
event: &es_models.Event{},
},
result: &User{PhoneCode: &PhoneCode{Expiry: time.Hour * 1}},
result: &Human{PhoneCode: &PhoneCode{Expiry: time.Hour * 1}},
},
}
for _, tt := range tests {
@@ -123,21 +124,21 @@ func TestAppendUserPhoneCodeAddedEvent(t *testing.T) {
func TestAppendUserPhoneVerifiedEvent(t *testing.T) {
type args struct {
user *User
user *Human
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append user phone event",
args: args{
user: &User{Phone: &Phone{PhoneNumber: "PhoneNumber"}},
user: &Human{Phone: &Phone{PhoneNumber: "PhoneNumber"}},
event: &es_models.Event{},
},
result: &User{Phone: &Phone{PhoneNumber: "PhoneNumber", IsPhoneVerified: true}},
result: &Human{Phone: &Phone{PhoneNumber: "PhoneNumber", IsPhoneVerified: true}},
},
}
for _, tt := range tests {

View File

@@ -10,7 +10,6 @@ import (
type Profile struct {
es_models.ObjectRoot
UserName string `json:"userName,omitempty"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
NickName string `json:"nickName,omitempty"`
@@ -18,7 +17,7 @@ type Profile struct {
PreferredLanguage language.Tag `json:"preferredLanguage,omitempty"`
Gender int32 `json:"gender,omitempty"`
isUserNameUnique bool `json:"-"`
isUserNameUnique bool
}
func (p *Profile) Changes(changed *Profile) map[string]interface{} {
@@ -47,7 +46,6 @@ func (p *Profile) Changes(changed *Profile) map[string]interface{} {
func ProfileFromModel(profile *model.Profile) *Profile {
return &Profile{
ObjectRoot: profile.ObjectRoot,
UserName: profile.UserName,
FirstName: profile.FirstName,
LastName: profile.LastName,
NickName: profile.NickName,
@@ -60,7 +58,6 @@ func ProfileFromModel(profile *model.Profile) *Profile {
func ProfileToModel(profile *Profile) *model.Profile {
return &model.Profile{
ObjectRoot: profile.ObjectRoot,
UserName: profile.UserName,
FirstName: profile.FirstName,
LastName: profile.LastName,
NickName: profile.NickName,

View File

@@ -1,15 +1,16 @@
package model
import (
"testing"
user_model "github.com/caos/zitadel/internal/user/model"
"golang.org/x/text/language"
"testing"
)
func TestProfileChanges(t *testing.T) {
type args struct {
existing *Profile
new *Profile
existingProfile *Profile
newProfile *Profile
}
type res struct {
changesLen int
@@ -22,8 +23,8 @@ func TestProfileChanges(t *testing.T) {
{
name: "all attributes changed",
args: args{
existing: &Profile{UserName: "UserName", FirstName: "FirstName", LastName: "LastName", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
new: &Profile{UserName: "UserNameChanged", FirstName: "FirstNameChanged", LastName: "LastNameChanged", NickName: "NickNameChanged", DisplayName: "DisplayNameChanged", PreferredLanguage: language.English, Gender: int32(user_model.GenderMale)},
existingProfile: &Profile{FirstName: "FirstName", LastName: "LastName", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
newProfile: &Profile{FirstName: "FirstNameChanged", LastName: "LastNameChanged", NickName: "NickNameChanged", DisplayName: "DisplayNameChanged", PreferredLanguage: language.English, Gender: int32(user_model.GenderMale)},
},
res: res{
changesLen: 6,
@@ -32,18 +33,8 @@ func TestProfileChanges(t *testing.T) {
{
name: "no changes",
args: args{
existing: &Profile{UserName: "UserName", FirstName: "FirstName", LastName: "LastName", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
new: &Profile{UserName: "UserName", FirstName: "FirstName", LastName: "LastName", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
},
res: res{
changesLen: 0,
},
},
{
name: "username changed",
args: args{
existing: &Profile{UserName: "UserName", FirstName: "FirstName", LastName: "LastName", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
new: &Profile{UserName: "UserNameChanged", FirstName: "FirstName", LastName: "LastName", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
existingProfile: &Profile{FirstName: "FirstName", LastName: "LastName", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
newProfile: &Profile{FirstName: "FirstName", LastName: "LastName", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
},
res: res{
changesLen: 0,
@@ -52,8 +43,8 @@ func TestProfileChanges(t *testing.T) {
{
name: "empty names",
args: args{
existing: &Profile{UserName: "UserName", FirstName: "FirstName", LastName: "LastName", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
new: &Profile{UserName: "UserName", FirstName: "", LastName: "", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
existingProfile: &Profile{FirstName: "FirstName", LastName: "LastName", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
newProfile: &Profile{FirstName: "", LastName: "", NickName: "NickName", DisplayName: "DisplayName", PreferredLanguage: language.German, Gender: int32(user_model.GenderFemale)},
},
res: res{
changesLen: 0,
@@ -62,7 +53,7 @@ func TestProfileChanges(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
changes := tt.args.existing.Changes(tt.args.new)
changes := tt.args.existingProfile.Changes(tt.args.newProfile)
if len(changes) != tt.res.changesLen {
t.Errorf("got wrong changes len: expected: %v, actual: %v ", tt.res.changesLen, len(changes))
}

View File

@@ -2,10 +2,14 @@ package model
import "github.com/caos/zitadel/internal/eventstore/models"
//aggregates
const (
UserAggregate models.AggregateType = "user"
UserUserNameAggregate models.AggregateType = "user.username"
)
// the following consts are for user v1 events
const (
UserAdded models.EventType = "user.added"
UserRegistered models.EventType = "user.selfregistered"
InitializedUserCodeAdded models.EventType = "user.initialization.code.added"
@@ -16,12 +20,6 @@ const (
UserUserNameReserved models.EventType = "user.username.reserved"
UserUserNameReleased models.EventType = "user.username.released"
UserLocked models.EventType = "user.locked"
UserUnlocked models.EventType = "user.unlocked"
UserDeactivated models.EventType = "user.deactivated"
UserReactivated models.EventType = "user.reactivated"
UserRemoved models.EventType = "user.removed"
UserPasswordChanged models.EventType = "user.password.changed"
UserPasswordCodeAdded models.EventType = "user.password.code.added"
UserPasswordCodeSent models.EventType = "user.password.code.sent"
@@ -45,15 +43,77 @@ const (
UserAddressChanged models.EventType = "user.address.changed"
UserUserNameChanged models.EventType = "user.username.changed"
MfaOtpAdded models.EventType = "user.mfa.otp.added"
MfaOtpVerified models.EventType = "user.mfa.otp.verified"
MfaOtpRemoved models.EventType = "user.mfa.otp.removed"
MFAOTPAdded models.EventType = "user.mfa.otp.added"
MFAOTPVerified models.EventType = "user.mfa.otp.verified"
MFAOTPRemoved models.EventType = "user.mfa.otp.removed"
MfaOtpCheckSucceeded models.EventType = "user.mfa.otp.check.succeeded"
MfaOtpCheckFailed models.EventType = "user.mfa.otp.check.failed"
MfaInitSkipped models.EventType = "user.mfa.init.skipped"
MFAInitSkipped models.EventType = "user.mfa.init.skipped"
SignedOut models.EventType = "user.signed.out"
DomainClaimed models.EventType = "user.domain.claimed"
DomainClaimedSent models.EventType = "user.domain.claimed.sent"
)
//the following consts are for user(v2)
const (
UserNameReserved models.EventType = "user.username.reserved"
UserNameReleased models.EventType = "user.username.released"
UserLocked models.EventType = "user.locked"
UserUnlocked models.EventType = "user.unlocked"
UserDeactivated models.EventType = "user.deactivated"
UserReactivated models.EventType = "user.reactivated"
UserRemoved models.EventType = "user.removed"
)
// the following consts are for user(v2).human
const (
HumanAdded models.EventType = "user.human.added"
HumanRegistered models.EventType = "user.human.selfregistered"
InitializedHumanCodeAdded models.EventType = "user.human.initialization.code.added"
InitializedHumanCodeSent models.EventType = "user.human.initialization.code.sent"
InitializedHumanCheckSucceeded models.EventType = "user.human.initialization.check.succeeded"
InitializedHumanCheckFailed models.EventType = "user.human.initialization.check.failed"
HumanPasswordChanged models.EventType = "user.human.password.changed"
HumanPasswordCodeAdded models.EventType = "user.human.password.code.added"
HumanPasswordCodeSent models.EventType = "user.human.password.code.sent"
HumanPasswordCheckSucceeded models.EventType = "user.human.password.check.succeeded"
HumanPasswordCheckFailed models.EventType = "user.human.password.check.failed"
HumanEmailChanged models.EventType = "user.human.email.changed"
HumanEmailVerified models.EventType = "user.human.email.verified"
HumanEmailVerificationFailed models.EventType = "user.human.email.verification.failed"
HumanEmailCodeAdded models.EventType = "user.human.email.code.added"
HumanEmailCodeSent models.EventType = "user.human.email.code.sent"
HumanPhoneChanged models.EventType = "user.human.phone.changed"
HumanPhoneRemoved models.EventType = "user.human.phone.removed"
HumanPhoneVerified models.EventType = "user.human.phone.verified"
HumanPhoneVerificationFailed models.EventType = "user.human.phone.verification.failed"
HumanPhoneCodeAdded models.EventType = "user.human.phone.code.added"
HumanPhoneCodeSent models.EventType = "user.human.phone.code.sent"
HumanProfileChanged models.EventType = "user.human.profile.changed"
HumanAddressChanged models.EventType = "user.human.address.changed"
HumanMFAOTPAdded models.EventType = "user.human.mfa.otp.added"
HumanMFAOTPVerified models.EventType = "user.human.mfa.otp.verified"
HumanMFAOTPRemoved models.EventType = "user.human.mfa.otp.removed"
HumanMfaOtpCheckSucceeded models.EventType = "user.human.mfa.otp.check.succeeded"
HumanMfaOtpCheckFailed models.EventType = "user.human.mfa.otp.check.failed"
HumanMfaInitSkipped models.EventType = "user.human.mfa.init.skipped"
HumanSignedOut models.EventType = "user.human.signed.out"
)
// the following consts are for user(v2).machines
const (
MachineAdded models.EventType = "user.machine.added"
MachineChanged models.EventType = "user.machine.changed"
MachineKeyAdded models.EventType = "user.machine.key.added"
MachineKeyRemoved models.EventType = "user.machine.key.removed"
)

View File

@@ -2,141 +2,90 @@ package model
import (
"encoding/json"
"time"
"strings"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/errors"
caos_errs "github.com/caos/zitadel/internal/errors"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/user/model"
)
const (
UserVersion = "v1"
UserVersion = "v2"
)
type User struct {
es_models.ObjectRoot
State int32 `json:"-"`
*Password
*Profile
*Email
*Phone
*Address
InitCode *InitUserCode `json:"-"`
EmailCode *EmailCode `json:"-"`
PhoneCode *PhoneCode `json:"-"`
PasswordCode *PasswordCode `json:"-"`
OTP *OTP `json:"-"`
}
State int32 `json:"-"`
UserName string `json:"userName"`
type InitUserCode struct {
es_models.ObjectRoot
Code *crypto.CryptoValue `json:"code,omitempty"`
Expiry time.Duration `json:"expiry,omitempty"`
*Human
*Machine
}
func UserFromModel(user *model.User) *User {
converted := &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,
}
if user.Password != nil {
converted.Password = PasswordFromModel(user.Password)
}
if user.Profile != nil {
converted.Profile = ProfileFromModel(user.Profile)
}
if user.Email != nil {
converted.Email = EmailFromModel(user.Email)
}
if user.Phone != nil {
converted.Phone = PhoneFromModel(user.Phone)
}
if user.Address != nil {
converted.Address = AddressFromModel(user.Address)
}
if user.OTP != nil {
converted.OTP = OTPFromModel(user.OTP)
}
return converted
}
func UserToModel(user *User) *model.User {
converted := &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),
}
if user.Password != nil {
converted.Password = PasswordToModel(user.Password)
}
if user.Profile != nil {
converted.Profile = ProfileToModel(user.Profile)
}
if user.Email != nil {
converted.Email = EmailToModel(user.Email)
}
if user.Phone != nil {
converted.Phone = PhoneToModel(user.Phone)
}
if user.Address != nil {
converted.Address = AddressToModel(user.Address)
}
if user.InitCode != nil {
converted.InitCode = InitCodeToModel(user.InitCode)
}
if user.EmailCode != nil {
converted.EmailCode = EmailCodeToModel(user.EmailCode)
}
if user.PhoneCode != nil {
converted.PhoneCode = PhoneCodeToModel(user.PhoneCode)
}
if user.PasswordCode != nil {
converted.PasswordCode = PasswordCodeToModel(user.PasswordCode)
}
if user.OTP != nil {
converted.OTP = OTPToModel(user.OTP)
}
return converted
}
func InitCodeFromModel(code *model.InitUserCode) *InitUserCode {
if code == nil {
return nil
}
return &InitUserCode{
ObjectRoot: code.ObjectRoot,
Expiry: code.Expiry,
Code: code.Code,
UserName: user.UserName,
Human: human,
Machine: machine,
}
}
func InitCodeToModel(code *InitUserCode) *model.InitUserCode {
return &model.InitUserCode{
ObjectRoot: code.ObjectRoot,
Expiry: code.Expiry,
Code: code.Code,
}
}
func (p *User) AppendEvents(events ...*es_models.Event) error {
func (u *User) AppendEvents(events ...*es_models.Event) error {
for _, event := range events {
if err := p.AppendEvent(event); err != nil {
if err := u.AppendEvent(event); err != nil {
return err
}
}
return nil
}
func (u *User) AppendEvent(event *es_models.Event) (err error) {
func (u *User) AppendEvent(event *es_models.Event) error {
u.ObjectRoot.AppendEvent(event)
switch event.Type {
case UserAdded,
HumanAdded,
MachineAdded,
UserRegistered,
HumanRegistered,
UserProfileChanged,
DomainClaimed,
UserUserNameChanged:
u.setData(event)
err := u.setData(event)
if err != nil {
return err
}
case UserDeactivated:
u.appendDeactivatedEvent()
case UserReactivated:
@@ -145,71 +94,31 @@ func (u *User) AppendEvent(event *es_models.Event) (err error) {
u.appendLockedEvent()
case UserUnlocked:
u.appendUnlockedEvent()
case InitializedUserCodeAdded:
u.appendInitUsercodeCreatedEvent(event)
case UserPasswordChanged:
err = u.appendUserPasswordChangedEvent(event)
case UserPasswordCodeAdded:
err = u.appendPasswordSetRequestedEvent(event)
case UserEmailChanged:
err = u.appendUserEmailChangedEvent(event)
case UserEmailCodeAdded:
err = u.appendUserEmailCodeAddedEvent(event)
case UserEmailVerified:
u.appendUserEmailVerifiedEvent()
case UserPhoneChanged:
err = u.appendUserPhoneChangedEvent(event)
case UserPhoneCodeAdded:
err = u.appendUserPhoneCodeAddedEvent(event)
case UserPhoneVerified:
u.appendUserPhoneVerifiedEvent()
case UserPhoneRemoved:
u.appendUserPhoneRemovedEvent()
case UserAddressChanged:
err = u.appendUserAddressChangedEvent(event)
case MfaOtpAdded:
err = u.appendOtpAddedEvent(event)
case MfaOtpVerified:
u.appendOtpVerifiedEvent()
case MfaOtpRemoved:
u.appendOtpRemovedEvent()
}
if err != nil {
return err
}
u.ComputeObject()
return nil
}
func (u *User) ComputeObject() {
if u.State == 0 {
if u.Email != nil && u.IsEmailVerified {
u.State = int32(model.UserStateActive)
} else {
u.State = int32(model.UserStateInitial)
}
if u.Human != nil {
u.Human.User = u
return u.Human.AppendEvent(event)
} else if u.Machine != nil {
u.Machine.User = u
return u.Machine.AppendEvent(event)
}
if u.Password != nil && u.Password.ObjectRoot.IsZero() {
u.Password.ObjectRoot = u.ObjectRoot
if strings.HasPrefix(string(event.Type), "user.human") || event.AggregateVersion == "v1" {
u.Human = &Human{User: u}
return u.Human.AppendEvent(event)
}
if u.Profile != nil && u.Profile.ObjectRoot.IsZero() {
u.Profile.ObjectRoot = u.ObjectRoot
}
if u.Email != nil && u.Email.ObjectRoot.IsZero() {
u.Email.ObjectRoot = u.ObjectRoot
}
if u.Phone != nil && u.Phone.ObjectRoot.IsZero() {
u.Phone.ObjectRoot = u.ObjectRoot
}
if u.Address != nil && u.Address.ObjectRoot.IsZero() {
u.Address.ObjectRoot = u.ObjectRoot
if strings.HasPrefix(string(event.Type), "user.machine") {
u.Machine = &Machine{User: u}
return u.Machine.AppendEvent(event)
}
return errors.ThrowNotFound(nil, "MODEL-x9TaX", "Errors.UserType.Undefined")
}
func (u *User) setData(event *es_models.Event) error {
if err := json.Unmarshal(event.Data, u); err != nil {
logging.Log("EVEN-8ujgd").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(err, "MODEL-sj4jd", "could not unmarshal event")
logging.Log("EVEN-ZDzQy").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(err, "MODEL-yGmhh", "could not unmarshal event")
}
return nil
}
@@ -229,23 +138,3 @@ func (u *User) appendLockedEvent() {
func (u *User) appendUnlockedEvent() {
u.State = int32(model.UserStateActive)
}
func (u *User) appendInitUsercodeCreatedEvent(event *es_models.Event) error {
initCode := new(InitUserCode)
err := initCode.SetData(event)
if err != nil {
return err
}
initCode.ObjectRoot.CreationDate = event.CreationDate
u.InitCode = initCode
return nil
}
func (c *InitUserCode) SetData(event *es_models.Event) error {
c.ObjectRoot.AppendEvent(event)
if err := json.Unmarshal(event.Data, c); err != nil {
logging.Log("EVEN-7duwe").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(err, "MODEL-lo34s", "could not unmarshal event")
}
return nil
}

View File

@@ -0,0 +1,231 @@
package model
import (
"encoding/json"
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/crypto"
caos_errs "github.com/caos/zitadel/internal/errors"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/user/model"
)
type Human struct {
*User `json:"-"`
*Password
*Profile
*Email
*Phone
*Address
InitCode *InitUserCode `json:"-"`
EmailCode *EmailCode `json:"-"`
PhoneCode *PhoneCode `json:"-"`
PasswordCode *PasswordCode `json:"-"`
OTP *OTP `json:"-"`
}
type InitUserCode struct {
es_models.ObjectRoot
Code *crypto.CryptoValue `json:"code,omitempty"`
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)
}
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.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)
}
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 {
return err
}
}
return nil
}
func (h *Human) AppendEvent(event *es_models.Event) (err error) {
switch event.Type {
case UserAdded,
UserRegistered,
UserProfileChanged,
HumanAdded,
HumanRegistered,
HumanProfileChanged:
h.setData(event)
case InitializedUserCodeAdded,
InitializedHumanCodeAdded:
h.appendInitUsercodeCreatedEvent(event)
case UserPasswordChanged,
HumanPasswordChanged:
err = h.appendUserPasswordChangedEvent(event)
case UserPasswordCodeAdded,
HumanPasswordCodeAdded:
err = h.appendPasswordSetRequestedEvent(event)
case UserEmailChanged,
HumanEmailChanged:
err = h.appendUserEmailChangedEvent(event)
case UserEmailCodeAdded,
HumanEmailCodeAdded:
err = h.appendUserEmailCodeAddedEvent(event)
case UserEmailVerified,
HumanEmailVerified:
h.appendUserEmailVerifiedEvent()
case UserPhoneChanged,
HumanPhoneChanged:
err = h.appendUserPhoneChangedEvent(event)
case UserPhoneCodeAdded,
HumanPhoneCodeAdded:
err = h.appendUserPhoneCodeAddedEvent(event)
case UserPhoneVerified,
HumanPhoneVerified:
h.appendUserPhoneVerifiedEvent()
case UserPhoneRemoved,
HumanPhoneRemoved:
h.appendUserPhoneRemovedEvent()
case UserAddressChanged,
HumanAddressChanged:
err = h.appendUserAddressChangedEvent(event)
case MFAOTPAdded,
HumanMFAOTPAdded:
err = h.appendOTPAddedEvent(event)
case MFAOTPVerified,
HumanMFAOTPVerified:
h.appendOTPVerifiedEvent()
case MFAOTPRemoved,
HumanMFAOTPRemoved:
h.appendOTPRemovedEvent()
}
if err != nil {
return err
}
h.ComputeObject()
return nil
}
func (h *Human) ComputeObject() {
if h.State == int32(model.UserStateUnspecified) {
if h.Email != nil && h.IsEmailVerified {
h.State = int32(model.UserStateActive)
} else {
h.State = int32(model.UserStateInitial)
}
}
if h.Password != nil && h.Password.ObjectRoot.IsZero() {
h.Password.ObjectRoot = h.User.ObjectRoot
}
if h.Profile != nil && h.Profile.ObjectRoot.IsZero() {
h.Profile.ObjectRoot = h.User.ObjectRoot
}
if h.Email != nil && h.Email.ObjectRoot.IsZero() {
h.Email.ObjectRoot = h.User.ObjectRoot
}
if h.Phone != nil && h.Phone.ObjectRoot.IsZero() {
h.Phone.ObjectRoot = h.User.ObjectRoot
}
if h.Address != nil && h.Address.ObjectRoot.IsZero() {
h.Address.ObjectRoot = h.User.ObjectRoot
}
}
func (u *Human) setData(event *es_models.Event) error {
if err := json.Unmarshal(event.Data, u); err != nil {
logging.Log("EVEN-8ujgd").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(err, "MODEL-sj4jd", "could not unmarshal event")
}
return nil
}
func (u *Human) appendInitUsercodeCreatedEvent(event *es_models.Event) error {
initCode := new(InitUserCode)
err := initCode.SetData(event)
if err != nil {
return err
}
initCode.ObjectRoot.CreationDate = event.CreationDate
u.InitCode = initCode
return nil
}
func (c *InitUserCode) SetData(event *es_models.Event) error {
c.ObjectRoot.AppendEvent(event)
if err := json.Unmarshal(event.Data, c); err != nil {
logging.Log("EVEN-7duwe").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(err, "MODEL-lo34s", "could not unmarshal event")
}
return nil
}

View File

@@ -2,10 +2,11 @@ package model
import (
"encoding/json"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/user/model"
"testing"
"time"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/user/model"
)
func TestAppendDeactivatedEvent(t *testing.T) {
@@ -118,23 +119,23 @@ func TestAppendUnlockEvent(t *testing.T) {
func TestAppendInitUserCodeEvent(t *testing.T) {
type args struct {
user *User
user *Human
code *InitUserCode
event *es_models.Event
}
tests := []struct {
name string
args args
result *User
result *Human
}{
{
name: "append init user code event",
args: args{
user: &User{},
user: &Human{},
code: &InitUserCode{Expiry: time.Hour * 30},
event: &es_models.Event{},
},
result: &User{InitCode: &InitUserCode{Expiry: time.Hour * 30}},
result: &Human{InitCode: &InitUserCode{Expiry: time.Hour * 30}},
},
}
for _, tt := range tests {

View File

@@ -0,0 +1,134 @@
package model
import (
"encoding/json"
"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"
"github.com/caos/zitadel/internal/user/model"
)
type Machine struct {
*User `json:"-"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}
func (sa *Machine) AppendEvents(events ...*models.Event) error {
for _, event := range events {
if err := sa.AppendEvent(event); err != nil {
return err
}
}
return nil
}
func (sa *Machine) AppendEvent(event *models.Event) (err error) {
switch event.Type {
case MachineAdded, MachineChanged:
err = sa.setData(event)
case MachineKeyAdded:
fallthrough
case MachineKeyRemoved:
logging.Log("MODEL-iBgOc").Warn("key unimplemented")
}
return err
}
func (sa *Machine) setData(event *models.Event) error {
if err := json.Unmarshal(event.Data, sa); err != nil {
logging.Log("EVEN-8ujgd").WithError(err).Error("could not unmarshal event data")
return errors.ThrowInternal(err, "MODEL-GwjY9", "could not unmarshal event")
}
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"`
Type int32 `json:"type,omitempty"`
ExpirationDate time.Time `json:"expirationDate,omitempty"`
PublicKey *crypto.CryptoValue `json:"publicKey,omitempty"`
privateKey []byte
}
func (key *MachineKey) AppendEvents(events ...*es_models.Event) error {
for _, event := range events {
err := key.AppendEvent(event)
if err != nil {
return err
}
}
return nil
}
func (key *MachineKey) AppendEvent(event *es_models.Event) error {
switch event.Type {
case MachineKeyAdded:
}
return nil
}
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: model.MachineKeyType(machine.Type),
}
}
func (key *MachineKey) GenerateMachineKeyPair(keySize int, alg crypto.EncryptionAlgorithm) error {
privateKey, publicKey, err := crypto.GenerateKeyPair(keySize)
if err != nil {
return err
}
publicKeyBytes, err := crypto.PublicKeyToBytes(publicKey)
if err != nil {
return err
}
key.PublicKey, err = crypto.Encrypt(publicKeyBytes, alg)
if err != nil {
return err
}
key.privateKey = crypto.PrivateKeyToBytes(privateKey)
return nil
}