2021-02-12 15:51:12 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-09 06:55:48 +00:00
|
|
|
"time"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/keypair"
|
2021-02-12 15:51:12 +00:00
|
|
|
)
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) GenerateSigningKeyPair(ctx context.Context, algorithm string) error {
|
|
|
|
privateCrypto, publicCrypto, err := crypto.GenerateEncryptedKeyPair(c.keySize, c.keyAlgorithm)
|
2021-02-12 15:51:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
keyID, err := c.idGenerator.Next()
|
2021-02-12 15:51:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
privateKeyExp := time.Now().UTC().Add(c.privateKeyLifetime)
|
|
|
|
publicKeyExp := time.Now().UTC().Add(c.publicKeyLifetime)
|
2021-02-12 15:51:12 +00:00
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
keyPairWriteModel := NewKeyPairWriteModel(keyID, authz.GetInstance(ctx).InstanceID())
|
2021-02-12 15:51:12 +00:00
|
|
|
keyAgg := KeyPairAggregateFromWriteModel(&keyPairWriteModel.WriteModel)
|
2022-01-03 08:19:07 +00:00
|
|
|
_, err = c.eventstore.Push(ctx, keypair.NewAddedEvent(
|
2021-02-18 13:48:27 +00:00
|
|
|
ctx,
|
|
|
|
keyAgg,
|
|
|
|
domain.KeyUsageSigning,
|
|
|
|
algorithm,
|
|
|
|
privateCrypto, publicCrypto,
|
|
|
|
privateKeyExp, publicKeyExp))
|
|
|
|
return err
|
2021-02-12 15:51:12 +00:00
|
|
|
}
|