2020-08-26 09:56:23 +02:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
2021-07-06 16:39:48 +02:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2021-09-14 15:15:01 +02:00
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
2021-07-06 16:39:48 +02:00
|
|
|
|
2020-08-26 09:56:23 +02:00
|
|
|
es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
|
|
|
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
2021-07-06 16:39:48 +02:00
|
|
|
"github.com/lib/pq"
|
|
|
|
|
2020-08-26 09:56:23 +02:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-08-26 09:56:23 +02:00
|
|
|
"github.com/caos/zitadel/internal/iam/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
IDPConfigKeyIdpConfigID = "idp_config_id"
|
|
|
|
IDPConfigKeyAggregateID = "aggregate_id"
|
|
|
|
IDPConfigKeyName = "name"
|
|
|
|
IDPConfigKeyProviderType = "idp_provider_type"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 17:10:02 +02:00
|
|
|
StylingType int32 `json:"stylingType" gorm:"column:styling_type"`
|
2020-08-26 09:56:23 +02: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 09:49:49 +02:00
|
|
|
AutoRegister bool `json:"autoRegister" gorm:"column:auto_register"`
|
2020-08-26 09:56:23 +02:00
|
|
|
|
2021-07-06 16:39:48 +02: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 pq.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"`
|
2021-09-14 15:15:01 +02:00
|
|
|
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 09:56:23 +02:00
|
|
|
|
|
|
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:26:28 +02:00
|
|
|
func IDPConfigViewToModel(idp *IDPConfigView) *model.IDPConfigView {
|
2021-09-14 15:15:01 +02:00
|
|
|
view := &model.IDPConfigView{
|
2021-07-06 16:39:48 +02:00
|
|
|
IDPConfigID: idp.IDPConfigID,
|
|
|
|
AggregateID: idp.AggregateID,
|
|
|
|
State: model.IDPConfigState(idp.IDPState),
|
|
|
|
Name: idp.Name,
|
|
|
|
StylingType: model.IDPStylingType(idp.StylingType),
|
2021-09-10 09:49:49 +02:00
|
|
|
AutoRegister: idp.AutoRegister,
|
2021-07-06 16:39:48 +02: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 09:56:23 +02:00
|
|
|
}
|
2021-09-14 15:15:01 +02: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 09:56:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func IdpConfigViewsToModel(idps []*IDPConfigView) []*model.IDPConfigView {
|
|
|
|
result := make([]*model.IDPConfigView, len(idps))
|
|
|
|
for i, idp := range idps {
|
2020-09-18 13:26:28 +02:00
|
|
|
result[i] = IDPConfigViewToModel(idp)
|
2020-08-26 09:56:23 +02:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPConfigView) AppendEvent(providerType model.IDPProviderType, event *models.Event) (err error) {
|
|
|
|
i.Sequence = event.Sequence
|
|
|
|
i.ChangeDate = event.CreationDate
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.IDPConfigAdded, org_es_model.IDPConfigAdded:
|
|
|
|
i.setRootData(event)
|
|
|
|
i.CreationDate = event.CreationDate
|
|
|
|
i.IDPProviderType = int32(providerType)
|
|
|
|
err = i.SetData(event)
|
|
|
|
case es_model.OIDCIDPConfigAdded, org_es_model.OIDCIDPConfigAdded:
|
|
|
|
i.IsOIDC = true
|
|
|
|
err = i.SetData(event)
|
|
|
|
case es_model.OIDCIDPConfigChanged, org_es_model.OIDCIDPConfigChanged,
|
2021-09-14 15:15:01 +02:00
|
|
|
es_model.IDPConfigChanged, org_es_model.IDPConfigChanged,
|
|
|
|
models.EventType(org.IDPJWTConfigAddedEventType), models.EventType(iam.IDPJWTConfigAddedEventType),
|
|
|
|
models.EventType(org.IDPJWTConfigChangedEventType), models.EventType(iam.IDPJWTConfigChangedEventType):
|
2020-08-26 09:56:23 +02:00
|
|
|
err = i.SetData(event)
|
|
|
|
case es_model.IDPConfigDeactivated, org_es_model.IDPConfigDeactivated:
|
|
|
|
i.IDPState = int32(model.IDPConfigStateInactive)
|
|
|
|
case es_model.IDPConfigReactivated, org_es_model.IDPConfigReactivated:
|
|
|
|
i.IDPState = int32(model.IDPConfigStateActive)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IDPConfigView) setRootData(event *models.Event) {
|
|
|
|
r.AggregateID = event.AggregateID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IDPConfigView) SetData(event *models.Event) error {
|
|
|
|
if err := json.Unmarshal(event.Data, r); err != nil {
|
|
|
|
logging.Log("EVEN-Smkld").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return caos_errs.ThrowInternal(err, "MODEL-lub6s", "Could not unmarshal data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|