2022-04-21 10:37:39 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2022-09-23 12:08:10 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
"github.com/zitadel/oidc/v2/pkg/oidc"
|
2022-04-28 08:30:41 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/authn"
|
2022-04-26 23:01:45 +00:00
|
|
|
instance_grpc "github.com/zitadel/zitadel/internal/api/grpc/instance"
|
2023-01-17 20:35:41 +00:00
|
|
|
member_grpc "github.com/zitadel/zitadel/internal/api/grpc/member"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/object"
|
2022-12-09 13:04:33 +00:00
|
|
|
z_oidc "github.com/zitadel/zitadel/internal/api/oidc"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
2022-09-23 12:08:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
|
|
|
instance_pb "github.com/zitadel/zitadel/pkg/grpc/instance"
|
|
|
|
system_pb "github.com/zitadel/zitadel/pkg/grpc/system"
|
2022-04-21 10:37:39 +00:00
|
|
|
)
|
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
func CreateInstancePbToSetupInstance(req *system_pb.CreateInstanceRequest, defaultInstance command.InstanceSetup, externalDomain string) *command.InstanceSetup {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance := defaultInstance
|
2022-12-09 13:04:33 +00:00
|
|
|
if req.InstanceName != "" {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.InstanceName = req.InstanceName
|
|
|
|
instance.Org.Name = req.InstanceName
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
if req.CustomDomain != "" {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.CustomDomain = req.CustomDomain
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
if req.FirstOrgName != "" {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.Org.Name = req.FirstOrgName
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if user := req.GetMachine(); user != nil {
|
2023-01-03 09:16:36 +00:00
|
|
|
defaultMachine := instance.Org.Machine
|
|
|
|
if defaultMachine == nil {
|
|
|
|
defaultMachine = new(command.AddMachine)
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.Org.Machine = createInstancePbToAddMachine(user, *defaultMachine)
|
|
|
|
instance.Org.Human = nil
|
|
|
|
} else if user := req.GetHuman(); user != nil {
|
|
|
|
defaultHuman := instance.Org.Human
|
|
|
|
if instance.Org.Human != nil {
|
|
|
|
defaultHuman = new(command.AddHuman)
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.Org.Human = createInstancePbToAddHuman(user, *defaultHuman, instance.DomainPolicy.UserLoginMustBeDomain, instance.Org.Name, externalDomain)
|
|
|
|
instance.Org.Machine = nil
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 09:16:36 +00:00
|
|
|
if lang := language.Make(req.DefaultLanguage); !lang.IsRoot() {
|
|
|
|
instance.DefaultLanguage = lang
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 09:16:36 +00:00
|
|
|
return &instance
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 09:16:36 +00:00
|
|
|
func createInstancePbToAddHuman(req *system_pb.CreateInstanceRequest_Human, defaultHuman command.AddHuman, userLoginMustBeDomain bool, org, externalDomain string) *command.AddHuman {
|
|
|
|
user := defaultHuman
|
|
|
|
if req.Email != nil {
|
2023-03-14 19:20:38 +00:00
|
|
|
user.Email.Address = domain.EmailAddress(req.Email.Email)
|
2023-01-03 09:16:36 +00:00
|
|
|
user.Email.Verified = req.Email.IsEmailVerified
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
if req.Profile != nil {
|
|
|
|
if req.Profile.FirstName != "" {
|
|
|
|
user.FirstName = req.Profile.FirstName
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
if req.Profile.LastName != "" {
|
|
|
|
user.LastName = req.Profile.LastName
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
if req.Profile.PreferredLanguage != "" {
|
|
|
|
lang, err := language.Parse(req.Profile.PreferredLanguage)
|
2022-12-09 13:04:33 +00:00
|
|
|
if err == nil {
|
2023-01-03 09:16:36 +00:00
|
|
|
user.PreferredLanguage = lang
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check if default username is email style or else append @<orgname>.<custom-domain>
|
|
|
|
// this way we have the same value as before changing `UserLoginMustBeDomain` to false
|
2023-01-03 09:16:36 +00:00
|
|
|
if !userLoginMustBeDomain && !strings.Contains(user.Username, "@") {
|
|
|
|
user.Username = user.Username + "@" + domain.NewIAMDomainName(org, externalDomain)
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
if req.UserName != "" {
|
|
|
|
user.Username = req.UserName
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 09:16:36 +00:00
|
|
|
if req.Password != nil {
|
|
|
|
user.Password = req.Password.Password
|
|
|
|
user.PasswordChangeRequired = req.Password.PasswordChangeRequired
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
return &user
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 09:16:36 +00:00
|
|
|
func createInstancePbToAddMachine(req *system_pb.CreateInstanceRequest_Machine, defaultMachine command.AddMachine) (machine *command.AddMachine) {
|
|
|
|
machine = new(command.AddMachine)
|
2022-12-09 13:04:33 +00:00
|
|
|
if defaultMachine.Machine != nil {
|
2023-01-03 09:16:36 +00:00
|
|
|
machineCopy := *defaultMachine.Machine
|
|
|
|
machine.Machine = &machineCopy
|
|
|
|
} else {
|
|
|
|
machine.Machine = new(command.Machine)
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
|
|
|
|
if req.UserName != "" {
|
|
|
|
machine.Machine.Username = req.UserName
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
if req.Name != "" {
|
|
|
|
machine.Machine.Name = req.Name
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 09:16:36 +00:00
|
|
|
if defaultMachine.Pat != nil || req.PersonalAccessToken != nil {
|
|
|
|
pat := command.AddPat{
|
|
|
|
// Scopes are currently static and can not be overwritten
|
2023-07-07 15:31:42 +00:00
|
|
|
Scopes: []string{oidc.ScopeOpenID, oidc.ScopeProfile, z_oidc.ScopeUserMetaData, z_oidc.ScopeResourceOwner},
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-06-15 06:16:39 +00:00
|
|
|
if req.GetPersonalAccessToken().GetExpirationDate().IsValid() {
|
2023-01-03 09:16:36 +00:00
|
|
|
pat.ExpirationDate = req.PersonalAccessToken.ExpirationDate.AsTime()
|
2023-06-15 06:16:39 +00:00
|
|
|
} else if defaultMachine.Pat != nil && !defaultMachine.Pat.ExpirationDate.IsZero() {
|
|
|
|
pat.ExpirationDate = defaultMachine.Pat.ExpirationDate
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
machine.Pat = &pat
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 09:16:36 +00:00
|
|
|
if defaultMachine.MachineKey != nil || req.MachineKey != nil {
|
2022-12-09 13:04:33 +00:00
|
|
|
machineKey := command.AddMachineKey{}
|
|
|
|
if defaultMachine.MachineKey != nil {
|
|
|
|
machineKey = *defaultMachine.MachineKey
|
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
if req.MachineKey != nil {
|
|
|
|
if req.MachineKey.Type != 0 {
|
|
|
|
machineKey.Type = authn.KeyTypeToDomain(req.MachineKey.Type)
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
if req.MachineKey.ExpirationDate.IsValid() {
|
|
|
|
machineKey.ExpirationDate = req.MachineKey.ExpirationDate.AsTime()
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
machine.MachineKey = &machineKey
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
|
|
|
|
return machine
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 12:08:10 +00:00
|
|
|
func AddInstancePbToSetupInstance(req *system_pb.AddInstanceRequest, defaultInstance command.InstanceSetup, externalDomain string) *command.InstanceSetup {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance := defaultInstance
|
|
|
|
|
2022-04-21 10:37:39 +00:00
|
|
|
if req.InstanceName != "" {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.InstanceName = req.InstanceName
|
|
|
|
instance.Org.Name = req.InstanceName
|
2022-04-21 10:37:39 +00:00
|
|
|
}
|
|
|
|
if req.CustomDomain != "" {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.CustomDomain = req.CustomDomain
|
2022-04-21 10:37:39 +00:00
|
|
|
}
|
|
|
|
if req.FirstOrgName != "" {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.Org.Name = req.FirstOrgName
|
|
|
|
}
|
|
|
|
|
|
|
|
if defaultInstance.Org.Human != nil {
|
|
|
|
// used to not overwrite the default human later
|
|
|
|
humanCopy := *defaultInstance.Org.Human
|
|
|
|
instance.Org.Human = &humanCopy
|
|
|
|
} else {
|
|
|
|
instance.Org.Human = new(command.AddHuman)
|
2022-04-21 10:37:39 +00:00
|
|
|
}
|
2022-04-27 15:18:34 +00:00
|
|
|
if req.OwnerEmail.Email != "" {
|
2023-03-14 19:20:38 +00:00
|
|
|
instance.Org.Human.Email.Address = domain.EmailAddress(req.OwnerEmail.Email)
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.Org.Human.Email.Verified = req.OwnerEmail.IsEmailVerified
|
2022-04-21 10:37:39 +00:00
|
|
|
}
|
2022-04-27 15:18:34 +00:00
|
|
|
if req.OwnerProfile != nil {
|
|
|
|
if req.OwnerProfile.FirstName != "" {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.Org.Human.FirstName = req.OwnerProfile.FirstName
|
2022-04-27 15:18:34 +00:00
|
|
|
}
|
|
|
|
if req.OwnerProfile.LastName != "" {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.Org.Human.LastName = req.OwnerProfile.LastName
|
2022-04-27 15:18:34 +00:00
|
|
|
}
|
|
|
|
if req.OwnerProfile.PreferredLanguage != "" {
|
|
|
|
lang, err := language.Parse(req.OwnerProfile.PreferredLanguage)
|
|
|
|
if err == nil {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.Org.Human.PreferredLanguage = lang
|
2022-04-27 15:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-21 10:37:39 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
if req.OwnerUserName != "" {
|
|
|
|
instance.Org.Human.Username = req.OwnerUserName
|
|
|
|
}
|
2022-09-23 12:08:10 +00:00
|
|
|
// check if default username is email style or else append @<orgname>.<custom-domain>
|
|
|
|
// this way we have the same value as before changing `UserLoginMustBeDomain` to false
|
2023-01-03 09:16:36 +00:00
|
|
|
if !instance.DomainPolicy.UserLoginMustBeDomain && !strings.Contains(instance.Org.Human.Username, "@") {
|
|
|
|
instance.Org.Human.Username = instance.Org.Human.Username + "@" + domain.NewIAMDomainName(instance.Org.Name, externalDomain)
|
2022-04-21 10:37:39 +00:00
|
|
|
}
|
2022-04-27 15:18:34 +00:00
|
|
|
if req.OwnerPassword != nil {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.Org.Human.Password = req.OwnerPassword.Password
|
|
|
|
instance.Org.Human.PasswordChangeRequired = req.OwnerPassword.PasswordChangeRequired
|
2022-04-21 10:37:39 +00:00
|
|
|
}
|
2022-05-03 13:58:38 +00:00
|
|
|
if lang := language.Make(req.DefaultLanguage); lang != language.Und {
|
2023-01-03 09:16:36 +00:00
|
|
|
instance.DefaultLanguage = lang
|
2022-05-03 13:58:38 +00:00
|
|
|
}
|
2022-04-27 15:18:34 +00:00
|
|
|
|
2023-01-03 09:16:36 +00:00
|
|
|
return &instance
|
2022-04-21 10:37:39 +00:00
|
|
|
}
|
2023-01-03 09:16:36 +00:00
|
|
|
|
2022-04-21 10:37:39 +00:00
|
|
|
func ListInstancesRequestToModel(req *system_pb.ListInstancesRequest) (*query.InstanceSearchQueries, error) {
|
|
|
|
offset, limit, asc := object.ListQueryToModel(req.Query)
|
|
|
|
queries, err := instance_grpc.InstanceQueriesToModel(req.Queries)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &query.InstanceSearchQueries{
|
|
|
|
SearchRequest: query.SearchRequest{
|
|
|
|
Offset: offset,
|
|
|
|
Limit: limit,
|
|
|
|
Asc: asc,
|
|
|
|
SortingColumn: fieldNameToInstanceColumn(req.SortingColumn),
|
|
|
|
},
|
|
|
|
Queries: queries,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fieldNameToInstanceColumn(fieldName instance_pb.FieldName) query.Column {
|
|
|
|
switch fieldName {
|
|
|
|
case instance_pb.FieldName_FIELD_NAME_ID:
|
|
|
|
return query.InstanceColumnID
|
|
|
|
case instance_pb.FieldName_FIELD_NAME_NAME:
|
|
|
|
return query.InstanceColumnName
|
|
|
|
case instance_pb.FieldName_FIELD_NAME_CREATION_DATE:
|
|
|
|
return query.InstanceColumnCreationDate
|
|
|
|
default:
|
|
|
|
return query.Column{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListInstanceDomainsRequestToModel(req *system_pb.ListDomainsRequest) (*query.InstanceDomainSearchQueries, error) {
|
|
|
|
offset, limit, asc := object.ListQueryToModel(req.Query)
|
|
|
|
queries, err := instance_grpc.DomainQueriesToModel(req.Queries)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &query.InstanceDomainSearchQueries{
|
|
|
|
SearchRequest: query.SearchRequest{
|
|
|
|
Offset: offset,
|
|
|
|
Limit: limit,
|
|
|
|
Asc: asc,
|
|
|
|
SortingColumn: fieldNameToInstanceDomainColumn(req.SortingColumn),
|
|
|
|
},
|
|
|
|
Queries: queries,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fieldNameToInstanceDomainColumn(fieldName instance_pb.DomainFieldName) query.Column {
|
|
|
|
switch fieldName {
|
|
|
|
case instance_pb.DomainFieldName_DOMAIN_FIELD_NAME_DOMAIN:
|
|
|
|
return query.InstanceDomainDomainCol
|
|
|
|
case instance_pb.DomainFieldName_DOMAIN_FIELD_NAME_GENERATED:
|
|
|
|
return query.InstanceDomainIsGeneratedCol
|
|
|
|
case instance_pb.DomainFieldName_DOMAIN_FIELD_NAME_PRIMARY:
|
|
|
|
return query.InstanceDomainIsPrimaryCol
|
|
|
|
case instance_pb.DomainFieldName_DOMAIN_FIELD_NAME_CREATION_DATE:
|
|
|
|
return query.InstanceDomainCreationDateCol
|
|
|
|
default:
|
|
|
|
return query.Column{}
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 20:35:41 +00:00
|
|
|
|
|
|
|
func ListIAMMembersRequestToQuery(req *system_pb.ListIAMMembersRequest) (*query.IAMMembersQuery, error) {
|
|
|
|
offset, limit, asc := object.ListQueryToModel(req.Query)
|
|
|
|
queries, err := member_grpc.MemberQueriesToQuery(req.Queries)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &query.IAMMembersQuery{
|
|
|
|
MembersQuery: query.MembersQuery{
|
|
|
|
SearchRequest: query.SearchRequest{
|
|
|
|
Offset: offset,
|
|
|
|
Limit: limit,
|
|
|
|
Asc: asc,
|
|
|
|
// SortingColumn: model.IAMMemberSearchKey, //TOOD: not implemented in proto
|
|
|
|
},
|
|
|
|
Queries: queries,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|