2020-04-23 05:54:40 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/project/model"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OIDCConfig struct {
|
|
|
|
es_models.ObjectRoot
|
2020-08-10 07:34:56 +00:00
|
|
|
Version int32 `json:"oidcVersion,omitempty"`
|
2020-04-23 05:54:40 +00:00
|
|
|
AppID string `json:"appId"`
|
|
|
|
ClientID string `json:"clientId,omitempty"`
|
|
|
|
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
|
|
|
|
RedirectUris []string `json:"redirectUris,omitempty"`
|
|
|
|
ResponseTypes []int32 `json:"responseTypes,omitempty"`
|
|
|
|
GrantTypes []int32 `json:"grantTypes,omitempty"`
|
|
|
|
ApplicationType int32 `json:"applicationType,omitempty"`
|
|
|
|
AuthMethodType int32 `json:"authMethodType,omitempty"`
|
|
|
|
PostLogoutRedirectUris []string `json:"postLogoutRedirectUris,omitempty"`
|
2020-08-10 07:34:56 +00:00
|
|
|
DevMode bool `json:"devMode,omitempty"`
|
2020-04-23 05:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OIDCConfig) Changes(changed *OIDCConfig) map[string]interface{} {
|
|
|
|
changes := make(map[string]interface{}, 1)
|
|
|
|
changes["appId"] = c.AppID
|
|
|
|
if !reflect.DeepEqual(c.RedirectUris, changed.RedirectUris) {
|
|
|
|
changes["redirectUris"] = changed.RedirectUris
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(c.ResponseTypes, changed.ResponseTypes) {
|
|
|
|
changes["responseTypes"] = changed.ResponseTypes
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(c.GrantTypes, changed.GrantTypes) {
|
|
|
|
changes["grantTypes"] = changed.GrantTypes
|
|
|
|
}
|
|
|
|
if c.ApplicationType != changed.ApplicationType {
|
|
|
|
changes["applicationType"] = changed.ApplicationType
|
|
|
|
}
|
|
|
|
if c.AuthMethodType != changed.AuthMethodType {
|
|
|
|
changes["authMethodType"] = changed.AuthMethodType
|
|
|
|
}
|
2020-08-10 07:34:56 +00:00
|
|
|
if c.Version != changed.Version {
|
|
|
|
changes["oidcVersion"] = changed.Version
|
|
|
|
}
|
2020-04-23 05:54:40 +00:00
|
|
|
if !reflect.DeepEqual(c.PostLogoutRedirectUris, changed.PostLogoutRedirectUris) {
|
|
|
|
changes["postLogoutRedirectUris"] = changed.PostLogoutRedirectUris
|
|
|
|
}
|
2020-08-10 07:34:56 +00:00
|
|
|
if c.DevMode != changed.DevMode {
|
|
|
|
changes["devMode"] = changed.DevMode
|
|
|
|
}
|
2020-04-23 05:54:40 +00:00
|
|
|
return changes
|
|
|
|
}
|
|
|
|
|
|
|
|
func OIDCConfigFromModel(config *model.OIDCConfig) *OIDCConfig {
|
|
|
|
responseTypes := make([]int32, len(config.ResponseTypes))
|
|
|
|
for i, rt := range config.ResponseTypes {
|
|
|
|
responseTypes[i] = int32(rt)
|
|
|
|
}
|
|
|
|
grantTypes := make([]int32, len(config.GrantTypes))
|
|
|
|
for i, rt := range config.GrantTypes {
|
|
|
|
grantTypes[i] = int32(rt)
|
|
|
|
}
|
|
|
|
return &OIDCConfig{
|
2020-05-11 10:16:29 +00:00
|
|
|
ObjectRoot: config.ObjectRoot,
|
2020-04-23 05:54:40 +00:00
|
|
|
AppID: config.AppID,
|
2020-08-10 07:34:56 +00:00
|
|
|
Version: int32(config.OIDCVersion),
|
2020-04-23 05:54:40 +00:00
|
|
|
ClientID: config.ClientID,
|
|
|
|
ClientSecret: config.ClientSecret,
|
|
|
|
RedirectUris: config.RedirectUris,
|
|
|
|
ResponseTypes: responseTypes,
|
|
|
|
GrantTypes: grantTypes,
|
|
|
|
ApplicationType: int32(config.ApplicationType),
|
|
|
|
AuthMethodType: int32(config.AuthMethodType),
|
|
|
|
PostLogoutRedirectUris: config.PostLogoutRedirectUris,
|
2020-08-10 07:34:56 +00:00
|
|
|
DevMode: config.DevMode,
|
2020-04-23 05:54:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OIDCConfigToModel(config *OIDCConfig) *model.OIDCConfig {
|
|
|
|
responseTypes := make([]model.OIDCResponseType, len(config.ResponseTypes))
|
|
|
|
for i, rt := range config.ResponseTypes {
|
|
|
|
responseTypes[i] = model.OIDCResponseType(rt)
|
|
|
|
}
|
|
|
|
grantTypes := make([]model.OIDCGrantType, len(config.GrantTypes))
|
|
|
|
for i, rt := range config.GrantTypes {
|
|
|
|
grantTypes[i] = model.OIDCGrantType(rt)
|
|
|
|
}
|
2020-08-10 07:34:56 +00:00
|
|
|
oidcConfig := &model.OIDCConfig{
|
2020-05-11 10:16:29 +00:00
|
|
|
ObjectRoot: config.ObjectRoot,
|
2020-04-23 05:54:40 +00:00
|
|
|
AppID: config.AppID,
|
2020-08-10 07:34:56 +00:00
|
|
|
OIDCVersion: model.OIDCVersion(config.Version),
|
2020-04-23 05:54:40 +00:00
|
|
|
ClientID: config.ClientID,
|
|
|
|
ClientSecret: config.ClientSecret,
|
|
|
|
RedirectUris: config.RedirectUris,
|
|
|
|
ResponseTypes: responseTypes,
|
|
|
|
GrantTypes: grantTypes,
|
|
|
|
ApplicationType: model.OIDCApplicationType(config.ApplicationType),
|
|
|
|
AuthMethodType: model.OIDCAuthMethodType(config.AuthMethodType),
|
|
|
|
PostLogoutRedirectUris: config.PostLogoutRedirectUris,
|
2020-08-10 07:34:56 +00:00
|
|
|
DevMode: config.DevMode,
|
2020-04-23 05:54:40 +00:00
|
|
|
}
|
2020-08-10 07:34:56 +00:00
|
|
|
oidcConfig.FillCompliance()
|
|
|
|
return oidcConfig
|
2020-04-23 05:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) appendAddOIDCConfigEvent(event *es_models.Event) error {
|
|
|
|
config := new(OIDCConfig)
|
|
|
|
err := config.setData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.ObjectRoot.CreationDate = event.CreationDate
|
|
|
|
if i, a := GetApplication(p.Applications, config.AppID); a != nil {
|
2020-06-23 12:47:47 +00:00
|
|
|
p.Applications[i].Type = int32(model.AppTypeOIDC)
|
2020-04-23 05:54:40 +00:00
|
|
|
p.Applications[i].OIDCConfig = config
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) appendChangeOIDCConfigEvent(event *es_models.Event) error {
|
|
|
|
config := new(OIDCConfig)
|
|
|
|
err := config.setData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, a := GetApplication(p.Applications, config.AppID); a != nil {
|
|
|
|
p.Applications[i].OIDCConfig.setData(event)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OIDCConfig) setData(event *es_models.Event) error {
|
|
|
|
o.ObjectRoot.AppendEvent(event)
|
|
|
|
if err := json.Unmarshal(event.Data, o); err != nil {
|
|
|
|
logging.Log("EVEN-d8e3s").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|