2021-02-12 15:51:12 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
keypair "github.com/caos/zitadel/internal/repository/keypair"
|
|
|
|
"github.com/caos/zitadel/internal/repository/project"
|
2021-02-12 15:51:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type KeyPairWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
Usage domain.KeyUsage
|
|
|
|
Algorithm string
|
|
|
|
PrivateKey *domain.Key
|
|
|
|
PublicKey *domain.Key
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewKeyPairWriteModel(aggregateID, resourceOwner string) *KeyPairWriteModel {
|
|
|
|
return &KeyPairWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: aggregateID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (wm *KeyPairWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-02-12 15:51:12 +00:00
|
|
|
wm.WriteModel.AppendEvents(events...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *KeyPairWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *keypair.AddedEvent:
|
|
|
|
wm.Usage = e.Usage
|
|
|
|
wm.Algorithm = e.Algorithm
|
|
|
|
wm.PrivateKey = &domain.Key{
|
|
|
|
Key: e.PrivateKey.Key,
|
|
|
|
Expiry: e.PrivateKey.Expiry,
|
|
|
|
}
|
|
|
|
wm.PublicKey = &domain.Key{
|
|
|
|
Key: e.PublicKey.Key,
|
|
|
|
Expiry: e.PublicKey.Expiry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *KeyPairWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(project.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(keypair.AddedEventType).
|
|
|
|
Builder()
|
2021-02-12 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func KeyPairAggregateFromWriteModel(wm *eventstore.WriteModel) *eventstore.Aggregate {
|
|
|
|
return eventstore.AggregateFromWriteModel(wm, keypair.AggregateType, keypair.AggregateVersion)
|
|
|
|
|
2021-02-12 15:51:12 +00:00
|
|
|
}
|