2021-01-28 05:35:26 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-12 14:20:17 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-02-22 11:27:47 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
http_util "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/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-01-28 05:35:26 +00:00
|
|
|
)
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
type addOIDCApp struct {
|
|
|
|
AddApp
|
2023-04-11 15:07:32 +00:00
|
|
|
Version domain.OIDCVersion
|
|
|
|
RedirectUris []string
|
|
|
|
ResponseTypes []domain.OIDCResponseType
|
|
|
|
GrantTypes []domain.OIDCGrantType
|
|
|
|
ApplicationType domain.OIDCApplicationType
|
|
|
|
AuthMethodType domain.OIDCAuthMethodType
|
|
|
|
PostLogoutRedirectUris []string
|
|
|
|
DevMode bool
|
|
|
|
AccessTokenType domain.OIDCTokenType
|
|
|
|
AccessTokenRoleAssertion bool
|
|
|
|
IDTokenRoleAssertion bool
|
|
|
|
IDTokenUserinfoAssertion bool
|
|
|
|
ClockSkew time.Duration
|
|
|
|
AdditionalOrigins []string
|
|
|
|
SkipSuccessPageForNativeApp bool
|
2022-04-12 14:20:17 +00:00
|
|
|
|
|
|
|
ClientID string
|
2024-04-05 09:35:49 +00:00
|
|
|
ClientSecret string
|
2022-04-12 14:20:17 +00:00
|
|
|
ClientSecretPlain string
|
|
|
|
}
|
|
|
|
|
2022-10-18 15:07:30 +00:00
|
|
|
// AddOIDCAppCommand prepares the commands to add an oidc app. The ClientID will be set during the CreateCommands
|
2024-04-05 09:35:49 +00:00
|
|
|
func (c *Commands) AddOIDCAppCommand(app *addOIDCApp) 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-NnavI", "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-Fef31", "Errors.Invalid.Argument")
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if app.ClockSkew > time.Second*5 || app.ClockSkew < 0 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "V2-PnCMS", "Errors.Invalid.Argument")
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, origin := range app.AdditionalOrigins {
|
2024-02-12 08:56:55 +00:00
|
|
|
if !http_util.IsOrigin(strings.TrimSpace(origin)) {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "V2-DqWPX", "Errors.Invalid.Argument")
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !domain.ContainsRequiredGrantTypes(app.ResponseTypes, app.GrantTypes) {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "V2-sLpW1", "Errors.Invalid.Argument")
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) (_ []eventstore.Command, err 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-6swVG", "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-VMSQ1", "Errors.Internal")
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if app.AuthMethodType == domain.OIDCAuthMethodTypeBasic || app.AuthMethodType == domain.OIDCAuthMethodTypePost {
|
2024-04-05 09:35:49 +00:00
|
|
|
app.ClientSecret, 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.NewOIDCConfigAddedEvent(
|
|
|
|
ctx,
|
|
|
|
&app.Aggregate.Aggregate,
|
|
|
|
app.Version,
|
|
|
|
app.ID,
|
|
|
|
app.ClientID,
|
|
|
|
app.ClientSecret,
|
2024-02-12 08:56:55 +00:00
|
|
|
trimStringSliceWhiteSpaces(app.RedirectUris),
|
2022-04-12 14:20:17 +00:00
|
|
|
app.ResponseTypes,
|
|
|
|
app.GrantTypes,
|
|
|
|
app.ApplicationType,
|
|
|
|
app.AuthMethodType,
|
2024-02-12 08:56:55 +00:00
|
|
|
trimStringSliceWhiteSpaces(app.PostLogoutRedirectUris),
|
2022-04-12 14:20:17 +00:00
|
|
|
app.DevMode,
|
|
|
|
app.AccessTokenType,
|
|
|
|
app.AccessTokenRoleAssertion,
|
|
|
|
app.IDTokenRoleAssertion,
|
|
|
|
app.IDTokenUserinfoAssertion,
|
|
|
|
app.ClockSkew,
|
2024-02-12 08:56:55 +00:00
|
|
|
trimStringSliceWhiteSpaces(app.AdditionalOrigins),
|
2023-04-11 15:07:32 +00:00
|
|
|
app.SkipSuccessPageForNativeApp,
|
2022-04-12 14:20:17 +00:00
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func (c *Commands) AddOIDCApplicationWithID(ctx context.Context, oidcApp *domain.OIDCApp, resourceOwner, appID string) (_ *domain.OIDCApp, 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
|
|
|
existingApp, err := c.getOIDCAppWriteModel(ctx, oidcApp.AggregateID, appID, resourceOwner)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
if existingApp.State != domain.AppStateUnspecified {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "PROJECT-lxowmp", "Errors.Project.App.AlreadyExisting")
|
2021-02-18 13:48:27 +00:00
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
|
2024-07-04 08:31:40 +00:00
|
|
|
_, err = c.getProjectByID(ctx, oidcApp.AggregateID, resourceOwner)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(err, "PROJECT-3m9s2", "Errors.Project.NotFound")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
|
2024-07-04 08:31:40 +00:00
|
|
|
return c.addOIDCApplicationWithID(ctx, oidcApp, resourceOwner, appID)
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func (c *Commands) AddOIDCApplication(ctx context.Context, oidcApp *domain.OIDCApp, resourceOwner string) (_ *domain.OIDCApp, err error) {
|
2022-07-28 13:42:35 +00:00
|
|
|
if oidcApp == nil || oidcApp.AggregateID == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "PROJECT-34Fm0", "Errors.Project.App.Invalid")
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
2024-07-04 08:31:40 +00:00
|
|
|
_, err = c.getProjectByID(ctx, oidcApp.AggregateID, resourceOwner)
|
2022-07-28 13:42:35 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(err, "PROJECT-3m9ss", "Errors.Project.NotFound")
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 17:46:26 +00:00
|
|
|
if oidcApp.AppName == "" || !oidcApp.IsValid() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "PROJECT-1n8df", "Errors.Project.App.Invalid")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
|
|
|
|
appID, err := c.idGenerator.Next()
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
2022-07-28 13:42:35 +00:00
|
|
|
return nil, err
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 08:31:40 +00:00
|
|
|
return c.addOIDCApplicationWithID(ctx, oidcApp, resourceOwner, appID)
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 08:31:40 +00:00
|
|
|
func (c *Commands) addOIDCApplicationWithID(ctx context.Context, oidcApp *domain.OIDCApp, resourceOwner string, appID string) (_ *domain.OIDCApp, 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
|
|
|
addedApplication := NewOIDCApplicationWriteModel(oidcApp.AggregateID, resourceOwner)
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&addedApplication.WriteModel)
|
|
|
|
|
|
|
|
oidcApp.AppID = appID
|
|
|
|
|
|
|
|
events := []eventstore.Command{
|
2022-04-12 14:20:17 +00:00
|
|
|
project_repo.NewApplicationAddedEvent(ctx, projectAgg, oidcApp.AppID, oidcApp.AppName),
|
2021-02-18 13:48:27 +00:00
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
var plain string
|
2024-07-04 08:31:40 +00:00
|
|
|
err = domain.SetNewClientID(oidcApp, c.idGenerator)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
2022-07-28 13:42:35 +00:00
|
|
|
return nil, err
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
plain, err = domain.SetNewClientSecretIfNeeded(oidcApp, func() (string, string, error) {
|
|
|
|
return c.newHashedSecret(ctx, c.eventstore.Filter) //nolint:staticcheck
|
|
|
|
})
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
2022-07-28 13:42:35 +00:00
|
|
|
return nil, err
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
events = append(events, project_repo.NewOIDCConfigAddedEvent(ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
projectAgg,
|
2021-01-28 05:35:26 +00:00
|
|
|
oidcApp.OIDCVersion,
|
|
|
|
oidcApp.AppID,
|
|
|
|
oidcApp.ClientID,
|
2024-04-05 09:35:49 +00:00
|
|
|
oidcApp.EncodedHash,
|
2024-02-12 08:56:55 +00:00
|
|
|
trimStringSliceWhiteSpaces(oidcApp.RedirectUris),
|
2021-01-28 05:35:26 +00:00
|
|
|
oidcApp.ResponseTypes,
|
|
|
|
oidcApp.GrantTypes,
|
|
|
|
oidcApp.ApplicationType,
|
|
|
|
oidcApp.AuthMethodType,
|
2024-02-12 08:56:55 +00:00
|
|
|
trimStringSliceWhiteSpaces(oidcApp.PostLogoutRedirectUris),
|
2021-01-28 05:35:26 +00:00
|
|
|
oidcApp.DevMode,
|
|
|
|
oidcApp.AccessTokenType,
|
|
|
|
oidcApp.AccessTokenRoleAssertion,
|
|
|
|
oidcApp.IDTokenRoleAssertion,
|
|
|
|
oidcApp.IDTokenUserinfoAssertion,
|
2021-05-19 07:17:38 +00:00
|
|
|
oidcApp.ClockSkew,
|
2024-02-12 08:56:55 +00:00
|
|
|
trimStringSliceWhiteSpaces(oidcApp.AdditionalOrigins),
|
2023-04-11 15:07:32 +00:00
|
|
|
oidcApp.SkipNativeAppSuccessPage,
|
|
|
|
))
|
2021-01-28 05:35:26 +00:00
|
|
|
|
2022-07-28 13:42:35 +00:00
|
|
|
addedApplication.AppID = oidcApp.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 := oidcWriteModelToOIDCConfig(addedApplication)
|
2024-04-05 09:35:49 +00:00
|
|
|
result.ClientSecretString = plain
|
2022-07-28 13:42:35 +00:00
|
|
|
result.FillCompliance()
|
|
|
|
return result, nil
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) ChangeOIDCApplication(ctx context.Context, oidc *domain.OIDCApp, resourceOwner string) (*domain.OIDCApp, error) {
|
2021-03-15 11:51:15 +00:00
|
|
|
if !oidc.IsValid() || oidc.AppID == "" || oidc.AggregateID == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-5m9fs", "Errors.Project.App.OIDCConfigInvalid")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
existingOIDC, err := c.getOIDCAppWriteModel(ctx, oidc.AggregateID, oidc.AppID, resourceOwner)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existingOIDC.State == domain.AppStateUnspecified || existingOIDC.State == domain.AppStateRemoved {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-2n8uU", "Errors.Project.App.NotExisting")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
2021-06-27 09:20:59 +00:00
|
|
|
if !existingOIDC.IsOIDC() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-GBr34", "Errors.Project.App.IsNotOIDC")
|
2021-06-27 09:20:59 +00:00
|
|
|
}
|
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,
|
2024-02-12 08:56:55 +00:00
|
|
|
trimStringSliceWhiteSpaces(oidc.RedirectUris),
|
|
|
|
trimStringSliceWhiteSpaces(oidc.PostLogoutRedirectUris),
|
2021-01-28 05:35:26 +00:00
|
|
|
oidc.ResponseTypes,
|
|
|
|
oidc.GrantTypes,
|
|
|
|
oidc.ApplicationType,
|
|
|
|
oidc.AuthMethodType,
|
|
|
|
oidc.OIDCVersion,
|
|
|
|
oidc.AccessTokenType,
|
|
|
|
oidc.DevMode,
|
|
|
|
oidc.AccessTokenRoleAssertion,
|
|
|
|
oidc.IDTokenRoleAssertion,
|
|
|
|
oidc.IDTokenUserinfoAssertion,
|
2021-05-19 07:17:38 +00:00
|
|
|
oidc.ClockSkew,
|
2024-02-12 08:56:55 +00:00
|
|
|
trimStringSliceWhiteSpaces(oidc.AdditionalOrigins),
|
2023-04-11 15:07:32 +00:00
|
|
|
oidc.SkipNativeAppSuccessPage,
|
|
|
|
)
|
2021-01-28 05:35:26 +00:00
|
|
|
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-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(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
|
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func (c *Commands) ChangeOIDCApplicationSecret(ctx context.Context, projectID, appID, resourceOwner string) (*domain.OIDCApp, error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
if projectID == "" || appID == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-99i83", "Errors.IDMissing")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
existingOIDC, err := c.getOIDCAppWriteModel(ctx, projectID, appID, resourceOwner)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existingOIDC.State == domain.AppStateUnspecified || existingOIDC.State == domain.AppStateRemoved {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-2g66f", "Errors.Project.App.NotExisting")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
2021-06-27 09:20:59 +00:00
|
|
|
if !existingOIDC.IsOIDC() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-Ghrh3", "Errors.Project.App.IsNotOIDC")
|
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-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingOIDC.WriteModel)
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, project_repo.NewOIDCConfigSecretChangedEvent(ctx, projectAgg, appID, encodedHash))
|
2021-02-18 13:48:27 +00:00
|
|
|
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)
|
2024-04-05 09:35:49 +00:00
|
|
|
result.ClientSecretString = plain
|
2021-01-28 05:35:26 +00:00
|
|
|
return result, err
|
|
|
|
}
|
2021-02-22 11:27:47 +00:00
|
|
|
|
2021-04-21 11:23:05 +00:00
|
|
|
func (c *Commands) VerifyOIDCClientSecret(ctx context.Context, projectID, appID, secret string) (err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
app, err := c.getOIDCAppWriteModel(ctx, projectID, appID, "")
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !app.State.Exists() {
|
2024-04-05 09:35:49 +00:00
|
|
|
return zerrors.ThrowPreconditionFailed(nil, "COMMAND-D8hba", "Errors.Project.App.NotExisting")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2021-06-27 09:20:59 +00:00
|
|
|
if !app.IsOIDC() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-BHgn2", "Errors.Project.App.IsNotOIDC")
|
2021-06-27 09:20: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-D6hba", "Errors.Project.App.OIDCConfigInvalid")
|
2021-02-22 11:27:47 +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-02-22 11:27:47 +00:00
|
|
|
spanPasswordComparison.EndWithError(err)
|
2024-08-28 18:19:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return zerrors.ThrowInvalidArgument(err, "COMMAND-Bz542", "Errors.Project.App.ClientSecretInvalid")
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2024-08-28 18:19:50 +00:00
|
|
|
if updated != "" {
|
|
|
|
c.oidcUpdateSecret(ctx, projectAgg, appID, updated)
|
|
|
|
}
|
|
|
|
return nil
|
2024-04-05 09:35:49 +00:00
|
|
|
}
|
|
|
|
|
2024-08-28 18:19:50 +00:00
|
|
|
func (c *Commands) OIDCUpdateSecret(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.oidcUpdateSecret(ctx, &agg.Aggregate, appID, updated)
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 10:56:33 +00:00
|
|
|
func (c *Commands) getOIDCAppWriteModel(ctx context.Context, projectID, appID, resourceOwner string) (_ *OIDCApplicationWriteModel, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-02-22 11:27:47 +00:00
|
|
|
appWriteModel := NewOIDCApplicationWriteModelWithAppID(projectID, appID, resourceOwner)
|
2024-06-19 10:56:33 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, appWriteModel)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return appWriteModel, nil
|
|
|
|
}
|
2022-04-25 08:01:17 +00:00
|
|
|
|
|
|
|
func getOIDCAppWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer, projectID, appID, resourceOwner string) (*OIDCApplicationWriteModel, error) {
|
|
|
|
appWriteModel := NewOIDCApplicationWriteModelWithAppID(projectID, appID, resourceOwner)
|
|
|
|
events, err := filter(ctx, appWriteModel.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
return appWriteModel, nil
|
|
|
|
}
|
|
|
|
appWriteModel.AppendEvents(events...)
|
|
|
|
err = appWriteModel.Reduce()
|
|
|
|
return appWriteModel, err
|
|
|
|
}
|
2024-02-12 08:56:55 +00:00
|
|
|
|
|
|
|
func trimStringSliceWhiteSpaces(slice []string) []string {
|
|
|
|
for i, s := range slice {
|
|
|
|
slice[i] = strings.TrimSpace(s)
|
|
|
|
}
|
|
|
|
return slice
|
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
|
2024-08-28 18:19:50 +00:00
|
|
|
func (c *Commands) oidcUpdateSecret(ctx context.Context, agg *eventstore.Aggregate, appID, updated string) {
|
|
|
|
c.asyncPush(ctx, project_repo.NewOIDCConfigSecretHashUpdatedEvent(ctx, agg, appID, updated))
|
2024-04-05 09:35:49 +00:00
|
|
|
}
|