2020-08-26 07:56:23 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/caos/logging"
|
2021-02-23 14:13:04 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-08-26 07:56:23 +00:00
|
|
|
"github.com/caos/zitadel/internal/iam/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IDPConfig struct {
|
|
|
|
es_models.ObjectRoot
|
2020-09-18 11:26:28 +00:00
|
|
|
IDPConfigID string `json:"idpConfigId"`
|
|
|
|
State int32 `json:"-"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Type int32 `json:"idpType,omitempty"`
|
2020-10-19 15:10:02 +00:00
|
|
|
StylingType int32 `json:"stylingType,omitempty"`
|
2020-09-18 11:26:28 +00:00
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
OIDCIDPConfig *OIDCIDPConfig `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type IDPConfigID struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
IDPConfigID string `json:"idpConfigId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetIDPConfig(idps []*IDPConfig, id string) (int, *IDPConfig) {
|
|
|
|
for i, idp := range idps {
|
|
|
|
if idp.IDPConfigID == id {
|
|
|
|
return i, idp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *IDPConfig) Changes(changed *IDPConfig) map[string]interface{} {
|
|
|
|
changes := make(map[string]interface{}, 1)
|
|
|
|
changes["idpConfigId"] = c.IDPConfigID
|
|
|
|
if changed.Name != "" && c.Name != changed.Name {
|
|
|
|
changes["name"] = changed.Name
|
|
|
|
}
|
2020-10-20 06:23:56 +00:00
|
|
|
if c.StylingType != changed.StylingType {
|
2020-10-19 15:10:02 +00:00
|
|
|
changes["stylingType"] = changed.StylingType
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
return changes
|
|
|
|
}
|
|
|
|
|
|
|
|
func IDPConfigsToModel(idps []*IDPConfig) []*model.IDPConfig {
|
|
|
|
convertedIDPConfigs := make([]*model.IDPConfig, len(idps))
|
|
|
|
for i, idp := range idps {
|
|
|
|
convertedIDPConfigs[i] = IDPConfigToModel(idp)
|
|
|
|
}
|
|
|
|
return convertedIDPConfigs
|
|
|
|
}
|
|
|
|
|
|
|
|
func IDPConfigsFromModel(idps []*model.IDPConfig) []*IDPConfig {
|
|
|
|
convertedIDPConfigs := make([]*IDPConfig, len(idps))
|
|
|
|
for i, idp := range idps {
|
|
|
|
convertedIDPConfigs[i] = IDPConfigFromModel(idp)
|
|
|
|
}
|
|
|
|
return convertedIDPConfigs
|
|
|
|
}
|
|
|
|
|
|
|
|
func IDPConfigFromModel(idp *model.IDPConfig) *IDPConfig {
|
|
|
|
converted := &IDPConfig{
|
|
|
|
ObjectRoot: idp.ObjectRoot,
|
|
|
|
IDPConfigID: idp.IDPConfigID,
|
|
|
|
Name: idp.Name,
|
|
|
|
State: int32(idp.State),
|
|
|
|
Type: int32(idp.Type),
|
2020-10-19 15:10:02 +00:00
|
|
|
StylingType: int32(idp.StylingType),
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
if idp.OIDCConfig != nil {
|
|
|
|
converted.OIDCIDPConfig = OIDCIDPConfigFromModel(idp.OIDCConfig)
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func IDPConfigToModel(idp *IDPConfig) *model.IDPConfig {
|
|
|
|
converted := &model.IDPConfig{
|
|
|
|
ObjectRoot: idp.ObjectRoot,
|
|
|
|
IDPConfigID: idp.IDPConfigID,
|
|
|
|
Name: idp.Name,
|
2020-10-19 15:10:02 +00:00
|
|
|
StylingType: model.IDPStylingType(idp.StylingType),
|
2020-08-26 07:56:23 +00:00
|
|
|
State: model.IDPConfigState(idp.State),
|
|
|
|
Type: model.IdpConfigType(idp.Type),
|
|
|
|
}
|
|
|
|
if idp.OIDCIDPConfig != nil {
|
|
|
|
converted.OIDCConfig = OIDCIDPConfigToModel(idp.OIDCIDPConfig)
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iam *IAM) appendAddIDPConfigEvent(event *es_models.Event) error {
|
|
|
|
idp := new(IDPConfig)
|
|
|
|
err := idp.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
idp.ObjectRoot.CreationDate = event.CreationDate
|
|
|
|
iam.IDPs = append(iam.IDPs, idp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iam *IAM) appendChangeIDPConfigEvent(event *es_models.Event) error {
|
|
|
|
idp := new(IDPConfig)
|
|
|
|
err := idp.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if i, idpConfig := GetIDPConfig(iam.IDPs, idp.IDPConfigID); idpConfig != nil {
|
|
|
|
iam.IDPs[i].SetData(event)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iam *IAM) appendRemoveIDPConfigEvent(event *es_models.Event) error {
|
|
|
|
idp := new(IDPConfig)
|
|
|
|
err := idp.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if i, idpConfig := GetIDPConfig(iam.IDPs, idp.IDPConfigID); idpConfig != nil {
|
|
|
|
iam.IDPs[i] = iam.IDPs[len(iam.IDPs)-1]
|
|
|
|
iam.IDPs[len(iam.IDPs)-1] = nil
|
|
|
|
iam.IDPs = iam.IDPs[:len(iam.IDPs)-1]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iam *IAM) appendIDPConfigStateEvent(event *es_models.Event, state model.IDPConfigState) error {
|
|
|
|
idp := new(IDPConfig)
|
|
|
|
err := idp.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, idpConfig := GetIDPConfig(iam.IDPs, idp.IDPConfigID); idpConfig != nil {
|
|
|
|
idpConfig.State = int32(state)
|
|
|
|
iam.IDPs[i] = idpConfig
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *IDPConfig) SetData(event *es_models.Event) error {
|
|
|
|
c.ObjectRoot.AppendEvent(event)
|
|
|
|
if err := json.Unmarshal(event.Data, c); err != nil {
|
|
|
|
logging.Log("EVEN-Msj9w").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|