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
|
|
|
}
|
|
|
|
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
app.ClientID, err = domain.NewClientID(c.idGenerator, project.Name)
|
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
|
|
|
|
|
|
|
project, 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-04-05 09:35:49 +00:00
|
|
|
return c.addOIDCApplicationWithID(ctx, oidcApp, resourceOwner, project, 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
|
|
|
}
|
|
|
|
project, err := c.getProjectByID(ctx, oidcApp.AggregateID, resourceOwner)
|
|
|
|
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-04-05 09:35:49 +00:00
|
|
|
return c.addOIDCApplicationWithID(ctx, oidcApp, resourceOwner, project, appID)
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func (c *Commands) addOIDCApplicationWithID(ctx context.Context, oidcApp *domain.OIDCApp, resourceOwner string, project *domain.Project, 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
|
2022-07-28 13:42:35 +00:00
|
|
|
err = domain.SetNewClientID(oidcApp, c.idGenerator, project)
|
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)
|
|
|
|
if err == nil {
|
2024-04-05 09:35:49 +00:00
|
|
|
c.oidcSecretCheckSucceeded(ctx, projectAgg, appID, updated)
|
|
|
|
return nil
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
c.oidcSecretCheckFailed(ctx, projectAgg, appID)
|
|
|
|
return zerrors.ThrowInvalidArgument(err, "COMMAND-Bz542", "Errors.Project.App.ClientSecretInvalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) OIDCSecretCheckSucceeded(ctx context.Context, appID, projectID, resourceOwner, updated string) {
|
|
|
|
agg := project_repo.NewAggregate(projectID, resourceOwner)
|
|
|
|
c.oidcSecretCheckSucceeded(ctx, &agg.Aggregate, appID, updated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) OIDCSecretCheckFailed(ctx context.Context, appID, projectID, resourceOwner string) {
|
|
|
|
agg := project_repo.NewAggregate(projectID, resourceOwner)
|
|
|
|
c.oidcSecretCheckFailed(ctx, &agg.Aggregate, appID)
|
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
|
|
|
|
|
|
|
func (c *Commands) oidcSecretCheckSucceeded(ctx context.Context, agg *eventstore.Aggregate, appID, updated string) {
|
|
|
|
cmds := append(
|
|
|
|
make([]eventstore.Command, 0, 2),
|
|
|
|
project_repo.NewOIDCConfigSecretCheckSucceededEvent(ctx, agg, appID),
|
|
|
|
)
|
|
|
|
if updated != "" {
|
|
|
|
cmds = append(cmds, project_repo.NewOIDCConfigSecretHashUpdatedEvent(ctx, agg, appID, updated))
|
|
|
|
}
|
|
|
|
c.asyncPush(ctx, cmds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) oidcSecretCheckFailed(ctx context.Context, agg *eventstore.Aggregate, appID string) {
|
|
|
|
c.asyncPush(ctx, project_repo.NewOIDCConfigSecretCheckFailedEvent(ctx, agg, appID))
|
|
|
|
}
|