2021-02-22 11:27:47 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-12 14:20:17 +00:00
|
|
|
"strings"
|
2021-10-26 09:33:59 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
project_repo "github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-02-22 11:27:47 +00:00
|
|
|
)
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
type addAPIApp struct {
|
|
|
|
AddApp
|
|
|
|
AuthMethodType domain.APIAuthMethodType
|
|
|
|
|
|
|
|
ClientID string
|
2024-04-05 09:35:49 +00:00
|
|
|
EncodedHash string
|
2022-04-12 14:20:17 +00:00
|
|
|
ClientSecretPlain string
|
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func (c *Commands) AddAPIAppCommand(app *addAPIApp) preparation.Validation {
|
2022-04-12 14:20:17 +00:00
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if app.ID == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "PROJE-XHsKt", "Errors.Invalid.Argument")
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
if app.Name = strings.TrimSpace(app.Name); app.Name == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "PROJE-F7g21", "Errors.Invalid.Argument")
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
project, err := projectWriteModel(ctx, filter, app.Aggregate.ID, app.Aggregate.ResourceOwner)
|
|
|
|
if err != nil || !project.State.Valid() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(err, "PROJE-Sf2gb", "Errors.Project.NotFound")
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 08:31:40 +00:00
|
|
|
app.ClientID, err = c.idGenerator.Next()
|
2022-04-12 14:20:17 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "V2-f0pgP", "Errors.Internal")
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if app.AuthMethodType == domain.APIAuthMethodTypeBasic {
|
2024-04-05 09:35:49 +00:00
|
|
|
app.EncodedHash, app.ClientSecretPlain, err = c.newHashedSecret(ctx, filter)
|
2022-04-12 14:20:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return []eventstore.Command{
|
|
|
|
project_repo.NewApplicationAddedEvent(
|
|
|
|
ctx,
|
|
|
|
&app.Aggregate.Aggregate,
|
|
|
|
app.ID,
|
|
|
|
app.Name,
|
|
|
|
),
|
|
|
|
project_repo.NewAPIConfigAddedEvent(
|
|
|
|
ctx,
|
|
|
|
&app.Aggregate.Aggregate,
|
|
|
|
app.ID,
|
|
|
|
app.ClientID,
|
2024-04-05 09:35:49 +00:00
|
|
|
app.EncodedHash,
|
2022-04-12 14:20:17 +00:00
|
|
|
app.AuthMethodType,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func (c *Commands) AddAPIApplicationWithID(ctx context.Context, apiApp *domain.APIApp, resourceOwner, appID string) (_ *domain.APIApp, err error) {
|
2024-06-19 10:56:33 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-07-28 13:42:35 +00:00
|
|
|
existingAPI, err := c.getAPIAppWriteModel(ctx, apiApp.AggregateID, appID, resourceOwner)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
if existingAPI.State != domain.AppStateUnspecified {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "PROJECT-mabu12", "Errors.Project.App.AlreadyExisting")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2024-07-04 08:31:40 +00:00
|
|
|
_, err = c.getProjectByID(ctx, apiApp.AggregateID, resourceOwner)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(err, "PROJECT-9fnsa", "Errors.Project.NotFound")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
|
2024-07-04 08:31:40 +00:00
|
|
|
return c.addAPIApplicationWithID(ctx, apiApp, resourceOwner, appID)
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func (c *Commands) AddAPIApplication(ctx context.Context, apiApp *domain.APIApp, resourceOwner string) (_ *domain.APIApp, err error) {
|
2022-07-28 13:42:35 +00:00
|
|
|
if apiApp == nil || apiApp.AggregateID == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "PROJECT-5m9E", "Errors.Project.App.Invalid")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2024-07-04 08:31:40 +00:00
|
|
|
_, err = c.getProjectByID(ctx, apiApp.AggregateID, resourceOwner)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(err, "PROJECT-9fnsf", "Errors.Project.NotFound")
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !apiApp.IsValid() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "PROJECT-Bff2g", "Errors.Project.App.Invalid")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:42:35 +00:00
|
|
|
appID, err := c.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-04 08:31:40 +00:00
|
|
|
return c.addAPIApplicationWithID(ctx, apiApp, resourceOwner, appID)
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 08:31:40 +00:00
|
|
|
func (c *Commands) addAPIApplicationWithID(ctx context.Context, apiApp *domain.APIApp, resourceOwner string, appID string) (_ *domain.APIApp, err error) {
|
2024-06-19 10:56:33 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-07-28 13:42:35 +00:00
|
|
|
apiApp.AppID = appID
|
|
|
|
|
|
|
|
addedApplication := NewAPIApplicationWriteModel(apiApp.AggregateID, resourceOwner)
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&addedApplication.WriteModel)
|
|
|
|
|
|
|
|
events := []eventstore.Command{
|
|
|
|
project_repo.NewApplicationAddedEvent(ctx, projectAgg, apiApp.AppID, apiApp.AppName),
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
var plain string
|
2024-07-04 08:31:40 +00:00
|
|
|
err = domain.SetNewClientID(apiApp, c.idGenerator)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
2022-07-28 13:42:35 +00:00
|
|
|
return nil, err
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
plain, err = domain.SetNewClientSecretIfNeeded(apiApp, func() (string, string, error) {
|
|
|
|
return c.newHashedSecret(ctx, c.eventstore.Filter) //nolint:staticcheck
|
|
|
|
})
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
2022-07-28 13:42:35 +00:00
|
|
|
return nil, err
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
events = append(events, project_repo.NewAPIConfigAddedEvent(ctx,
|
2021-02-22 11:27:47 +00:00
|
|
|
projectAgg,
|
2022-07-28 13:42:35 +00:00
|
|
|
apiApp.AppID,
|
|
|
|
apiApp.ClientID,
|
2024-04-05 09:35:49 +00:00
|
|
|
apiApp.EncodedHash,
|
2022-07-28 13:42:35 +00:00
|
|
|
apiApp.AuthMethodType))
|
2021-02-22 11:27:47 +00:00
|
|
|
|
2022-07-28 13:42:35 +00:00
|
|
|
addedApplication.AppID = apiApp.AppID
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addedApplication, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := apiWriteModelToAPIConfig(addedApplication)
|
2024-04-05 09:35:49 +00:00
|
|
|
result.ClientSecretString = plain
|
2022-07-28 13:42:35 +00:00
|
|
|
return result, nil
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) ChangeAPIApplication(ctx context.Context, apiApp *domain.APIApp, resourceOwner string) (*domain.APIApp, error) {
|
2021-04-09 09:50:06 +00:00
|
|
|
if apiApp.AppID == "" || apiApp.AggregateID == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-1m900", "Errors.Project.App.APIConfigInvalid")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
existingAPI, err := c.getAPIAppWriteModel(ctx, apiApp.AggregateID, apiApp.AppID, resourceOwner)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existingAPI.State == domain.AppStateUnspecified || existingAPI.State == domain.AppStateRemoved {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-2n8uU", "Errors.Project.App.NotExisting")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2021-06-27 09:20:59 +00:00
|
|
|
if !existingAPI.IsAPI() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-Gnwt3", "Errors.Project.App.IsNotAPI")
|
2021-06-27 09:20:59 +00:00
|
|
|
}
|
2021-02-22 11:27:47 +00:00
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingAPI.WriteModel)
|
|
|
|
changedEvent, hasChanged, err := existingAPI.NewChangedEvent(
|
|
|
|
ctx,
|
|
|
|
projectAgg,
|
|
|
|
apiApp.AppID,
|
|
|
|
apiApp.AuthMethodType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-1m88i", "Errors.NoChangesFound")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingAPI, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-15 11:51:15 +00:00
|
|
|
return apiWriteModelToAPIConfig(existingAPI), nil
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func (c *Commands) ChangeAPIApplicationSecret(ctx context.Context, projectID, appID, resourceOwner string) (*domain.APIApp, error) {
|
2021-02-22 11:27:47 +00:00
|
|
|
if projectID == "" || appID == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-99i83", "Errors.IDMissing")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
existingAPI, err := c.getAPIAppWriteModel(ctx, projectID, appID, resourceOwner)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existingAPI.State == domain.AppStateUnspecified || existingAPI.State == domain.AppStateRemoved {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-2g66f", "Errors.Project.App.NotExisting")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2021-06-27 09:20:59 +00:00
|
|
|
if !existingAPI.IsAPI() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-aeH4", "Errors.Project.App.IsNotAPI")
|
2021-06-27 09:20:59 +00:00
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
encodedHash, plain, err := c.newHashedSecret(ctx, c.eventstore.Filter) //nolint:staticcheck
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingAPI.WriteModel)
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, project_repo.NewAPIConfigSecretChangedEvent(ctx, projectAgg, appID, encodedHash))
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingAPI, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := apiWriteModelToAPIConfig(existingAPI)
|
2024-04-05 09:35:49 +00:00
|
|
|
result.ClientSecretString = plain
|
2021-02-22 11:27:47 +00:00
|
|
|
return result, err
|
|
|
|
}
|
2021-10-26 09:33:59 +00:00
|
|
|
|
|
|
|
func (c *Commands) VerifyAPIClientSecret(ctx context.Context, projectID, appID, secret string) (err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
app, err := c.getAPIAppWriteModel(ctx, projectID, appID, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !app.State.Exists() {
|
2024-04-05 09:35:49 +00:00
|
|
|
return zerrors.ThrowPreconditionFailed(nil, "COMMAND-DFnbf", "Errors.Project.App.NotExisting")
|
2021-10-26 09:33:59 +00:00
|
|
|
}
|
|
|
|
if !app.IsAPI() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-Bf3fw", "Errors.Project.App.IsNotAPI")
|
2021-10-26 09:33:59 +00:00
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
if app.HashedSecret == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowPreconditionFailed(nil, "COMMAND-D3t5g", "Errors.Project.App.APIConfigInvalid")
|
2021-10-26 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&app.WriteModel)
|
2024-04-05 09:35:49 +00:00
|
|
|
ctx, spanPasswordComparison := tracing.NewNamedSpan(ctx, "passwap.Verify")
|
|
|
|
updated, err := c.secretHasher.Verify(app.HashedSecret, secret)
|
2021-10-26 09:33:59 +00:00
|
|
|
spanPasswordComparison.EndWithError(err)
|
2024-08-28 18:19:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return zerrors.ThrowInvalidArgument(err, "COMMAND-SADfg", "Errors.Project.App.ClientSecretInvalid")
|
2021-10-26 09:33:59 +00:00
|
|
|
}
|
2024-08-28 18:19:50 +00:00
|
|
|
if updated != "" {
|
|
|
|
c.apiUpdateSecret(ctx, projectAgg, app.AppID, updated)
|
|
|
|
}
|
|
|
|
return nil
|
2024-04-05 09:35:49 +00:00
|
|
|
}
|
|
|
|
|
2024-08-28 18:19:50 +00:00
|
|
|
func (c *Commands) APIUpdateSecret(ctx context.Context, appID, projectID, resourceOwner, updated string) {
|
2024-04-05 09:35:49 +00:00
|
|
|
agg := project_repo.NewAggregate(projectID, resourceOwner)
|
2024-08-28 18:19:50 +00:00
|
|
|
c.apiUpdateSecret(ctx, &agg.Aggregate, appID, updated)
|
2021-10-26 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 10:56:33 +00:00
|
|
|
func (c *Commands) getAPIAppWriteModel(ctx context.Context, projectID, appID, resourceOwner string) (_ *APIApplicationWriteModel, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-02-22 11:27:47 +00:00
|
|
|
appWriteModel := NewAPIApplicationWriteModelWithAppID(projectID, appID, resourceOwner)
|
2024-06-19 10:56:33 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, appWriteModel)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return appWriteModel, nil
|
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
|
2024-08-28 18:19:50 +00:00
|
|
|
func (c *Commands) apiUpdateSecret(ctx context.Context, agg *eventstore.Aggregate, appID, updated string) {
|
|
|
|
c.asyncPush(ctx, project_repo.NewAPIConfigSecretHashUpdatedEvent(ctx, agg, appID, updated))
|
2024-04-05 09:35:49 +00:00
|
|
|
}
|