zitadel/internal/repository/user/human_phone.go
Tim Möhlmann f680dd934d
refactor: rename package errors to zerrors (#7039)
* chore: rename package errors to zerrors

* rename package errors to gerrors

* fix error related linting issues

* fix zitadel error assertion

* fix gosimple linting issues

* fix deprecated linting issues

* resolve gci linting issues

* fix import structure

---------

Co-authored-by: Elio Bischof <elio@zitadel.com>
2023-12-08 15:30:55 +01:00

236 lines
6.2 KiB
Go

package user
import (
"context"
"time"
"github.com/zitadel/zitadel/internal/api/http"
"github.com/zitadel/zitadel/internal/crypto"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/zerrors"
)
const (
phoneEventPrefix = humanEventPrefix + "phone."
HumanPhoneChangedType = phoneEventPrefix + "changed"
HumanPhoneRemovedType = phoneEventPrefix + "removed"
HumanPhoneVerifiedType = phoneEventPrefix + "verified"
HumanPhoneVerificationFailedType = phoneEventPrefix + "verification.failed"
HumanPhoneCodeAddedType = phoneEventPrefix + "code.added"
HumanPhoneCodeSentType = phoneEventPrefix + "code.sent"
)
type HumanPhoneChangedEvent struct {
eventstore.BaseEvent `json:"-"`
PhoneNumber domain.PhoneNumber `json:"phone,omitempty"`
}
func (e *HumanPhoneChangedEvent) Payload() interface{} {
return e
}
func (e *HumanPhoneChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewHumanPhoneChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, phone domain.PhoneNumber) *HumanPhoneChangedEvent {
return &HumanPhoneChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
HumanPhoneChangedType,
),
PhoneNumber: phone,
}
}
func HumanPhoneChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
phoneChangedEvent := &HumanPhoneChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(phoneChangedEvent)
if err != nil {
return nil, zerrors.ThrowInternal(err, "USER-5M0pd", "unable to unmarshal human phone changed")
}
return phoneChangedEvent, nil
}
type HumanPhoneRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *HumanPhoneRemovedEvent) Payload() interface{} {
return nil
}
func (e *HumanPhoneRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewHumanPhoneRemovedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneRemovedEvent {
return &HumanPhoneRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
HumanPhoneRemovedType,
),
}
}
func HumanPhoneRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
return &HumanPhoneRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type HumanPhoneVerifiedEvent struct {
eventstore.BaseEvent `json:"-"`
IsPhoneVerified bool `json:"-"`
}
func (e *HumanPhoneVerifiedEvent) Payload() interface{} {
return nil
}
func (e *HumanPhoneVerifiedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewHumanPhoneVerifiedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneVerifiedEvent {
return &HumanPhoneVerifiedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
HumanPhoneVerifiedType,
),
}
}
func HumanPhoneVerifiedEventMapper(event eventstore.Event) (eventstore.Event, error) {
return &HumanPhoneVerifiedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
IsPhoneVerified: true,
}, nil
}
type HumanPhoneVerificationFailedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *HumanPhoneVerificationFailedEvent) Payload() interface{} {
return nil
}
func (e *HumanPhoneVerificationFailedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewHumanPhoneVerificationFailedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneVerificationFailedEvent {
return &HumanPhoneVerificationFailedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
HumanPhoneVerificationFailedType,
),
}
}
func HumanPhoneVerificationFailedEventMapper(event eventstore.Event) (eventstore.Event, error) {
return &HumanPhoneVerificationFailedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type HumanPhoneCodeAddedEvent struct {
eventstore.BaseEvent `json:"-"`
Code *crypto.CryptoValue `json:"code,omitempty"`
Expiry time.Duration `json:"expiry,omitempty"`
CodeReturned bool `json:"code_returned,omitempty"`
TriggeredAtOrigin string `json:"triggerOrigin,omitempty"`
}
func (e *HumanPhoneCodeAddedEvent) Payload() interface{} {
return e
}
func (e *HumanPhoneCodeAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func (e *HumanPhoneCodeAddedEvent) TriggerOrigin() string {
return e.TriggeredAtOrigin
}
func NewHumanPhoneCodeAddedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
code *crypto.CryptoValue,
expiry time.Duration,
) *HumanPhoneCodeAddedEvent {
return NewHumanPhoneCodeAddedEventV2(ctx, aggregate, code, expiry, false)
}
func NewHumanPhoneCodeAddedEventV2(
ctx context.Context,
aggregate *eventstore.Aggregate,
code *crypto.CryptoValue,
expiry time.Duration,
codeReturned bool,
) *HumanPhoneCodeAddedEvent {
return &HumanPhoneCodeAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
HumanPhoneCodeAddedType,
),
Code: code,
Expiry: expiry,
CodeReturned: codeReturned,
TriggeredAtOrigin: http.ComposedOrigin(ctx),
}
}
func HumanPhoneCodeAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
codeAdded := &HumanPhoneCodeAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(codeAdded)
if err != nil {
return nil, zerrors.ThrowInternal(err, "USER-6Ms9d", "unable to unmarshal human phone code added")
}
return codeAdded, nil
}
type HumanPhoneCodeSentEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *HumanPhoneCodeSentEvent) Payload() interface{} {
return e
}
func (e *HumanPhoneCodeSentEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewHumanPhoneCodeSentEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneCodeSentEvent {
return &HumanPhoneCodeSentEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
HumanPhoneCodeSentType,
),
}
}
func HumanPhoneCodeSentEventMapper(event eventstore.Event) (eventstore.Event, error) {
return &HumanPhoneCodeSentEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}