2021-01-04 14:52:13 +01:00
|
|
|
package user
|
2020-12-10 16:18:52 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 14:48:27 +01:00
|
|
|
|
2020-12-10 16:18:52 +01:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-12-10 16:18:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueExternalIDPType = "external_idps"
|
2021-01-04 14:52:13 +01:00
|
|
|
externalIDPEventPrefix = humanEventPrefix + "externalidp."
|
|
|
|
externalLoginEventPrefix = humanEventPrefix + "externallogin."
|
2020-12-10 16:18:52 +01:00
|
|
|
|
|
|
|
HumanExternalIDPAddedType = externalIDPEventPrefix + "added"
|
|
|
|
HumanExternalIDPRemovedType = externalIDPEventPrefix + "removed"
|
|
|
|
HumanExternalIDPCascadeRemovedType = externalIDPEventPrefix + "cascade.removed"
|
|
|
|
|
|
|
|
HumanExternalLoginCheckSucceededType = externalLoginEventPrefix + "check.succeeded"
|
|
|
|
)
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func NewAddExternalIDPUniqueConstraint(idpConfigID, externalUserID string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueExternalIDPType,
|
2021-01-21 10:49:38 +01:00
|
|
|
idpConfigID+externalUserID,
|
|
|
|
"Errors.User.ExternalIDP.AlreadyExists")
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func NewRemoveExternalIDPUniqueConstraint(idpConfigID, externalUserID string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewRemoveEventUniqueConstraint(
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueExternalIDPType,
|
2021-01-21 10:49:38 +01:00
|
|
|
idpConfigID+externalUserID)
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type HumanExternalIDPAddedEvent 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
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *HumanExternalIDPAddedEvent) Data() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *HumanExternalIDPAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-15 13:31:24 +01:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddExternalIDPUniqueConstraint(e.IDPConfigID, e.ExternalUserID)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *HumanExternalIDPAddedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewHumanExternalIDPAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
idpConfigID,
|
|
|
|
displayName,
|
|
|
|
externalUserID string,
|
|
|
|
) *HumanExternalIDPAddedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &HumanExternalIDPAddedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
HumanExternalIDPAddedType,
|
|
|
|
),
|
2021-02-15 13:31:24 +01:00
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
DisplayName: displayName,
|
|
|
|
ExternalUserID: externalUserID,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func HumanExternalIDPAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &HumanExternalIDPAddedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-6M9sd", "unable to unmarshal user external idp added")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type HumanExternalIDPRemovedEvent 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
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *HumanExternalIDPRemovedEvent) Data() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *HumanExternalIDPRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-15 13:31:24 +01:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewRemoveExternalIDPUniqueConstraint(e.IDPConfigID, e.ExternalUserID)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *HumanExternalIDPRemovedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewHumanExternalIDPRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
idpConfigID,
|
|
|
|
externalUserID string,
|
|
|
|
) *HumanExternalIDPRemovedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &HumanExternalIDPRemovedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
HumanExternalIDPRemovedType,
|
|
|
|
),
|
2021-02-15 13:31:24 +01:00
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
ExternalUserID: externalUserID,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func HumanExternalIDPRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &HumanExternalIDPRemovedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-5Gm9s", "unable to unmarshal user external idp removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type HumanExternalIDPCascadeRemovedEvent 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
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *HumanExternalIDPCascadeRemovedEvent) Data() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *HumanExternalIDPCascadeRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-15 13:31:24 +01:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewRemoveExternalIDPUniqueConstraint(e.IDPConfigID, e.ExternalUserID)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *HumanExternalIDPCascadeRemovedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewHumanExternalIDPCascadeRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
idpConfigID,
|
|
|
|
externalUserID string,
|
|
|
|
) *HumanExternalIDPCascadeRemovedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &HumanExternalIDPCascadeRemovedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
HumanExternalIDPCascadeRemovedType,
|
|
|
|
),
|
2021-02-15 13:31:24 +01:00
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
ExternalUserID: externalUserID,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func HumanExternalIDPCascadeRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &HumanExternalIDPCascadeRemovedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-2M0sd", "unable to unmarshal user external idp cascade removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type HumanExternalIDPCheckSucceededEvent 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
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *HumanExternalIDPCheckSucceededEvent) Data() interface{} {
|
2021-02-08 11:30:30 +01:00
|
|
|
return e
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *HumanExternalIDPCheckSucceededEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *HumanExternalIDPCheckSucceededEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewHumanExternalIDPCheckSucceededEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
info *AuthRequestInfo) *HumanExternalIDPCheckSucceededEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &HumanExternalIDPCheckSucceededEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
HumanExternalLoginCheckSucceededType,
|
|
|
|
),
|
2021-02-08 11:30:30 +01:00
|
|
|
AuthRequestInfo: info,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func HumanExternalIDPCheckSucceededEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
2021-02-08 11:30:30 +01:00
|
|
|
e := &HumanExternalIDPCheckSucceededEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2021-02-08 11:30:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-2M0sd", "unable to unmarshal user external idp check succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|