2022-04-12 14:20:17 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-12 14:20:17 +00:00
|
|
|
)
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
type encrypedCodeFunc func(ctx context.Context, filter preparation.FilterToQueryReducer, typ domain.SecretGeneratorType, alg crypto.EncryptionAlgorithm) (*EncryptedCode, error)
|
2023-05-24 10:22:00 +00:00
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
type encryptedCodeWithDefaultFunc func(ctx context.Context, filter preparation.FilterToQueryReducer, typ domain.SecretGeneratorType, alg crypto.EncryptionAlgorithm, defaultConfig *crypto.GeneratorConfig) (*EncryptedCode, error)
|
2023-08-24 09:41:52 +00:00
|
|
|
|
|
|
|
var emptyConfig = &crypto.GeneratorConfig{}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
type EncryptedCode struct {
|
2023-04-26 05:47:57 +00:00
|
|
|
Crypted *crypto.CryptoValue
|
|
|
|
Plain string
|
|
|
|
Expiry time.Duration
|
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func newEncryptedCode(ctx context.Context, filter preparation.FilterToQueryReducer, typ domain.SecretGeneratorType, alg crypto.EncryptionAlgorithm) (*EncryptedCode, error) {
|
|
|
|
return newEncryptedCodeWithDefaultConfig(ctx, filter, typ, alg, emptyConfig)
|
2023-08-24 09:41:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func newEncryptedCodeWithDefaultConfig(ctx context.Context, filter preparation.FilterToQueryReducer, typ domain.SecretGeneratorType, alg crypto.EncryptionAlgorithm, defaultConfig *crypto.GeneratorConfig) (*EncryptedCode, error) {
|
|
|
|
gen, config, err := encryptedCodeGenerator(ctx, filter, typ, alg, defaultConfig)
|
2022-04-12 14:20:17 +00:00
|
|
|
if err != nil {
|
2023-04-26 05:47:57 +00:00
|
|
|
return nil, err
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
2023-05-24 10:22:00 +00:00
|
|
|
crypted, plain, err := crypto.NewCode(gen)
|
2022-04-12 14:20:17 +00:00
|
|
|
if err != nil {
|
2023-04-26 05:47:57 +00:00
|
|
|
return nil, err
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
return &EncryptedCode{
|
2023-05-24 10:22:00 +00:00
|
|
|
Crypted: crypted,
|
|
|
|
Plain: plain,
|
|
|
|
Expiry: config.Expiry,
|
|
|
|
}, nil
|
|
|
|
}
|
2023-04-26 05:47:57 +00:00
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func encryptedCodeGenerator(ctx context.Context, filter preparation.FilterToQueryReducer, typ domain.SecretGeneratorType, alg crypto.EncryptionAlgorithm, defaultConfig *crypto.GeneratorConfig) (crypto.Generator, *crypto.GeneratorConfig, error) {
|
|
|
|
config, err := cryptoGeneratorConfigWithDefault(ctx, filter, typ, defaultConfig)
|
2023-05-24 10:22:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
return crypto.NewEncryptionGenerator(*config, alg), config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type hashedSecretFunc func(ctx context.Context, filter preparation.FilterToQueryReducer) (encodedHash, plain string, err error)
|
|
|
|
|
|
|
|
func newHashedSecretWithDefault(hasher *crypto.Hasher, defaultConfig *crypto.GeneratorConfig) hashedSecretFunc {
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) (encodedHash string, plain string, err error) {
|
|
|
|
config, err := cryptoGeneratorConfigWithDefault(ctx, filter, domain.SecretGeneratorTypeAppSecret, defaultConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
generator := crypto.NewHashGenerator(*config, hasher)
|
|
|
|
return generator.NewCode()
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func cryptoGeneratorConfig(ctx context.Context, filter preparation.FilterToQueryReducer, typ domain.SecretGeneratorType) (*crypto.GeneratorConfig, error) {
|
|
|
|
return cryptoGeneratorConfigWithDefault(ctx, filter, typ, emptyConfig)
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
2023-08-23 08:04:29 +00:00
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func cryptoGeneratorConfigWithDefault(ctx context.Context, filter preparation.FilterToQueryReducer, typ domain.SecretGeneratorType, defaultConfig *crypto.GeneratorConfig) (*crypto.GeneratorConfig, error) {
|
2023-08-23 08:04:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
if wm.State != domain.SecretGeneratorStateActive {
|
2023-08-24 09:41:52 +00:00
|
|
|
return defaultConfig, nil
|
2023-08-23 08:04:29 +00:00
|
|
|
}
|
|
|
|
return &crypto.GeneratorConfig{
|
|
|
|
Length: wm.Length,
|
|
|
|
Expiry: wm.Expiry,
|
|
|
|
IncludeLowerLetters: wm.IncludeLowerLetters,
|
|
|
|
IncludeUpperLetters: wm.IncludeUpperLetters,
|
|
|
|
IncludeDigits: wm.IncludeDigits,
|
|
|
|
IncludeSymbols: wm.IncludeSymbols,
|
|
|
|
}, nil
|
|
|
|
}
|