2022-02-16 16:49:17 +01:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/object"
|
|
|
|
obj_grpc "github.com/zitadel/zitadel/internal/api/grpc/object"
|
2024-02-28 12:21:11 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2022-06-10 12:39:38 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/notification/channels/smtp"
|
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-04-27 01:01:45 +02:00
|
|
|
admin_pb "github.com/zitadel/zitadel/pkg/grpc/admin"
|
|
|
|
settings_pb "github.com/zitadel/zitadel/pkg/grpc/settings"
|
2022-02-16 16:49:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func listSecretGeneratorToModel(req *admin_pb.ListSecretGeneratorsRequest) (*query.SecretGeneratorSearchQueries, error) {
|
|
|
|
offset, limit, asc := object.ListQueryToModel(req.Query)
|
|
|
|
queries, err := SecretGeneratorQueriesToModel(req.Queries)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &query.SecretGeneratorSearchQueries{
|
|
|
|
SearchRequest: query.SearchRequest{
|
|
|
|
Offset: offset,
|
|
|
|
Limit: limit,
|
|
|
|
Asc: asc,
|
|
|
|
},
|
|
|
|
Queries: queries,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SecretGeneratorQueriesToModel(queries []*settings_pb.SecretGeneratorQuery) (_ []query.SearchQuery, err error) {
|
|
|
|
q := make([]query.SearchQuery, len(queries))
|
|
|
|
for i, query := range queries {
|
|
|
|
q[i], err = SecretGeneratorQueryToModel(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return q, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SecretGeneratorQueryToModel(apiQuery *settings_pb.SecretGeneratorQuery) (query.SearchQuery, error) {
|
|
|
|
switch q := apiQuery.Query.(type) {
|
|
|
|
case *settings_pb.SecretGeneratorQuery_TypeQuery:
|
|
|
|
domainType := SecretGeneratorTypeToDomain(q.TypeQuery.GeneratorType)
|
|
|
|
return query.NewSecretGeneratorTypeSearchQuery(int32(domainType))
|
|
|
|
default:
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-fm9es", "List.Query.Invalid")
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateSecretGeneratorToConfig(req *admin_pb.UpdateSecretGeneratorRequest) *crypto.GeneratorConfig {
|
|
|
|
return &crypto.GeneratorConfig{
|
|
|
|
Length: uint(req.Length),
|
|
|
|
Expiry: req.Expiry.AsDuration(),
|
|
|
|
IncludeUpperLetters: req.IncludeUpperLetters,
|
|
|
|
IncludeLowerLetters: req.IncludeLowerLetters,
|
|
|
|
IncludeDigits: req.IncludeDigits,
|
|
|
|
IncludeSymbols: req.IncludeSymbols,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 16:46:02 +02:00
|
|
|
func SecretGeneratorsToPb(generators []*query.SecretGenerator) []*settings_pb.SecretGenerator {
|
|
|
|
list := make([]*settings_pb.SecretGenerator, len(generators))
|
|
|
|
for i, generator := range generators {
|
|
|
|
list[i] = SecretGeneratorToPb(generator)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2022-02-16 16:49:17 +01:00
|
|
|
func SecretGeneratorToPb(generator *query.SecretGenerator) *settings_pb.SecretGenerator {
|
|
|
|
mapped := &settings_pb.SecretGenerator{
|
2023-07-26 13:00:41 +02:00
|
|
|
GeneratorType: SecretGeneratorTypeToPb(generator.GeneratorType),
|
2022-02-16 16:49:17 +01:00
|
|
|
Length: uint32(generator.Length),
|
|
|
|
Expiry: durationpb.New(generator.Expiry),
|
|
|
|
IncludeUpperLetters: generator.IncludeUpperLetters,
|
|
|
|
IncludeLowerLetters: generator.IncludeLowerLetters,
|
|
|
|
IncludeDigits: generator.IncludeDigits,
|
|
|
|
IncludeSymbols: generator.IncludeSymbols,
|
|
|
|
Details: obj_grpc.ToViewDetailsPb(generator.Sequence, generator.CreationDate, generator.ChangeDate, generator.AggregateID),
|
|
|
|
}
|
|
|
|
return mapped
|
|
|
|
}
|
|
|
|
|
|
|
|
func SecretGeneratorTypeToPb(generatorType domain.SecretGeneratorType) settings_pb.SecretGeneratorType {
|
|
|
|
switch generatorType {
|
|
|
|
case domain.SecretGeneratorTypeInitCode:
|
|
|
|
return settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_INIT_CODE
|
|
|
|
case domain.SecretGeneratorTypeVerifyEmailCode:
|
|
|
|
return settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_VERIFY_EMAIL_CODE
|
|
|
|
case domain.SecretGeneratorTypeVerifyPhoneCode:
|
|
|
|
return settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_VERIFY_PHONE_CODE
|
|
|
|
case domain.SecretGeneratorTypePasswordResetCode:
|
|
|
|
return settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_PASSWORD_RESET_CODE
|
|
|
|
case domain.SecretGeneratorTypePasswordlessInitCode:
|
|
|
|
return settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_PASSWORDLESS_INIT_CODE
|
|
|
|
case domain.SecretGeneratorTypeAppSecret:
|
|
|
|
return settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_APP_SECRET
|
2023-07-26 13:00:41 +02:00
|
|
|
case domain.SecretGeneratorTypeOTPSMS:
|
|
|
|
return settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_OTP_SMS
|
|
|
|
case domain.SecretGeneratorTypeOTPEmail:
|
|
|
|
return settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_OTP_EMAIL
|
2022-02-16 16:49:17 +01:00
|
|
|
default:
|
|
|
|
return settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_UNSPECIFIED
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SecretGeneratorTypeToDomain(generatorType settings_pb.SecretGeneratorType) domain.SecretGeneratorType {
|
|
|
|
switch generatorType {
|
|
|
|
case settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_INIT_CODE:
|
|
|
|
return domain.SecretGeneratorTypeInitCode
|
|
|
|
case settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_VERIFY_EMAIL_CODE:
|
|
|
|
return domain.SecretGeneratorTypeVerifyEmailCode
|
|
|
|
case settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_VERIFY_PHONE_CODE:
|
|
|
|
return domain.SecretGeneratorTypeVerifyPhoneCode
|
|
|
|
case settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_PASSWORD_RESET_CODE:
|
|
|
|
return domain.SecretGeneratorTypePasswordResetCode
|
|
|
|
case settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_PASSWORDLESS_INIT_CODE:
|
|
|
|
return domain.SecretGeneratorTypePasswordlessInitCode
|
|
|
|
case settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_APP_SECRET:
|
|
|
|
return domain.SecretGeneratorTypeAppSecret
|
2023-07-26 13:00:41 +02:00
|
|
|
case settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_OTP_SMS:
|
|
|
|
return domain.SecretGeneratorTypeOTPSMS
|
|
|
|
case settings_pb.SecretGeneratorType_SECRET_GENERATOR_TYPE_OTP_EMAIL:
|
|
|
|
return domain.SecretGeneratorTypeOTPEmail
|
2022-02-16 16:49:17 +01:00
|
|
|
default:
|
|
|
|
return domain.SecretGeneratorTypeUnspecified
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 18:24:30 +01:00
|
|
|
func AddSMTPToConfig(req *admin_pb.AddSMTPConfigRequest) *smtp.Config {
|
|
|
|
return &smtp.Config{
|
2024-04-11 09:16:10 +02:00
|
|
|
Description: req.Description,
|
2023-08-29 09:08:24 +02:00
|
|
|
Tls: req.Tls,
|
|
|
|
From: req.SenderAddress,
|
|
|
|
FromName: req.SenderName,
|
|
|
|
ReplyToAddress: req.ReplyToAddress,
|
2022-06-10 12:39:38 +02:00
|
|
|
SMTP: smtp.SMTP{
|
|
|
|
Host: req.Host,
|
|
|
|
User: req.User,
|
|
|
|
Password: req.Password,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 18:24:30 +01:00
|
|
|
func UpdateSMTPToConfig(req *admin_pb.UpdateSMTPConfigRequest) *smtp.Config {
|
|
|
|
return &smtp.Config{
|
2024-04-11 09:16:10 +02:00
|
|
|
Description: req.Description,
|
2023-08-29 09:08:24 +02:00
|
|
|
Tls: req.Tls,
|
|
|
|
From: req.SenderAddress,
|
|
|
|
FromName: req.SenderName,
|
|
|
|
ReplyToAddress: req.ReplyToAddress,
|
2022-02-16 16:49:17 +01:00
|
|
|
SMTP: smtp.SMTP{
|
2024-04-11 09:16:10 +02:00
|
|
|
Host: req.Host,
|
|
|
|
User: req.User,
|
|
|
|
Password: req.Password,
|
2022-02-16 16:49:17 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SMTPConfigToPb(smtp *query.SMTPConfig) *settings_pb.SMTPConfig {
|
|
|
|
mapped := &settings_pb.SMTPConfig{
|
2024-04-11 09:16:10 +02:00
|
|
|
Description: smtp.Description,
|
2023-08-29 09:08:24 +02:00
|
|
|
Tls: smtp.TLS,
|
|
|
|
SenderAddress: smtp.SenderAddress,
|
|
|
|
SenderName: smtp.SenderName,
|
|
|
|
ReplyToAddress: smtp.ReplyToAddress,
|
|
|
|
Host: smtp.Host,
|
|
|
|
User: smtp.User,
|
2024-04-11 09:16:10 +02:00
|
|
|
Details: obj_grpc.ToViewDetailsPb(smtp.Sequence, smtp.CreationDate, smtp.ChangeDate, smtp.ResourceOwner),
|
|
|
|
Id: smtp.ID,
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
|
|
|
return mapped
|
|
|
|
}
|
2022-12-14 07:17:36 +01:00
|
|
|
|
|
|
|
func SecurityPolicyToPb(policy *query.SecurityPolicy) *settings_pb.SecurityPolicy {
|
|
|
|
return &settings_pb.SecurityPolicy{
|
|
|
|
Details: obj_grpc.ToViewDetailsPb(policy.Sequence, policy.CreationDate, policy.ChangeDate, policy.AggregateID),
|
2024-02-28 12:21:11 +02:00
|
|
|
EnableIframeEmbedding: policy.EnableIframeEmbedding,
|
2022-12-14 07:17:36 +01:00
|
|
|
AllowedOrigins: policy.AllowedOrigins,
|
2024-02-28 12:21:11 +02:00
|
|
|
EnableImpersonation: policy.EnableImpersonation,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func securityPolicyToCommand(req *admin_pb.SetSecurityPolicyRequest) *command.SecurityPolicy {
|
|
|
|
return &command.SecurityPolicy{
|
|
|
|
EnableIframeEmbedding: req.GetEnableIframeEmbedding(),
|
|
|
|
AllowedOrigins: req.GetAllowedOrigins(),
|
|
|
|
EnableImpersonation: req.GetEnableImpersonation(),
|
2022-12-14 07:17:36 +01:00
|
|
|
}
|
|
|
|
}
|