2021-01-07 15:06:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-07 15:06:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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) 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 {
|
2021-07-06 11:55:57 +00:00
|
|
|
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
2021-02-18 13:48:27 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(user.HumanMFAOTPAddedType,
|
|
|
|
user.HumanMFAOTPVerifiedType,
|
|
|
|
user.HumanMFAOTPRemovedType,
|
2021-04-06 07:57:58 +00:00
|
|
|
user.UserRemovedType,
|
|
|
|
user.UserV1MFAOTPAddedType,
|
|
|
|
user.UserV1MFAOTPVerifiedType,
|
2021-07-06 11:55:57 +00:00
|
|
|
user.UserV1MFAOTPRemovedType).
|
|
|
|
Builder()
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
if wm.ResourceOwner != "" {
|
|
|
|
query.ResourceOwner(wm.ResourceOwner)
|
|
|
|
}
|
|
|
|
return query
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|