2022-04-05 05:58:09 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-03 07:25:25 +00:00
|
|
|
"regexp"
|
2022-04-05 05:58:09 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/http"
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
2022-04-05 05:58:09 +00:00
|
|
|
)
|
|
|
|
|
2022-08-03 07:25:25 +00:00
|
|
|
var (
|
|
|
|
allowDomainRunes = regexp.MustCompile("^[a-zA-Z0-9\\.\\-]+$")
|
|
|
|
)
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func (c *Commands) AddInstanceDomain(ctx context.Context, instanceDomain string) (*domain.ObjectDetails, error) {
|
|
|
|
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
2022-04-25 09:16:36 +00:00
|
|
|
validation := c.addInstanceDomain(instanceAgg, instanceDomain, false)
|
2022-04-05 05:58:09 +00:00
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
2023-10-19 10:19:10 +00:00
|
|
|
EventDate: events[len(events)-1].CreatedAt(),
|
2022-04-05 05:58:09 +00:00
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().InstanceID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-14 12:19:18 +00:00
|
|
|
func (c *Commands) SetPrimaryInstanceDomain(ctx context.Context, instanceDomain string) (*domain.ObjectDetails, error) {
|
|
|
|
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
2022-04-25 08:01:17 +00:00
|
|
|
validation := setPrimaryInstanceDomain(instanceAgg, instanceDomain)
|
2022-04-14 12:19:18 +00:00
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
2023-10-19 10:19:10 +00:00
|
|
|
EventDate: events[len(events)-1].CreatedAt(),
|
2022-04-14 12:19:18 +00:00
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().InstanceID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func (c *Commands) RemoveInstanceDomain(ctx context.Context, instanceDomain string) (*domain.ObjectDetails, error) {
|
|
|
|
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
2022-04-25 08:01:17 +00:00
|
|
|
validation := removeInstanceDomain(instanceAgg, instanceDomain)
|
2022-04-05 05:58:09 +00:00
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
2023-10-19 10:19:10 +00:00
|
|
|
EventDate: events[len(events)-1].CreatedAt(),
|
2022-04-05 05:58:09 +00:00
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().InstanceID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:26:24 +00:00
|
|
|
func (c *Commands) addGeneratedInstanceDomain(ctx context.Context, a *instance.Aggregate, instanceName string) ([]preparation.Validation, error) {
|
|
|
|
domain, err := domain.NewGeneratedInstanceDomain(instanceName, authz.GetInstance(ctx).RequestedDomain())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-25 14:36:10 +00:00
|
|
|
return []preparation.Validation{
|
|
|
|
c.addInstanceDomain(a, domain, true),
|
|
|
|
setPrimaryInstanceDomain(a, domain),
|
2022-05-16 09:26:24 +00:00
|
|
|
}, nil
|
2022-04-21 10:37:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:16:36 +00:00
|
|
|
func (c *Commands) addInstanceDomain(a *instance.Aggregate, instanceDomain string, generated bool) preparation.Validation {
|
2022-04-05 05:58:09 +00:00
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if instanceDomain = strings.TrimSpace(instanceDomain); instanceDomain == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "INST-28nlD", "Errors.Invalid.Argument")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
2022-08-03 07:25:25 +00:00
|
|
|
if !allowDomainRunes.MatchString(instanceDomain) {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "INST-S3v3w", "Errors.Instance.Domain.InvalidCharacter")
|
|
|
|
}
|
2022-04-05 05:58:09 +00:00
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
2022-04-25 08:01:17 +00:00
|
|
|
domainWriteModel, err := getInstanceDomainWriteModel(ctx, filter, instanceDomain)
|
2022-04-05 05:58:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if domainWriteModel.State == domain.InstanceDomainStateActive {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowAlreadyExists(nil, "INST-i2nl", "Errors.Instance.Domain.AlreadyExists")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
2022-04-21 10:37:39 +00:00
|
|
|
events := []eventstore.Command{
|
|
|
|
instance.NewDomainAddedEvent(ctx, &a.Aggregate, instanceDomain, generated),
|
|
|
|
}
|
2022-07-20 09:20:49 +00:00
|
|
|
consoleChangeEvent, err := c.updateConsoleRedirectURIs(ctx, filter, instanceDomain)
|
2022-04-14 12:19:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-07 14:15:19 +00:00
|
|
|
if consoleChangeEvent == nil {
|
|
|
|
return events, nil
|
|
|
|
}
|
2022-07-20 09:20:49 +00:00
|
|
|
return append(events, consoleChangeEvent), nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 10:37:39 +00:00
|
|
|
|
2022-07-20 09:20:49 +00:00
|
|
|
func (c *Commands) prepareUpdateConsoleRedirectURIs(instanceDomain string) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if instanceDomain = strings.TrimSpace(instanceDomain); instanceDomain == "" {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "INST-E3j3s", "Errors.Invalid.Argument")
|
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
consoleChangeEvent, err := c.updateConsoleRedirectURIs(ctx, filter, instanceDomain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-07 14:15:19 +00:00
|
|
|
if consoleChangeEvent == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-07-20 09:20:49 +00:00
|
|
|
return []eventstore.Command{
|
|
|
|
consoleChangeEvent,
|
|
|
|
}, nil
|
2022-04-14 12:19:18 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 09:20:49 +00:00
|
|
|
func (c *Commands) updateConsoleRedirectURIs(ctx context.Context, filter preparation.FilterToQueryReducer, instanceDomain string) (*project.OIDCConfigChangedEvent, error) {
|
|
|
|
appWriteModel, err := getOIDCAppWriteModel(ctx, filter, authz.GetInstance(ctx).ProjectID(), authz.GetInstance(ctx).ConsoleApplicationID(), "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !appWriteModel.State.Exists() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
redirectURI := http.BuildHTTP(instanceDomain, c.externalPort, c.externalSecure) + consoleRedirectPath
|
|
|
|
changes := make([]project.OIDCConfigChanges, 0, 2)
|
|
|
|
if !containsURI(appWriteModel.RedirectUris, redirectURI) {
|
|
|
|
changes = append(changes, project.ChangeRedirectURIs(append(appWriteModel.RedirectUris, redirectURI)))
|
|
|
|
}
|
|
|
|
postLogoutRedirectURI := http.BuildHTTP(instanceDomain, c.externalPort, c.externalSecure) + consolePostLogoutPath
|
|
|
|
if !containsURI(appWriteModel.PostLogoutRedirectUris, postLogoutRedirectURI) {
|
|
|
|
changes = append(changes, project.ChangePostLogoutRedirectURIs(append(appWriteModel.PostLogoutRedirectUris, postLogoutRedirectURI)))
|
|
|
|
}
|
2023-07-07 14:15:19 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-07-20 09:20:49 +00:00
|
|
|
return project.NewOIDCConfigChangedEvent(
|
|
|
|
ctx,
|
|
|
|
ProjectAggregateFromWriteModel(&appWriteModel.WriteModel),
|
|
|
|
appWriteModel.AppID,
|
|
|
|
changes,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
func setPrimaryInstanceDomain(a *instance.Aggregate, instanceDomain string) preparation.Validation {
|
2022-04-14 12:19:18 +00:00
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if instanceDomain = strings.TrimSpace(instanceDomain); instanceDomain == "" {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "INST-9mWjf", "Errors.Invalid.Argument")
|
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
2022-04-25 08:01:17 +00:00
|
|
|
domainWriteModel, err := getInstanceDomainWriteModel(ctx, filter, instanceDomain)
|
2022-04-14 12:19:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !domainWriteModel.State.Exists() {
|
|
|
|
return nil, errors.ThrowNotFound(nil, "INSTANCE-9nkWf", "Errors.Instance.Domain.NotFound")
|
|
|
|
}
|
|
|
|
return []eventstore.Command{instance.NewDomainPrimarySetEvent(ctx, &a.Aggregate, instanceDomain)}, nil
|
2022-04-05 05:58:09 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
func removeInstanceDomain(a *instance.Aggregate, instanceDomain string) preparation.Validation {
|
2022-04-05 05:58:09 +00:00
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if instanceDomain = strings.TrimSpace(instanceDomain); instanceDomain == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "INST-39nls", "Errors.Invalid.Argument")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
2022-04-25 08:01:17 +00:00
|
|
|
domainWriteModel, err := getInstanceDomainWriteModel(ctx, filter, instanceDomain)
|
2022-04-05 05:58:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if domainWriteModel.State != domain.InstanceDomainStateActive {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowNotFound(nil, "INSTANCE-8ls9f", "Errors.Instance.Domain.NotFound")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
if domainWriteModel.Generated {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "INSTANCE-9hn3n", "Errors.Instance.Domain.GeneratedNotRemovable")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
return []eventstore.Command{instance.NewDomainRemovedEvent(ctx, &a.Aggregate, instanceDomain)}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
func getInstanceDomainWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer, domain string) (*InstanceDomainWriteModel, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
domainWriteModel := NewInstanceDomainWriteModel(ctx, domain)
|
2022-04-25 08:01:17 +00:00
|
|
|
events, err := filter(ctx, domainWriteModel.Query())
|
2022-04-05 05:58:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-25 08:01:17 +00:00
|
|
|
if len(events) == 0 {
|
|
|
|
return domainWriteModel, nil
|
|
|
|
}
|
|
|
|
domainWriteModel.AppendEvents(events...)
|
|
|
|
err = domainWriteModel.Reduce()
|
|
|
|
return domainWriteModel, err
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
2022-07-20 09:20:49 +00:00
|
|
|
|
|
|
|
func containsURI(uris []string, uri string) bool {
|
|
|
|
for _, u := range uris {
|
|
|
|
if u == uri {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-10-20 12:36:52 +00:00
|
|
|
|
|
|
|
func (c *Commands) getInstanceDomainsWriteModel(ctx context.Context, instanceID string) (*InstanceDomainsWriteModel, error) {
|
|
|
|
domainsWriteModel := NewInstanceDomainsWriteModel(instanceID)
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, domainsWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return domainsWriteModel, nil
|
|
|
|
}
|