2021-01-04 13:52:13 +00:00
|
|
|
package user
|
2020-12-10 15:18:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-12-10 15:18:52 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
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"
|
|
|
|
HumanPasswordCodeAddedType = passwordEventPrefix + "code.added"
|
|
|
|
HumanPasswordCodeSentType = passwordEventPrefix + "code.sent"
|
|
|
|
HumanPasswordCheckSucceededType = passwordEventPrefix + "check.succeeded"
|
|
|
|
HumanPasswordCheckFailedType = passwordEventPrefix + "check.failed"
|
|
|
|
)
|
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
type HumanPasswordChangedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Secret *crypto.CryptoValue `json:"secret,omitempty"`
|
2021-01-06 10:12:56 +00:00
|
|
|
ChangeRequired bool `json:"changeRequired"`
|
2021-01-07 15:06:45 +00:00
|
|
|
UserAgentID string `json:"userAgentID,omitempty"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
func (e *HumanPasswordChangedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPasswordChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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,
|
2020-12-10 15:18:52 +00:00
|
|
|
secret *crypto.CryptoValue,
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
Secret: secret,
|
|
|
|
ChangeRequired: changeRequired,
|
2021-01-07 15:06:45 +00:00
|
|
|
UserAgentID: userAgentID,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func HumanPasswordChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
2021-01-06 10:12:56 +00:00
|
|
|
humanAdded := &HumanPasswordChangedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, humanAdded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-4M0sd", "unable to unmarshal human password changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
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:"-"`
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
Code *crypto.CryptoValue `json:"code,omitempty"`
|
|
|
|
Expiry time.Duration `json:"expiry,omitempty"`
|
|
|
|
NotificationType domain.NotificationType `json:"notificationType,omitempty"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanPasswordCodeAddedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPasswordCodeAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
) *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,
|
|
|
|
),
|
|
|
|
Code: code,
|
|
|
|
Expiry: expiry,
|
|
|
|
NotificationType: notificationType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func HumanPasswordCodeAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
humanAdded := &HumanPasswordCodeAddedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, humanAdded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-Ms90d", "unable to unmarshal human password code added")
|
|
|
|
}
|
|
|
|
|
|
|
|
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:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanPasswordCodeSentEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPasswordCodeSentEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func HumanPasswordCodeSentEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &HumanPasswordCodeSentEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanPasswordCheckSucceededEvent) Data() interface{} {
|
2021-02-08 10:30:30 +00:00
|
|
|
return e
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPasswordCheckSucceededEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func HumanPasswordCheckSucceededEventMapper(event *repository.Event) (eventstore.EventReader, 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
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, humanAdded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-5M9sd", "unable to unmarshal human password check succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanPasswordCheckFailedEvent) Data() interface{} {
|
2021-02-08 10:30:30 +00:00
|
|
|
return e
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPasswordCheckFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func HumanPasswordCheckFailedEventMapper(event *repository.Event) (eventstore.EventReader, 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
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, humanAdded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-4m9fs", "unable to unmarshal human password check failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return humanAdded, nil
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|