2020-12-10 15:18:52 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2023-10-10 13:20:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/http"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-10-10 13:20:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-12-10 15:18:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueUsername = "usernames"
|
2020-12-10 15:18:52 +00:00
|
|
|
userEventTypePrefix = eventstore.EventType("user.")
|
|
|
|
UserLockedType = userEventTypePrefix + "locked"
|
|
|
|
UserUnlockedType = userEventTypePrefix + "unlocked"
|
|
|
|
UserDeactivatedType = userEventTypePrefix + "deactivated"
|
|
|
|
UserReactivatedType = userEventTypePrefix + "reactivated"
|
|
|
|
UserRemovedType = userEventTypePrefix + "removed"
|
|
|
|
UserTokenAddedType = userEventTypePrefix + "token.added"
|
2024-05-16 05:07:56 +00:00
|
|
|
UserTokenV2AddedType = userEventTypePrefix + "token.v2.added"
|
2021-11-03 07:35:24 +00:00
|
|
|
UserTokenRemovedType = userEventTypePrefix + "token.removed"
|
2024-03-20 10:18:46 +00:00
|
|
|
UserImpersonatedType = userEventTypePrefix + "impersonated"
|
2020-12-10 15:18:52 +00:00
|
|
|
UserDomainClaimedType = userEventTypePrefix + "domain.claimed"
|
|
|
|
UserDomainClaimedSentType = userEventTypePrefix + "domain.claimed.sent"
|
|
|
|
UserUserNameChangedType = userEventTypePrefix + "username.changed"
|
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func NewAddUsernameUniqueConstraint(userName, resourceOwner string, userLoginMustBeDomain bool) *eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
uniqueUserName := userName
|
|
|
|
if userLoginMustBeDomain {
|
|
|
|
uniqueUserName = userName + resourceOwner
|
|
|
|
}
|
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueUsername,
|
2021-01-21 09:49:38 +00:00
|
|
|
uniqueUserName,
|
|
|
|
"Errors.User.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func NewRemoveUsernameUniqueConstraint(userName, resourceOwner string, userLoginMustBeDomain bool) *eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
uniqueUserName := userName
|
|
|
|
if userLoginMustBeDomain {
|
|
|
|
uniqueUserName = userName + resourceOwner
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return eventstore.NewRemoveUniqueConstraint(
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueUsername,
|
2021-01-21 09:49:38 +00:00
|
|
|
uniqueUserName)
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserLockedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserLockedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserLockedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewUserLockedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserLockedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserLockedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserLockedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func UserLockedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserLockedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserUnlockedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserUnlockedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserUnlockedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewUserUnlockedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserUnlockedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserUnlockedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserUnlockedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func UserUnlockedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserUnlockedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserDeactivatedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserDeactivatedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserDeactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewUserDeactivatedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserDeactivatedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserDeactivatedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserDeactivatedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func UserDeactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserDeactivatedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserReactivatedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserReactivatedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserReactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewUserReactivatedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserReactivatedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserReactivatedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserReactivatedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func UserReactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserReactivatedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserRemovedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
2021-01-21 09:49:38 +00:00
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
userName string
|
2021-11-02 09:08:47 +00:00
|
|
|
externalIDPs []*domain.UserIDPLink
|
2021-02-18 13:48:27 +00:00
|
|
|
loginMustBeDomain bool
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserRemovedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
events := make([]*eventstore.UniqueConstraint, 0)
|
2021-08-24 07:22:21 +00:00
|
|
|
if e.userName != "" {
|
|
|
|
events = append(events, NewRemoveUsernameUniqueConstraint(e.userName, e.Aggregate().ResourceOwner, e.loginMustBeDomain))
|
|
|
|
}
|
|
|
|
for _, idp := range e.externalIDPs {
|
2021-11-02 09:08:47 +00:00
|
|
|
events = append(events, NewRemoveUserIDPLinkUniqueConstraint(idp.IDPConfigID, idp.ExternalUserID))
|
2021-08-24 07:22:21 +00:00
|
|
|
}
|
|
|
|
return events
|
2021-01-21 09:49:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewUserRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
userName string,
|
2021-11-02 09:08:47 +00:00
|
|
|
externalIDPs []*domain.UserIDPLink,
|
2021-02-18 13:48:27 +00:00
|
|
|
userLoginMustBeDomain bool,
|
|
|
|
) *UserRemovedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserRemovedEvent{
|
2021-02-18 13:48:27 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
2020-12-10 15:18:52 +00:00
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserRemovedType,
|
|
|
|
),
|
2021-02-18 13:48:27 +00:00
|
|
|
userName: userName,
|
2021-08-24 07:22:21 +00:00
|
|
|
externalIDPs: externalIDPs,
|
2021-02-18 13:48:27 +00:00
|
|
|
loginMustBeDomain: userLoginMustBeDomain,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func UserRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserRemovedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserTokenAddedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2024-03-20 10:18:46 +00:00
|
|
|
TokenID string `json:"tokenId,omitempty"`
|
|
|
|
ApplicationID string `json:"applicationId,omitempty"`
|
|
|
|
UserAgentID string `json:"userAgentId,omitempty"`
|
|
|
|
RefreshTokenID string `json:"refreshTokenID,omitempty"`
|
|
|
|
Audience []string `json:"audience,omitempty"`
|
|
|
|
Scopes []string `json:"scopes,omitempty"`
|
|
|
|
AuthMethodsReferences []string `json:"authMethodsReferences,omitempty"`
|
|
|
|
AuthTime time.Time `json:"authTime,omitempty"`
|
|
|
|
Expiration time.Time `json:"expiration,omitempty"`
|
|
|
|
PreferredLanguage string `json:"preferredLanguage,omitempty"`
|
|
|
|
Reason domain.TokenReason `json:"reason,omitempty"`
|
|
|
|
Actor *domain.TokenActor `json:"actor,omitempty"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserTokenAddedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserTokenAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewUserTokenAddedEvent(
|
2020-12-10 15:18:52 +00:00
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
tokenID,
|
|
|
|
applicationID,
|
|
|
|
userAgentID,
|
2021-11-03 07:35:24 +00:00
|
|
|
preferredLanguage,
|
|
|
|
refreshTokenID string,
|
2020-12-10 15:18:52 +00:00
|
|
|
audience,
|
2024-03-20 10:18:46 +00:00
|
|
|
scopes,
|
|
|
|
authMethodsReferences []string,
|
|
|
|
authTime,
|
2020-12-10 15:18:52 +00:00
|
|
|
expiration time.Time,
|
2024-03-20 10:18:46 +00:00
|
|
|
reason domain.TokenReason,
|
|
|
|
actor *domain.TokenActor,
|
2021-01-04 13:52:13 +00:00
|
|
|
) *UserTokenAddedEvent {
|
|
|
|
return &UserTokenAddedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserTokenAddedType,
|
|
|
|
),
|
2021-01-21 09:49:38 +00:00
|
|
|
TokenID: tokenID,
|
|
|
|
ApplicationID: applicationID,
|
|
|
|
UserAgentID: userAgentID,
|
2021-11-03 07:35:24 +00:00
|
|
|
RefreshTokenID: refreshTokenID,
|
2021-01-21 09:49:38 +00:00
|
|
|
Audience: audience,
|
|
|
|
Scopes: scopes,
|
|
|
|
Expiration: expiration,
|
|
|
|
PreferredLanguage: preferredLanguage,
|
2024-03-20 10:18:46 +00:00
|
|
|
Reason: reason,
|
|
|
|
Actor: actor,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func UserTokenAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
tokenAdded := &UserTokenAddedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(tokenAdded)
|
2020-12-10 15:18:52 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "USER-7M9sd", "unable to unmarshal token added")
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tokenAdded, nil
|
|
|
|
}
|
|
|
|
|
2024-05-16 05:07:56 +00:00
|
|
|
type UserTokenV2AddedEvent struct {
|
|
|
|
*eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
TokenID string `json:"tokenId,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UserTokenV2AddedEvent) Payload() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UserTokenV2AddedEvent) SetBaseEvent(b *eventstore.BaseEvent) {
|
|
|
|
e.BaseEvent = b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UserTokenV2AddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserTokenV2AddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
tokenID string,
|
|
|
|
) *UserTokenV2AddedEvent {
|
|
|
|
return &UserTokenV2AddedEvent{
|
|
|
|
BaseEvent: eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
UserTokenV2AddedType,
|
|
|
|
),
|
|
|
|
TokenID: tokenID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 10:18:46 +00:00
|
|
|
type UserImpersonatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
ApplicationID string `json:"applicationId,omitempty"`
|
|
|
|
Actor *domain.TokenActor `json:"actor,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UserImpersonatedEvent) Payload() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UserImpersonatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UserImpersonatedEvent) SetBaseEvent(base *eventstore.BaseEvent) {
|
|
|
|
e.BaseEvent = *base
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserImpersonatedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
applicationID string,
|
|
|
|
actor *domain.TokenActor,
|
2024-03-26 06:28:17 +00:00
|
|
|
) *UserImpersonatedEvent {
|
|
|
|
return &UserImpersonatedEvent{
|
2024-03-20 10:18:46 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
UserImpersonatedType,
|
|
|
|
),
|
|
|
|
ApplicationID: applicationID,
|
|
|
|
Actor: actor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 07:35:24 +00:00
|
|
|
type UserTokenRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
TokenID string `json:"tokenId"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserTokenRemovedEvent) Payload() interface{} {
|
2021-11-03 07:35:24 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UserTokenRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-11-03 07:35:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserTokenRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
tokenID string,
|
|
|
|
) *UserTokenRemovedEvent {
|
|
|
|
return &UserTokenRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
UserTokenRemovedType,
|
|
|
|
),
|
|
|
|
TokenID: tokenID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func UserTokenRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-11-03 07:35:24 +00:00
|
|
|
tokenRemoved := &UserTokenRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(tokenRemoved)
|
2021-11-03 07:35:24 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "USER-7M9sd", "unable to unmarshal token added")
|
2021-11-03 07:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tokenRemoved, nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
type DomainClaimedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-02-15 12:31:24 +00:00
|
|
|
UserName string `json:"userName"`
|
2023-10-10 13:20:53 +00:00
|
|
|
TriggeredAtOrigin string `json:"triggerOrigin,omitempty"`
|
2022-12-06 08:01:31 +00:00
|
|
|
oldUserName string
|
|
|
|
userLoginMustBeDomain bool
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *DomainClaimedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *DomainClaimedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{
|
2021-02-18 13:48:27 +00:00
|
|
|
NewRemoveUsernameUniqueConstraint(e.oldUserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
|
|
|
NewAddUsernameUniqueConstraint(e.UserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
2021-02-15 12:31:24 +00:00
|
|
|
}
|
2021-01-21 09:49:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 13:20:53 +00:00
|
|
|
func (e *DomainClaimedEvent) TriggerOrigin() string {
|
|
|
|
return e.TriggeredAtOrigin
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
func NewDomainClaimedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-02-15 12:31:24 +00:00
|
|
|
userName,
|
|
|
|
oldUserName string,
|
|
|
|
userLoginMustBeDomain bool,
|
2020-12-10 15:18:52 +00:00
|
|
|
) *DomainClaimedEvent {
|
|
|
|
return &DomainClaimedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserDomainClaimedType,
|
|
|
|
),
|
2021-02-15 12:31:24 +00:00
|
|
|
UserName: userName,
|
|
|
|
oldUserName: oldUserName,
|
|
|
|
userLoginMustBeDomain: userLoginMustBeDomain,
|
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved
ZITADEL currently selects the instance context based on a HTTP header
(see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and
checks it against the list of instance domains. Let's call it instance
or API domain.
For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in
emails, ...) the requested domain (instance domain) will be used. Let's
call it the public domain.
In cases of proxied setups, all exposed domains (public domains) require
the domain to be managed as instance domain.
This can either be done using the "ExternalDomain" in the runtime config
or via system API, which requires a validation through CustomerPortal on
zitadel.cloud.
# How the Problems Are Solved
- Two new headers / header list are added:
- `InstanceHostHeaders`: an ordered list (first sent wins), which will
be used to match the instance.
(For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader`
and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked
afterwards as well)
- `PublicHostHeaders`: an ordered list (first sent wins), which will be
used as public host / domain. This will be checked against a list of
trusted domains on the instance.
- The middleware intercepts all requests to the API and passes a
`DomainCtx` object with the hosts and protocol into the context
(previously only a computed `origin` was passed)
- HTTP / GRPC server do not longer try to match the headers to instances
themself, but use the passed `http.DomainContext` in their interceptors.
- The `RequestedHost` and `RequestedDomain` from authz.Instance are
removed in favor of the `http.DomainContext`
- When authenticating to or signing out from Console UI, the current
`http.DomainContext(ctx).Origin` (already checked by instance
interceptor for validity) is used to compute and dynamically add a
`redirect_uri` and `post_logout_redirect_uri`.
- Gateway passes all configured host headers (previously only did
`x-zitadel-*`)
- Admin API allows to manage trusted domain
# Additional Changes
None
# Additional Context
- part of #8279
- open topics:
- "single-instance" mode
- Console UI
2024-07-31 15:00:38 +00:00
|
|
|
TriggeredAtOrigin: http.DomainContext(ctx).Origin(),
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func DomainClaimedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2020-12-10 15:18:52 +00:00
|
|
|
domainClaimed := &DomainClaimedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(domainClaimed)
|
2020-12-10 15:18:52 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "USER-aR8jc", "unable to unmarshal domain claimed")
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return domainClaimed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DomainClaimedSentEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *DomainClaimedSentEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *DomainClaimedSentEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
func NewDomainClaimedSentEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
) *DomainClaimedSentEvent {
|
|
|
|
return &DomainClaimedSentEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserDomainClaimedSentType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func DomainClaimedSentEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2020-12-10 15:18:52 +00:00
|
|
|
return &DomainClaimedSentEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UsernameChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2022-12-06 08:01:31 +00:00
|
|
|
UserName string `json:"userName"`
|
|
|
|
oldUserName string
|
|
|
|
userLoginMustBeDomain bool
|
|
|
|
oldUserLoginMustBeDomain bool
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UsernameChangedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *UsernameChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{
|
2022-12-06 08:01:31 +00:00
|
|
|
NewRemoveUsernameUniqueConstraint(e.oldUserName, e.Aggregate().ResourceOwner, e.oldUserLoginMustBeDomain),
|
2021-02-18 13:48:27 +00:00
|
|
|
NewAddUsernameUniqueConstraint(e.UserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
2021-01-21 09:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
func NewUsernameChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-21 09:49:38 +00:00
|
|
|
oldUserName,
|
|
|
|
newUserName string,
|
|
|
|
userLoginMustBeDomain bool,
|
2022-12-06 08:01:31 +00:00
|
|
|
opts ...UsernameChangedEventOption,
|
2020-12-10 15:18:52 +00:00
|
|
|
) *UsernameChangedEvent {
|
2022-12-06 08:01:31 +00:00
|
|
|
event := &UsernameChangedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserUserNameChangedType,
|
|
|
|
),
|
2022-12-06 08:01:31 +00:00
|
|
|
UserName: newUserName,
|
|
|
|
oldUserName: oldUserName,
|
|
|
|
userLoginMustBeDomain: userLoginMustBeDomain,
|
|
|
|
oldUserLoginMustBeDomain: userLoginMustBeDomain,
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(event)
|
|
|
|
}
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
|
|
|
type UsernameChangedEventOption func(*UsernameChangedEvent)
|
|
|
|
|
|
|
|
// UsernameChangedEventWithPolicyChange signals that the change occurs because of / during a domain policy change
|
|
|
|
// (will ensure the unique constraint change is handled correctly)
|
|
|
|
func UsernameChangedEventWithPolicyChange() UsernameChangedEventOption {
|
|
|
|
return func(e *UsernameChangedEvent) {
|
|
|
|
e.oldUserLoginMustBeDomain = !e.userLoginMustBeDomain
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func UsernameChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2020-12-10 15:18:52 +00:00
|
|
|
domainClaimed := &UsernameChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(domainClaimed)
|
2020-12-10 15:18:52 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "USER-4Bm9s", "unable to unmarshal username changed")
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return domainClaimed, nil
|
|
|
|
}
|