2021-01-04 14:52:13 +01:00
|
|
|
package idpconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-01-03 09:19:07 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-20 11:06:52 +01:00
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-04 14:52:13 +01:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-09-14 15:15:01 +02:00
|
|
|
OIDCConfigAddedEventType eventstore.EventType = "oidc.config.added"
|
|
|
|
OIDCConfigChangedEventType eventstore.EventType = "oidc.config.changed"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type OIDCConfigAddedEvent struct {
|
2021-02-23 15:13:04 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
2021-01-04 14:52:13 +01:00
|
|
|
|
2021-07-06 16:39:48 +02:00
|
|
|
IDPConfigID string `json:"idpConfigId"`
|
|
|
|
ClientID string `json:"clientId,omitempty"`
|
|
|
|
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
|
|
|
|
Issuer string `json:"issuer,omitempty"`
|
|
|
|
AuthorizationEndpoint string `json:"authorizationEndpoint,omitempty"`
|
|
|
|
TokenEndpoint string `json:"tokenEndpoint,omitempty"`
|
|
|
|
Scopes []string `json:"scopes,omitempty"`
|
2021-01-04 14:52:13 +01:00
|
|
|
|
|
|
|
IDPDisplayNameMapping domain.OIDCMappingField `json:"idpDisplayNameMapping,omitempty"`
|
|
|
|
UserNameMapping domain.OIDCMappingField `json:"usernameMapping,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *OIDCConfigAddedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *OIDCConfigAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewOIDCConfigAddedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
clientID,
|
|
|
|
idpConfigID,
|
2021-07-06 16:39:48 +02:00
|
|
|
issuer,
|
|
|
|
authorizationEndpoint,
|
|
|
|
tokenEndpoint string,
|
2021-01-04 14:52:13 +01:00
|
|
|
clientSecret *crypto.CryptoValue,
|
|
|
|
idpDisplayNameMapping,
|
|
|
|
userNameMapping domain.OIDCMappingField,
|
|
|
|
scopes ...string,
|
|
|
|
) *OIDCConfigAddedEvent {
|
|
|
|
|
|
|
|
return &OIDCConfigAddedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
ClientID: clientID,
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
Issuer: issuer,
|
2021-07-06 16:39:48 +02:00
|
|
|
AuthorizationEndpoint: authorizationEndpoint,
|
|
|
|
TokenEndpoint: tokenEndpoint,
|
2021-01-04 14:52:13 +01:00
|
|
|
Scopes: scopes,
|
|
|
|
IDPDisplayNameMapping: idpDisplayNameMapping,
|
|
|
|
UserNameMapping: userNameMapping,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func OIDCConfigAddedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &OIDCConfigAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type OIDCConfigChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
IDPConfigID string `json:"idpConfigId"`
|
|
|
|
|
2021-07-06 16:39:48 +02:00
|
|
|
ClientID *string `json:"clientId,omitempty"`
|
|
|
|
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
|
|
|
|
Issuer *string `json:"issuer,omitempty"`
|
|
|
|
AuthorizationEndpoint *string `json:"authorizationEndpoint,omitempty"`
|
|
|
|
TokenEndpoint *string `json:"tokenEndpoint,omitempty"`
|
|
|
|
Scopes []string `json:"scopes,omitempty"`
|
2021-01-04 14:52:13 +01:00
|
|
|
|
2021-01-20 11:06:52 +01:00
|
|
|
IDPDisplayNameMapping *domain.OIDCMappingField `json:"idpDisplayNameMapping,omitempty"`
|
|
|
|
UserNameMapping *domain.OIDCMappingField `json:"usernameMapping,omitempty"`
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *OIDCConfigChangedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *OIDCConfigChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewOIDCConfigChangedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
2021-01-20 11:06:52 +01:00
|
|
|
idpConfigID string,
|
|
|
|
changes []OIDCConfigChanges,
|
|
|
|
) (*OIDCConfigChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "IDPCONFIG-ADzr5", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changeEvent := &OIDCConfigChangedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
}
|
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type OIDCConfigChanges func(*OIDCConfigChangedEvent)
|
|
|
|
|
|
|
|
func ChangeClientID(clientID string) func(*OIDCConfigChangedEvent) {
|
|
|
|
return func(e *OIDCConfigChangedEvent) {
|
|
|
|
e.ClientID = &clientID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeClientSecret(secret *crypto.CryptoValue) func(*OIDCConfigChangedEvent) {
|
|
|
|
return func(e *OIDCConfigChangedEvent) {
|
|
|
|
e.ClientSecret = secret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeIssuer(issuer string) func(*OIDCConfigChangedEvent) {
|
|
|
|
return func(e *OIDCConfigChangedEvent) {
|
|
|
|
e.Issuer = &issuer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 16:39:48 +02:00
|
|
|
func ChangeAuthorizationEndpoint(authorizationEndpoint string) func(*OIDCConfigChangedEvent) {
|
|
|
|
return func(e *OIDCConfigChangedEvent) {
|
|
|
|
e.AuthorizationEndpoint = &authorizationEndpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeTokenEndpoint(tokenEndpoint string) func(*OIDCConfigChangedEvent) {
|
|
|
|
return func(e *OIDCConfigChangedEvent) {
|
|
|
|
e.TokenEndpoint = &tokenEndpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:06:52 +01:00
|
|
|
func ChangeIDPDisplayNameMapping(idpDisplayNameMapping domain.OIDCMappingField) func(*OIDCConfigChangedEvent) {
|
|
|
|
return func(e *OIDCConfigChangedEvent) {
|
|
|
|
e.IDPDisplayNameMapping = &idpDisplayNameMapping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeUserNameMapping(userNameMapping domain.OIDCMappingField) func(*OIDCConfigChangedEvent) {
|
|
|
|
return func(e *OIDCConfigChangedEvent) {
|
|
|
|
e.UserNameMapping = &userNameMapping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeScopes(scopes []string) func(*OIDCConfigChangedEvent) {
|
|
|
|
return func(e *OIDCConfigChangedEvent) {
|
|
|
|
e.Scopes = scopes
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func OIDCConfigChangedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &OIDCConfigChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|