2021-03-10 13:32:56 +00:00
|
|
|
package keypair
|
2021-02-12 15:51:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-03 08:19:07 +00:00
|
|
|
"time"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-02-12 15:51:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
eventTypePrefix = eventstore.EventType("key_pair.")
|
|
|
|
AddedEventType = eventTypePrefix + "added"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2024-08-14 14:18:14 +00:00
|
|
|
Usage crypto.KeyUsage `json:"usage"`
|
2021-02-12 15:51:12 +00:00
|
|
|
Algorithm string `json:"algorithm"`
|
|
|
|
PrivateKey *Key `json:"privateKey"`
|
|
|
|
PublicKey *Key `json:"publicKey"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Key struct {
|
|
|
|
Key *crypto.CryptoValue `json:"key"`
|
|
|
|
Expiry time.Time `json:"expiry"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *AddedEvent) Payload() interface{} {
|
2021-02-12 15:51:12 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *AddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-02-12 15:51:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAddedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2024-08-14 14:18:14 +00:00
|
|
|
usage crypto.KeyUsage,
|
2021-02-12 15:51:12 +00:00
|
|
|
algorithm string,
|
|
|
|
privateCrypto,
|
|
|
|
publicCrypto *crypto.CryptoValue,
|
|
|
|
privateKeyExpiration,
|
|
|
|
publicKeyExpiration time.Time) *AddedEvent {
|
|
|
|
return &AddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-02-12 15:51:12 +00:00
|
|
|
AddedEventType,
|
|
|
|
),
|
|
|
|
Usage: usage,
|
|
|
|
Algorithm: algorithm,
|
|
|
|
PrivateKey: &Key{
|
|
|
|
Key: privateCrypto,
|
|
|
|
Expiry: privateKeyExpiration,
|
|
|
|
},
|
|
|
|
PublicKey: &Key{
|
|
|
|
Key: publicCrypto,
|
|
|
|
Expiry: publicKeyExpiration,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func AddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-02-12 15:51:12 +00:00
|
|
|
e := &AddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(e)
|
2021-02-12 15:51:12 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "KEY-4n8vs", "unable to unmarshal key pair added")
|
2021-02-12 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|