2021-01-28 05:35:26 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-18 13:48:27 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2021-01-28 05:35:26 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/project"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r *CommandSide) AddOIDCApplication(ctx context.Context, application *domain.OIDCApp, resourceOwner string) (_ *domain.OIDCApp, err error) {
|
|
|
|
project, err := r.getProjectByID(ctx, application.AggregateID, resourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addedApplication := NewOIDCApplicationWriteModel(application.AggregateID, resourceOwner)
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&addedApplication.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
events, stringPw, err := r.addOIDCApplication(ctx, projectAgg, project, application, resourceOwner)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addedApplication.AppID = application.AppID
|
2021-02-18 13:48:27 +00:00
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addedApplication, pushedEvents...)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := oidcWriteModelToOIDCConfig(addedApplication)
|
|
|
|
result.ClientSecretString = stringPw
|
|
|
|
result.FillCompliance()
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func (r *CommandSide) addOIDCApplication(ctx context.Context, projectAgg *eventstore.Aggregate, proj *domain.Project, oidcApp *domain.OIDCApp, resourceOwner string) (events []eventstore.EventPusher, stringPW string, err error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
if !oidcApp.IsValid() {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, "", caos_errs.ThrowPreconditionFailed(nil, "PROJECT-Bff2g", "Errors.Application.Invalid")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
oidcApp.AppID, err = r.idGenerator.Next()
|
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, "", err
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
events = []eventstore.EventPusher{
|
|
|
|
project.NewApplicationAddedEvent(ctx, projectAgg, oidcApp.AppID, oidcApp.AppName, resourceOwner),
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
|
|
|
|
var stringPw string
|
|
|
|
err = oidcApp.GenerateNewClientID(r.idGenerator, proj)
|
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, "", err
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
stringPw, err = oidcApp.GenerateClientSecretIfNeeded(r.applicationSecretGenerator)
|
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, "", err
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
events = append(events, project.NewOIDCConfigAddedEvent(ctx,
|
|
|
|
projectAgg,
|
2021-01-28 05:35:26 +00:00
|
|
|
oidcApp.OIDCVersion,
|
|
|
|
oidcApp.AppID,
|
|
|
|
oidcApp.ClientID,
|
|
|
|
oidcApp.ClientSecret,
|
|
|
|
oidcApp.RedirectUris,
|
|
|
|
oidcApp.ResponseTypes,
|
|
|
|
oidcApp.GrantTypes,
|
|
|
|
oidcApp.ApplicationType,
|
|
|
|
oidcApp.AuthMethodType,
|
|
|
|
oidcApp.PostLogoutRedirectUris,
|
|
|
|
oidcApp.DevMode,
|
|
|
|
oidcApp.AccessTokenType,
|
|
|
|
oidcApp.AccessTokenRoleAssertion,
|
|
|
|
oidcApp.IDTokenRoleAssertion,
|
|
|
|
oidcApp.IDTokenUserinfoAssertion,
|
|
|
|
oidcApp.ClockSkew))
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
return events, stringPw, nil
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) ChangeOIDCApplication(ctx context.Context, oidc *domain.OIDCApp, resourceOwner string) (*domain.OIDCApp, error) {
|
|
|
|
if !oidc.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-1m900", "Errors.Project.App.OIDCConfigInvalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
existingOIDC, err := r.getOIDCAppWriteModel(ctx, oidc.AggregateID, oidc.AppID, resourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existingOIDC.State == domain.AppStateUnspecified || existingOIDC.State == domain.AppStateRemoved {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-2n8uU", "Errors.Project.App.NotExisting")
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingOIDC.WriteModel)
|
2021-01-28 05:35:26 +00:00
|
|
|
changedEvent, hasChanged, err := existingOIDC.NewChangedEvent(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
projectAgg,
|
2021-01-28 05:35:26 +00:00
|
|
|
oidc.AppID,
|
|
|
|
oidc.ClientID,
|
|
|
|
oidc.RedirectUris,
|
|
|
|
oidc.PostLogoutRedirectUris,
|
|
|
|
oidc.ResponseTypes,
|
|
|
|
oidc.GrantTypes,
|
|
|
|
oidc.ApplicationType,
|
|
|
|
oidc.AuthMethodType,
|
|
|
|
oidc.OIDCVersion,
|
|
|
|
oidc.AccessTokenType,
|
|
|
|
oidc.DevMode,
|
|
|
|
oidc.AccessTokenRoleAssertion,
|
|
|
|
oidc.IDTokenRoleAssertion,
|
|
|
|
oidc.IDTokenUserinfoAssertion,
|
|
|
|
oidc.ClockSkew)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-1m88i", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, changedEvent)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
err = AppendAndReduce(existingOIDC, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
result := oidcWriteModelToOIDCConfig(existingOIDC)
|
|
|
|
result.FillCompliance()
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) ChangeOIDCApplicationSecret(ctx context.Context, projectID, appID, resourceOwner string) (*domain.OIDCApp, error) {
|
|
|
|
if projectID == "" || appID == "" {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-99i83", "Errors.IDMissing")
|
|
|
|
}
|
|
|
|
|
|
|
|
existingOIDC, err := r.getOIDCAppWriteModel(ctx, projectID, appID, resourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existingOIDC.State == domain.AppStateUnspecified || existingOIDC.State == domain.AppStateRemoved {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-2g66f", "Errors.Project.App.NotExisting")
|
|
|
|
}
|
|
|
|
cryptoSecret, stringPW, err := domain.NewClientSecret(r.applicationSecretGenerator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingOIDC.WriteModel)
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, project.NewOIDCConfigSecretChangedEvent(ctx, projectAgg, appID, cryptoSecret))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingOIDC, pushedEvents...)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := oidcWriteModelToOIDCConfig(existingOIDC)
|
|
|
|
result.ClientSecretString = stringPW
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
func (r *CommandSide) getOIDCAppWriteModel(ctx context.Context, projectID, appID, resourceOwner string) (*OIDCApplicationWriteModel, error) {
|
|
|
|
appWriteModel := NewOIDCApplicationWriteModelWithAppIDC(projectID, appID, resourceOwner)
|
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, appWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return appWriteModel, nil
|
|
|
|
}
|