2020-08-26 07:56:23 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2021-07-06 14:39:48 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2021-07-06 14:39:48 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/iam/model"
|
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
|
|
|
|
2022-08-31 07:52:43 +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.StringArray `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
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPConfigView) AppendEvent(providerType model.IDPProviderType, event *models.Event) (err error) {
|
|
|
|
i.Sequence = event.Sequence
|
|
|
|
i.ChangeDate = event.CreationDate
|
2022-03-31 09:36:26 +00:00
|
|
|
switch eventstore.EventType(event.Type) {
|
|
|
|
case instance.IDPConfigAddedEventType, org.IDPConfigAddedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
i.setRootData(event)
|
|
|
|
i.CreationDate = event.CreationDate
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IDPConfigView) setRootData(event *models.Event) {
|
|
|
|
r.AggregateID = event.AggregateID
|
2022-03-23 08:02:39 +00:00
|
|
|
r.InstanceID = event.InstanceID
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IDPConfigView) SetData(event *models.Event) error {
|
|
|
|
if err := json.Unmarshal(event.Data, r); err != nil {
|
2022-03-31 09:36:26 +00:00
|
|
|
logging.New().WithError(err).Error("could not unmarshal event data")
|
2020-08-26 07:56:23 +00:00
|
|
|
return caos_errs.ThrowInternal(err, "MODEL-lub6s", "Could not unmarshal data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|