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/logging"
|
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/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/id"
|
|
|
|
project_repo "github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
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
|
|
|
|
ClientSecret *crypto.CryptoValue
|
|
|
|
ClientSecretPlain string
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddAPIAppCommand(app *addAPIApp, clientSecretAlg crypto.HashAlgorithm) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if app.ID == "" {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "PROJE-XHsKt", "Errors.Invalid.Argument")
|
|
|
|
}
|
|
|
|
if app.Name = strings.TrimSpace(app.Name); app.Name == "" {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "PROJE-F7g21", "Errors.Invalid.Argument")
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
return nil, errors.ThrowNotFound(err, "PROJE-Sf2gb", "Errors.Project.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
app.ClientID, err = domain.NewClientID(id.SonyFlakeGenerator, project.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "V2-f0pgP", "Errors.Internal")
|
|
|
|
}
|
|
|
|
|
|
|
|
//requires client secret
|
|
|
|
// TODO(release blocking):we have to return the secret
|
|
|
|
if app.AuthMethodType == domain.APIAuthMethodTypeBasic {
|
|
|
|
app.ClientSecret, app.ClientSecretPlain, err = newAppClientSecret(ctx, filter, clientSecretAlg)
|
|
|
|
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,
|
|
|
|
app.ClientSecret,
|
|
|
|
app.AuthMethodType,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
func (c *Commands) AddAPIApplication(ctx context.Context, application *domain.APIApp, resourceOwner string, appSecretGenerator crypto.Generator) (_ *domain.APIApp, err error) {
|
2021-03-15 11:51:15 +00:00
|
|
|
if application == nil || application.AggregateID == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "PROJECT-5m9E", "Errors.Application.Invalid")
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
project, err := c.getProjectByID(ctx, application.AggregateID, resourceOwner)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(err, "PROJECT-9fnsf", "Errors.Project.NotFound")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
addedApplication := NewAPIApplicationWriteModel(application.AggregateID, resourceOwner)
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&addedApplication.WriteModel)
|
2022-02-16 15:49:17 +00:00
|
|
|
events, stringPw, err := c.addAPIApplication(ctx, projectAgg, project, application, resourceOwner, appSecretGenerator)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addedApplication.AppID = application.AppID
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, events...)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addedApplication, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := apiWriteModelToAPIConfig(addedApplication)
|
|
|
|
result.ClientSecretString = stringPw
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
func (c *Commands) addAPIApplication(ctx context.Context, projectAgg *eventstore.Aggregate, proj *domain.Project, apiAppApp *domain.APIApp, resourceOwner string, appSecretGenerator crypto.Generator) (events []eventstore.Command, stringPW string, err error) {
|
2021-02-22 11:27:47 +00:00
|
|
|
if !apiAppApp.IsValid() {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, "", errors.ThrowInvalidArgument(nil, "PROJECT-Bff2g", "Errors.Application.Invalid")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
apiAppApp.AppID, err = c.idGenerator.Next()
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
events = []eventstore.Command{
|
2022-04-12 14:20:17 +00:00
|
|
|
project_repo.NewApplicationAddedEvent(ctx, projectAgg, apiAppApp.AppID, apiAppApp.AppName),
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var stringPw string
|
2021-02-24 10:17:39 +00:00
|
|
|
err = domain.SetNewClientID(apiAppApp, c.idGenerator, proj)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
stringPw, err = domain.SetNewClientSecretIfNeeded(apiAppApp, appSecretGenerator)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
events = append(events, project_repo.NewAPIConfigAddedEvent(ctx,
|
2021-02-22 11:27:47 +00:00
|
|
|
projectAgg,
|
|
|
|
apiAppApp.AppID,
|
|
|
|
apiAppApp.ClientID,
|
|
|
|
apiAppApp.ClientSecret,
|
|
|
|
apiAppApp.AuthMethodType))
|
|
|
|
|
|
|
|
return events, stringPw, nil
|
|
|
|
}
|
|
|
|
|
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 == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.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 {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.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() {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.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 {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.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
|
|
|
}
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
func (c *Commands) ChangeAPIApplicationSecret(ctx context.Context, projectID, appID, resourceOwner string, appSecretGenerator crypto.Generator) (*domain.APIApp, error) {
|
2021-02-22 11:27:47 +00:00
|
|
|
if projectID == "" || appID == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.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 {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.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() {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "COMMAND-aeH4", "Errors.Project.App.IsNotAPI")
|
2021-06-27 09:20:59 +00:00
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
cryptoSecret, stringPW, err := domain.NewClientSecret(appSecretGenerator)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingAPI.WriteModel)
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, project_repo.NewAPIConfigSecretChangedEvent(ctx, projectAgg, appID, cryptoSecret))
|
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)
|
|
|
|
result.ClientSecretString = stringPW
|
|
|
|
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() {
|
2022-04-12 14:20:17 +00:00
|
|
|
return errors.ThrowPreconditionFailed(nil, "COMMAND-DFnbf", "Errors.Project.App.NoExisting")
|
2021-10-26 09:33:59 +00:00
|
|
|
}
|
|
|
|
if !app.IsAPI() {
|
2022-04-12 14:20:17 +00:00
|
|
|
return errors.ThrowInvalidArgument(nil, "COMMAND-Bf3fw", "Errors.Project.App.IsNotAPI")
|
2021-10-26 09:33:59 +00:00
|
|
|
}
|
|
|
|
if app.ClientSecret == nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
return errors.ThrowPreconditionFailed(nil, "COMMAND-D3t5g", "Errors.Project.App.APIConfigInvalid")
|
2021-10-26 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&app.WriteModel)
|
|
|
|
ctx, spanPasswordComparison := tracing.NewNamedSpan(ctx, "crypto.CompareHash")
|
|
|
|
err = crypto.CompareHash(app.ClientSecret, []byte(secret), c.userPasswordAlg)
|
|
|
|
spanPasswordComparison.EndWithError(err)
|
|
|
|
if err == nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
_, err = c.eventstore.Push(ctx, project_repo.NewAPIConfigSecretCheckSucceededEvent(ctx, projectAgg, app.AppID))
|
2021-10-26 09:33:59 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
_, err = c.eventstore.Push(ctx, project_repo.NewAPIConfigSecretCheckFailedEvent(ctx, projectAgg, app.AppID))
|
2021-10-26 09:33:59 +00:00
|
|
|
logging.Log("COMMAND-g3f12").OnError(err).Error("could not push event APIClientSecretCheckFailed")
|
2022-04-12 14:20:17 +00:00
|
|
|
return errors.ThrowInvalidArgument(nil, "COMMAND-SADfg", "Errors.Project.App.ClientSecretInvalid")
|
2021-10-26 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) getAPIAppWriteModel(ctx context.Context, projectID, appID, resourceOwner string) (*APIApplicationWriteModel, error) {
|
2021-02-22 11:27:47 +00:00
|
|
|
appWriteModel := NewAPIApplicationWriteModelWithAppID(projectID, appID, resourceOwner)
|
2021-02-24 10:17:39 +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
|
|
|
|
}
|