2021-01-04 13:52:13 +00:00
|
|
|
package user
|
2020-12-10 15:18:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-11-30 07:57:51 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
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
|
|
|
phoneEventPrefix = humanEventPrefix + "phone."
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanPhoneChangedType = phoneEventPrefix + "changed"
|
|
|
|
HumanPhoneRemovedType = phoneEventPrefix + "removed"
|
|
|
|
HumanPhoneVerifiedType = phoneEventPrefix + "verified"
|
|
|
|
HumanPhoneVerificationFailedType = phoneEventPrefix + "verification.failed"
|
|
|
|
HumanPhoneCodeAddedType = phoneEventPrefix + "code.added"
|
|
|
|
HumanPhoneCodeSentType = phoneEventPrefix + "code.sent"
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneChangedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
PhoneNumber string `json:"phone,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanPhoneChangedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPhoneChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 10:12:56 +00:00
|
|
|
func NewHumanPhoneChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, phone string) *HumanPhoneChangedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneChangedEvent{
|
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
|
|
|
HumanPhoneChangedType,
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
PhoneNumber: phone,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func HumanPhoneChangedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
phoneChangedEvent := &HumanPhoneChangedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, phoneChangedEvent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-5M0pd", "unable to unmarshal human phone changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return phoneChangedEvent, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneRemovedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanPhoneRemovedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPhoneRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewHumanPhoneRemovedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneRemovedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneRemovedEvent{
|
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
|
|
|
HumanPhoneRemovedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func HumanPhoneRemovedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-11-30 07:57:51 +00:00
|
|
|
return &HumanPhoneRemovedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneVerifiedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
IsPhoneVerified bool `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanPhoneVerifiedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPhoneVerifiedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewHumanPhoneVerifiedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneVerifiedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneVerifiedEvent{
|
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
|
|
|
HumanPhoneVerifiedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func HumanPhoneVerifiedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneVerifiedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
IsPhoneVerified: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneVerificationFailedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanPhoneVerificationFailedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPhoneVerificationFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewHumanPhoneVerificationFailedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneVerificationFailedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneVerificationFailedEvent{
|
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
|
|
|
HumanPhoneVerificationFailedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func HumanPhoneVerificationFailedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneVerificationFailedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneCodeAddedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Code *crypto.CryptoValue `json:"code,omitempty"`
|
|
|
|
Expiry time.Duration `json:"expiry,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanPhoneCodeAddedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPhoneCodeAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewHumanPhoneCodeAddedEvent(
|
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
|
|
|
) *HumanPhoneCodeAddedEvent {
|
|
|
|
return &HumanPhoneCodeAddedEvent{
|
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
|
|
|
HumanPhoneCodeAddedType,
|
|
|
|
),
|
|
|
|
Code: code,
|
|
|
|
Expiry: expiry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func HumanPhoneCodeAddedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
codeAdded := &HumanPhoneCodeAddedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, codeAdded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-6Ms9d", "unable to unmarshal human phone code added")
|
|
|
|
}
|
|
|
|
|
|
|
|
return codeAdded, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneCodeSentEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanPhoneCodeSentEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanPhoneCodeSentEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewHumanPhoneCodeSentEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneCodeSentEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneCodeSentEvent{
|
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
|
|
|
HumanPhoneCodeSentType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func HumanPhoneCodeSentEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneCodeSentEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|