2020-08-26 07:56:23 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2022-08-31 07:52:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/iam/model"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-08-26 07:56:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
IDPConfigKeyIdpConfigID = "idp_config_id"
|
|
|
|
IDPConfigKeyAggregateID = "aggregate_id"
|
|
|
|
IDPConfigKeyName = "name"
|
|
|
|
IDPConfigKeyProviderType = "idp_provider_type"
|
2022-03-23 08:02:39 +00:00
|
|
|
IDPConfigKeyInstanceID = "instance_id"
|
2022-11-30 16:01:17 +00:00
|
|
|
IDPConfigKeyOwnerRemoved = "owner_removed"
|
2020-08-26 07:56:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IDPConfigView struct {
|
|
|
|
IDPConfigID string `json:"idpConfigId" gorm:"column:idp_config_id;primary_key"`
|
|
|
|
AggregateID string `json:"-" gorm:"column:aggregate_id"`
|
|
|
|
Name string `json:"name" gorm:"column:name"`
|
2020-10-19 15:10:02 +00:00
|
|
|
StylingType int32 `json:"stylingType" gorm:"column:styling_type"`
|
2020-08-26 07:56:23 +00:00
|
|
|
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
|
|
|
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
|
|
|
IDPState int32 `json:"-" gorm:"column:idp_state"`
|
|
|
|
IDPProviderType int32 `json:"-" gorm:"column:idp_provider_type"`
|
2021-09-10 07:49:49 +00:00
|
|
|
AutoRegister bool `json:"autoRegister" gorm:"column:auto_register"`
|
2020-08-26 07:56:23 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
IsOIDC bool `json:"-" gorm:"column:is_oidc"`
|
|
|
|
OIDCClientID string `json:"clientId" gorm:"column:oidc_client_id"`
|
|
|
|
OIDCClientSecret *crypto.CryptoValue `json:"clientSecret" gorm:"column:oidc_client_secret"`
|
|
|
|
OIDCIssuer string `json:"issuer" gorm:"column:oidc_issuer"`
|
|
|
|
OIDCScopes database.TextArray[string] `json:"scopes" gorm:"column:oidc_scopes"`
|
|
|
|
OIDCIDPDisplayNameMapping int32 `json:"idpDisplayNameMapping" gorm:"column:oidc_idp_display_name_mapping"`
|
|
|
|
OIDCUsernameMapping int32 `json:"usernameMapping" gorm:"column:oidc_idp_username_mapping"`
|
|
|
|
OAuthAuthorizationEndpoint string `json:"authorizationEndpoint" gorm:"column:oauth_authorization_endpoint"`
|
|
|
|
OAuthTokenEndpoint string `json:"tokenEndpoint" gorm:"column:oauth_token_endpoint"`
|
|
|
|
JWTEndpoint string `json:"jwtEndpoint" gorm:"jwt_endpoint"`
|
|
|
|
JWTKeysEndpoint string `json:"keysEndpoint" gorm:"jwt_keys_endpoint"`
|
|
|
|
JWTHeaderName string `json:"headerName" gorm:"jwt_header_name"`
|
2020-08-26 07:56:23 +00:00
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
2022-04-19 06:26:12 +00:00
|
|
|
InstanceID string `json:"instanceID" gorm:"column:instance_id;primary_key"`
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 11:26:28 +00:00
|
|
|
func IDPConfigViewToModel(idp *IDPConfigView) *model.IDPConfigView {
|
2021-09-14 13:15:01 +00:00
|
|
|
view := &model.IDPConfigView{
|
2021-07-06 14:39:48 +00:00
|
|
|
IDPConfigID: idp.IDPConfigID,
|
|
|
|
AggregateID: idp.AggregateID,
|
|
|
|
State: model.IDPConfigState(idp.IDPState),
|
|
|
|
Name: idp.Name,
|
|
|
|
StylingType: model.IDPStylingType(idp.StylingType),
|
2021-09-10 07:49:49 +00:00
|
|
|
AutoRegister: idp.AutoRegister,
|
2021-07-06 14:39:48 +00:00
|
|
|
Sequence: idp.Sequence,
|
|
|
|
CreationDate: idp.CreationDate,
|
|
|
|
ChangeDate: idp.ChangeDate,
|
|
|
|
IDPProviderType: model.IDPProviderType(idp.IDPProviderType),
|
|
|
|
IsOIDC: idp.IsOIDC,
|
|
|
|
OIDCClientID: idp.OIDCClientID,
|
|
|
|
OIDCClientSecret: idp.OIDCClientSecret,
|
|
|
|
OIDCScopes: idp.OIDCScopes,
|
|
|
|
OIDCIDPDisplayNameMapping: model.OIDCMappingField(idp.OIDCIDPDisplayNameMapping),
|
|
|
|
OIDCUsernameMapping: model.OIDCMappingField(idp.OIDCUsernameMapping),
|
|
|
|
OAuthAuthorizationEndpoint: idp.OAuthAuthorizationEndpoint,
|
|
|
|
OAuthTokenEndpoint: idp.OAuthTokenEndpoint,
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
2021-09-14 13:15:01 +00:00
|
|
|
if idp.IsOIDC {
|
|
|
|
view.OIDCIssuer = idp.OIDCIssuer
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
view.JWTEndpoint = idp.JWTEndpoint
|
|
|
|
view.JWTIssuer = idp.OIDCIssuer
|
|
|
|
view.JWTKeysEndpoint = idp.JWTKeysEndpoint
|
|
|
|
view.JWTHeaderName = idp.JWTHeaderName
|
|
|
|
return view
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (i *IDPConfigView) AppendEvent(providerType model.IDPProviderType, event eventstore.Event) (err error) {
|
|
|
|
i.Sequence = event.Sequence()
|
|
|
|
i.ChangeDate = event.CreatedAt()
|
|
|
|
switch event.Type() {
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.IDPConfigAddedEventType, org.IDPConfigAddedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
i.setRootData(event)
|
2023-10-19 10:19:10 +00:00
|
|
|
i.CreationDate = event.CreatedAt()
|
2020-08-26 07:56:23 +00:00
|
|
|
i.IDPProviderType = int32(providerType)
|
|
|
|
err = i.SetData(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.IDPOIDCConfigAddedEventType, org.IDPOIDCConfigAddedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
i.IsOIDC = true
|
|
|
|
err = i.SetData(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.IDPOIDCConfigChangedEventType, org.IDPOIDCConfigChangedEventType,
|
|
|
|
instance.IDPConfigChangedEventType, org.IDPConfigChangedEventType,
|
|
|
|
org.IDPJWTConfigAddedEventType, instance.IDPJWTConfigAddedEventType,
|
|
|
|
org.IDPJWTConfigChangedEventType, instance.IDPJWTConfigChangedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
err = i.SetData(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.IDPConfigDeactivatedEventType, org.IDPConfigDeactivatedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
i.IDPState = int32(model.IDPConfigStateInactive)
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.IDPConfigReactivatedEventType, org.IDPConfigReactivatedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
i.IDPState = int32(model.IDPConfigStateActive)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (r *IDPConfigView) setRootData(event eventstore.Event) {
|
|
|
|
r.AggregateID = event.Aggregate().ID
|
|
|
|
r.InstanceID = event.Aggregate().InstanceID
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (r *IDPConfigView) SetData(event eventstore.Event) error {
|
|
|
|
err := event.Unmarshal(r)
|
|
|
|
if err != nil {
|
2022-03-31 09:36:26 +00:00
|
|
|
logging.New().WithError(err).Error("could not unmarshal event data")
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowInternal(err, "MODEL-lub6s", "Could not unmarshal data")
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|