2021-01-04 14:52:13 +01:00
|
|
|
package user
|
2020-12-10 16:18:52 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-18 14:48:27 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2020-12-10 16:18:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-02 10:08:47 +01:00
|
|
|
UniqueUserIDPLinkType = "external_idps"
|
|
|
|
UserIDPLinkEventPrefix = humanEventPrefix + "externalidp."
|
|
|
|
idpLoginEventPrefix = humanEventPrefix + "externallogin."
|
2020-12-10 16:18:52 +01:00
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
UserIDPLinkAddedType = UserIDPLinkEventPrefix + "added"
|
|
|
|
UserIDPLinkRemovedType = UserIDPLinkEventPrefix + "removed"
|
|
|
|
UserIDPLinkCascadeRemovedType = UserIDPLinkEventPrefix + "cascade.removed"
|
2023-08-04 11:35:36 +02:00
|
|
|
UserIDPExternalIDMigratedType = UserIDPLinkEventPrefix + "id.migrated"
|
2020-12-10 16:18:52 +01:00
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
UserIDPLoginCheckSucceededType = idpLoginEventPrefix + "check.succeeded"
|
2020-12-10 16:18:52 +01:00
|
|
|
)
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func NewAddUserIDPLinkUniqueConstraint(idpConfigID, externalUserID string) *eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-11-02 10:08:47 +01:00
|
|
|
UniqueUserIDPLinkType,
|
2021-01-21 10:49:38 +01:00
|
|
|
idpConfigID+externalUserID,
|
|
|
|
"Errors.User.ExternalIDP.AlreadyExists")
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func NewRemoveUserIDPLinkUniqueConstraint(idpConfigID, externalUserID string) *eventstore.UniqueConstraint {
|
|
|
|
return eventstore.NewRemoveUniqueConstraint(
|
2021-11-02 10:08:47 +01:00
|
|
|
UniqueUserIDPLinkType,
|
2021-01-21 10:49:38 +01:00
|
|
|
idpConfigID+externalUserID)
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
type UserIDPLinkAddedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-02-15 13:31:24 +01:00
|
|
|
IDPConfigID string `json:"idpConfigId,omitempty"`
|
|
|
|
ExternalUserID string `json:"userId,omitempty"`
|
|
|
|
DisplayName string `json:"displayName,omitempty"`
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserIDPLinkAddedEvent) Payload() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserIDPLinkAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{NewAddUserIDPLinkUniqueConstraint(e.IDPConfigID, e.ExternalUserID)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
func NewUserIDPLinkAddedEvent(
|
2021-02-18 14:48:27 +01:00
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
idpConfigID,
|
|
|
|
displayName,
|
|
|
|
externalUserID string,
|
2021-11-02 10:08:47 +01:00
|
|
|
) *UserIDPLinkAddedEvent {
|
|
|
|
return &UserIDPLinkAddedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-11-02 10:08:47 +01:00
|
|
|
UserIDPLinkAddedType,
|
2020-12-10 16:18:52 +01:00
|
|
|
),
|
2021-02-15 13:31:24 +01:00
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
DisplayName: displayName,
|
|
|
|
ExternalUserID: externalUserID,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func UserIDPLinkAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-11-02 10:08:47 +01:00
|
|
|
e := &UserIDPLinkAddedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-10 16:18:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-6M9sd", "unable to unmarshal user external idp added")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
type UserIDPLinkRemovedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-02-15 13:31:24 +01:00
|
|
|
IDPConfigID string `json:"idpConfigId"`
|
|
|
|
ExternalUserID string `json:"userId,omitempty"`
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserIDPLinkRemovedEvent) Payload() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserIDPLinkRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{NewRemoveUserIDPLinkUniqueConstraint(e.IDPConfigID, e.ExternalUserID)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
func NewUserIDPLinkRemovedEvent(
|
2021-02-18 14:48:27 +01:00
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
idpConfigID,
|
|
|
|
externalUserID string,
|
2021-11-02 10:08:47 +01:00
|
|
|
) *UserIDPLinkRemovedEvent {
|
|
|
|
return &UserIDPLinkRemovedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-11-02 10:08:47 +01:00
|
|
|
UserIDPLinkRemovedType,
|
2020-12-10 16:18:52 +01:00
|
|
|
),
|
2021-02-15 13:31:24 +01:00
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
ExternalUserID: externalUserID,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func UserIDPLinkRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-11-02 10:08:47 +01:00
|
|
|
e := &UserIDPLinkRemovedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-10 16:18:52 +01:00
|
|
|
if err != nil {
|
2023-10-19 12:19:10 +02:00
|
|
|
return nil, errors.ThrowInternal(err, "USER-eAWoT", "unable to unmarshal user external idp removed")
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
type UserIDPLinkCascadeRemovedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-02-15 13:31:24 +01:00
|
|
|
IDPConfigID string `json:"idpConfigId"`
|
|
|
|
ExternalUserID string `json:"userId,omitempty"`
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserIDPLinkCascadeRemovedEvent) Payload() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserIDPLinkCascadeRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{NewRemoveUserIDPLinkUniqueConstraint(e.IDPConfigID, e.ExternalUserID)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
func NewUserIDPLinkCascadeRemovedEvent(
|
2021-02-18 14:48:27 +01:00
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
idpConfigID,
|
|
|
|
externalUserID string,
|
2021-11-02 10:08:47 +01:00
|
|
|
) *UserIDPLinkCascadeRemovedEvent {
|
|
|
|
return &UserIDPLinkCascadeRemovedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-11-02 10:08:47 +01:00
|
|
|
UserIDPLinkCascadeRemovedType,
|
2020-12-10 16:18:52 +01:00
|
|
|
),
|
2021-02-15 13:31:24 +01:00
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
ExternalUserID: externalUserID,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func UserIDPLinkCascadeRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-11-02 10:08:47 +01:00
|
|
|
e := &UserIDPLinkCascadeRemovedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-10 16:18:52 +01:00
|
|
|
if err != nil {
|
2021-11-02 10:08:47 +01:00
|
|
|
return nil, errors.ThrowInternal(err, "USER-dKGqO", "unable to unmarshal user external idp cascade removed")
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
type UserIDPCheckSucceededEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
2021-02-08 11:30:30 +01:00
|
|
|
*AuthRequestInfo
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserIDPCheckSucceededEvent) Payload() interface{} {
|
2021-02-08 11:30:30 +01:00
|
|
|
return e
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserIDPCheckSucceededEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
func NewUserIDPCheckSucceededEvent(
|
2021-02-18 14:48:27 +01:00
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
2021-11-02 10:08:47 +01:00
|
|
|
info *AuthRequestInfo) *UserIDPCheckSucceededEvent {
|
|
|
|
return &UserIDPCheckSucceededEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-11-02 10:08:47 +01:00
|
|
|
UserIDPLoginCheckSucceededType,
|
2020-12-10 16:18:52 +01:00
|
|
|
),
|
2021-02-08 11:30:30 +01:00
|
|
|
AuthRequestInfo: info,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func UserIDPCheckSucceededEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-11-02 10:08:47 +01:00
|
|
|
e := &UserIDPCheckSucceededEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2021-02-08 11:30:30 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-02-08 11:30:30 +01:00
|
|
|
if err != nil {
|
2021-11-02 10:08:47 +01:00
|
|
|
return nil, errors.ThrowInternal(err, "USER-oikSS", "unable to unmarshal user external idp check succeeded")
|
2021-02-08 11:30:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
2023-08-04 11:35:36 +02:00
|
|
|
|
|
|
|
type UserIDPExternalIDMigratedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
IDPConfigID string `json:"idpConfigId"`
|
|
|
|
PreviousID string `json:"previousId"`
|
|
|
|
NewID string `json:"newId"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserIDPExternalIDMigratedEvent) Payload() interface{} {
|
2023-08-04 11:35:36 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *UserIDPExternalIDMigratedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-08-04 11:35:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UserIDPExternalIDMigratedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
|
|
|
|
e.BaseEvent = *event
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserIDPExternalIDMigratedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
idpConfigID,
|
|
|
|
previousID,
|
|
|
|
newID string,
|
|
|
|
) *UserIDPExternalIDMigratedEvent {
|
|
|
|
return &UserIDPExternalIDMigratedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
UserIDPExternalIDMigratedType,
|
|
|
|
),
|
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
PreviousID: previousID,
|
|
|
|
NewID: newID,
|
|
|
|
}
|
|
|
|
}
|