2021-09-14 15:15:01 +02:00
|
|
|
package idpconfig
|
|
|
|
|
|
|
|
import (
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-09-14 15:15:01 +02:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2021-09-14 15:15:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
JWTConfigAddedEventType eventstore.EventType = "jwt.config.added"
|
|
|
|
JWTConfigChangedEventType eventstore.EventType = "jwt.config.changed"
|
|
|
|
)
|
|
|
|
|
|
|
|
type JWTConfigAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
IDPConfigID string `json:"idpConfigId"`
|
|
|
|
JWTEndpoint string `json:"jwtEndpoint,omitempty"`
|
|
|
|
Issuer string `json:"issuer,omitempty"`
|
|
|
|
KeysEndpoint string `json:"keysEndpoint,omitempty"`
|
|
|
|
HeaderName string `json:"headerName,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *JWTConfigAddedEvent) Payload() interface{} {
|
2021-09-14 15:15:01 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *JWTConfigAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-09-14 15:15:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewJWTConfigAddedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
idpConfigID,
|
|
|
|
jwtEndpoint,
|
|
|
|
issuer,
|
|
|
|
keysEndpoint,
|
|
|
|
headerName string,
|
|
|
|
) *JWTConfigAddedEvent {
|
|
|
|
return &JWTConfigAddedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
JWTEndpoint: jwtEndpoint,
|
|
|
|
Issuer: issuer,
|
|
|
|
KeysEndpoint: keysEndpoint,
|
|
|
|
HeaderName: headerName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func JWTConfigAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-09-14 15:15:01 +02:00
|
|
|
e := &JWTConfigAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-09-14 15:15:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "JWT-m0fwf", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type JWTConfigChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
IDPConfigID string `json:"idpConfigId"`
|
|
|
|
|
|
|
|
JWTEndpoint *string `json:"jwtEndpoint,omitempty"`
|
|
|
|
Issuer *string `json:"issuer,omitempty"`
|
|
|
|
KeysEndpoint *string `json:"keysEndpoint,omitempty"`
|
|
|
|
HeaderName *string `json:"headerName,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *JWTConfigChangedEvent) Payload() interface{} {
|
2021-09-14 15:15:01 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *JWTConfigChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-09-14 15:15:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewJWTConfigChangedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
idpConfigID string,
|
|
|
|
changes []JWTConfigChanges,
|
|
|
|
) (*JWTConfigChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "IDPCONFIG-fn93s", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changeEvent := &JWTConfigChangedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
}
|
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type JWTConfigChanges func(*JWTConfigChangedEvent)
|
|
|
|
|
|
|
|
func ChangeJWTEndpoint(jwtEndpoint string) func(*JWTConfigChangedEvent) {
|
|
|
|
return func(e *JWTConfigChangedEvent) {
|
|
|
|
e.JWTEndpoint = &jwtEndpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeJWTIssuer(issuer string) func(*JWTConfigChangedEvent) {
|
|
|
|
return func(e *JWTConfigChangedEvent) {
|
|
|
|
e.Issuer = &issuer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeKeysEndpoint(keysEndpoint string) func(*JWTConfigChangedEvent) {
|
|
|
|
return func(e *JWTConfigChangedEvent) {
|
|
|
|
e.KeysEndpoint = &keysEndpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeHeaderName(headerName string) func(*JWTConfigChangedEvent) {
|
|
|
|
return func(e *JWTConfigChangedEvent) {
|
|
|
|
e.HeaderName = &headerName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func JWTConfigChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-09-14 15:15:01 +02:00
|
|
|
e := &JWTConfigChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-09-14 15:15:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "JWT-fk3fs", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|