2021-01-07 15:06:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/user"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HumanOTPWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
State domain.MFAState
|
2021-01-07 15:06:45 +00:00
|
|
|
Secret *crypto.CryptoValue
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewHumanOTPWriteModel(userID, resourceOwner string) *HumanOTPWriteModel {
|
2021-01-07 15:06:45 +00:00
|
|
|
return &HumanOTPWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-07 15:06:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanOTPWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
2021-01-15 08:32:59 +00:00
|
|
|
wm.WriteModel.AppendEvents(events...)
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanOTPWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanOTPAddedEvent:
|
|
|
|
wm.Secret = e.Secret
|
2021-01-15 08:32:59 +00:00
|
|
|
wm.State = domain.MFAStateNotReady
|
|
|
|
case *user.HumanOTPVerifiedEvent:
|
|
|
|
wm.State = domain.MFAStateReady
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.HumanOTPRemovedEvent:
|
2021-01-15 08:32:59 +00:00
|
|
|
wm.State = domain.MFAStateRemoved
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.UserRemovedEvent:
|
2021-01-15 08:32:59 +00:00
|
|
|
wm.State = domain.MFAStateRemoved
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanOTPWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, user.AggregateType).
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
ResourceOwner(wm.ResourceOwner)
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|