2021-01-04 13:52:13 +00:00
|
|
|
package user
|
2020-12-10 15:18:52 +00:00
|
|
|
|
|
|
|
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/crypto"
|
|
|
|
"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-01-04 13:52:13 +00:00
|
|
|
passwordEventPrefix = humanEventPrefix + "password."
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanPasswordChangedType = passwordEventPrefix + "changed"
|
2023-01-25 08:49:41 +00:00
|
|
|
HumanPasswordChangeSentType = passwordEventPrefix + "change.sent"
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanPasswordCodeAddedType = passwordEventPrefix + "code.added"
|
|
|
|
HumanPasswordCodeSentType = passwordEventPrefix + "code.sent"
|
|
|
|
HumanPasswordCheckSucceededType = passwordEventPrefix + "check.succeeded"
|
|
|
|
HumanPasswordCheckFailedType = passwordEventPrefix + "check.failed"
|
2023-07-28 07:09:15 +00:00
|
|
|
HumanPasswordHashUpdatedType = passwordEventPrefix + "hash.updated"
|
2020-12-10 15:18:52 +00:00
|
|
|
)
|
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
type HumanPasswordChangedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
// New events only use EncodedHash. However, the secret field
|
|
|
|
// is preserved to handle events older than the switch to Passwap.
|
2023-10-10 13:20:53 +00:00
|
|
|
Secret *crypto.CryptoValue `json:"secret,omitempty"`
|
|
|
|
EncodedHash string `json:"encodedHash,omitempty"`
|
|
|
|
ChangeRequired bool `json:"changeRequired"`
|
|
|
|
UserAgentID string `json:"userAgentID,omitempty"`
|
|
|
|
TriggeredAtOrigin string `json:"triggerOrigin,omitempty"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordChangedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-10 13:20:53 +00:00
|
|
|
func (e *HumanPasswordChangedEvent) TriggerOrigin() string {
|
|
|
|
return e.TriggeredAtOrigin
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewHumanPasswordChangedEvent(
|
2020-12-10 15:18:52 +00:00
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2023-07-14 06:49:57 +00:00
|
|
|
encodeHash string,
|
2020-12-10 15:18:52 +00:00
|
|
|
changeRequired bool,
|
2021-01-07 15:06:45 +00:00
|
|
|
userAgentID string,
|
2021-01-06 10:12:56 +00:00
|
|
|
) *HumanPasswordChangedEvent {
|
|
|
|
return &HumanPasswordChangedEvent{
|
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
|
|
|
HumanPasswordChangedType,
|
|
|
|
),
|
2023-10-10 13:20:53 +00:00
|
|
|
EncodedHash: encodeHash,
|
|
|
|
ChangeRequired: changeRequired,
|
|
|
|
UserAgentID: userAgentID,
|
|
|
|
TriggeredAtOrigin: http.ComposedOrigin(ctx),
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPasswordChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-06 10:12:56 +00:00
|
|
|
humanAdded := &HumanPasswordChangedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(humanAdded)
|
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-4M0sd", "unable to unmarshal human password changed")
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return humanAdded, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPasswordCodeAddedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-10-10 13:20:53 +00:00
|
|
|
Code *crypto.CryptoValue `json:"code,omitempty"`
|
|
|
|
Expiry time.Duration `json:"expiry,omitempty"`
|
|
|
|
NotificationType domain.NotificationType `json:"notificationType,omitempty"`
|
|
|
|
URLTemplate string `json:"url_template,omitempty"`
|
|
|
|
CodeReturned bool `json:"code_returned,omitempty"`
|
|
|
|
TriggeredAtOrigin string `json:"triggerOrigin,omitempty"`
|
2024-04-24 15:50:58 +00:00
|
|
|
// AuthRequest is only used in V1 Login UI
|
|
|
|
AuthRequestID string `json:"authRequestID,omitempty"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordCodeAddedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordCodeAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-10 13:20:53 +00:00
|
|
|
func (e *HumanPasswordCodeAddedEvent) TriggerOrigin() string {
|
|
|
|
return e.TriggeredAtOrigin
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewHumanPasswordCodeAddedEvent(
|
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
|
|
|
code *crypto.CryptoValue,
|
|
|
|
expiry time.Duration,
|
2021-01-04 13:52:13 +00:00
|
|
|
notificationType domain.NotificationType,
|
2024-04-24 15:50:58 +00:00
|
|
|
authRequestID string,
|
2023-06-20 15:34:06 +00:00
|
|
|
) *HumanPasswordCodeAddedEvent {
|
2024-04-24 15:50:58 +00:00
|
|
|
return &HumanPasswordCodeAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
HumanPasswordCodeAddedType,
|
|
|
|
),
|
|
|
|
Code: code,
|
|
|
|
Expiry: expiry,
|
|
|
|
NotificationType: notificationType,
|
|
|
|
TriggeredAtOrigin: http.ComposedOrigin(ctx),
|
|
|
|
AuthRequestID: authRequestID,
|
|
|
|
}
|
2023-06-20 15:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHumanPasswordCodeAddedEventV2(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
code *crypto.CryptoValue,
|
|
|
|
expiry time.Duration,
|
|
|
|
notificationType domain.NotificationType,
|
|
|
|
urlTemplate string,
|
|
|
|
codeReturned bool,
|
2021-01-04 13:52:13 +00:00
|
|
|
) *HumanPasswordCodeAddedEvent {
|
|
|
|
return &HumanPasswordCodeAddedEvent{
|
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
|
|
|
HumanPasswordCodeAddedType,
|
|
|
|
),
|
2023-10-10 13:20:53 +00:00
|
|
|
Code: code,
|
|
|
|
Expiry: expiry,
|
|
|
|
NotificationType: notificationType,
|
|
|
|
URLTemplate: urlTemplate,
|
|
|
|
CodeReturned: codeReturned,
|
|
|
|
TriggeredAtOrigin: http.ComposedOrigin(ctx),
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPasswordCodeAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
humanAdded := &HumanPasswordCodeAddedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(humanAdded)
|
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-Ms90d", "unable to unmarshal human password code added")
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return humanAdded, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPasswordCodeSentEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordCodeSentEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordCodeSentEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewHumanPasswordCodeSentEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPasswordCodeSentEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPasswordCodeSentEvent{
|
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
|
|
|
HumanPasswordCodeSentType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPasswordCodeSentEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPasswordCodeSentEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-01-25 08:49:41 +00:00
|
|
|
type HumanPasswordChangeSentEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordChangeSentEvent) Payload() interface{} {
|
2023-01-25 08:49:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordChangeSentEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-01-25 08:49:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHumanPasswordChangeSentEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPasswordChangeSentEvent {
|
|
|
|
return &HumanPasswordChangeSentEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
HumanPasswordChangeSentType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPasswordChangeSentEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-01-25 08:49:41 +00:00
|
|
|
return &HumanPasswordChangeSentEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPasswordCheckSucceededEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
2021-02-08 10:30:30 +00:00
|
|
|
*AuthRequestInfo
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordCheckSucceededEvent) Payload() interface{} {
|
2021-02-08 10:30:30 +00:00
|
|
|
return e
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordCheckSucceededEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewHumanPasswordCheckSucceededEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
info *AuthRequestInfo,
|
|
|
|
) *HumanPasswordCheckSucceededEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPasswordCheckSucceededEvent{
|
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
|
|
|
HumanPasswordCheckSucceededType,
|
|
|
|
),
|
2021-02-08 10:30:30 +00:00
|
|
|
AuthRequestInfo: info,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPasswordCheckSucceededEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-02-08 10:30:30 +00:00
|
|
|
humanAdded := &HumanPasswordCheckSucceededEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(humanAdded)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "USER-5M9sd", "unable to unmarshal human password check succeeded")
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return humanAdded, nil
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPasswordCheckFailedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
2021-02-08 10:30:30 +00:00
|
|
|
*AuthRequestInfo
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordCheckFailedEvent) Payload() interface{} {
|
2021-02-08 10:30:30 +00:00
|
|
|
return e
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordCheckFailedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewHumanPasswordCheckFailedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
info *AuthRequestInfo,
|
|
|
|
) *HumanPasswordCheckFailedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPasswordCheckFailedEvent{
|
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
|
|
|
HumanPasswordCheckFailedType,
|
|
|
|
),
|
2021-02-08 10:30:30 +00:00
|
|
|
AuthRequestInfo: info,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPasswordCheckFailedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-02-08 10:30:30 +00:00
|
|
|
humanAdded := &HumanPasswordCheckFailedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(humanAdded)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "USER-4m9fs", "unable to unmarshal human password check failed")
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return humanAdded, nil
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
2023-07-14 06:49:57 +00:00
|
|
|
|
|
|
|
type HumanPasswordHashUpdatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
EncodedHash string `json:"encodedHash,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordHashUpdatedEvent) Payload() interface{} {
|
2023-07-14 06:49:57 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPasswordHashUpdatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-07-14 06:49:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HumanPasswordHashUpdatedEvent) SetBaseEvent(base *eventstore.BaseEvent) {
|
|
|
|
e.BaseEvent = *base
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHumanPasswordHashUpdatedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
encoded string,
|
|
|
|
) *HumanPasswordHashUpdatedEvent {
|
|
|
|
return &HumanPasswordHashUpdatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
2023-07-28 07:09:15 +00:00
|
|
|
HumanPasswordHashUpdatedType,
|
2023-07-14 06:49:57 +00:00
|
|
|
),
|
|
|
|
EncodedHash: encoded,
|
|
|
|
}
|
|
|
|
}
|