mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-01 13:53:08 +00:00
61 lines
1.7 KiB
Go
61 lines
1.7 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
)
|
|
|
|
type cryptoCode struct {
|
|
value *crypto.CryptoValue
|
|
plain string
|
|
expiry time.Duration
|
|
}
|
|
|
|
func newCryptoCode(ctx context.Context, filter preparation.FilterToQueryReducer, typ domain.SecretGeneratorType, alg crypto.Crypto) (*cryptoCode, error) {
|
|
config, err := secretGeneratorConfig(ctx, filter, typ)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
code := &cryptoCode{
|
|
expiry: config.Expiry,
|
|
}
|
|
|
|
switch a := alg.(type) {
|
|
case crypto.HashAlgorithm:
|
|
code.value, code.plain, err = crypto.NewCode(crypto.NewHashGenerator(*config, a))
|
|
case crypto.EncryptionAlgorithm:
|
|
code.value, code.plain, err = crypto.NewCode(crypto.NewEncryptionGenerator(*config, a))
|
|
default:
|
|
return nil, errors.ThrowInternal(nil, "COMMA-RreV6", "Errors.Internal")
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return code, nil
|
|
}
|
|
|
|
func secretGeneratorConfig(ctx context.Context, filter preparation.FilterToQueryReducer, typ domain.SecretGeneratorType) (*crypto.GeneratorConfig, error) {
|
|
wm := NewInstanceSecretGeneratorConfigWriteModel(ctx, typ)
|
|
events, err := filter(ctx, wm.Query())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
wm.AppendEvents(events...)
|
|
if err := wm.Reduce(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &crypto.GeneratorConfig{
|
|
Length: wm.Length,
|
|
Expiry: wm.Expiry,
|
|
IncludeLowerLetters: wm.IncludeLowerLetters,
|
|
IncludeUpperLetters: wm.IncludeUpperLetters,
|
|
IncludeDigits: wm.IncludeDigits,
|
|
IncludeSymbols: wm.IncludeSymbols,
|
|
}, nil
|
|
}
|